blob: 733308697094def8d479f6f08f03a44af8a4ea64 [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 Oliveira568c6342015-06-01 12:49:57 +020089static int intel_set_mode(struct drm_atomic_state *state);
Jesse Barneseb1bfe82014-02-12 12:26:25 -080090static int intel_framebuffer_init(struct drm_device *dev,
91 struct intel_framebuffer *ifb,
92 struct drm_mode_fb_cmd2 *mode_cmd,
93 struct drm_i915_gem_object *obj);
Daniel Vetter5b18e572014-04-24 23:55:06 +020094static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
95static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +020096static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -070097 struct intel_link_m_n *m_n,
98 struct intel_link_m_n *m2_n2);
Daniel Vetter29407aa2014-04-24 23:55:08 +020099static void ironlake_set_pipeconf(struct drm_crtc *crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +0200100static void haswell_set_pipeconf(struct drm_crtc *crtc);
101static void intel_set_pipe_csc(struct drm_crtc *crtc);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200102static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200103 const struct intel_crtc_state *pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200104static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200105 const struct intel_crtc_state *pipe_config);
Matt Roperea2c67b2014-12-23 10:41:52 -0800106static void intel_begin_crtc_commit(struct drm_crtc *crtc);
107static void intel_finish_crtc_commit(struct drm_crtc *crtc);
Chandra Konduru549e2bf2015-04-07 15:28:38 -0700108static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
109 struct intel_crtc_state *crtc_state);
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200110static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
111 int num_connectors);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100112
Dave Airlie0e32b392014-05-02 14:02:48 +1000113static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
114{
115 if (!connector->mst_port)
116 return connector->encoder;
117 else
118 return &connector->mst_port->mst_encoders[pipe]->base;
119}
120
Jesse Barnes79e53942008-11-07 14:24:08 -0800121typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400122 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -0800123} intel_range_t;
124
125typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400126 int dot_limit;
127 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800128} intel_p2_t;
129
Ma Lingd4906092009-03-18 20:13:27 +0800130typedef struct intel_limit intel_limit_t;
131struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -0400132 intel_range_t dot, vco, n, m, m1, m2, p, p1;
133 intel_p2_t p2;
Ma Lingd4906092009-03-18 20:13:27 +0800134};
Jesse Barnes79e53942008-11-07 14:24:08 -0800135
Daniel Vetterd2acd212012-10-20 20:57:43 +0200136int
137intel_pch_rawclk(struct drm_device *dev)
138{
139 struct drm_i915_private *dev_priv = dev->dev_private;
140
141 WARN_ON(!HAS_PCH_SPLIT(dev));
142
143 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
144}
145
Chris Wilson021357a2010-09-07 20:54:59 +0100146static inline u32 /* units of 100MHz */
147intel_fdi_link_freq(struct drm_device *dev)
148{
Chris Wilson8b99e682010-10-13 09:59:17 +0100149 if (IS_GEN5(dev)) {
150 struct drm_i915_private *dev_priv = dev->dev_private;
151 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
152 } else
153 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +0100154}
155
Daniel Vetter5d536e22013-07-06 12:52:06 +0200156static const intel_limit_t intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400157 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200158 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200159 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400160 .m = { .min = 96, .max = 140 },
161 .m1 = { .min = 18, .max = 26 },
162 .m2 = { .min = 6, .max = 16 },
163 .p = { .min = 4, .max = 128 },
164 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700165 .p2 = { .dot_limit = 165000,
166 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700167};
168
Daniel Vetter5d536e22013-07-06 12:52:06 +0200169static const intel_limit_t intel_limits_i8xx_dvo = {
170 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200171 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200172 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200173 .m = { .min = 96, .max = 140 },
174 .m1 = { .min = 18, .max = 26 },
175 .m2 = { .min = 6, .max = 16 },
176 .p = { .min = 4, .max = 128 },
177 .p1 = { .min = 2, .max = 33 },
178 .p2 = { .dot_limit = 165000,
179 .p2_slow = 4, .p2_fast = 4 },
180};
181
Keith Packarde4b36692009-06-05 19:22:17 -0700182static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400183 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200184 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200185 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400186 .m = { .min = 96, .max = 140 },
187 .m1 = { .min = 18, .max = 26 },
188 .m2 = { .min = 6, .max = 16 },
189 .p = { .min = 4, .max = 128 },
190 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700191 .p2 = { .dot_limit = 165000,
192 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700193};
Eric Anholt273e27c2011-03-30 13:01:10 -0700194
Keith Packarde4b36692009-06-05 19:22:17 -0700195static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400196 .dot = { .min = 20000, .max = 400000 },
197 .vco = { .min = 1400000, .max = 2800000 },
198 .n = { .min = 1, .max = 6 },
199 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100200 .m1 = { .min = 8, .max = 18 },
201 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400202 .p = { .min = 5, .max = 80 },
203 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700204 .p2 = { .dot_limit = 200000,
205 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700206};
207
208static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400209 .dot = { .min = 20000, .max = 400000 },
210 .vco = { .min = 1400000, .max = 2800000 },
211 .n = { .min = 1, .max = 6 },
212 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100213 .m1 = { .min = 8, .max = 18 },
214 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400215 .p = { .min = 7, .max = 98 },
216 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700217 .p2 = { .dot_limit = 112000,
218 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700219};
220
Eric Anholt273e27c2011-03-30 13:01:10 -0700221
Keith Packarde4b36692009-06-05 19:22:17 -0700222static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700223 .dot = { .min = 25000, .max = 270000 },
224 .vco = { .min = 1750000, .max = 3500000},
225 .n = { .min = 1, .max = 4 },
226 .m = { .min = 104, .max = 138 },
227 .m1 = { .min = 17, .max = 23 },
228 .m2 = { .min = 5, .max = 11 },
229 .p = { .min = 10, .max = 30 },
230 .p1 = { .min = 1, .max = 3},
231 .p2 = { .dot_limit = 270000,
232 .p2_slow = 10,
233 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800234 },
Keith Packarde4b36692009-06-05 19:22:17 -0700235};
236
237static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700238 .dot = { .min = 22000, .max = 400000 },
239 .vco = { .min = 1750000, .max = 3500000},
240 .n = { .min = 1, .max = 4 },
241 .m = { .min = 104, .max = 138 },
242 .m1 = { .min = 16, .max = 23 },
243 .m2 = { .min = 5, .max = 11 },
244 .p = { .min = 5, .max = 80 },
245 .p1 = { .min = 1, .max = 8},
246 .p2 = { .dot_limit = 165000,
247 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700248};
249
250static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700251 .dot = { .min = 20000, .max = 115000 },
252 .vco = { .min = 1750000, .max = 3500000 },
253 .n = { .min = 1, .max = 3 },
254 .m = { .min = 104, .max = 138 },
255 .m1 = { .min = 17, .max = 23 },
256 .m2 = { .min = 5, .max = 11 },
257 .p = { .min = 28, .max = 112 },
258 .p1 = { .min = 2, .max = 8 },
259 .p2 = { .dot_limit = 0,
260 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800261 },
Keith Packarde4b36692009-06-05 19:22:17 -0700262};
263
264static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700265 .dot = { .min = 80000, .max = 224000 },
266 .vco = { .min = 1750000, .max = 3500000 },
267 .n = { .min = 1, .max = 3 },
268 .m = { .min = 104, .max = 138 },
269 .m1 = { .min = 17, .max = 23 },
270 .m2 = { .min = 5, .max = 11 },
271 .p = { .min = 14, .max = 42 },
272 .p1 = { .min = 2, .max = 6 },
273 .p2 = { .dot_limit = 0,
274 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800275 },
Keith Packarde4b36692009-06-05 19:22:17 -0700276};
277
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500278static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400279 .dot = { .min = 20000, .max = 400000},
280 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700281 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400282 .n = { .min = 3, .max = 6 },
283 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700284 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400285 .m1 = { .min = 0, .max = 0 },
286 .m2 = { .min = 0, .max = 254 },
287 .p = { .min = 5, .max = 80 },
288 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700289 .p2 = { .dot_limit = 200000,
290 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700291};
292
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500293static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400294 .dot = { .min = 20000, .max = 400000 },
295 .vco = { .min = 1700000, .max = 3500000 },
296 .n = { .min = 3, .max = 6 },
297 .m = { .min = 2, .max = 256 },
298 .m1 = { .min = 0, .max = 0 },
299 .m2 = { .min = 0, .max = 254 },
300 .p = { .min = 7, .max = 112 },
301 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700302 .p2 = { .dot_limit = 112000,
303 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700304};
305
Eric Anholt273e27c2011-03-30 13:01:10 -0700306/* Ironlake / Sandybridge
307 *
308 * We calculate clock using (register_value + 2) for N/M1/M2, so here
309 * the range value for them is (actual_value - 2).
310 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800311static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700312 .dot = { .min = 25000, .max = 350000 },
313 .vco = { .min = 1760000, .max = 3510000 },
314 .n = { .min = 1, .max = 5 },
315 .m = { .min = 79, .max = 127 },
316 .m1 = { .min = 12, .max = 22 },
317 .m2 = { .min = 5, .max = 9 },
318 .p = { .min = 5, .max = 80 },
319 .p1 = { .min = 1, .max = 8 },
320 .p2 = { .dot_limit = 225000,
321 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700322};
323
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800324static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700325 .dot = { .min = 25000, .max = 350000 },
326 .vco = { .min = 1760000, .max = 3510000 },
327 .n = { .min = 1, .max = 3 },
328 .m = { .min = 79, .max = 118 },
329 .m1 = { .min = 12, .max = 22 },
330 .m2 = { .min = 5, .max = 9 },
331 .p = { .min = 28, .max = 112 },
332 .p1 = { .min = 2, .max = 8 },
333 .p2 = { .dot_limit = 225000,
334 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800335};
336
337static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700338 .dot = { .min = 25000, .max = 350000 },
339 .vco = { .min = 1760000, .max = 3510000 },
340 .n = { .min = 1, .max = 3 },
341 .m = { .min = 79, .max = 127 },
342 .m1 = { .min = 12, .max = 22 },
343 .m2 = { .min = 5, .max = 9 },
344 .p = { .min = 14, .max = 56 },
345 .p1 = { .min = 2, .max = 8 },
346 .p2 = { .dot_limit = 225000,
347 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800348};
349
Eric Anholt273e27c2011-03-30 13:01:10 -0700350/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800351static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700352 .dot = { .min = 25000, .max = 350000 },
353 .vco = { .min = 1760000, .max = 3510000 },
354 .n = { .min = 1, .max = 2 },
355 .m = { .min = 79, .max = 126 },
356 .m1 = { .min = 12, .max = 22 },
357 .m2 = { .min = 5, .max = 9 },
358 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400359 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700360 .p2 = { .dot_limit = 225000,
361 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800362};
363
364static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700365 .dot = { .min = 25000, .max = 350000 },
366 .vco = { .min = 1760000, .max = 3510000 },
367 .n = { .min = 1, .max = 3 },
368 .m = { .min = 79, .max = 126 },
369 .m1 = { .min = 12, .max = 22 },
370 .m2 = { .min = 5, .max = 9 },
371 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400372 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700373 .p2 = { .dot_limit = 225000,
374 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800375};
376
Ville Syrjälädc730512013-09-24 21:26:30 +0300377static const intel_limit_t intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300378 /*
379 * These are the data rate limits (measured in fast clocks)
380 * since those are the strictest limits we have. The fast
381 * clock and actual rate limits are more relaxed, so checking
382 * them would make no difference.
383 */
384 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200385 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700386 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700387 .m1 = { .min = 2, .max = 3 },
388 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300389 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300390 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700391};
392
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300393static const intel_limit_t intel_limits_chv = {
394 /*
395 * These are the data rate limits (measured in fast clocks)
396 * since those are the strictest limits we have. The fast
397 * clock and actual rate limits are more relaxed, so checking
398 * them would make no difference.
399 */
400 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200401 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300402 .n = { .min = 1, .max = 1 },
403 .m1 = { .min = 2, .max = 2 },
404 .m2 = { .min = 24 << 22, .max = 175 << 22 },
405 .p1 = { .min = 2, .max = 4 },
406 .p2 = { .p2_slow = 1, .p2_fast = 14 },
407};
408
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200409static const intel_limit_t intel_limits_bxt = {
410 /* FIXME: find real dot limits */
411 .dot = { .min = 0, .max = INT_MAX },
412 .vco = { .min = 4800000, .max = 6480000 },
413 .n = { .min = 1, .max = 1 },
414 .m1 = { .min = 2, .max = 2 },
415 /* FIXME: find real m2 limits */
416 .m2 = { .min = 2 << 22, .max = 255 << 22 },
417 .p1 = { .min = 2, .max = 4 },
418 .p2 = { .p2_slow = 1, .p2_fast = 20 },
419};
420
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300421static void vlv_clock(int refclk, intel_clock_t *clock)
422{
423 clock->m = clock->m1 * clock->m2;
424 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200425 if (WARN_ON(clock->n == 0 || clock->p == 0))
426 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300427 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
428 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300429}
430
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200431static bool
432needs_modeset(struct drm_crtc_state *state)
433{
434 return state->mode_changed || state->active_changed;
435}
436
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300437/**
438 * Returns whether any output on the specified pipe is of the specified type
439 */
Damien Lespiau40935612014-10-29 11:16:59 +0000440bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300441{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300442 struct drm_device *dev = crtc->base.dev;
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300443 struct intel_encoder *encoder;
444
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300445 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300446 if (encoder->type == type)
447 return true;
448
449 return false;
450}
451
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200452/**
453 * Returns whether any output on the specified pipe will have the specified
454 * type after a staged modeset is complete, i.e., the same as
455 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
456 * encoder->crtc.
457 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200458static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
459 int type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200460{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200461 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300462 struct drm_connector *connector;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200463 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200464 struct intel_encoder *encoder;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200465 int i, num_connectors = 0;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200466
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300467 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200468 if (connector_state->crtc != crtc_state->base.crtc)
469 continue;
470
471 num_connectors++;
472
473 encoder = to_intel_encoder(connector_state->best_encoder);
474 if (encoder->type == type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200475 return true;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200476 }
477
478 WARN_ON(num_connectors == 0);
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200479
480 return false;
481}
482
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200483static const intel_limit_t *
484intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800485{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200486 struct drm_device *dev = crtc_state->base.crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800487 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800488
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200489 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100490 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000491 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800492 limit = &intel_limits_ironlake_dual_lvds_100m;
493 else
494 limit = &intel_limits_ironlake_dual_lvds;
495 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000496 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800497 limit = &intel_limits_ironlake_single_lvds_100m;
498 else
499 limit = &intel_limits_ironlake_single_lvds;
500 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200501 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800502 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800503
504 return limit;
505}
506
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200507static const intel_limit_t *
508intel_g4x_limit(struct intel_crtc_state *crtc_state)
Ma Ling044c7c42009-03-18 20:13:23 +0800509{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200510 struct drm_device *dev = crtc_state->base.crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800511 const intel_limit_t *limit;
512
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200513 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100514 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700515 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800516 else
Keith Packarde4b36692009-06-05 19:22:17 -0700517 limit = &intel_limits_g4x_single_channel_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200518 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
519 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700520 limit = &intel_limits_g4x_hdmi;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200521 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700522 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800523 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700524 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800525
526 return limit;
527}
528
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200529static const intel_limit_t *
530intel_limit(struct intel_crtc_state *crtc_state, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800531{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200532 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800533 const intel_limit_t *limit;
534
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200535 if (IS_BROXTON(dev))
536 limit = &intel_limits_bxt;
537 else if (HAS_PCH_SPLIT(dev))
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200538 limit = intel_ironlake_limit(crtc_state, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800539 else if (IS_G4X(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200540 limit = intel_g4x_limit(crtc_state);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500541 } else if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200542 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500543 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800544 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500545 limit = &intel_limits_pineview_sdvo;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300546 } else if (IS_CHERRYVIEW(dev)) {
547 limit = &intel_limits_chv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700548 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjälädc730512013-09-24 21:26:30 +0300549 limit = &intel_limits_vlv;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100550 } else if (!IS_GEN2(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200551 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100552 limit = &intel_limits_i9xx_lvds;
553 else
554 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800555 } else {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200556 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700557 limit = &intel_limits_i8xx_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200558 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Keith Packarde4b36692009-06-05 19:22:17 -0700559 limit = &intel_limits_i8xx_dvo;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200560 else
561 limit = &intel_limits_i8xx_dac;
Jesse Barnes79e53942008-11-07 14:24:08 -0800562 }
563 return limit;
564}
565
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500566/* m1 is reserved as 0 in Pineview, n is a ring counter */
567static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800568{
Shaohua Li21778322009-02-23 15:19:16 +0800569 clock->m = clock->m2 + 2;
570 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200571 if (WARN_ON(clock->n == 0 || clock->p == 0))
572 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300573 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
574 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Shaohua Li21778322009-02-23 15:19:16 +0800575}
576
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200577static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
578{
579 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
580}
581
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200582static void i9xx_clock(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800583{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200584 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800585 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200586 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
587 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300588 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
589 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Jesse Barnes79e53942008-11-07 14:24:08 -0800590}
591
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300592static void chv_clock(int refclk, intel_clock_t *clock)
593{
594 clock->m = clock->m1 * clock->m2;
595 clock->p = clock->p1 * clock->p2;
596 if (WARN_ON(clock->n == 0 || clock->p == 0))
597 return;
598 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
599 clock->n << 22);
600 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
601}
602
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800603#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800604/**
605 * Returns whether the given set of divisors are valid for a given refclk with
606 * the given connectors.
607 */
608
Chris Wilson1b894b52010-12-14 20:04:54 +0000609static bool intel_PLL_is_valid(struct drm_device *dev,
610 const intel_limit_t *limit,
611 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800612{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300613 if (clock->n < limit->n.min || limit->n.max < clock->n)
614 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800615 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400616 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800617 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400618 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800619 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400620 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300621
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200622 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300623 if (clock->m1 <= clock->m2)
624 INTELPllInvalid("m1 <= m2\n");
625
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200626 if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300627 if (clock->p < limit->p.min || limit->p.max < clock->p)
628 INTELPllInvalid("p out of range\n");
629 if (clock->m < limit->m.min || limit->m.max < clock->m)
630 INTELPllInvalid("m out of range\n");
631 }
632
Jesse Barnes79e53942008-11-07 14:24:08 -0800633 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400634 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800635 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
636 * connector, etc., rather than just a single range.
637 */
638 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400639 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800640
641 return true;
642}
643
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300644static int
645i9xx_select_p2_div(const intel_limit_t *limit,
646 const struct intel_crtc_state *crtc_state,
647 int target)
Jesse Barnes79e53942008-11-07 14:24:08 -0800648{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300649 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800650
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200651 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800652 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100653 * For LVDS just rely on its current settings for dual-channel.
654 * We haven't figured out how to reliably set up different
655 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800656 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100657 if (intel_is_dual_link_lvds(dev))
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300658 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800659 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300660 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800661 } else {
662 if (target < limit->p2.dot_limit)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300663 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800664 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300665 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800666 }
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300667}
668
669static bool
670i9xx_find_best_dpll(const intel_limit_t *limit,
671 struct intel_crtc_state *crtc_state,
672 int target, int refclk, intel_clock_t *match_clock,
673 intel_clock_t *best_clock)
674{
675 struct drm_device *dev = crtc_state->base.crtc->dev;
676 intel_clock_t clock;
677 int err = target;
Jesse Barnes79e53942008-11-07 14:24:08 -0800678
Akshay Joshi0206e352011-08-16 15:34:10 -0400679 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800680
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300681 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
682
Zhao Yakui42158662009-11-20 11:24:18 +0800683 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
684 clock.m1++) {
685 for (clock.m2 = limit->m2.min;
686 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200687 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800688 break;
689 for (clock.n = limit->n.min;
690 clock.n <= limit->n.max; clock.n++) {
691 for (clock.p1 = limit->p1.min;
692 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800693 int this_err;
694
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200695 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000696 if (!intel_PLL_is_valid(dev, limit,
697 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800698 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800699 if (match_clock &&
700 clock.p != match_clock->p)
701 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800702
703 this_err = abs(clock.dot - target);
704 if (this_err < err) {
705 *best_clock = clock;
706 err = this_err;
707 }
708 }
709 }
710 }
711 }
712
713 return (err != target);
714}
715
Ma Lingd4906092009-03-18 20:13:27 +0800716static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200717pnv_find_best_dpll(const intel_limit_t *limit,
718 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200719 int target, int refclk, intel_clock_t *match_clock,
720 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200721{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300722 struct drm_device *dev = crtc_state->base.crtc->dev;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200723 intel_clock_t clock;
724 int err = target;
725
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200726 memset(best_clock, 0, sizeof(*best_clock));
727
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300728 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
729
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200730 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
731 clock.m1++) {
732 for (clock.m2 = limit->m2.min;
733 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200734 for (clock.n = limit->n.min;
735 clock.n <= limit->n.max; clock.n++) {
736 for (clock.p1 = limit->p1.min;
737 clock.p1 <= limit->p1.max; clock.p1++) {
738 int this_err;
739
740 pineview_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800741 if (!intel_PLL_is_valid(dev, limit,
742 &clock))
743 continue;
744 if (match_clock &&
745 clock.p != match_clock->p)
746 continue;
747
748 this_err = abs(clock.dot - target);
749 if (this_err < err) {
750 *best_clock = clock;
751 err = this_err;
752 }
753 }
754 }
755 }
756 }
757
758 return (err != target);
759}
760
Ma Lingd4906092009-03-18 20:13:27 +0800761static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200762g4x_find_best_dpll(const intel_limit_t *limit,
763 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200764 int target, int refclk, intel_clock_t *match_clock,
765 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800766{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300767 struct drm_device *dev = crtc_state->base.crtc->dev;
Ma Lingd4906092009-03-18 20:13:27 +0800768 intel_clock_t clock;
769 int max_n;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300770 bool found = false;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400771 /* approximately equals target * 0.00585 */
772 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800773
774 memset(best_clock, 0, sizeof(*best_clock));
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300775
776 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
777
Ma Lingd4906092009-03-18 20:13:27 +0800778 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200779 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800780 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200781 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800782 for (clock.m1 = limit->m1.max;
783 clock.m1 >= limit->m1.min; clock.m1--) {
784 for (clock.m2 = limit->m2.max;
785 clock.m2 >= limit->m2.min; clock.m2--) {
786 for (clock.p1 = limit->p1.max;
787 clock.p1 >= limit->p1.min; clock.p1--) {
788 int this_err;
789
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200790 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000791 if (!intel_PLL_is_valid(dev, limit,
792 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800793 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000794
795 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800796 if (this_err < err_most) {
797 *best_clock = clock;
798 err_most = this_err;
799 max_n = clock.n;
800 found = true;
801 }
802 }
803 }
804 }
805 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800806 return found;
807}
Ma Lingd4906092009-03-18 20:13:27 +0800808
Imre Deakd5dd62b2015-03-17 11:40:03 +0200809/*
810 * Check if the calculated PLL configuration is more optimal compared to the
811 * best configuration and error found so far. Return the calculated error.
812 */
813static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
814 const intel_clock_t *calculated_clock,
815 const intel_clock_t *best_clock,
816 unsigned int best_error_ppm,
817 unsigned int *error_ppm)
818{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200819 /*
820 * For CHV ignore the error and consider only the P value.
821 * Prefer a bigger P value based on HW requirements.
822 */
823 if (IS_CHERRYVIEW(dev)) {
824 *error_ppm = 0;
825
826 return calculated_clock->p > best_clock->p;
827 }
828
Imre Deak24be4e42015-03-17 11:40:04 +0200829 if (WARN_ON_ONCE(!target_freq))
830 return false;
831
Imre Deakd5dd62b2015-03-17 11:40:03 +0200832 *error_ppm = div_u64(1000000ULL *
833 abs(target_freq - calculated_clock->dot),
834 target_freq);
835 /*
836 * Prefer a better P value over a better (smaller) error if the error
837 * is small. Ensure this preference for future configurations too by
838 * setting the error to 0.
839 */
840 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
841 *error_ppm = 0;
842
843 return true;
844 }
845
846 return *error_ppm + 10 < best_error_ppm;
847}
848
Zhenyu Wang2c072452009-06-05 15:38:42 +0800849static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200850vlv_find_best_dpll(const intel_limit_t *limit,
851 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200852 int target, int refclk, intel_clock_t *match_clock,
853 intel_clock_t *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700854{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200855 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300856 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300857 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300858 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300859 /* min update 19.2 MHz */
860 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300861 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700862
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300863 target *= 5; /* fast clock */
864
865 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700866
867 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300868 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300869 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300870 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300871 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300872 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700873 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300874 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200875 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300876
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300877 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
878 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300879
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300880 vlv_clock(refclk, &clock);
881
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300882 if (!intel_PLL_is_valid(dev, limit,
883 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300884 continue;
885
Imre Deakd5dd62b2015-03-17 11:40:03 +0200886 if (!vlv_PLL_is_optimal(dev, target,
887 &clock,
888 best_clock,
889 bestppm, &ppm))
890 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300891
Imre Deakd5dd62b2015-03-17 11:40:03 +0200892 *best_clock = clock;
893 bestppm = ppm;
894 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700895 }
896 }
897 }
898 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700899
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300900 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700901}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700902
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300903static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200904chv_find_best_dpll(const intel_limit_t *limit,
905 struct intel_crtc_state *crtc_state,
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300906 int target, int refclk, intel_clock_t *match_clock,
907 intel_clock_t *best_clock)
908{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200909 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300910 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200911 unsigned int best_error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300912 intel_clock_t clock;
913 uint64_t m2;
914 int found = false;
915
916 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200917 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300918
919 /*
920 * Based on hardware doc, the n always set to 1, and m1 always
921 * set to 2. If requires to support 200Mhz refclk, we need to
922 * revisit this because n may not 1 anymore.
923 */
924 clock.n = 1, clock.m1 = 2;
925 target *= 5; /* fast clock */
926
927 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
928 for (clock.p2 = limit->p2.p2_fast;
929 clock.p2 >= limit->p2.p2_slow;
930 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +0200931 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300932
933 clock.p = clock.p1 * clock.p2;
934
935 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
936 clock.n) << 22, refclk * clock.m1);
937
938 if (m2 > INT_MAX/clock.m1)
939 continue;
940
941 clock.m2 = m2;
942
943 chv_clock(refclk, &clock);
944
945 if (!intel_PLL_is_valid(dev, limit, &clock))
946 continue;
947
Imre Deak9ca3ba02015-03-17 11:40:05 +0200948 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
949 best_error_ppm, &error_ppm))
950 continue;
951
952 *best_clock = clock;
953 best_error_ppm = error_ppm;
954 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300955 }
956 }
957
958 return found;
959}
960
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200961bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
962 intel_clock_t *best_clock)
963{
964 int refclk = i9xx_get_refclk(crtc_state, 0);
965
966 return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
967 target_clock, refclk, NULL, best_clock);
968}
969
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300970bool intel_crtc_active(struct drm_crtc *crtc)
971{
972 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
973
974 /* Be paranoid as we can arrive here with only partial
975 * state retrieved from the hardware during setup.
976 *
Damien Lespiau241bfc32013-09-25 16:45:37 +0100977 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300978 * as Haswell has gained clock readout/fastboot support.
979 *
Dave Airlie66e514c2014-04-03 07:51:54 +1000980 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300981 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -0700982 *
983 * FIXME: The intel_crtc->active here should be switched to
984 * crtc->state->active once we have proper CRTC states wired up
985 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300986 */
Matt Roperc3d1f432015-03-09 10:19:23 -0700987 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200988 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300989}
990
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200991enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
992 enum pipe pipe)
993{
994 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
995 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
996
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200997 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200998}
999
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001000static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1001{
1002 struct drm_i915_private *dev_priv = dev->dev_private;
1003 u32 reg = PIPEDSL(pipe);
1004 u32 line1, line2;
1005 u32 line_mask;
1006
1007 if (IS_GEN2(dev))
1008 line_mask = DSL_LINEMASK_GEN2;
1009 else
1010 line_mask = DSL_LINEMASK_GEN3;
1011
1012 line1 = I915_READ(reg) & line_mask;
1013 mdelay(5);
1014 line2 = I915_READ(reg) & line_mask;
1015
1016 return line1 == line2;
1017}
1018
Keith Packardab7ad7f2010-10-03 00:33:06 -07001019/*
1020 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001021 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001022 *
1023 * After disabling a pipe, we can't wait for vblank in the usual way,
1024 * spinning on the vblank interrupt status bit, since we won't actually
1025 * see an interrupt when the pipe is disabled.
1026 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001027 * On Gen4 and above:
1028 * wait for the pipe register state bit to turn off
1029 *
1030 * Otherwise:
1031 * wait for the display line value to settle (it usually
1032 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001033 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001034 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001035static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001036{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001037 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001038 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001039 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001040 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001041
Keith Packardab7ad7f2010-10-03 00:33:06 -07001042 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001043 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001044
Keith Packardab7ad7f2010-10-03 00:33:06 -07001045 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +01001046 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1047 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001048 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001049 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001050 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001051 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001052 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001053 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001054}
1055
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001056/*
1057 * ibx_digital_port_connected - is the specified port connected?
1058 * @dev_priv: i915 private structure
1059 * @port: the port to test
1060 *
1061 * Returns true if @port is connected, false otherwise.
1062 */
1063bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
1064 struct intel_digital_port *port)
1065{
1066 u32 bit;
1067
Damien Lespiauc36346e2012-12-13 16:09:03 +00001068 if (HAS_PCH_IBX(dev_priv->dev)) {
Robin Schroereba905b2014-05-18 02:24:50 +02001069 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +00001070 case PORT_B:
1071 bit = SDE_PORTB_HOTPLUG;
1072 break;
1073 case PORT_C:
1074 bit = SDE_PORTC_HOTPLUG;
1075 break;
1076 case PORT_D:
1077 bit = SDE_PORTD_HOTPLUG;
1078 break;
1079 default:
1080 return true;
1081 }
1082 } else {
Robin Schroereba905b2014-05-18 02:24:50 +02001083 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +00001084 case PORT_B:
1085 bit = SDE_PORTB_HOTPLUG_CPT;
1086 break;
1087 case PORT_C:
1088 bit = SDE_PORTC_HOTPLUG_CPT;
1089 break;
1090 case PORT_D:
1091 bit = SDE_PORTD_HOTPLUG_CPT;
1092 break;
1093 default:
1094 return true;
1095 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001096 }
1097
1098 return I915_READ(SDEISR) & bit;
1099}
1100
Jesse Barnesb24e7172011-01-04 15:09:30 -08001101static const char *state_string(bool enabled)
1102{
1103 return enabled ? "on" : "off";
1104}
1105
1106/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001107void assert_pll(struct drm_i915_private *dev_priv,
1108 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001109{
1110 int reg;
1111 u32 val;
1112 bool cur_state;
1113
1114 reg = DPLL(pipe);
1115 val = I915_READ(reg);
1116 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001117 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001118 "PLL state assertion failure (expected %s, current %s)\n",
1119 state_string(state), state_string(cur_state));
1120}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001121
Jani Nikula23538ef2013-08-27 15:12:22 +03001122/* XXX: the dsi pll is shared between MIPI DSI ports */
1123static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1124{
1125 u32 val;
1126 bool cur_state;
1127
Ville Syrjäläa5805162015-05-26 20:42:30 +03001128 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001129 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001130 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001131
1132 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001133 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001134 "DSI PLL state assertion failure (expected %s, current %s)\n",
1135 state_string(state), state_string(cur_state));
1136}
1137#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1138#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1139
Daniel Vetter55607e82013-06-16 21:42:39 +02001140struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +02001141intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -08001142{
Daniel Vettere2b78262013-06-07 23:10:03 +02001143 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1144
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001145 if (crtc->config->shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +02001146 return NULL;
1147
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001148 return &dev_priv->shared_dplls[crtc->config->shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +02001149}
1150
Jesse Barnesb24e7172011-01-04 15:09:30 -08001151/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +02001152void assert_shared_dpll(struct drm_i915_private *dev_priv,
1153 struct intel_shared_dpll *pll,
1154 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001155{
Jesse Barnes040484a2011-01-03 12:14:26 -08001156 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +02001157 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001158
Chris Wilson92b27b02012-05-20 18:10:50 +01001159 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +02001160 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001161 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001162
Daniel Vetter53589012013-06-05 13:34:16 +02001163 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Rob Clarke2c719b2014-12-15 13:56:32 -05001164 I915_STATE_WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +02001165 "%s assertion failure (expected %s, current %s)\n",
1166 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001167}
Jesse Barnes040484a2011-01-03 12:14:26 -08001168
1169static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1170 enum pipe pipe, bool state)
1171{
1172 int reg;
1173 u32 val;
1174 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001175 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1176 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001177
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001178 if (HAS_DDI(dev_priv->dev)) {
1179 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -02001180 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001181 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -02001182 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001183 } else {
1184 reg = FDI_TX_CTL(pipe);
1185 val = I915_READ(reg);
1186 cur_state = !!(val & FDI_TX_ENABLE);
1187 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001188 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001189 "FDI TX state assertion failure (expected %s, current %s)\n",
1190 state_string(state), state_string(cur_state));
1191}
1192#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1193#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1194
1195static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1196 enum pipe pipe, bool state)
1197{
1198 int reg;
1199 u32 val;
1200 bool cur_state;
1201
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001202 reg = FDI_RX_CTL(pipe);
1203 val = I915_READ(reg);
1204 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001205 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001206 "FDI RX state assertion failure (expected %s, current %s)\n",
1207 state_string(state), state_string(cur_state));
1208}
1209#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1210#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1211
1212static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1213 enum pipe pipe)
1214{
1215 int reg;
1216 u32 val;
1217
1218 /* ILK FDI PLL is always enabled */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001219 if (INTEL_INFO(dev_priv->dev)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001220 return;
1221
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001222 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001223 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001224 return;
1225
Jesse Barnes040484a2011-01-03 12:14:26 -08001226 reg = FDI_TX_CTL(pipe);
1227 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001228 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 -08001229}
1230
Daniel Vetter55607e82013-06-16 21:42:39 +02001231void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1232 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001233{
1234 int reg;
1235 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001236 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001237
1238 reg = FDI_RX_CTL(pipe);
1239 val = I915_READ(reg);
Daniel Vetter55607e82013-06-16 21:42:39 +02001240 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001241 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001242 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1243 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001244}
1245
Daniel Vetterb680c372014-09-19 18:27:27 +02001246void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1247 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001248{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001249 struct drm_device *dev = dev_priv->dev;
1250 int pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001251 u32 val;
1252 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001253 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001254
Jani Nikulabedd4db2014-08-22 15:04:13 +03001255 if (WARN_ON(HAS_DDI(dev)))
1256 return;
1257
1258 if (HAS_PCH_SPLIT(dev)) {
1259 u32 port_sel;
1260
Jesse Barnesea0760c2011-01-04 15:09:32 -08001261 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001262 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1263
1264 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1265 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1266 panel_pipe = PIPE_B;
1267 /* XXX: else fix for eDP */
1268 } else if (IS_VALLEYVIEW(dev)) {
1269 /* presumably write lock depends on pipe, not port select */
1270 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1271 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001272 } else {
1273 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001274 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1275 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001276 }
1277
1278 val = I915_READ(pp_reg);
1279 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001280 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001281 locked = false;
1282
Rob Clarke2c719b2014-12-15 13:56:32 -05001283 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001284 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001285 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001286}
1287
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001288static void assert_cursor(struct drm_i915_private *dev_priv,
1289 enum pipe pipe, bool state)
1290{
1291 struct drm_device *dev = dev_priv->dev;
1292 bool cur_state;
1293
Paulo Zanonid9d82082014-02-27 16:30:56 -03001294 if (IS_845G(dev) || IS_I865G(dev))
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001295 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001296 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001297 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001298
Rob Clarke2c719b2014-12-15 13:56:32 -05001299 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001300 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1301 pipe_name(pipe), state_string(state), state_string(cur_state));
1302}
1303#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1304#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1305
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001306void assert_pipe(struct drm_i915_private *dev_priv,
1307 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001308{
1309 int reg;
1310 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001311 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001312 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1313 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001314
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001315 /* if we need the pipe quirk it must be always on */
1316 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1317 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001318 state = true;
1319
Daniel Vetterf458ebb2014-09-30 10:56:39 +02001320 if (!intel_display_power_is_enabled(dev_priv,
Paulo Zanonib97186f2013-05-03 12:15:36 -03001321 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001322 cur_state = false;
1323 } else {
1324 reg = PIPECONF(cpu_transcoder);
1325 val = I915_READ(reg);
1326 cur_state = !!(val & PIPECONF_ENABLE);
1327 }
1328
Rob Clarke2c719b2014-12-15 13:56:32 -05001329 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001330 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001331 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001332}
1333
Chris Wilson931872f2012-01-16 23:01:13 +00001334static void assert_plane(struct drm_i915_private *dev_priv,
1335 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001336{
1337 int reg;
1338 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001339 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001340
1341 reg = DSPCNTR(plane);
1342 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001343 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001344 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001345 "plane %c assertion failure (expected %s, current %s)\n",
1346 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001347}
1348
Chris Wilson931872f2012-01-16 23:01:13 +00001349#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1350#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1351
Jesse Barnesb24e7172011-01-04 15:09:30 -08001352static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1353 enum pipe pipe)
1354{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001355 struct drm_device *dev = dev_priv->dev;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001356 int reg, i;
1357 u32 val;
1358 int cur_pipe;
1359
Ville Syrjälä653e1022013-06-04 13:49:05 +03001360 /* Primary planes are fixed to pipes on gen4+ */
1361 if (INTEL_INFO(dev)->gen >= 4) {
Adam Jackson28c057942011-10-07 14:38:42 -04001362 reg = DSPCNTR(pipe);
1363 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001364 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001365 "plane %c assertion failure, should be disabled but not\n",
1366 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001367 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001368 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001369
Jesse Barnesb24e7172011-01-04 15:09:30 -08001370 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001371 for_each_pipe(dev_priv, i) {
Jesse Barnesb24e7172011-01-04 15:09:30 -08001372 reg = DSPCNTR(i);
1373 val = I915_READ(reg);
1374 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1375 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001376 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001377 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1378 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001379 }
1380}
1381
Jesse Barnes19332d72013-03-28 09:55:38 -07001382static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1383 enum pipe pipe)
1384{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001385 struct drm_device *dev = dev_priv->dev;
Damien Lespiau1fe47782014-03-03 17:31:47 +00001386 int reg, sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001387 u32 val;
1388
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001389 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001390 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001391 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001392 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001393 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1394 sprite, pipe_name(pipe));
1395 }
1396 } else if (IS_VALLEYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001397 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +00001398 reg = SPCNTR(pipe, sprite);
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001399 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001400 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001401 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001402 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001403 }
1404 } else if (INTEL_INFO(dev)->gen >= 7) {
1405 reg = SPRCTL(pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001406 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001407 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001408 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001409 plane_name(pipe), pipe_name(pipe));
1410 } else if (INTEL_INFO(dev)->gen >= 5) {
1411 reg = DVSCNTR(pipe);
1412 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001413 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001414 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1415 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001416 }
1417}
1418
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001419static void assert_vblank_disabled(struct drm_crtc *crtc)
1420{
Rob Clarke2c719b2014-12-15 13:56:32 -05001421 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001422 drm_crtc_vblank_put(crtc);
1423}
1424
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001425static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
Jesse Barnes92f25842011-01-04 15:09:34 -08001426{
1427 u32 val;
1428 bool enabled;
1429
Rob Clarke2c719b2014-12-15 13:56:32 -05001430 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001431
Jesse Barnes92f25842011-01-04 15:09:34 -08001432 val = I915_READ(PCH_DREF_CONTROL);
1433 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1434 DREF_SUPERSPREAD_SOURCE_MASK));
Rob Clarke2c719b2014-12-15 13:56:32 -05001435 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
Jesse Barnes92f25842011-01-04 15:09:34 -08001436}
1437
Daniel Vetterab9412b2013-05-03 11:49:46 +02001438static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1439 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001440{
1441 int reg;
1442 u32 val;
1443 bool enabled;
1444
Daniel Vetterab9412b2013-05-03 11:49:46 +02001445 reg = PCH_TRANSCONF(pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001446 val = I915_READ(reg);
1447 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001448 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001449 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1450 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001451}
1452
Keith Packard4e634382011-08-06 10:39:45 -07001453static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1454 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001455{
1456 if ((val & DP_PORT_EN) == 0)
1457 return false;
1458
1459 if (HAS_PCH_CPT(dev_priv->dev)) {
1460 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1461 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1462 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1463 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001464 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1465 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1466 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001467 } else {
1468 if ((val & DP_PIPE_MASK) != (pipe << 30))
1469 return false;
1470 }
1471 return true;
1472}
1473
Keith Packard1519b992011-08-06 10:35:34 -07001474static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1475 enum pipe pipe, u32 val)
1476{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001477 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001478 return false;
1479
1480 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001481 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001482 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001483 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1484 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1485 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001486 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001487 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001488 return false;
1489 }
1490 return true;
1491}
1492
1493static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1494 enum pipe pipe, u32 val)
1495{
1496 if ((val & LVDS_PORT_EN) == 0)
1497 return false;
1498
1499 if (HAS_PCH_CPT(dev_priv->dev)) {
1500 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1501 return false;
1502 } else {
1503 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1504 return false;
1505 }
1506 return true;
1507}
1508
1509static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1510 enum pipe pipe, u32 val)
1511{
1512 if ((val & ADPA_DAC_ENABLE) == 0)
1513 return false;
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 & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1519 return false;
1520 }
1521 return true;
1522}
1523
Jesse Barnes291906f2011-02-02 12:28:03 -08001524static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001525 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001526{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001527 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001528 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001529 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001530 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001531
Rob Clarke2c719b2014-12-15 13:56:32 -05001532 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001533 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001534 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001535}
1536
1537static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1538 enum pipe pipe, int reg)
1539{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001540 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001541 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001542 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001543 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001544
Rob Clarke2c719b2014-12-15 13:56:32 -05001545 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001546 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001547 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001548}
1549
1550static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1551 enum pipe pipe)
1552{
1553 int reg;
1554 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001555
Keith Packardf0575e92011-07-25 22:12:43 -07001556 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1557 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1558 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001559
1560 reg = PCH_ADPA;
1561 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001562 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001563 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001564 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001565
1566 reg = PCH_LVDS;
1567 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001568 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001569 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001570 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001571
Paulo Zanonie2debe92013-02-18 19:00:27 -03001572 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1573 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1574 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001575}
1576
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001577static void intel_init_dpio(struct drm_device *dev)
1578{
1579 struct drm_i915_private *dev_priv = dev->dev_private;
1580
1581 if (!IS_VALLEYVIEW(dev))
1582 return;
1583
Chon Ming Leea09cadd2014-04-09 13:28:14 +03001584 /*
1585 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1586 * CHV x1 PHY (DP/HDMI D)
1587 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1588 */
1589 if (IS_CHERRYVIEW(dev)) {
1590 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1591 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1592 } else {
1593 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1594 }
Jesse Barnes5382f5f352013-12-16 16:34:24 -08001595}
1596
Ville Syrjäläd288f652014-10-28 13:20:22 +02001597static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001598 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001599{
Daniel Vetter426115c2013-07-11 22:13:42 +02001600 struct drm_device *dev = crtc->base.dev;
1601 struct drm_i915_private *dev_priv = dev->dev_private;
1602 int reg = DPLL(crtc->pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02001603 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001604
Daniel Vetter426115c2013-07-11 22:13:42 +02001605 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001606
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001607 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001608 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1609
1610 /* PLL is protected by panel, make sure we can write it */
Jani Nikula6a9e7362014-08-22 15:06:35 +03001611 if (IS_MOBILE(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001612 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001613
Daniel Vetter426115c2013-07-11 22:13:42 +02001614 I915_WRITE(reg, dpll);
1615 POSTING_READ(reg);
1616 udelay(150);
1617
1618 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1619 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1620
Ville Syrjäläd288f652014-10-28 13:20:22 +02001621 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
Daniel Vetter426115c2013-07-11 22:13:42 +02001622 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001623
1624 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001625 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001626 POSTING_READ(reg);
1627 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001628 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001629 POSTING_READ(reg);
1630 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001631 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001632 POSTING_READ(reg);
1633 udelay(150); /* wait for warmup */
1634}
1635
Ville Syrjäläd288f652014-10-28 13:20:22 +02001636static void chv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001637 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001638{
1639 struct drm_device *dev = crtc->base.dev;
1640 struct drm_i915_private *dev_priv = dev->dev_private;
1641 int pipe = crtc->pipe;
1642 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001643 u32 tmp;
1644
1645 assert_pipe_disabled(dev_priv, crtc->pipe);
1646
1647 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1648
Ville Syrjäläa5805162015-05-26 20:42:30 +03001649 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001650
1651 /* Enable back the 10bit clock to display controller */
1652 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1653 tmp |= DPIO_DCLKP_EN;
1654 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1655
Ville Syrjälä54433e92015-05-26 20:42:31 +03001656 mutex_unlock(&dev_priv->sb_lock);
1657
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001658 /*
1659 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1660 */
1661 udelay(1);
1662
1663 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001664 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001665
1666 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001667 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001668 DRM_ERROR("PLL %d failed to lock\n", pipe);
1669
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001670 /* not sure when this should be written */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001671 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001672 POSTING_READ(DPLL_MD(pipe));
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001673}
1674
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001675static int intel_num_dvo_pipes(struct drm_device *dev)
1676{
1677 struct intel_crtc *crtc;
1678 int count = 0;
1679
1680 for_each_intel_crtc(dev, crtc)
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001681 count += crtc->base.state->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001682 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001683
1684 return count;
1685}
1686
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001687static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001688{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001689 struct drm_device *dev = crtc->base.dev;
1690 struct drm_i915_private *dev_priv = dev->dev_private;
1691 int reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001692 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001693
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001694 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001695
1696 /* No really, not for ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001697 BUG_ON(INTEL_INFO(dev)->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001698
1699 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001700 if (IS_MOBILE(dev) && !IS_I830(dev))
1701 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001702
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001703 /* Enable DVO 2x clock on both PLLs if necessary */
1704 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1705 /*
1706 * It appears to be important that we don't enable this
1707 * for the current pipe before otherwise configuring the
1708 * PLL. No idea how this should be handled if multiple
1709 * DVO outputs are enabled simultaneosly.
1710 */
1711 dpll |= DPLL_DVO_2X_MODE;
1712 I915_WRITE(DPLL(!crtc->pipe),
1713 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1714 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001715
1716 /* Wait for the clocks to stabilize. */
1717 POSTING_READ(reg);
1718 udelay(150);
1719
1720 if (INTEL_INFO(dev)->gen >= 4) {
1721 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001722 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001723 } else {
1724 /* The pixel multiplier can only be updated once the
1725 * DPLL is enabled and the clocks are stable.
1726 *
1727 * So write it again.
1728 */
1729 I915_WRITE(reg, dpll);
1730 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001731
1732 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001733 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001734 POSTING_READ(reg);
1735 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001736 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001737 POSTING_READ(reg);
1738 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001739 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001740 POSTING_READ(reg);
1741 udelay(150); /* wait for warmup */
1742}
1743
1744/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001745 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001746 * @dev_priv: i915 private structure
1747 * @pipe: pipe PLL to disable
1748 *
1749 * Disable the PLL for @pipe, making sure the pipe is off first.
1750 *
1751 * Note! This is for pre-ILK only.
1752 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001753static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001754{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001755 struct drm_device *dev = crtc->base.dev;
1756 struct drm_i915_private *dev_priv = dev->dev_private;
1757 enum pipe pipe = crtc->pipe;
1758
1759 /* Disable DVO 2x clock on both PLLs if necessary */
1760 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001761 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001762 !intel_num_dvo_pipes(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001763 I915_WRITE(DPLL(PIPE_B),
1764 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1765 I915_WRITE(DPLL(PIPE_A),
1766 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1767 }
1768
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001769 /* Don't disable pipe or pipe PLLs if needed */
1770 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1771 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001772 return;
1773
1774 /* Make sure the pipe isn't still relying on us */
1775 assert_pipe_disabled(dev_priv, pipe);
1776
Daniel Vetter50b44a42013-06-05 13:34:33 +02001777 I915_WRITE(DPLL(pipe), 0);
1778 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001779}
1780
Jesse Barnesf6071162013-10-01 10:41:38 -07001781static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1782{
1783 u32 val = 0;
1784
1785 /* Make sure the pipe isn't still relying on us */
1786 assert_pipe_disabled(dev_priv, pipe);
1787
Imre Deake5cbfbf2014-01-09 17:08:16 +02001788 /*
1789 * Leave integrated clock source and reference clock enabled for pipe B.
1790 * The latter is needed for VGA hotplug / manual detection.
1791 */
Jesse Barnesf6071162013-10-01 10:41:38 -07001792 if (pipe == PIPE_B)
Imre Deake5cbfbf2014-01-09 17:08:16 +02001793 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07001794 I915_WRITE(DPLL(pipe), val);
1795 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001796
1797}
1798
1799static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1800{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001801 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001802 u32 val;
1803
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001804 /* Make sure the pipe isn't still relying on us */
1805 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001806
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001807 /* Set PLL en = 0 */
Ville Syrjäläd17ec4c2014-06-28 02:03:59 +03001808 val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001809 if (pipe != PIPE_A)
1810 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1811 I915_WRITE(DPLL(pipe), val);
1812 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001813
Ville Syrjäläa5805162015-05-26 20:42:30 +03001814 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001815
1816 /* Disable 10bit clock to display controller */
1817 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1818 val &= ~DPIO_DCLKP_EN;
1819 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1820
Ville Syrjälä61407f62014-05-27 16:32:55 +03001821 /* disable left/right clock distribution */
1822 if (pipe != PIPE_B) {
1823 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1824 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1825 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1826 } else {
1827 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1828 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1829 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1830 }
1831
Ville Syrjäläa5805162015-05-26 20:42:30 +03001832 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001833}
1834
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001835void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001836 struct intel_digital_port *dport,
1837 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001838{
1839 u32 port_mask;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001840 int dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001841
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001842 switch (dport->port) {
1843 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001844 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001845 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001846 break;
1847 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001848 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001849 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001850 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001851 break;
1852 case PORT_D:
1853 port_mask = DPLL_PORTD_READY_MASK;
1854 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001855 break;
1856 default:
1857 BUG();
1858 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001859
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001860 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1861 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1862 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001863}
1864
Daniel Vetterb14b1052014-04-24 23:55:13 +02001865static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1866{
1867 struct drm_device *dev = crtc->base.dev;
1868 struct drm_i915_private *dev_priv = dev->dev_private;
1869 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1870
Chris Wilsonbe19f0f2014-05-28 16:16:42 +01001871 if (WARN_ON(pll == NULL))
1872 return;
1873
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001874 WARN_ON(!pll->config.crtc_mask);
Daniel Vetterb14b1052014-04-24 23:55:13 +02001875 if (pll->active == 0) {
1876 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1877 WARN_ON(pll->on);
1878 assert_shared_dpll_disabled(dev_priv, pll);
1879
1880 pll->mode_set(dev_priv, pll);
1881 }
1882}
1883
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001884/**
Daniel Vetter85b38942014-04-24 23:55:14 +02001885 * intel_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001886 * @dev_priv: i915 private structure
1887 * @pipe: pipe PLL to enable
1888 *
1889 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1890 * drives the transcoder clock.
1891 */
Daniel Vetter85b38942014-04-24 23:55:14 +02001892static void intel_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001893{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001894 struct drm_device *dev = crtc->base.dev;
1895 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001896 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001897
Daniel Vetter87a875b2013-06-05 13:34:19 +02001898 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001899 return;
1900
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001901 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001902 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001903
Damien Lespiau74dd6922014-07-29 18:06:17 +01001904 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
Daniel Vetter46edb022013-06-05 13:34:12 +02001905 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001906 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001907
Daniel Vettercdbd2312013-06-05 13:34:03 +02001908 if (pll->active++) {
1909 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001910 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001911 return;
1912 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001913 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001914
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001915 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1916
Daniel Vetter46edb022013-06-05 13:34:12 +02001917 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001918 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001919 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001920}
1921
Damien Lespiauf6daaec2014-08-09 23:00:56 +01001922static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001923{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001924 struct drm_device *dev = crtc->base.dev;
1925 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001926 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001927
Jesse Barnes92f25842011-01-04 15:09:34 -08001928 /* PCH only available on ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001929 BUG_ON(INTEL_INFO(dev)->gen < 5);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02001930 if (pll == NULL)
1931 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001932
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02001933 if (WARN_ON(!(pll->config.crtc_mask & (1 << drm_crtc_index(&crtc->base)))))
Chris Wilson48da64a2012-05-13 20:16:12 +01001934 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001935
Daniel Vetter46edb022013-06-05 13:34:12 +02001936 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1937 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001938 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001939
Chris Wilson48da64a2012-05-13 20:16:12 +01001940 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001941 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001942 return;
1943 }
1944
Daniel Vettere9d69442013-06-05 13:34:15 +02001945 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001946 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001947 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001948 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001949
Daniel Vetter46edb022013-06-05 13:34:12 +02001950 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001951 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001952 pll->on = false;
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001953
1954 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
Jesse Barnes92f25842011-01-04 15:09:34 -08001955}
1956
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001957static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1958 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001959{
Daniel Vetter23670b322012-11-01 09:15:30 +01001960 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001961 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001962 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001963 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001964
1965 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001966 BUG_ON(!HAS_PCH_SPLIT(dev));
Jesse Barnes040484a2011-01-03 12:14:26 -08001967
1968 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001969 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001970 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001971
1972 /* FDI must be feeding us bits for PCH ports */
1973 assert_fdi_tx_enabled(dev_priv, pipe);
1974 assert_fdi_rx_enabled(dev_priv, pipe);
1975
Daniel Vetter23670b322012-11-01 09:15:30 +01001976 if (HAS_PCH_CPT(dev)) {
1977 /* Workaround: Set the timing override bit before enabling the
1978 * pch transcoder. */
1979 reg = TRANS_CHICKEN2(pipe);
1980 val = I915_READ(reg);
1981 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1982 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001983 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001984
Daniel Vetterab9412b2013-05-03 11:49:46 +02001985 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001986 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001987 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001988
1989 if (HAS_PCH_IBX(dev_priv->dev)) {
1990 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001991 * Make the BPC in transcoder be consistent with
1992 * that in pipeconf reg. For HDMI we must use 8bpc
1993 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07001994 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001995 val &= ~PIPECONF_BPC_MASK;
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001996 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
1997 val |= PIPECONF_8BPC;
1998 else
1999 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07002000 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002001
2002 val &= ~TRANS_INTERLACE_MASK;
2003 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002004 if (HAS_PCH_IBX(dev_priv->dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002005 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002006 val |= TRANS_LEGACY_INTERLACED_ILK;
2007 else
2008 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002009 else
2010 val |= TRANS_PROGRESSIVE;
2011
Jesse Barnes040484a2011-01-03 12:14:26 -08002012 I915_WRITE(reg, val | TRANS_ENABLE);
2013 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002014 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08002015}
2016
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002017static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02002018 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08002019{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002020 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002021
2022 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03002023 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002024
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002025 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01002026 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02002027 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002028
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002029 /* Workaround: set timing override bit. */
2030 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002031 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002032 I915_WRITE(_TRANSA_CHICKEN2, val);
2033
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02002034 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02002035 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002036
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02002037 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
2038 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02002039 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002040 else
2041 val |= TRANS_PROGRESSIVE;
2042
Daniel Vetterab9412b2013-05-03 11:49:46 +02002043 I915_WRITE(LPT_TRANSCONF, val);
2044 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02002045 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002046}
2047
Paulo Zanonib8a4f402012-10-31 18:12:42 -02002048static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
2049 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08002050{
Daniel Vetter23670b322012-11-01 09:15:30 +01002051 struct drm_device *dev = dev_priv->dev;
2052 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08002053
2054 /* FDI relies on the transcoder */
2055 assert_fdi_tx_disabled(dev_priv, pipe);
2056 assert_fdi_rx_disabled(dev_priv, pipe);
2057
Jesse Barnes291906f2011-02-02 12:28:03 -08002058 /* Ports must be off as well */
2059 assert_pch_ports_disabled(dev_priv, pipe);
2060
Daniel Vetterab9412b2013-05-03 11:49:46 +02002061 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002062 val = I915_READ(reg);
2063 val &= ~TRANS_ENABLE;
2064 I915_WRITE(reg, val);
2065 /* wait for PCH transcoder off, transcoder state */
2066 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002067 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01002068
2069 if (!HAS_PCH_IBX(dev)) {
2070 /* Workaround: Clear the timing override chicken bit again. */
2071 reg = TRANS_CHICKEN2(pipe);
2072 val = I915_READ(reg);
2073 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2074 I915_WRITE(reg, val);
2075 }
Jesse Barnes040484a2011-01-03 12:14:26 -08002076}
2077
Paulo Zanoniab4d9662012-10-31 18:12:55 -02002078static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002079{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002080 u32 val;
2081
Daniel Vetterab9412b2013-05-03 11:49:46 +02002082 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002083 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02002084 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002085 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02002086 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02002087 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002088
2089 /* Workaround: clear timing override bit. */
2090 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002091 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002092 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08002093}
2094
2095/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002096 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02002097 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08002098 *
Paulo Zanoni03722642014-01-17 13:51:09 -02002099 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08002100 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002101 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02002102static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002103{
Paulo Zanoni03722642014-01-17 13:51:09 -02002104 struct drm_device *dev = crtc->base.dev;
2105 struct drm_i915_private *dev_priv = dev->dev_private;
2106 enum pipe pipe = crtc->pipe;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002107 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2108 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002109 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002110 int reg;
2111 u32 val;
2112
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002113 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002114 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002115 assert_sprites_disabled(dev_priv, pipe);
2116
Paulo Zanoni681e5812012-12-06 11:12:38 -02002117 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002118 pch_transcoder = TRANSCODER_A;
2119 else
2120 pch_transcoder = pipe;
2121
Jesse Barnesb24e7172011-01-04 15:09:30 -08002122 /*
2123 * A pipe without a PLL won't actually be able to drive bits from
2124 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2125 * need the check.
2126 */
Imre Deak50360402015-01-16 00:55:16 -08002127 if (HAS_GMCH_DISPLAY(dev_priv->dev))
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002128 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03002129 assert_dsi_pll_enabled(dev_priv);
2130 else
2131 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002132 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002133 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002134 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002135 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002136 assert_fdi_tx_pll_enabled(dev_priv,
2137 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08002138 }
2139 /* FIXME: assert CPU port conditions for SNB+ */
2140 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08002141
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002142 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002143 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002144 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002145 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2146 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002147 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002148 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002149
2150 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002151 POSTING_READ(reg);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002152}
2153
2154/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002155 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002156 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002157 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002158 * Disable the pipe of @crtc, making sure that various hardware
2159 * specific requirements are met, if applicable, e.g. plane
2160 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002161 *
2162 * Will wait until the pipe has shut down before returning.
2163 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002164static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002165{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002166 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002167 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002168 enum pipe pipe = crtc->pipe;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002169 int reg;
2170 u32 val;
2171
2172 /*
2173 * Make sure planes won't keep trying to pump pixels to us,
2174 * or we might hang the display.
2175 */
2176 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002177 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002178 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002179
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002180 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002181 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002182 if ((val & PIPECONF_ENABLE) == 0)
2183 return;
2184
Ville Syrjälä67adc642014-08-15 01:21:57 +03002185 /*
2186 * Double wide has implications for planes
2187 * so best keep it disabled when not needed.
2188 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002189 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002190 val &= ~PIPECONF_DOUBLE_WIDE;
2191
2192 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002193 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2194 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002195 val &= ~PIPECONF_ENABLE;
2196
2197 I915_WRITE(reg, val);
2198 if ((val & PIPECONF_ENABLE) == 0)
2199 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002200}
2201
Chris Wilson693db182013-03-05 14:52:39 +00002202static bool need_vtd_wa(struct drm_device *dev)
2203{
2204#ifdef CONFIG_INTEL_IOMMU
2205 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2206 return true;
2207#endif
2208 return false;
2209}
2210
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002211unsigned int
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002212intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
2213 uint64_t fb_format_modifier)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002214{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002215 unsigned int tile_height;
2216 uint32_t pixel_bytes;
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002217
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002218 switch (fb_format_modifier) {
2219 case DRM_FORMAT_MOD_NONE:
2220 tile_height = 1;
2221 break;
2222 case I915_FORMAT_MOD_X_TILED:
2223 tile_height = IS_GEN2(dev) ? 16 : 8;
2224 break;
2225 case I915_FORMAT_MOD_Y_TILED:
2226 tile_height = 32;
2227 break;
2228 case I915_FORMAT_MOD_Yf_TILED:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002229 pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
2230 switch (pixel_bytes) {
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002231 default:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002232 case 1:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002233 tile_height = 64;
2234 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002235 case 2:
2236 case 4:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002237 tile_height = 32;
2238 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002239 case 8:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002240 tile_height = 16;
2241 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002242 case 16:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002243 WARN_ONCE(1,
2244 "128-bit pixels are not supported for display!");
2245 tile_height = 16;
2246 break;
2247 }
2248 break;
2249 default:
2250 MISSING_CASE(fb_format_modifier);
2251 tile_height = 1;
2252 break;
2253 }
Daniel Vetter091df6c2015-02-10 17:16:10 +00002254
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002255 return tile_height;
2256}
2257
2258unsigned int
2259intel_fb_align_height(struct drm_device *dev, unsigned int height,
2260 uint32_t pixel_format, uint64_t fb_format_modifier)
2261{
2262 return ALIGN(height, intel_tile_height(dev, pixel_format,
2263 fb_format_modifier));
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002264}
2265
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002266static int
2267intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
2268 const struct drm_plane_state *plane_state)
2269{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002270 struct intel_rotation_info *info = &view->rotation_info;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002271
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002272 *view = i915_ggtt_view_normal;
2273
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002274 if (!plane_state)
2275 return 0;
2276
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002277 if (!intel_rotation_90_or_270(plane_state->rotation))
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002278 return 0;
2279
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002280 *view = i915_ggtt_view_rotated;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002281
2282 info->height = fb->height;
2283 info->pixel_format = fb->pixel_format;
2284 info->pitch = fb->pitches[0];
2285 info->fb_modifier = fb->modifier[0];
2286
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002287 return 0;
2288}
2289
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002290static unsigned int intel_linear_alignment(struct drm_i915_private *dev_priv)
2291{
2292 if (INTEL_INFO(dev_priv)->gen >= 9)
2293 return 256 * 1024;
Ville Syrjälä985b8bb2015-06-11 16:31:15 +03002294 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
2295 IS_VALLEYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002296 return 128 * 1024;
2297 else if (INTEL_INFO(dev_priv)->gen >= 4)
2298 return 4 * 1024;
2299 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03002300 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002301}
2302
Chris Wilson127bd2a2010-07-23 23:32:05 +01002303int
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002304intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2305 struct drm_framebuffer *fb,
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002306 const struct drm_plane_state *plane_state,
John Harrison91af1272015-06-18 13:14:56 +01002307 struct intel_engine_cs *pipelined,
2308 struct drm_i915_gem_request **pipelined_request)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002309{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002310 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002311 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002312 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002313 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002314 u32 alignment;
2315 int ret;
2316
Matt Roperebcdd392014-07-09 16:22:11 -07002317 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2318
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002319 switch (fb->modifier[0]) {
2320 case DRM_FORMAT_MOD_NONE:
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002321 alignment = intel_linear_alignment(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002322 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002323 case I915_FORMAT_MOD_X_TILED:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002324 if (INTEL_INFO(dev)->gen >= 9)
2325 alignment = 256 * 1024;
2326 else {
2327 /* pin() will align the object as required by fence */
2328 alignment = 0;
2329 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002330 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002331 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiau1327b9a2015-02-27 11:15:20 +00002332 case I915_FORMAT_MOD_Yf_TILED:
2333 if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2334 "Y tiling bo slipped through, driver bug!\n"))
2335 return -EINVAL;
2336 alignment = 1 * 1024 * 1024;
2337 break;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002338 default:
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002339 MISSING_CASE(fb->modifier[0]);
2340 return -EINVAL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002341 }
2342
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002343 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2344 if (ret)
2345 return ret;
2346
Chris Wilson693db182013-03-05 14:52:39 +00002347 /* Note that the w/a also requires 64 PTE of padding following the
2348 * bo. We currently fill all unused PTE with the shadow page and so
2349 * we should always have valid PTE following the scanout preventing
2350 * the VT-d warning.
2351 */
2352 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2353 alignment = 256 * 1024;
2354
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002355 /*
2356 * Global gtt pte registers are special registers which actually forward
2357 * writes to a chunk of system memory. Which means that there is no risk
2358 * that the register values disappear as soon as we call
2359 * intel_runtime_pm_put(), so it is correct to wrap only the
2360 * pin/unpin/fence and not more.
2361 */
2362 intel_runtime_pm_get(dev_priv);
2363
Chris Wilsonce453d82011-02-21 14:43:56 +00002364 dev_priv->mm.interruptible = false;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00002365 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
John Harrison91af1272015-06-18 13:14:56 +01002366 pipelined_request, &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002367 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00002368 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002369
2370 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2371 * fence, whereas 965+ only requires a fence if using
2372 * framebuffer compression. For simplicity, we always install
2373 * a fence as the cost is not that onerous.
2374 */
Chris Wilson06d98132012-04-17 15:31:24 +01002375 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002376 if (ret)
2377 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002378
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002379 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002380
Chris Wilsonce453d82011-02-21 14:43:56 +00002381 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002382 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002383 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002384
2385err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002386 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilsonce453d82011-02-21 14:43:56 +00002387err_interruptible:
2388 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002389 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002390 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002391}
2392
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002393static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
2394 const struct drm_plane_state *plane_state)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002395{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002396 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002397 struct i915_ggtt_view view;
2398 int ret;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002399
Matt Roperebcdd392014-07-09 16:22:11 -07002400 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2401
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002402 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2403 WARN_ONCE(ret, "Couldn't get view from plane state!");
2404
Chris Wilson1690e1e2011-12-14 13:57:08 +01002405 i915_gem_object_unpin_fence(obj);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002406 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002407}
2408
Daniel Vetterc2c75132012-07-05 12:17:30 +02002409/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2410 * is assumed to be a power-of-two. */
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002411unsigned long intel_gen4_compute_page_offset(struct drm_i915_private *dev_priv,
2412 int *x, int *y,
Chris Wilsonbc752862013-02-21 20:04:31 +00002413 unsigned int tiling_mode,
2414 unsigned int cpp,
2415 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002416{
Chris Wilsonbc752862013-02-21 20:04:31 +00002417 if (tiling_mode != I915_TILING_NONE) {
2418 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002419
Chris Wilsonbc752862013-02-21 20:04:31 +00002420 tile_rows = *y / 8;
2421 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002422
Chris Wilsonbc752862013-02-21 20:04:31 +00002423 tiles = *x / (512/cpp);
2424 *x %= 512/cpp;
2425
2426 return tile_rows * pitch * 8 + tiles * 4096;
2427 } else {
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002428 unsigned int alignment = intel_linear_alignment(dev_priv) - 1;
Chris Wilsonbc752862013-02-21 20:04:31 +00002429 unsigned int offset;
2430
2431 offset = *y * pitch + *x * cpp;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002432 *y = (offset & alignment) / pitch;
2433 *x = ((offset & alignment) - *y * pitch) / cpp;
2434 return offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002435 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002436}
2437
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002438static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002439{
2440 switch (format) {
2441 case DISPPLANE_8BPP:
2442 return DRM_FORMAT_C8;
2443 case DISPPLANE_BGRX555:
2444 return DRM_FORMAT_XRGB1555;
2445 case DISPPLANE_BGRX565:
2446 return DRM_FORMAT_RGB565;
2447 default:
2448 case DISPPLANE_BGRX888:
2449 return DRM_FORMAT_XRGB8888;
2450 case DISPPLANE_RGBX888:
2451 return DRM_FORMAT_XBGR8888;
2452 case DISPPLANE_BGRX101010:
2453 return DRM_FORMAT_XRGB2101010;
2454 case DISPPLANE_RGBX101010:
2455 return DRM_FORMAT_XBGR2101010;
2456 }
2457}
2458
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002459static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2460{
2461 switch (format) {
2462 case PLANE_CTL_FORMAT_RGB_565:
2463 return DRM_FORMAT_RGB565;
2464 default:
2465 case PLANE_CTL_FORMAT_XRGB_8888:
2466 if (rgb_order) {
2467 if (alpha)
2468 return DRM_FORMAT_ABGR8888;
2469 else
2470 return DRM_FORMAT_XBGR8888;
2471 } else {
2472 if (alpha)
2473 return DRM_FORMAT_ARGB8888;
2474 else
2475 return DRM_FORMAT_XRGB8888;
2476 }
2477 case PLANE_CTL_FORMAT_XRGB_2101010:
2478 if (rgb_order)
2479 return DRM_FORMAT_XBGR2101010;
2480 else
2481 return DRM_FORMAT_XRGB2101010;
2482 }
2483}
2484
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002485static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002486intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2487 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002488{
2489 struct drm_device *dev = crtc->base.dev;
2490 struct drm_i915_gem_object *obj = NULL;
2491 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002492 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002493 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2494 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2495 PAGE_SIZE);
2496
2497 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002498
Chris Wilsonff2652e2014-03-10 08:07:02 +00002499 if (plane_config->size == 0)
2500 return false;
2501
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002502 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2503 base_aligned,
2504 base_aligned,
2505 size_aligned);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002506 if (!obj)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002507 return false;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002508
Damien Lespiau49af4492015-01-20 12:51:44 +00002509 obj->tiling_mode = plane_config->tiling;
2510 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002511 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002512
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002513 mode_cmd.pixel_format = fb->pixel_format;
2514 mode_cmd.width = fb->width;
2515 mode_cmd.height = fb->height;
2516 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002517 mode_cmd.modifier[0] = fb->modifier[0];
2518 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002519
2520 mutex_lock(&dev->struct_mutex);
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002521 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002522 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002523 DRM_DEBUG_KMS("intel fb init failed\n");
2524 goto out_unref_obj;
2525 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002526 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002527
Daniel Vetterf6936e22015-03-26 12:17:05 +01002528 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002529 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002530
2531out_unref_obj:
2532 drm_gem_object_unreference(&obj->base);
2533 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002534 return false;
2535}
2536
Matt Roperafd65eb2015-02-03 13:10:04 -08002537/* Update plane->state->fb to match plane->fb after driver-internal updates */
2538static void
2539update_state_fb(struct drm_plane *plane)
2540{
2541 if (plane->fb == plane->state->fb)
2542 return;
2543
2544 if (plane->state->fb)
2545 drm_framebuffer_unreference(plane->state->fb);
2546 plane->state->fb = plane->fb;
2547 if (plane->state->fb)
2548 drm_framebuffer_reference(plane->state->fb);
2549}
2550
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002551static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002552intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2553 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002554{
2555 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002556 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002557 struct drm_crtc *c;
2558 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002559 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002560 struct drm_plane *primary = intel_crtc->base.primary;
2561 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002562
Damien Lespiau2d140302015-02-05 17:22:18 +00002563 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002564 return;
2565
Daniel Vetterf6936e22015-03-26 12:17:05 +01002566 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002567 fb = &plane_config->fb->base;
2568 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002569 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002570
Damien Lespiau2d140302015-02-05 17:22:18 +00002571 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002572
2573 /*
2574 * Failed to alloc the obj, check to see if we should share
2575 * an fb with another CRTC instead
2576 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002577 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002578 i = to_intel_crtc(c);
2579
2580 if (c == &intel_crtc->base)
2581 continue;
2582
Matt Roper2ff8fde2014-07-08 07:50:07 -07002583 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002584 continue;
2585
Daniel Vetter88595ac2015-03-26 12:42:24 +01002586 fb = c->primary->fb;
2587 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002588 continue;
2589
Daniel Vetter88595ac2015-03-26 12:42:24 +01002590 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002591 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002592 drm_framebuffer_reference(fb);
2593 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002594 }
2595 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002596
2597 return;
2598
2599valid_fb:
2600 obj = intel_fb_obj(fb);
2601 if (obj->tiling_mode != I915_TILING_NONE)
2602 dev_priv->preserve_bios_swizzle = true;
2603
2604 primary->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002605 primary->crtc = primary->state->crtc = &intel_crtc->base;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002606 update_state_fb(primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002607 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
Daniel Vetter88595ac2015-03-26 12:42:24 +01002608 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002609}
2610
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002611static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2612 struct drm_framebuffer *fb,
2613 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002614{
2615 struct drm_device *dev = crtc->dev;
2616 struct drm_i915_private *dev_priv = dev->dev_private;
2617 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002618 struct drm_plane *primary = crtc->primary;
2619 bool visible = to_intel_plane_state(primary->state)->visible;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002620 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002621 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002622 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002623 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002624 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302625 int pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002626
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002627 if (!visible || !fb) {
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002628 I915_WRITE(reg, 0);
2629 if (INTEL_INFO(dev)->gen >= 4)
2630 I915_WRITE(DSPSURF(plane), 0);
2631 else
2632 I915_WRITE(DSPADDR(plane), 0);
2633 POSTING_READ(reg);
2634 return;
2635 }
2636
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002637 obj = intel_fb_obj(fb);
2638 if (WARN_ON(obj == NULL))
2639 return;
2640
2641 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2642
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002643 dspcntr = DISPPLANE_GAMMA_ENABLE;
2644
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002645 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002646
2647 if (INTEL_INFO(dev)->gen < 4) {
2648 if (intel_crtc->pipe == PIPE_B)
2649 dspcntr |= DISPPLANE_SEL_PIPE_B;
2650
2651 /* pipesrc and dspsize control the size that is scaled from,
2652 * which should always be the user's requested size.
2653 */
2654 I915_WRITE(DSPSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002655 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2656 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002657 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002658 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2659 I915_WRITE(PRIMSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002660 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2661 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002662 I915_WRITE(PRIMPOS(plane), 0);
2663 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002664 }
2665
Ville Syrjälä57779d02012-10-31 17:50:14 +02002666 switch (fb->pixel_format) {
2667 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002668 dspcntr |= DISPPLANE_8BPP;
2669 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002670 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002671 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002672 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002673 case DRM_FORMAT_RGB565:
2674 dspcntr |= DISPPLANE_BGRX565;
2675 break;
2676 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002677 dspcntr |= DISPPLANE_BGRX888;
2678 break;
2679 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002680 dspcntr |= DISPPLANE_RGBX888;
2681 break;
2682 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002683 dspcntr |= DISPPLANE_BGRX101010;
2684 break;
2685 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002686 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002687 break;
2688 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002689 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002690 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002691
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002692 if (INTEL_INFO(dev)->gen >= 4 &&
2693 obj->tiling_mode != I915_TILING_NONE)
2694 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002695
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002696 if (IS_G4X(dev))
2697 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2698
Ville Syrjäläb98971272014-08-27 16:51:22 +03002699 linear_offset = y * fb->pitches[0] + x * pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002700
Daniel Vetterc2c75132012-07-05 12:17:30 +02002701 if (INTEL_INFO(dev)->gen >= 4) {
2702 intel_crtc->dspaddr_offset =
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002703 intel_gen4_compute_page_offset(dev_priv,
2704 &x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002705 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002706 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002707 linear_offset -= intel_crtc->dspaddr_offset;
2708 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002709 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002710 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002711
Matt Roper8e7d6882015-01-21 16:35:41 -08002712 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302713 dspcntr |= DISPPLANE_ROTATE_180;
2714
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002715 x += (intel_crtc->config->pipe_src_w - 1);
2716 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302717
2718 /* Finding the last pixel of the last line of the display
2719 data and adding to linear_offset*/
2720 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002721 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2722 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302723 }
2724
2725 I915_WRITE(reg, dspcntr);
2726
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002727 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002728 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002729 I915_WRITE(DSPSURF(plane),
2730 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002731 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002732 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002733 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002734 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002735 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002736}
2737
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002738static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2739 struct drm_framebuffer *fb,
2740 int x, int y)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002741{
2742 struct drm_device *dev = crtc->dev;
2743 struct drm_i915_private *dev_priv = dev->dev_private;
2744 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002745 struct drm_plane *primary = crtc->primary;
2746 bool visible = to_intel_plane_state(primary->state)->visible;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002747 struct drm_i915_gem_object *obj;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002748 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002749 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002750 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002751 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302752 int pixel_size;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002753
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002754 if (!visible || !fb) {
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002755 I915_WRITE(reg, 0);
2756 I915_WRITE(DSPSURF(plane), 0);
2757 POSTING_READ(reg);
2758 return;
2759 }
2760
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002761 obj = intel_fb_obj(fb);
2762 if (WARN_ON(obj == NULL))
2763 return;
2764
2765 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2766
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002767 dspcntr = DISPPLANE_GAMMA_ENABLE;
2768
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002769 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002770
2771 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2772 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2773
Ville Syrjälä57779d02012-10-31 17:50:14 +02002774 switch (fb->pixel_format) {
2775 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002776 dspcntr |= DISPPLANE_8BPP;
2777 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002778 case DRM_FORMAT_RGB565:
2779 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002780 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002781 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002782 dspcntr |= DISPPLANE_BGRX888;
2783 break;
2784 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002785 dspcntr |= DISPPLANE_RGBX888;
2786 break;
2787 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002788 dspcntr |= DISPPLANE_BGRX101010;
2789 break;
2790 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002791 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002792 break;
2793 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002794 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002795 }
2796
2797 if (obj->tiling_mode != I915_TILING_NONE)
2798 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002799
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002800 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002801 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002802
Ville Syrjäläb98971272014-08-27 16:51:22 +03002803 linear_offset = y * fb->pitches[0] + x * pixel_size;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002804 intel_crtc->dspaddr_offset =
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002805 intel_gen4_compute_page_offset(dev_priv,
2806 &x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002807 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002808 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002809 linear_offset -= intel_crtc->dspaddr_offset;
Matt Roper8e7d6882015-01-21 16:35:41 -08002810 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302811 dspcntr |= DISPPLANE_ROTATE_180;
2812
2813 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002814 x += (intel_crtc->config->pipe_src_w - 1);
2815 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302816
2817 /* Finding the last pixel of the last line of the display
2818 data and adding to linear_offset*/
2819 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002820 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2821 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302822 }
2823 }
2824
2825 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002826
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002827 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002828 I915_WRITE(DSPSURF(plane),
2829 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002830 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002831 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2832 } else {
2833 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2834 I915_WRITE(DSPLINOFF(plane), linear_offset);
2835 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002836 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002837}
2838
Damien Lespiaub3218032015-02-27 11:15:18 +00002839u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2840 uint32_t pixel_format)
2841{
2842 u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2843
2844 /*
2845 * The stride is either expressed as a multiple of 64 bytes
2846 * chunks for linear buffers or in number of tiles for tiled
2847 * buffers.
2848 */
2849 switch (fb_modifier) {
2850 case DRM_FORMAT_MOD_NONE:
2851 return 64;
2852 case I915_FORMAT_MOD_X_TILED:
2853 if (INTEL_INFO(dev)->gen == 2)
2854 return 128;
2855 return 512;
2856 case I915_FORMAT_MOD_Y_TILED:
2857 /* No need to check for old gens and Y tiling since this is
2858 * about the display engine and those will be blocked before
2859 * we get here.
2860 */
2861 return 128;
2862 case I915_FORMAT_MOD_Yf_TILED:
2863 if (bits_per_pixel == 8)
2864 return 64;
2865 else
2866 return 128;
2867 default:
2868 MISSING_CASE(fb_modifier);
2869 return 64;
2870 }
2871}
2872
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002873unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
2874 struct drm_i915_gem_object *obj)
2875{
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002876 const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002877
2878 if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002879 view = &i915_ggtt_view_rotated;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002880
2881 return i915_gem_obj_ggtt_offset_view(obj, view);
2882}
2883
Chandra Kondurua1b22782015-04-07 15:28:45 -07002884/*
2885 * This function detaches (aka. unbinds) unused scalers in hardware
2886 */
Maarten Lankhorst05832362015-06-15 12:33:48 +02002887static void skl_detach_scalers(struct intel_crtc *intel_crtc)
Chandra Kondurua1b22782015-04-07 15:28:45 -07002888{
2889 struct drm_device *dev;
2890 struct drm_i915_private *dev_priv;
2891 struct intel_crtc_scaler_state *scaler_state;
2892 int i;
2893
Chandra Kondurua1b22782015-04-07 15:28:45 -07002894 dev = intel_crtc->base.dev;
2895 dev_priv = dev->dev_private;
2896 scaler_state = &intel_crtc->config->scaler_state;
2897
2898 /* loop through and disable scalers that aren't in use */
2899 for (i = 0; i < intel_crtc->num_scalers; i++) {
2900 if (!scaler_state->scalers[i].in_use) {
2901 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, i), 0);
2902 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, i), 0);
2903 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, i), 0);
2904 DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
2905 intel_crtc->base.base.id, intel_crtc->pipe, i);
2906 }
2907 }
2908}
2909
Chandra Konduru6156a452015-04-27 13:48:39 -07002910u32 skl_plane_ctl_format(uint32_t pixel_format)
2911{
Chandra Konduru6156a452015-04-27 13:48:39 -07002912 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01002913 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002914 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07002915 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002916 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07002917 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002918 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07002919 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002920 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07002921 /*
2922 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2923 * to be already pre-multiplied. We need to add a knob (or a different
2924 * DRM_FORMAT) for user-space to configure that.
2925 */
2926 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002927 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07002928 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002929 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002930 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07002931 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002932 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002933 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002934 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002935 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002936 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002937 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07002938 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002939 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07002940 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002941 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002942 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002943 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002944 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01002945 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07002946 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002947
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002948 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002949}
2950
2951u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2952{
Chandra Konduru6156a452015-04-27 13:48:39 -07002953 switch (fb_modifier) {
2954 case DRM_FORMAT_MOD_NONE:
2955 break;
2956 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002957 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07002958 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002959 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07002960 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002961 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07002962 default:
2963 MISSING_CASE(fb_modifier);
2964 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002965
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002966 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002967}
2968
2969u32 skl_plane_ctl_rotation(unsigned int rotation)
2970{
Chandra Konduru6156a452015-04-27 13:48:39 -07002971 switch (rotation) {
2972 case BIT(DRM_ROTATE_0):
2973 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05302974 /*
2975 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
2976 * while i915 HW rotation is clockwise, thats why this swapping.
2977 */
Chandra Konduru6156a452015-04-27 13:48:39 -07002978 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05302979 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07002980 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002981 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07002982 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05302983 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07002984 default:
2985 MISSING_CASE(rotation);
2986 }
2987
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002988 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002989}
2990
Damien Lespiau70d21f02013-07-03 21:06:04 +01002991static void skylake_update_primary_plane(struct drm_crtc *crtc,
2992 struct drm_framebuffer *fb,
2993 int x, int y)
2994{
2995 struct drm_device *dev = crtc->dev;
2996 struct drm_i915_private *dev_priv = dev->dev_private;
2997 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002998 struct drm_plane *plane = crtc->primary;
2999 bool visible = to_intel_plane_state(plane->state)->visible;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003000 struct drm_i915_gem_object *obj;
3001 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303002 u32 plane_ctl, stride_div, stride;
3003 u32 tile_height, plane_offset, plane_size;
3004 unsigned int rotation;
3005 int x_offset, y_offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003006 unsigned long surf_addr;
Chandra Konduru6156a452015-04-27 13:48:39 -07003007 struct intel_crtc_state *crtc_state = intel_crtc->config;
3008 struct intel_plane_state *plane_state;
3009 int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
3010 int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
3011 int scaler_id = -1;
3012
Chandra Konduru6156a452015-04-27 13:48:39 -07003013 plane_state = to_intel_plane_state(plane->state);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003014
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03003015 if (!visible || !fb) {
Damien Lespiau70d21f02013-07-03 21:06:04 +01003016 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3017 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3018 POSTING_READ(PLANE_CTL(pipe, 0));
3019 return;
3020 }
3021
3022 plane_ctl = PLANE_CTL_ENABLE |
3023 PLANE_CTL_PIPE_GAMMA_ENABLE |
3024 PLANE_CTL_PIPE_CSC_ENABLE;
3025
Chandra Konduru6156a452015-04-27 13:48:39 -07003026 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3027 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003028 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303029
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303030 rotation = plane->state->rotation;
Chandra Konduru6156a452015-04-27 13:48:39 -07003031 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003032
Damien Lespiaub3218032015-02-27 11:15:18 +00003033 obj = intel_fb_obj(fb);
3034 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3035 fb->pixel_format);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303036 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
3037
Chandra Konduru6156a452015-04-27 13:48:39 -07003038 /*
3039 * FIXME: intel_plane_state->src, dst aren't set when transitional
3040 * update_plane helpers are called from legacy paths.
3041 * Once full atomic crtc is available, below check can be avoided.
3042 */
3043 if (drm_rect_width(&plane_state->src)) {
3044 scaler_id = plane_state->scaler_id;
3045 src_x = plane_state->src.x1 >> 16;
3046 src_y = plane_state->src.y1 >> 16;
3047 src_w = drm_rect_width(&plane_state->src) >> 16;
3048 src_h = drm_rect_height(&plane_state->src) >> 16;
3049 dst_x = plane_state->dst.x1;
3050 dst_y = plane_state->dst.y1;
3051 dst_w = drm_rect_width(&plane_state->dst);
3052 dst_h = drm_rect_height(&plane_state->dst);
3053
3054 WARN_ON(x != src_x || y != src_y);
3055 } else {
3056 src_w = intel_crtc->config->pipe_src_w;
3057 src_h = intel_crtc->config->pipe_src_h;
3058 }
3059
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303060 if (intel_rotation_90_or_270(rotation)) {
3061 /* stride = Surface height in tiles */
Chandra Konduru2614f172015-05-08 20:22:46 -07003062 tile_height = intel_tile_height(dev, fb->pixel_format,
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303063 fb->modifier[0]);
3064 stride = DIV_ROUND_UP(fb->height, tile_height);
Chandra Konduru6156a452015-04-27 13:48:39 -07003065 x_offset = stride * tile_height - y - src_h;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303066 y_offset = x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003067 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303068 } else {
3069 stride = fb->pitches[0] / stride_div;
3070 x_offset = x;
3071 y_offset = y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003072 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303073 }
3074 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003075
Damien Lespiau70d21f02013-07-03 21:06:04 +01003076 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303077 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3078 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3079 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003080
3081 if (scaler_id >= 0) {
3082 uint32_t ps_ctrl = 0;
3083
3084 WARN_ON(!dst_w || !dst_h);
3085 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3086 crtc_state->scaler_state.scalers[scaler_id].mode;
3087 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3088 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3089 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3090 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3091 I915_WRITE(PLANE_POS(pipe, 0), 0);
3092 } else {
3093 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3094 }
3095
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003096 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003097
3098 POSTING_READ(PLANE_SURF(pipe, 0));
3099}
3100
Jesse Barnes17638cd2011-06-24 12:19:23 -07003101/* Assume fb object is pinned & idle & fenced and just update base pointers */
3102static int
3103intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3104 int x, int y, enum mode_set_atomic state)
3105{
3106 struct drm_device *dev = crtc->dev;
3107 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003108
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01003109 if (dev_priv->display.disable_fbc)
3110 dev_priv->display.disable_fbc(dev);
Jesse Barnes81255562010-08-02 12:07:50 -07003111
Daniel Vetter29b9bde2014-04-24 23:55:01 +02003112 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3113
3114 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07003115}
3116
Ville Syrjälä75147472014-11-24 18:28:11 +02003117static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003118{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003119 struct drm_crtc *crtc;
3120
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003121 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003122 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3123 enum plane plane = intel_crtc->plane;
3124
3125 intel_prepare_page_flip(dev, plane);
3126 intel_finish_page_flip_plane(dev, plane);
3127 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003128}
3129
3130static void intel_update_primary_planes(struct drm_device *dev)
3131{
3132 struct drm_i915_private *dev_priv = dev->dev_private;
3133 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003134
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003135 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003136 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3137
Rob Clark51fd3712013-11-19 12:10:12 -05003138 drm_modeset_lock(&crtc->mutex, NULL);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003139 /*
3140 * FIXME: Once we have proper support for primary planes (and
3141 * disabling them without disabling the entire crtc) allow again
Dave Airlie66e514c2014-04-03 07:51:54 +10003142 * a NULL crtc->primary->fb.
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003143 */
Matt Roperf4510a22014-04-01 15:22:40 -07003144 if (intel_crtc->active && crtc->primary->fb)
Matt Roper262ca2b2014-03-18 17:22:55 -07003145 dev_priv->display.update_primary_plane(crtc,
Dave Airlie66e514c2014-04-03 07:51:54 +10003146 crtc->primary->fb,
Matt Roper262ca2b2014-03-18 17:22:55 -07003147 crtc->x,
3148 crtc->y);
Rob Clark51fd3712013-11-19 12:10:12 -05003149 drm_modeset_unlock(&crtc->mutex);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003150 }
3151}
3152
Ville Syrjälä75147472014-11-24 18:28:11 +02003153void intel_prepare_reset(struct drm_device *dev)
3154{
3155 /* no reset support for gen2 */
3156 if (IS_GEN2(dev))
3157 return;
3158
3159 /* reset doesn't touch the display */
3160 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3161 return;
3162
3163 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003164 /*
3165 * Disabling the crtcs gracefully seems nicer. Also the
3166 * g33 docs say we should at least disable all the planes.
3167 */
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02003168 intel_display_suspend(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003169}
3170
3171void intel_finish_reset(struct drm_device *dev)
3172{
3173 struct drm_i915_private *dev_priv = to_i915(dev);
3174
3175 /*
3176 * Flips in the rings will be nuked by the reset,
3177 * so complete all pending flips so that user space
3178 * will get its events and not get stuck.
3179 */
3180 intel_complete_page_flips(dev);
3181
3182 /* no reset support for gen2 */
3183 if (IS_GEN2(dev))
3184 return;
3185
3186 /* reset doesn't touch the display */
3187 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3188 /*
3189 * Flips in the rings have been nuked by the reset,
3190 * so update the base address of all primary
3191 * planes to the the last fb to make sure we're
3192 * showing the correct fb after a reset.
3193 */
3194 intel_update_primary_planes(dev);
3195 return;
3196 }
3197
3198 /*
3199 * The display has been reset as well,
3200 * so need a full re-initialization.
3201 */
3202 intel_runtime_pm_disable_interrupts(dev_priv);
3203 intel_runtime_pm_enable_interrupts(dev_priv);
3204
3205 intel_modeset_init_hw(dev);
3206
3207 spin_lock_irq(&dev_priv->irq_lock);
3208 if (dev_priv->display.hpd_irq_setup)
3209 dev_priv->display.hpd_irq_setup(dev);
3210 spin_unlock_irq(&dev_priv->irq_lock);
3211
3212 intel_modeset_setup_hw_state(dev, true);
3213
3214 intel_hpd_init(dev_priv);
3215
3216 drm_modeset_unlock_all(dev);
3217}
3218
Chris Wilson2e2f3512015-04-27 13:41:14 +01003219static void
Chris Wilson14667a42012-04-03 17:58:35 +01003220intel_finish_fb(struct drm_framebuffer *old_fb)
3221{
Matt Roper2ff8fde2014-07-08 07:50:07 -07003222 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
Chris Wilson2e2f3512015-04-27 13:41:14 +01003223 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilson14667a42012-04-03 17:58:35 +01003224 bool was_interruptible = dev_priv->mm.interruptible;
3225 int ret;
3226
Chris Wilson14667a42012-04-03 17:58:35 +01003227 /* Big Hammer, we also need to ensure that any pending
3228 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3229 * current scanout is retired before unpinning the old
Chris Wilson2e2f3512015-04-27 13:41:14 +01003230 * framebuffer. Note that we rely on userspace rendering
3231 * into the buffer attached to the pipe they are waiting
3232 * on. If not, userspace generates a GPU hang with IPEHR
3233 * point to the MI_WAIT_FOR_EVENT.
Chris Wilson14667a42012-04-03 17:58:35 +01003234 *
3235 * This should only fail upon a hung GPU, in which case we
3236 * can safely continue.
3237 */
3238 dev_priv->mm.interruptible = false;
Chris Wilson2e2f3512015-04-27 13:41:14 +01003239 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson14667a42012-04-03 17:58:35 +01003240 dev_priv->mm.interruptible = was_interruptible;
3241
Chris Wilson2e2f3512015-04-27 13:41:14 +01003242 WARN_ON(ret);
Chris Wilson14667a42012-04-03 17:58:35 +01003243}
3244
Chris Wilson7d5e3792014-03-04 13:15:08 +00003245static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3246{
3247 struct drm_device *dev = crtc->dev;
3248 struct drm_i915_private *dev_priv = dev->dev_private;
3249 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003250 bool pending;
3251
3252 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3253 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3254 return false;
3255
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003256 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003257 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003258 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003259
3260 return pending;
3261}
3262
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003263static void intel_update_pipe_size(struct intel_crtc *crtc)
3264{
3265 struct drm_device *dev = crtc->base.dev;
3266 struct drm_i915_private *dev_priv = dev->dev_private;
3267 const struct drm_display_mode *adjusted_mode;
3268
3269 if (!i915.fastboot)
3270 return;
3271
3272 /*
3273 * Update pipe size and adjust fitter if needed: the reason for this is
3274 * that in compute_mode_changes we check the native mode (not the pfit
3275 * mode) to see if we can flip rather than do a full mode set. In the
3276 * fastboot case, we'll flip, but if we don't update the pipesrc and
3277 * pfit state, we'll end up with a big fb scanned out into the wrong
3278 * sized surface.
3279 *
3280 * To fix this properly, we need to hoist the checks up into
3281 * compute_mode_changes (or above), check the actual pfit state and
3282 * whether the platform allows pfit disable with pipe active, and only
3283 * then update the pipesrc and pfit state, even on the flip path.
3284 */
3285
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003286 adjusted_mode = &crtc->config->base.adjusted_mode;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003287
3288 I915_WRITE(PIPESRC(crtc->pipe),
3289 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3290 (adjusted_mode->crtc_vdisplay - 1));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003291 if (!crtc->config->pch_pfit.enabled &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03003292 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3293 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003294 I915_WRITE(PF_CTL(crtc->pipe), 0);
3295 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3296 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3297 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003298 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3299 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003300}
3301
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003302static void intel_fdi_normal_train(struct drm_crtc *crtc)
3303{
3304 struct drm_device *dev = crtc->dev;
3305 struct drm_i915_private *dev_priv = dev->dev_private;
3306 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3307 int pipe = intel_crtc->pipe;
3308 u32 reg, temp;
3309
3310 /* enable normal train */
3311 reg = FDI_TX_CTL(pipe);
3312 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003313 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003314 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3315 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003316 } else {
3317 temp &= ~FDI_LINK_TRAIN_NONE;
3318 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003319 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003320 I915_WRITE(reg, temp);
3321
3322 reg = FDI_RX_CTL(pipe);
3323 temp = I915_READ(reg);
3324 if (HAS_PCH_CPT(dev)) {
3325 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3326 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3327 } else {
3328 temp &= ~FDI_LINK_TRAIN_NONE;
3329 temp |= FDI_LINK_TRAIN_NONE;
3330 }
3331 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3332
3333 /* wait one idle pattern time */
3334 POSTING_READ(reg);
3335 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003336
3337 /* IVB wants error correction enabled */
3338 if (IS_IVYBRIDGE(dev))
3339 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3340 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003341}
3342
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003343/* The FDI link training functions for ILK/Ibexpeak. */
3344static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3345{
3346 struct drm_device *dev = crtc->dev;
3347 struct drm_i915_private *dev_priv = dev->dev_private;
3348 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3349 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003350 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003351
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003352 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003353 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003354
Adam Jacksone1a44742010-06-25 15:32:14 -04003355 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3356 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003357 reg = FDI_RX_IMR(pipe);
3358 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003359 temp &= ~FDI_RX_SYMBOL_LOCK;
3360 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003361 I915_WRITE(reg, temp);
3362 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003363 udelay(150);
3364
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003365 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003366 reg = FDI_TX_CTL(pipe);
3367 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003368 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003369 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003370 temp &= ~FDI_LINK_TRAIN_NONE;
3371 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003372 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003373
Chris Wilson5eddb702010-09-11 13:48:45 +01003374 reg = FDI_RX_CTL(pipe);
3375 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003376 temp &= ~FDI_LINK_TRAIN_NONE;
3377 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003378 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3379
3380 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003381 udelay(150);
3382
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003383 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003384 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3385 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3386 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003387
Chris Wilson5eddb702010-09-11 13:48:45 +01003388 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003389 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003390 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003391 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3392
3393 if ((temp & FDI_RX_BIT_LOCK)) {
3394 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003395 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003396 break;
3397 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003398 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003399 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003400 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003401
3402 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003403 reg = FDI_TX_CTL(pipe);
3404 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003405 temp &= ~FDI_LINK_TRAIN_NONE;
3406 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003407 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003408
Chris Wilson5eddb702010-09-11 13:48:45 +01003409 reg = FDI_RX_CTL(pipe);
3410 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003411 temp &= ~FDI_LINK_TRAIN_NONE;
3412 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003413 I915_WRITE(reg, temp);
3414
3415 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003416 udelay(150);
3417
Chris Wilson5eddb702010-09-11 13:48:45 +01003418 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003419 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003420 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003421 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3422
3423 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003424 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003425 DRM_DEBUG_KMS("FDI train 2 done.\n");
3426 break;
3427 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003428 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003429 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003430 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003431
3432 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003433
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003434}
3435
Akshay Joshi0206e352011-08-16 15:34:10 -04003436static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003437 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3438 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3439 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3440 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3441};
3442
3443/* The FDI link training functions for SNB/Cougarpoint. */
3444static void gen6_fdi_link_train(struct drm_crtc *crtc)
3445{
3446 struct drm_device *dev = crtc->dev;
3447 struct drm_i915_private *dev_priv = dev->dev_private;
3448 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3449 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05003450 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003451
Adam Jacksone1a44742010-06-25 15:32:14 -04003452 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3453 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003454 reg = FDI_RX_IMR(pipe);
3455 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003456 temp &= ~FDI_RX_SYMBOL_LOCK;
3457 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003458 I915_WRITE(reg, temp);
3459
3460 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003461 udelay(150);
3462
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003463 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003464 reg = FDI_TX_CTL(pipe);
3465 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003466 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003467 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003468 temp &= ~FDI_LINK_TRAIN_NONE;
3469 temp |= FDI_LINK_TRAIN_PATTERN_1;
3470 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3471 /* SNB-B */
3472 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003473 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003474
Daniel Vetterd74cf322012-10-26 10:58:13 +02003475 I915_WRITE(FDI_RX_MISC(pipe),
3476 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3477
Chris Wilson5eddb702010-09-11 13:48:45 +01003478 reg = FDI_RX_CTL(pipe);
3479 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003480 if (HAS_PCH_CPT(dev)) {
3481 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3482 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3483 } else {
3484 temp &= ~FDI_LINK_TRAIN_NONE;
3485 temp |= FDI_LINK_TRAIN_PATTERN_1;
3486 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003487 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3488
3489 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003490 udelay(150);
3491
Akshay Joshi0206e352011-08-16 15:34:10 -04003492 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003493 reg = FDI_TX_CTL(pipe);
3494 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003495 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3496 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003497 I915_WRITE(reg, temp);
3498
3499 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003500 udelay(500);
3501
Sean Paulfa37d392012-03-02 12:53:39 -05003502 for (retry = 0; retry < 5; retry++) {
3503 reg = FDI_RX_IIR(pipe);
3504 temp = I915_READ(reg);
3505 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3506 if (temp & FDI_RX_BIT_LOCK) {
3507 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3508 DRM_DEBUG_KMS("FDI train 1 done.\n");
3509 break;
3510 }
3511 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003512 }
Sean Paulfa37d392012-03-02 12:53:39 -05003513 if (retry < 5)
3514 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003515 }
3516 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003517 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003518
3519 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003520 reg = FDI_TX_CTL(pipe);
3521 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003522 temp &= ~FDI_LINK_TRAIN_NONE;
3523 temp |= FDI_LINK_TRAIN_PATTERN_2;
3524 if (IS_GEN6(dev)) {
3525 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3526 /* SNB-B */
3527 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3528 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003529 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003530
Chris Wilson5eddb702010-09-11 13:48:45 +01003531 reg = FDI_RX_CTL(pipe);
3532 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003533 if (HAS_PCH_CPT(dev)) {
3534 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3535 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3536 } else {
3537 temp &= ~FDI_LINK_TRAIN_NONE;
3538 temp |= FDI_LINK_TRAIN_PATTERN_2;
3539 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003540 I915_WRITE(reg, temp);
3541
3542 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003543 udelay(150);
3544
Akshay Joshi0206e352011-08-16 15:34:10 -04003545 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003546 reg = FDI_TX_CTL(pipe);
3547 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003548 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3549 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003550 I915_WRITE(reg, temp);
3551
3552 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003553 udelay(500);
3554
Sean Paulfa37d392012-03-02 12:53:39 -05003555 for (retry = 0; retry < 5; retry++) {
3556 reg = FDI_RX_IIR(pipe);
3557 temp = I915_READ(reg);
3558 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3559 if (temp & FDI_RX_SYMBOL_LOCK) {
3560 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3561 DRM_DEBUG_KMS("FDI train 2 done.\n");
3562 break;
3563 }
3564 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003565 }
Sean Paulfa37d392012-03-02 12:53:39 -05003566 if (retry < 5)
3567 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003568 }
3569 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003570 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003571
3572 DRM_DEBUG_KMS("FDI train done.\n");
3573}
3574
Jesse Barnes357555c2011-04-28 15:09:55 -07003575/* Manual link training for Ivy Bridge A0 parts */
3576static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3577{
3578 struct drm_device *dev = crtc->dev;
3579 struct drm_i915_private *dev_priv = dev->dev_private;
3580 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3581 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003582 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003583
3584 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3585 for train result */
3586 reg = FDI_RX_IMR(pipe);
3587 temp = I915_READ(reg);
3588 temp &= ~FDI_RX_SYMBOL_LOCK;
3589 temp &= ~FDI_RX_BIT_LOCK;
3590 I915_WRITE(reg, temp);
3591
3592 POSTING_READ(reg);
3593 udelay(150);
3594
Daniel Vetter01a415f2012-10-27 15:58:40 +02003595 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3596 I915_READ(FDI_RX_IIR(pipe)));
3597
Jesse Barnes139ccd32013-08-19 11:04:55 -07003598 /* Try each vswing and preemphasis setting twice before moving on */
3599 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3600 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003601 reg = FDI_TX_CTL(pipe);
3602 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003603 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3604 temp &= ~FDI_TX_ENABLE;
3605 I915_WRITE(reg, temp);
3606
3607 reg = FDI_RX_CTL(pipe);
3608 temp = I915_READ(reg);
3609 temp &= ~FDI_LINK_TRAIN_AUTO;
3610 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3611 temp &= ~FDI_RX_ENABLE;
3612 I915_WRITE(reg, temp);
3613
3614 /* enable CPU FDI TX and PCH FDI RX */
3615 reg = FDI_TX_CTL(pipe);
3616 temp = I915_READ(reg);
3617 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003618 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003619 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003620 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003621 temp |= snb_b_fdi_train_param[j/2];
3622 temp |= FDI_COMPOSITE_SYNC;
3623 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3624
3625 I915_WRITE(FDI_RX_MISC(pipe),
3626 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3627
3628 reg = FDI_RX_CTL(pipe);
3629 temp = I915_READ(reg);
3630 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3631 temp |= FDI_COMPOSITE_SYNC;
3632 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3633
3634 POSTING_READ(reg);
3635 udelay(1); /* should be 0.5us */
3636
3637 for (i = 0; i < 4; i++) {
3638 reg = FDI_RX_IIR(pipe);
3639 temp = I915_READ(reg);
3640 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3641
3642 if (temp & FDI_RX_BIT_LOCK ||
3643 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3644 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3645 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3646 i);
3647 break;
3648 }
3649 udelay(1); /* should be 0.5us */
3650 }
3651 if (i == 4) {
3652 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3653 continue;
3654 }
3655
3656 /* Train 2 */
3657 reg = FDI_TX_CTL(pipe);
3658 temp = I915_READ(reg);
3659 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3660 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3661 I915_WRITE(reg, temp);
3662
3663 reg = FDI_RX_CTL(pipe);
3664 temp = I915_READ(reg);
3665 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3666 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003667 I915_WRITE(reg, temp);
3668
3669 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003670 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003671
Jesse Barnes139ccd32013-08-19 11:04:55 -07003672 for (i = 0; i < 4; i++) {
3673 reg = FDI_RX_IIR(pipe);
3674 temp = I915_READ(reg);
3675 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003676
Jesse Barnes139ccd32013-08-19 11:04:55 -07003677 if (temp & FDI_RX_SYMBOL_LOCK ||
3678 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3679 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3680 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3681 i);
3682 goto train_done;
3683 }
3684 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003685 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003686 if (i == 4)
3687 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003688 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003689
Jesse Barnes139ccd32013-08-19 11:04:55 -07003690train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003691 DRM_DEBUG_KMS("FDI train done.\n");
3692}
3693
Daniel Vetter88cefb62012-08-12 19:27:14 +02003694static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003695{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003696 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003697 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003698 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003699 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003700
Jesse Barnesc64e3112010-09-10 11:27:03 -07003701
Jesse Barnes0e23b992010-09-10 11:10:00 -07003702 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003703 reg = FDI_RX_CTL(pipe);
3704 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003705 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003706 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003707 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003708 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3709
3710 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003711 udelay(200);
3712
3713 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003714 temp = I915_READ(reg);
3715 I915_WRITE(reg, temp | FDI_PCDCLK);
3716
3717 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003718 udelay(200);
3719
Paulo Zanoni20749732012-11-23 15:30:38 -02003720 /* Enable CPU FDI TX PLL, always on for Ironlake */
3721 reg = FDI_TX_CTL(pipe);
3722 temp = I915_READ(reg);
3723 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3724 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003725
Paulo Zanoni20749732012-11-23 15:30:38 -02003726 POSTING_READ(reg);
3727 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003728 }
3729}
3730
Daniel Vetter88cefb62012-08-12 19:27:14 +02003731static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3732{
3733 struct drm_device *dev = intel_crtc->base.dev;
3734 struct drm_i915_private *dev_priv = dev->dev_private;
3735 int pipe = intel_crtc->pipe;
3736 u32 reg, temp;
3737
3738 /* Switch from PCDclk to Rawclk */
3739 reg = FDI_RX_CTL(pipe);
3740 temp = I915_READ(reg);
3741 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3742
3743 /* Disable CPU FDI TX PLL */
3744 reg = FDI_TX_CTL(pipe);
3745 temp = I915_READ(reg);
3746 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3747
3748 POSTING_READ(reg);
3749 udelay(100);
3750
3751 reg = FDI_RX_CTL(pipe);
3752 temp = I915_READ(reg);
3753 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3754
3755 /* Wait for the clocks to turn off. */
3756 POSTING_READ(reg);
3757 udelay(100);
3758}
3759
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003760static void ironlake_fdi_disable(struct drm_crtc *crtc)
3761{
3762 struct drm_device *dev = crtc->dev;
3763 struct drm_i915_private *dev_priv = dev->dev_private;
3764 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3765 int pipe = intel_crtc->pipe;
3766 u32 reg, temp;
3767
3768 /* disable CPU FDI tx and PCH FDI rx */
3769 reg = FDI_TX_CTL(pipe);
3770 temp = I915_READ(reg);
3771 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3772 POSTING_READ(reg);
3773
3774 reg = FDI_RX_CTL(pipe);
3775 temp = I915_READ(reg);
3776 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003777 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003778 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3779
3780 POSTING_READ(reg);
3781 udelay(100);
3782
3783 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003784 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003785 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003786
3787 /* still set train pattern 1 */
3788 reg = FDI_TX_CTL(pipe);
3789 temp = I915_READ(reg);
3790 temp &= ~FDI_LINK_TRAIN_NONE;
3791 temp |= FDI_LINK_TRAIN_PATTERN_1;
3792 I915_WRITE(reg, temp);
3793
3794 reg = FDI_RX_CTL(pipe);
3795 temp = I915_READ(reg);
3796 if (HAS_PCH_CPT(dev)) {
3797 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3798 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3799 } else {
3800 temp &= ~FDI_LINK_TRAIN_NONE;
3801 temp |= FDI_LINK_TRAIN_PATTERN_1;
3802 }
3803 /* BPC in FDI rx is consistent with that in PIPECONF */
3804 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003805 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003806 I915_WRITE(reg, temp);
3807
3808 POSTING_READ(reg);
3809 udelay(100);
3810}
3811
Chris Wilson5dce5b932014-01-20 10:17:36 +00003812bool intel_has_pending_fb_unpin(struct drm_device *dev)
3813{
3814 struct intel_crtc *crtc;
3815
3816 /* Note that we don't need to be called with mode_config.lock here
3817 * as our list of CRTC objects is static for the lifetime of the
3818 * device and so cannot disappear as we iterate. Similarly, we can
3819 * happily treat the predicates as racy, atomic checks as userspace
3820 * cannot claim and pin a new fb without at least acquring the
3821 * struct_mutex and so serialising with us.
3822 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003823 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003824 if (atomic_read(&crtc->unpin_work_count) == 0)
3825 continue;
3826
3827 if (crtc->unpin_work)
3828 intel_wait_for_vblank(dev, crtc->pipe);
3829
3830 return true;
3831 }
3832
3833 return false;
3834}
3835
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003836static void page_flip_completed(struct intel_crtc *intel_crtc)
3837{
3838 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3839 struct intel_unpin_work *work = intel_crtc->unpin_work;
3840
3841 /* ensure that the unpin work is consistent wrt ->pending. */
3842 smp_rmb();
3843 intel_crtc->unpin_work = NULL;
3844
3845 if (work->event)
3846 drm_send_vblank_event(intel_crtc->base.dev,
3847 intel_crtc->pipe,
3848 work->event);
3849
3850 drm_crtc_vblank_put(&intel_crtc->base);
3851
3852 wake_up_all(&dev_priv->pending_flip_queue);
3853 queue_work(dev_priv->wq, &work->work);
3854
3855 trace_i915_flip_complete(intel_crtc->plane,
3856 work->pending_flip_obj);
3857}
3858
Ville Syrjälä46a55d32014-05-21 14:04:46 +03003859void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003860{
Chris Wilson0f911282012-04-17 10:05:38 +01003861 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003862 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003863
Daniel Vetter2c10d572012-12-20 21:24:07 +01003864 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Chris Wilson9c787942014-09-05 07:13:25 +01003865 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3866 !intel_crtc_has_pending_flip(crtc),
3867 60*HZ) == 0)) {
3868 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003869
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003870 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003871 if (intel_crtc->unpin_work) {
3872 WARN_ONCE(1, "Removing stuck page flip\n");
3873 page_flip_completed(intel_crtc);
3874 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003875 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003876 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003877
Chris Wilson975d5682014-08-20 13:13:34 +01003878 if (crtc->primary->fb) {
3879 mutex_lock(&dev->struct_mutex);
3880 intel_finish_fb(crtc->primary->fb);
3881 mutex_unlock(&dev->struct_mutex);
3882 }
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003883}
3884
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003885/* Program iCLKIP clock to the desired frequency */
3886static void lpt_program_iclkip(struct drm_crtc *crtc)
3887{
3888 struct drm_device *dev = crtc->dev;
3889 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003890 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003891 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3892 u32 temp;
3893
Ville Syrjäläa5805162015-05-26 20:42:30 +03003894 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01003895
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003896 /* It is necessary to ungate the pixclk gate prior to programming
3897 * the divisors, and gate it back when it is done.
3898 */
3899 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3900
3901 /* Disable SSCCTL */
3902 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003903 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3904 SBI_SSCCTL_DISABLE,
3905 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003906
3907 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003908 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003909 auxdiv = 1;
3910 divsel = 0x41;
3911 phaseinc = 0x20;
3912 } else {
3913 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003914 * but the adjusted_mode->crtc_clock in in KHz. To get the
3915 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003916 * convert the virtual clock precision to KHz here for higher
3917 * precision.
3918 */
3919 u32 iclk_virtual_root_freq = 172800 * 1000;
3920 u32 iclk_pi_range = 64;
3921 u32 desired_divisor, msb_divisor_value, pi_value;
3922
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003923 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003924 msb_divisor_value = desired_divisor / iclk_pi_range;
3925 pi_value = desired_divisor % iclk_pi_range;
3926
3927 auxdiv = 0;
3928 divsel = msb_divisor_value - 2;
3929 phaseinc = pi_value;
3930 }
3931
3932 /* This should not happen with any sane values */
3933 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3934 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3935 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3936 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3937
3938 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 +03003939 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003940 auxdiv,
3941 divsel,
3942 phasedir,
3943 phaseinc);
3944
3945 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003946 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003947 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3948 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3949 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3950 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3951 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3952 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003953 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003954
3955 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003956 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003957 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3958 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003959 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003960
3961 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003962 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003963 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003964 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003965
3966 /* Wait for initialization time */
3967 udelay(24);
3968
3969 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01003970
Ville Syrjäläa5805162015-05-26 20:42:30 +03003971 mutex_unlock(&dev_priv->sb_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003972}
3973
Daniel Vetter275f01b22013-05-03 11:49:47 +02003974static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3975 enum pipe pch_transcoder)
3976{
3977 struct drm_device *dev = crtc->base.dev;
3978 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003979 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02003980
3981 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3982 I915_READ(HTOTAL(cpu_transcoder)));
3983 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3984 I915_READ(HBLANK(cpu_transcoder)));
3985 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3986 I915_READ(HSYNC(cpu_transcoder)));
3987
3988 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3989 I915_READ(VTOTAL(cpu_transcoder)));
3990 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3991 I915_READ(VBLANK(cpu_transcoder)));
3992 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3993 I915_READ(VSYNC(cpu_transcoder)));
3994 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3995 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3996}
3997
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02003998static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003999{
4000 struct drm_i915_private *dev_priv = dev->dev_private;
4001 uint32_t temp;
4002
4003 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004004 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004005 return;
4006
4007 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4008 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4009
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004010 temp &= ~FDI_BC_BIFURCATION_SELECT;
4011 if (enable)
4012 temp |= FDI_BC_BIFURCATION_SELECT;
4013
4014 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004015 I915_WRITE(SOUTH_CHICKEN1, temp);
4016 POSTING_READ(SOUTH_CHICKEN1);
4017}
4018
4019static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4020{
4021 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004022
4023 switch (intel_crtc->pipe) {
4024 case PIPE_A:
4025 break;
4026 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004027 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004028 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004029 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004030 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004031
4032 break;
4033 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004034 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004035
4036 break;
4037 default:
4038 BUG();
4039 }
4040}
4041
Jesse Barnesf67a5592011-01-05 10:31:48 -08004042/*
4043 * Enable PCH resources required for PCH ports:
4044 * - PCH PLLs
4045 * - FDI training & RX/TX
4046 * - update transcoder timings
4047 * - DP transcoding bits
4048 * - transcoder
4049 */
4050static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004051{
4052 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004053 struct drm_i915_private *dev_priv = dev->dev_private;
4054 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4055 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004056 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004057
Daniel Vetterab9412b2013-05-03 11:49:46 +02004058 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004059
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004060 if (IS_IVYBRIDGE(dev))
4061 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4062
Daniel Vettercd986ab2012-10-26 10:58:12 +02004063 /* Write the TU size bits before fdi link training, so that error
4064 * detection works. */
4065 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4066 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4067
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004068 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004069 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004070
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004071 /* We need to program the right clock selection before writing the pixel
4072 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004073 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004074 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004075
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004076 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004077 temp |= TRANS_DPLL_ENABLE(pipe);
4078 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004079 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004080 temp |= sel;
4081 else
4082 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004083 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004084 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004085
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004086 /* XXX: pch pll's can be enabled any time before we enable the PCH
4087 * transcoder, and we actually should do this to not upset any PCH
4088 * transcoder that already use the clock when we share it.
4089 *
4090 * Note that enable_shared_dpll tries to do the right thing, but
4091 * get_shared_dpll unconditionally resets the pll - we need that to have
4092 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004093 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004094
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004095 /* set transcoder timing, panel must allow it */
4096 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004097 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004098
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004099 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004100
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004101 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004102 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004103 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01004104 reg = TRANS_DP_CTL(pipe);
4105 temp = I915_READ(reg);
4106 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004107 TRANS_DP_SYNC_MASK |
4108 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004109 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004110 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004111
4112 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004113 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004114 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004115 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004116
4117 switch (intel_trans_dp_port_sel(crtc)) {
4118 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004119 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004120 break;
4121 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004122 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004123 break;
4124 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004125 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004126 break;
4127 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004128 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004129 }
4130
Chris Wilson5eddb702010-09-11 13:48:45 +01004131 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004132 }
4133
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004134 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004135}
4136
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004137static void lpt_pch_enable(struct drm_crtc *crtc)
4138{
4139 struct drm_device *dev = crtc->dev;
4140 struct drm_i915_private *dev_priv = dev->dev_private;
4141 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004142 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004143
Daniel Vetterab9412b2013-05-03 11:49:46 +02004144 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004145
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004146 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004147
Paulo Zanoni0540e482012-10-31 18:12:40 -02004148 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004149 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004150
Paulo Zanoni937bb612012-10-31 18:12:47 -02004151 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004152}
4153
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004154struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4155 struct intel_crtc_state *crtc_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004156{
Daniel Vettere2b78262013-06-07 23:10:03 +02004157 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004158 struct intel_shared_dpll *pll;
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004159 struct intel_shared_dpll_config *shared_dpll;
Daniel Vettere2b78262013-06-07 23:10:03 +02004160 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004161
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004162 shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
4163
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004164 if (HAS_PCH_IBX(dev_priv->dev)) {
4165 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02004166 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004167 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004168
Daniel Vetter46edb022013-06-05 13:34:12 +02004169 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4170 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004171
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004172 WARN_ON(shared_dpll[i].crtc_mask);
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004173
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004174 goto found;
4175 }
4176
Satheeshakrishna Mbcddf6102014-08-22 09:49:10 +05304177 if (IS_BROXTON(dev_priv->dev)) {
4178 /* PLL is attached to port in bxt */
4179 struct intel_encoder *encoder;
4180 struct intel_digital_port *intel_dig_port;
4181
4182 encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
4183 if (WARN_ON(!encoder))
4184 return NULL;
4185
4186 intel_dig_port = enc_to_dig_port(&encoder->base);
4187 /* 1:1 mapping between ports and PLLs */
4188 i = (enum intel_dpll_id)intel_dig_port->port;
4189 pll = &dev_priv->shared_dplls[i];
4190 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4191 crtc->base.base.id, pll->name);
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004192 WARN_ON(shared_dpll[i].crtc_mask);
Satheeshakrishna Mbcddf6102014-08-22 09:49:10 +05304193
4194 goto found;
4195 }
4196
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004197 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4198 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004199
4200 /* Only want to check enabled timings first */
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004201 if (shared_dpll[i].crtc_mask == 0)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004202 continue;
4203
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004204 if (memcmp(&crtc_state->dpll_hw_state,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004205 &shared_dpll[i].hw_state,
4206 sizeof(crtc_state->dpll_hw_state)) == 0) {
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004207 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004208 crtc->base.base.id, pll->name,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004209 shared_dpll[i].crtc_mask,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004210 pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004211 goto found;
4212 }
4213 }
4214
4215 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004216 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4217 pll = &dev_priv->shared_dplls[i];
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004218 if (shared_dpll[i].crtc_mask == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02004219 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4220 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004221 goto found;
4222 }
4223 }
4224
4225 return NULL;
4226
4227found:
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004228 if (shared_dpll[i].crtc_mask == 0)
4229 shared_dpll[i].hw_state =
4230 crtc_state->dpll_hw_state;
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004231
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004232 crtc_state->shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02004233 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4234 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02004235
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004236 shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004237
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004238 return pll;
4239}
4240
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004241static void intel_shared_dpll_commit(struct drm_atomic_state *state)
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004242{
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004243 struct drm_i915_private *dev_priv = to_i915(state->dev);
4244 struct intel_shared_dpll_config *shared_dpll;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004245 struct intel_shared_dpll *pll;
4246 enum intel_dpll_id i;
4247
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004248 if (!to_intel_atomic_state(state)->dpll_set)
4249 return;
4250
4251 shared_dpll = to_intel_atomic_state(state)->shared_dpll;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004252 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4253 pll = &dev_priv->shared_dplls[i];
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004254 pll->config = shared_dpll[i];
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004255 }
4256}
4257
Daniel Vettera1520312013-05-03 11:49:50 +02004258static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004259{
4260 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01004261 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004262 u32 temp;
4263
4264 temp = I915_READ(dslreg);
4265 udelay(500);
4266 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004267 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004268 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004269 }
4270}
4271
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004272static int
4273skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4274 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4275 int src_w, int src_h, int dst_w, int dst_h)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004276{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004277 struct intel_crtc_scaler_state *scaler_state =
4278 &crtc_state->scaler_state;
4279 struct intel_crtc *intel_crtc =
4280 to_intel_crtc(crtc_state->base.crtc);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004281 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004282
4283 need_scaling = intel_rotation_90_or_270(rotation) ?
4284 (src_h != dst_w || src_w != dst_h):
4285 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004286
4287 /*
4288 * if plane is being disabled or scaler is no more required or force detach
4289 * - free scaler binded to this plane/crtc
4290 * - in order to do this, update crtc->scaler_usage
4291 *
4292 * Here scaler state in crtc_state is set free so that
4293 * scaler can be assigned to other user. Actual register
4294 * update to free the scaler is done in plane/panel-fit programming.
4295 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4296 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004297 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004298 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004299 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004300 scaler_state->scalers[*scaler_id].in_use = 0;
4301
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004302 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4303 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4304 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004305 scaler_state->scaler_users);
4306 *scaler_id = -1;
4307 }
4308 return 0;
4309 }
4310
4311 /* range checks */
4312 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4313 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4314
4315 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4316 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004317 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004318 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004319 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004320 return -EINVAL;
4321 }
4322
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004323 /* mark this plane as a scaler user in crtc_state */
4324 scaler_state->scaler_users |= (1 << scaler_user);
4325 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4326 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4327 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4328 scaler_state->scaler_users);
4329
4330 return 0;
4331}
4332
4333/**
4334 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4335 *
4336 * @state: crtc's scaler state
4337 * @force_detach: whether to forcibly disable scaler
4338 *
4339 * Return
4340 * 0 - scaler_usage updated successfully
4341 * error - requested scaling cannot be supported or other error condition
4342 */
4343int skl_update_scaler_crtc(struct intel_crtc_state *state, int force_detach)
4344{
4345 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
4346 struct drm_display_mode *adjusted_mode =
4347 &state->base.adjusted_mode;
4348
4349 DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
4350 intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
4351
4352 return skl_update_scaler(state, force_detach, SKL_CRTC_INDEX,
4353 &state->scaler_state.scaler_id, DRM_ROTATE_0,
4354 state->pipe_src_w, state->pipe_src_h,
4355 adjusted_mode->hdisplay, adjusted_mode->hdisplay);
4356}
4357
4358/**
4359 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4360 *
4361 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004362 * @plane_state: atomic plane state to update
4363 *
4364 * Return
4365 * 0 - scaler_usage updated successfully
4366 * error - requested scaling cannot be supported or other error condition
4367 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004368static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4369 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004370{
4371
4372 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004373 struct intel_plane *intel_plane =
4374 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004375 struct drm_framebuffer *fb = plane_state->base.fb;
4376 int ret;
4377
4378 bool force_detach = !fb || !plane_state->visible;
4379
4380 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
4381 intel_plane->base.base.id, intel_crtc->pipe,
4382 drm_plane_index(&intel_plane->base));
4383
4384 ret = skl_update_scaler(crtc_state, force_detach,
4385 drm_plane_index(&intel_plane->base),
4386 &plane_state->scaler_id,
4387 plane_state->base.rotation,
4388 drm_rect_width(&plane_state->src) >> 16,
4389 drm_rect_height(&plane_state->src) >> 16,
4390 drm_rect_width(&plane_state->dst),
4391 drm_rect_height(&plane_state->dst));
4392
4393 if (ret || plane_state->scaler_id < 0)
4394 return ret;
4395
Chandra Kondurua1b22782015-04-07 15:28:45 -07004396 /* check colorkey */
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004397 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004398 DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004399 intel_plane->base.base.id);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004400 return -EINVAL;
4401 }
4402
4403 /* Check src format */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004404 switch (fb->pixel_format) {
4405 case DRM_FORMAT_RGB565:
4406 case DRM_FORMAT_XBGR8888:
4407 case DRM_FORMAT_XRGB8888:
4408 case DRM_FORMAT_ABGR8888:
4409 case DRM_FORMAT_ARGB8888:
4410 case DRM_FORMAT_XRGB2101010:
4411 case DRM_FORMAT_XBGR2101010:
4412 case DRM_FORMAT_YUYV:
4413 case DRM_FORMAT_YVYU:
4414 case DRM_FORMAT_UYVY:
4415 case DRM_FORMAT_VYUY:
4416 break;
4417 default:
4418 DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
4419 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4420 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004421 }
4422
Chandra Kondurua1b22782015-04-07 15:28:45 -07004423 return 0;
4424}
4425
4426static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004427{
4428 struct drm_device *dev = crtc->base.dev;
4429 struct drm_i915_private *dev_priv = dev->dev_private;
4430 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004431 struct intel_crtc_scaler_state *scaler_state =
4432 &crtc->config->scaler_state;
4433
4434 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4435
4436 /* To update pfit, first update scaler state */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004437 skl_update_scaler_crtc(crtc->config, !enable);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004438 intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
4439 skl_detach_scalers(crtc);
4440 if (!enable)
4441 return;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004442
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004443 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004444 int id;
4445
4446 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4447 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4448 return;
4449 }
4450
4451 id = scaler_state->scaler_id;
4452 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4453 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4454 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4455 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4456
4457 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004458 }
4459}
4460
Jesse Barnesb074cec2013-04-25 12:55:02 -07004461static void ironlake_pfit_enable(struct intel_crtc *crtc)
4462{
4463 struct drm_device *dev = crtc->base.dev;
4464 struct drm_i915_private *dev_priv = dev->dev_private;
4465 int pipe = crtc->pipe;
4466
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004467 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004468 /* Force use of hard-coded filter coefficients
4469 * as some pre-programmed values are broken,
4470 * e.g. x201.
4471 */
4472 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4473 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4474 PF_PIPE_SEL_IVB(pipe));
4475 else
4476 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004477 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4478 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004479 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004480}
4481
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004482void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004483{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004484 struct drm_device *dev = crtc->base.dev;
4485 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004486
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004487 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004488 return;
4489
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004490 /* We can only enable IPS after we enable a plane and wait for a vblank */
4491 intel_wait_for_vblank(dev, crtc->pipe);
4492
Paulo Zanonid77e4532013-09-24 13:52:55 -03004493 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004494 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004495 mutex_lock(&dev_priv->rps.hw_lock);
4496 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4497 mutex_unlock(&dev_priv->rps.hw_lock);
4498 /* Quoting Art Runyan: "its not safe to expect any particular
4499 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004500 * mailbox." Moreover, the mailbox may return a bogus state,
4501 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004502 */
4503 } else {
4504 I915_WRITE(IPS_CTL, IPS_ENABLE);
4505 /* The bit only becomes 1 in the next vblank, so this wait here
4506 * is essentially intel_wait_for_vblank. If we don't have this
4507 * and don't wait for vblanks until the end of crtc_enable, then
4508 * the HW state readout code will complain that the expected
4509 * IPS_CTL value is not the one we read. */
4510 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4511 DRM_ERROR("Timed out waiting for IPS enable\n");
4512 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004513}
4514
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004515void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004516{
4517 struct drm_device *dev = crtc->base.dev;
4518 struct drm_i915_private *dev_priv = dev->dev_private;
4519
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004520 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004521 return;
4522
4523 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004524 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004525 mutex_lock(&dev_priv->rps.hw_lock);
4526 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4527 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004528 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4529 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4530 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004531 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004532 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004533 POSTING_READ(IPS_CTL);
4534 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004535
4536 /* We need to wait for a vblank before we can disable the plane. */
4537 intel_wait_for_vblank(dev, crtc->pipe);
4538}
4539
4540/** Loads the palette/gamma unit for the CRTC with the prepared values */
4541static void intel_crtc_load_lut(struct drm_crtc *crtc)
4542{
4543 struct drm_device *dev = crtc->dev;
4544 struct drm_i915_private *dev_priv = dev->dev_private;
4545 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4546 enum pipe pipe = intel_crtc->pipe;
4547 int palreg = PALETTE(pipe);
4548 int i;
4549 bool reenable_ips = false;
4550
4551 /* The clocks have to be on to load the palette. */
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004552 if (!crtc->state->active)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004553 return;
4554
Imre Deak50360402015-01-16 00:55:16 -08004555 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03004556 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004557 assert_dsi_pll_enabled(dev_priv);
4558 else
4559 assert_pll_enabled(dev_priv, pipe);
4560 }
4561
4562 /* use legacy palette for Ironlake */
Sonika Jindal7a1db492014-07-22 11:18:27 +05304563 if (!HAS_GMCH_DISPLAY(dev))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004564 palreg = LGC_PALETTE(pipe);
4565
4566 /* Workaround : Do not read or write the pipe palette/gamma data while
4567 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4568 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004569 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03004570 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4571 GAMMA_MODE_MODE_SPLIT)) {
4572 hsw_disable_ips(intel_crtc);
4573 reenable_ips = true;
4574 }
4575
4576 for (i = 0; i < 256; i++) {
4577 I915_WRITE(palreg + 4 * i,
4578 (intel_crtc->lut_r[i] << 16) |
4579 (intel_crtc->lut_g[i] << 8) |
4580 intel_crtc->lut_b[i]);
4581 }
4582
4583 if (reenable_ips)
4584 hsw_enable_ips(intel_crtc);
4585}
4586
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004587static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004588{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004589 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004590 struct drm_device *dev = intel_crtc->base.dev;
4591 struct drm_i915_private *dev_priv = dev->dev_private;
4592
4593 mutex_lock(&dev->struct_mutex);
4594 dev_priv->mm.interruptible = false;
4595 (void) intel_overlay_switch_off(intel_crtc->overlay);
4596 dev_priv->mm.interruptible = true;
4597 mutex_unlock(&dev->struct_mutex);
4598 }
4599
4600 /* Let userspace switch the overlay on again. In most cases userspace
4601 * has to recompute where to put it anyway.
4602 */
4603}
4604
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004605/**
4606 * intel_post_enable_primary - Perform operations after enabling primary plane
4607 * @crtc: the CRTC whose primary plane was just enabled
4608 *
4609 * Performs potentially sleeping operations that must be done after the primary
4610 * plane is enabled, such as updating FBC and IPS. Note that this may be
4611 * called due to an explicit primary plane update, or due to an implicit
4612 * re-enable that is caused when a sprite plane is updated to no longer
4613 * completely hide the primary plane.
4614 */
4615static void
4616intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004617{
4618 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004619 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004620 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4621 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004622
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004623 /*
4624 * BDW signals flip done immediately if the plane
4625 * is disabled, even if the plane enable is already
4626 * armed to occur at the next vblank :(
4627 */
4628 if (IS_BROADWELL(dev))
4629 intel_wait_for_vblank(dev, pipe);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004630
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004631 /*
4632 * FIXME IPS should be fine as long as one plane is
4633 * enabled, but in practice it seems to have problems
4634 * when going from primary only to sprite only and vice
4635 * versa.
4636 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004637 hsw_enable_ips(intel_crtc);
4638
Daniel Vetterf99d7062014-06-19 16:01:59 +02004639 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004640 * Gen2 reports pipe underruns whenever all planes are disabled.
4641 * So don't enable underrun reporting before at least some planes
4642 * are enabled.
4643 * FIXME: Need to fix the logic to work when we turn off all planes
4644 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004645 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004646 if (IS_GEN2(dev))
4647 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4648
4649 /* Underruns don't raise interrupts, so check manually. */
4650 if (HAS_GMCH_DISPLAY(dev))
4651 i9xx_check_fifo_underruns(dev_priv);
4652}
4653
4654/**
4655 * intel_pre_disable_primary - Perform operations before disabling primary plane
4656 * @crtc: the CRTC whose primary plane is to be disabled
4657 *
4658 * Performs potentially sleeping operations that must be done before the
4659 * primary plane is disabled, such as updating FBC and IPS. Note that this may
4660 * be called due to an explicit primary plane update, or due to an implicit
4661 * disable that is caused when a sprite plane completely hides the primary
4662 * plane.
4663 */
4664static void
4665intel_pre_disable_primary(struct drm_crtc *crtc)
4666{
4667 struct drm_device *dev = crtc->dev;
4668 struct drm_i915_private *dev_priv = dev->dev_private;
4669 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4670 int pipe = intel_crtc->pipe;
4671
4672 /*
4673 * Gen2 reports pipe underruns whenever all planes are disabled.
4674 * So diasble underrun reporting before all the planes get disabled.
4675 * FIXME: Need to fix the logic to work when we turn off all planes
4676 * but leave the pipe running.
4677 */
4678 if (IS_GEN2(dev))
4679 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4680
4681 /*
4682 * Vblank time updates from the shadow to live plane control register
4683 * are blocked if the memory self-refresh mode is active at that
4684 * moment. So to make sure the plane gets truly disabled, disable
4685 * first the self-refresh mode. The self-refresh enable bit in turn
4686 * will be checked/applied by the HW only at the next frame start
4687 * event which is after the vblank start event, so we need to have a
4688 * wait-for-vblank between disabling the plane and the pipe.
4689 */
4690 if (HAS_GMCH_DISPLAY(dev))
4691 intel_set_memory_cxsr(dev_priv, false);
4692
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004693 /*
4694 * FIXME IPS should be fine as long as one plane is
4695 * enabled, but in practice it seems to have problems
4696 * when going from primary only to sprite only and vice
4697 * versa.
4698 */
4699 hsw_disable_ips(intel_crtc);
4700}
4701
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004702static void intel_post_plane_update(struct intel_crtc *crtc)
4703{
4704 struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
4705 struct drm_device *dev = crtc->base.dev;
4706 struct drm_plane *plane;
4707
4708 if (atomic->wait_vblank)
4709 intel_wait_for_vblank(dev, crtc->pipe);
4710
4711 intel_frontbuffer_flip(dev, atomic->fb_bits);
4712
4713 if (atomic->update_fbc) {
4714 mutex_lock(&dev->struct_mutex);
4715 intel_fbc_update(dev);
4716 mutex_unlock(&dev->struct_mutex);
4717 }
4718
4719 if (atomic->post_enable_primary)
4720 intel_post_enable_primary(&crtc->base);
4721
4722 drm_for_each_plane_mask(plane, dev, atomic->update_sprite_watermarks)
4723 intel_update_sprite_watermarks(plane, &crtc->base,
4724 0, 0, 0, false, false);
4725
4726 memset(atomic, 0, sizeof(*atomic));
4727}
4728
4729static void intel_pre_plane_update(struct intel_crtc *crtc)
4730{
4731 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02004732 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004733 struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
4734 struct drm_plane *p;
4735
4736 /* Track fb's for any planes being disabled */
4737
4738 drm_for_each_plane_mask(p, dev, atomic->disabled_planes) {
4739 struct intel_plane *plane = to_intel_plane(p);
4740 unsigned fb_bits = 0;
4741
4742 switch (p->type) {
4743 case DRM_PLANE_TYPE_PRIMARY:
4744 fb_bits = INTEL_FRONTBUFFER_PRIMARY(plane->pipe);
4745 break;
4746 case DRM_PLANE_TYPE_CURSOR:
4747 fb_bits = INTEL_FRONTBUFFER_CURSOR(plane->pipe);
4748 break;
4749 case DRM_PLANE_TYPE_OVERLAY:
4750 fb_bits = INTEL_FRONTBUFFER_SPRITE(plane->pipe);
4751 break;
4752 }
4753
4754 mutex_lock(&dev->struct_mutex);
4755 i915_gem_track_fb(intel_fb_obj(plane->base.fb), NULL, fb_bits);
4756 mutex_unlock(&dev->struct_mutex);
4757 }
4758
4759 if (atomic->wait_for_flips)
4760 intel_crtc_wait_for_pending_flips(&crtc->base);
4761
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02004762 if (atomic->disable_fbc &&
4763 dev_priv->fbc.crtc == crtc) {
4764 mutex_lock(&dev->struct_mutex);
4765 if (dev_priv->fbc.crtc == crtc)
4766 intel_fbc_disable(dev);
4767 mutex_unlock(&dev->struct_mutex);
4768 }
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004769
4770 if (atomic->pre_disable_primary)
4771 intel_pre_disable_primary(&crtc->base);
4772}
4773
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004774static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004775{
4776 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004777 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004778 struct drm_plane *p;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004779 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004780
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004781 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004782
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004783 drm_for_each_plane_mask(p, dev, plane_mask)
4784 to_intel_plane(p)->disable_plane(p, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004785
Daniel Vetterf99d7062014-06-19 16:01:59 +02004786 /*
4787 * FIXME: Once we grow proper nuclear flip support out of this we need
4788 * to compute the mask of flip planes precisely. For the time being
4789 * consider this a flip to a NULL plane.
4790 */
4791 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004792}
4793
Jesse Barnesf67a5592011-01-05 10:31:48 -08004794static void ironlake_crtc_enable(struct drm_crtc *crtc)
4795{
4796 struct drm_device *dev = crtc->dev;
4797 struct drm_i915_private *dev_priv = dev->dev_private;
4798 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004799 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004800 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004801
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004802 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08004803 return;
4804
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004805 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004806 intel_prepare_shared_dpll(intel_crtc);
4807
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004808 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304809 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004810
4811 intel_set_pipe_timings(intel_crtc);
4812
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004813 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004814 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004815 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004816 }
4817
4818 ironlake_set_pipeconf(crtc);
4819
Jesse Barnesf67a5592011-01-05 10:31:48 -08004820 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004821
Daniel Vettera72e4c92014-09-30 10:56:47 +02004822 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4823 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004824
Daniel Vetterf6736a12013-06-05 13:34:30 +02004825 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004826 if (encoder->pre_enable)
4827 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004828
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004829 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004830 /* Note: FDI PLL enabling _must_ be done before we enable the
4831 * cpu pipes, hence this is separate from all the other fdi/pch
4832 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004833 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004834 } else {
4835 assert_fdi_tx_disabled(dev_priv, pipe);
4836 assert_fdi_rx_disabled(dev_priv, pipe);
4837 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004838
Jesse Barnesb074cec2013-04-25 12:55:02 -07004839 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004840
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004841 /*
4842 * On ILK+ LUT must be loaded before the pipe is running but with
4843 * clocks enabled
4844 */
4845 intel_crtc_load_lut(crtc);
4846
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004847 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004848 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004849
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004850 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004851 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004852
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004853 assert_vblank_disabled(crtc);
4854 drm_crtc_vblank_on(crtc);
4855
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004856 for_each_encoder_on_crtc(dev, crtc, encoder)
4857 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004858
4859 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004860 cpt_verify_modeset(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004861}
4862
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004863/* IPS only exists on ULT machines and is tied to pipe A. */
4864static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4865{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004866 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004867}
4868
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004869static void haswell_crtc_enable(struct drm_crtc *crtc)
4870{
4871 struct drm_device *dev = crtc->dev;
4872 struct drm_i915_private *dev_priv = dev->dev_private;
4873 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4874 struct intel_encoder *encoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004875 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4876 struct intel_crtc_state *pipe_config =
4877 to_intel_crtc_state(crtc->state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004878
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004879 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004880 return;
4881
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004882 if (intel_crtc_to_shared_dpll(intel_crtc))
4883 intel_enable_shared_dpll(intel_crtc);
4884
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004885 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304886 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004887
4888 intel_set_pipe_timings(intel_crtc);
4889
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004890 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
4891 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
4892 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004893 }
4894
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004895 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004896 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004897 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004898 }
4899
4900 haswell_set_pipeconf(crtc);
4901
4902 intel_set_pipe_csc(crtc);
4903
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004904 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004905
Daniel Vettera72e4c92014-09-30 10:56:47 +02004906 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004907 for_each_encoder_on_crtc(dev, crtc, encoder)
4908 if (encoder->pre_enable)
4909 encoder->pre_enable(encoder);
4910
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004911 if (intel_crtc->config->has_pch_encoder) {
Daniel Vettera72e4c92014-09-30 10:56:47 +02004912 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4913 true);
Imre Deak4fe94672014-06-25 22:01:49 +03004914 dev_priv->display.fdi_link_train(crtc);
4915 }
4916
Paulo Zanoni1f544382012-10-24 11:32:00 -02004917 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004918
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004919 if (INTEL_INFO(dev)->gen == 9)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004920 skylake_pfit_update(intel_crtc, 1);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004921 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004922 ironlake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004923 else
4924 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004925
4926 /*
4927 * On ILK+ LUT must be loaded before the pipe is running but with
4928 * clocks enabled
4929 */
4930 intel_crtc_load_lut(crtc);
4931
Paulo Zanoni1f544382012-10-24 11:32:00 -02004932 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00004933 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004934
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004935 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004936 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004937
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004938 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004939 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004940
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004941 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10004942 intel_ddi_set_vc_payload_alloc(crtc, true);
4943
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004944 assert_vblank_disabled(crtc);
4945 drm_crtc_vblank_on(crtc);
4946
Jani Nikula8807e552013-08-30 19:40:32 +03004947 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004948 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004949 intel_opregion_notify_encoder(encoder, true);
4950 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004951
Paulo Zanonie4916942013-09-20 16:21:19 -03004952 /* If we change the relative order between pipe/planes enabling, we need
4953 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004954 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4955 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4956 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4957 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4958 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004959}
4960
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004961static void ironlake_pfit_disable(struct intel_crtc *crtc)
4962{
4963 struct drm_device *dev = crtc->base.dev;
4964 struct drm_i915_private *dev_priv = dev->dev_private;
4965 int pipe = crtc->pipe;
4966
4967 /* To avoid upsetting the power well on haswell only disable the pfit if
4968 * it's in use. The hw state code will make sure we get this right. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004969 if (crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004970 I915_WRITE(PF_CTL(pipe), 0);
4971 I915_WRITE(PF_WIN_POS(pipe), 0);
4972 I915_WRITE(PF_WIN_SZ(pipe), 0);
4973 }
4974}
4975
Jesse Barnes6be4a602010-09-10 10:26:01 -07004976static void ironlake_crtc_disable(struct drm_crtc *crtc)
4977{
4978 struct drm_device *dev = crtc->dev;
4979 struct drm_i915_private *dev_priv = dev->dev_private;
4980 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004981 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004982 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01004983 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004984
Daniel Vetterea9d7582012-07-10 10:42:52 +02004985 for_each_encoder_on_crtc(dev, crtc, encoder)
4986 encoder->disable(encoder);
4987
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004988 drm_crtc_vblank_off(crtc);
4989 assert_vblank_disabled(crtc);
4990
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004991 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02004992 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Daniel Vetterd925c592013-06-05 13:34:04 +02004993
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004994 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004995
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004996 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004997
Ville Syrjälä5a74f702015-05-05 17:17:38 +03004998 if (intel_crtc->config->has_pch_encoder)
4999 ironlake_fdi_disable(crtc);
5000
Daniel Vetterbf49ec82012-09-06 22:15:40 +02005001 for_each_encoder_on_crtc(dev, crtc, encoder)
5002 if (encoder->post_disable)
5003 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005004
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005005 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005006 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005007
Daniel Vetterd925c592013-06-05 13:34:04 +02005008 if (HAS_PCH_CPT(dev)) {
5009 /* disable TRANS_DP_CTL */
5010 reg = TRANS_DP_CTL(pipe);
5011 temp = I915_READ(reg);
5012 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5013 TRANS_DP_PORT_SEL_MASK);
5014 temp |= TRANS_DP_PORT_SEL_NONE;
5015 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005016
Daniel Vetterd925c592013-06-05 13:34:04 +02005017 /* disable DPLL_SEL */
5018 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005019 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005020 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005021 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005022
Daniel Vetterd925c592013-06-05 13:34:04 +02005023 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005024 }
Jesse Barnes6be4a602010-09-10 10:26:01 -07005025}
5026
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005027static void haswell_crtc_disable(struct drm_crtc *crtc)
5028{
5029 struct drm_device *dev = crtc->dev;
5030 struct drm_i915_private *dev_priv = dev->dev_private;
5031 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5032 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005033 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005034
Jani Nikula8807e552013-08-30 19:40:32 +03005035 for_each_encoder_on_crtc(dev, crtc, encoder) {
5036 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005037 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005038 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005039
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005040 drm_crtc_vblank_off(crtc);
5041 assert_vblank_disabled(crtc);
5042
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005043 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005044 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5045 false);
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005046 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005047
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005048 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005049 intel_ddi_set_vc_payload_alloc(crtc, false);
5050
Paulo Zanoniad80a812012-10-24 16:06:19 -02005051 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005052
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005053 if (INTEL_INFO(dev)->gen == 9)
Chandra Kondurua1b22782015-04-07 15:28:45 -07005054 skylake_pfit_update(intel_crtc, 0);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005055 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00005056 ironlake_pfit_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005057 else
5058 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005059
Paulo Zanoni1f544382012-10-24 11:32:00 -02005060 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005061
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005062 if (intel_crtc->config->has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02005063 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02005064 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02005065 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005066
Imre Deak97b040a2014-06-25 22:01:50 +03005067 for_each_encoder_on_crtc(dev, crtc, encoder)
5068 if (encoder->post_disable)
5069 encoder->post_disable(encoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005070}
5071
Jesse Barnes2dd24552013-04-25 12:55:01 -07005072static void i9xx_pfit_enable(struct intel_crtc *crtc)
5073{
5074 struct drm_device *dev = crtc->base.dev;
5075 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005076 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005077
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005078 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005079 return;
5080
Daniel Vetterc0b03412013-05-28 12:05:54 +02005081 /*
5082 * The panel fitter should only be adjusted whilst the pipe is disabled,
5083 * according to register description and PRM.
5084 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005085 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5086 assert_pipe_disabled(dev_priv, crtc->pipe);
5087
Jesse Barnesb074cec2013-04-25 12:55:02 -07005088 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5089 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005090
5091 /* Border color in case we don't scale up to the full screen. Black by
5092 * default, change to something else for debugging. */
5093 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005094}
5095
Dave Airlied05410f2014-06-05 13:22:59 +10005096static enum intel_display_power_domain port_to_power_domain(enum port port)
5097{
5098 switch (port) {
5099 case PORT_A:
5100 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
5101 case PORT_B:
5102 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
5103 case PORT_C:
5104 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
5105 case PORT_D:
5106 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
5107 default:
5108 WARN_ON_ONCE(1);
5109 return POWER_DOMAIN_PORT_OTHER;
5110 }
5111}
5112
Imre Deak77d22dc2014-03-05 16:20:52 +02005113#define for_each_power_domain(domain, mask) \
5114 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
5115 if ((1 << (domain)) & (mask))
5116
Imre Deak319be8a2014-03-04 19:22:57 +02005117enum intel_display_power_domain
5118intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005119{
Imre Deak319be8a2014-03-04 19:22:57 +02005120 struct drm_device *dev = intel_encoder->base.dev;
5121 struct intel_digital_port *intel_dig_port;
5122
5123 switch (intel_encoder->type) {
5124 case INTEL_OUTPUT_UNKNOWN:
5125 /* Only DDI platforms should ever use this output type */
5126 WARN_ON_ONCE(!HAS_DDI(dev));
5127 case INTEL_OUTPUT_DISPLAYPORT:
5128 case INTEL_OUTPUT_HDMI:
5129 case INTEL_OUTPUT_EDP:
5130 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005131 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005132 case INTEL_OUTPUT_DP_MST:
5133 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5134 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005135 case INTEL_OUTPUT_ANALOG:
5136 return POWER_DOMAIN_PORT_CRT;
5137 case INTEL_OUTPUT_DSI:
5138 return POWER_DOMAIN_PORT_DSI;
5139 default:
5140 return POWER_DOMAIN_PORT_OTHER;
5141 }
5142}
5143
5144static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
5145{
5146 struct drm_device *dev = crtc->dev;
5147 struct intel_encoder *intel_encoder;
5148 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5149 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005150 unsigned long mask;
5151 enum transcoder transcoder;
5152
5153 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
5154
5155 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5156 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005157 if (intel_crtc->config->pch_pfit.enabled ||
5158 intel_crtc->config->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005159 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5160
Imre Deak319be8a2014-03-04 19:22:57 +02005161 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5162 mask |= BIT(intel_display_port_power_domain(intel_encoder));
5163
Imre Deak77d22dc2014-03-05 16:20:52 +02005164 return mask;
5165}
5166
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005167static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
Imre Deak77d22dc2014-03-05 16:20:52 +02005168{
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005169 struct drm_device *dev = state->dev;
Imre Deak77d22dc2014-03-05 16:20:52 +02005170 struct drm_i915_private *dev_priv = dev->dev_private;
5171 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
5172 struct intel_crtc *crtc;
5173
5174 /*
5175 * First get all needed power domains, then put all unneeded, to avoid
5176 * any unnecessary toggling of the power wells.
5177 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005178 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005179 enum intel_display_power_domain domain;
5180
Matt Roper83d65732015-02-25 13:12:16 -08005181 if (!crtc->base.state->enable)
Imre Deak77d22dc2014-03-05 16:20:52 +02005182 continue;
5183
Imre Deak319be8a2014-03-04 19:22:57 +02005184 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
Imre Deak77d22dc2014-03-05 16:20:52 +02005185
5186 for_each_power_domain(domain, pipe_domains[crtc->pipe])
5187 intel_display_power_get(dev_priv, domain);
5188 }
5189
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005190 if (dev_priv->display.modeset_commit_cdclk) {
5191 unsigned int cdclk = to_intel_atomic_state(state)->cdclk;
5192
5193 if (cdclk != dev_priv->cdclk_freq &&
5194 !WARN_ON(!state->allow_modeset))
5195 dev_priv->display.modeset_commit_cdclk(state);
5196 }
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005197
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005198 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005199 enum intel_display_power_domain domain;
5200
5201 for_each_power_domain(domain, crtc->enabled_power_domains)
5202 intel_display_power_put(dev_priv, domain);
5203
5204 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
5205 }
5206
5207 intel_display_set_init_power(dev_priv, false);
5208}
5209
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005210static void intel_update_max_cdclk(struct drm_device *dev)
5211{
5212 struct drm_i915_private *dev_priv = dev->dev_private;
5213
5214 if (IS_SKYLAKE(dev)) {
5215 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5216
5217 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5218 dev_priv->max_cdclk_freq = 675000;
5219 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5220 dev_priv->max_cdclk_freq = 540000;
5221 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5222 dev_priv->max_cdclk_freq = 450000;
5223 else
5224 dev_priv->max_cdclk_freq = 337500;
5225 } else if (IS_BROADWELL(dev)) {
5226 /*
5227 * FIXME with extra cooling we can allow
5228 * 540 MHz for ULX and 675 Mhz for ULT.
5229 * How can we know if extra cooling is
5230 * available? PCI ID, VTB, something else?
5231 */
5232 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5233 dev_priv->max_cdclk_freq = 450000;
5234 else if (IS_BDW_ULX(dev))
5235 dev_priv->max_cdclk_freq = 450000;
5236 else if (IS_BDW_ULT(dev))
5237 dev_priv->max_cdclk_freq = 540000;
5238 else
5239 dev_priv->max_cdclk_freq = 675000;
Mika Kahola0904dea2015-06-12 10:11:32 +03005240 } else if (IS_CHERRYVIEW(dev)) {
5241 dev_priv->max_cdclk_freq = 320000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005242 } else if (IS_VALLEYVIEW(dev)) {
5243 dev_priv->max_cdclk_freq = 400000;
5244 } else {
5245 /* otherwise assume cdclk is fixed */
5246 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5247 }
5248
5249 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5250 dev_priv->max_cdclk_freq);
5251}
5252
5253static void intel_update_cdclk(struct drm_device *dev)
5254{
5255 struct drm_i915_private *dev_priv = dev->dev_private;
5256
5257 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5258 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5259 dev_priv->cdclk_freq);
5260
5261 /*
5262 * Program the gmbus_freq based on the cdclk frequency.
5263 * BSpec erroneously claims we should aim for 4MHz, but
5264 * in fact 1MHz is the correct frequency.
5265 */
5266 if (IS_VALLEYVIEW(dev)) {
5267 /*
5268 * Program the gmbus_freq based on the cdclk frequency.
5269 * BSpec erroneously claims we should aim for 4MHz, but
5270 * in fact 1MHz is the correct frequency.
5271 */
5272 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5273 }
5274
5275 if (dev_priv->max_cdclk_freq == 0)
5276 intel_update_max_cdclk(dev);
5277}
5278
Damien Lespiau70d0c572015-06-04 18:21:29 +01005279static void broxton_set_cdclk(struct drm_device *dev, int frequency)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305280{
5281 struct drm_i915_private *dev_priv = dev->dev_private;
5282 uint32_t divider;
5283 uint32_t ratio;
5284 uint32_t current_freq;
5285 int ret;
5286
5287 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5288 switch (frequency) {
5289 case 144000:
5290 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5291 ratio = BXT_DE_PLL_RATIO(60);
5292 break;
5293 case 288000:
5294 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5295 ratio = BXT_DE_PLL_RATIO(60);
5296 break;
5297 case 384000:
5298 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5299 ratio = BXT_DE_PLL_RATIO(60);
5300 break;
5301 case 576000:
5302 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5303 ratio = BXT_DE_PLL_RATIO(60);
5304 break;
5305 case 624000:
5306 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5307 ratio = BXT_DE_PLL_RATIO(65);
5308 break;
5309 case 19200:
5310 /*
5311 * Bypass frequency with DE PLL disabled. Init ratio, divider
5312 * to suppress GCC warning.
5313 */
5314 ratio = 0;
5315 divider = 0;
5316 break;
5317 default:
5318 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5319
5320 return;
5321 }
5322
5323 mutex_lock(&dev_priv->rps.hw_lock);
5324 /* Inform power controller of upcoming frequency change */
5325 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5326 0x80000000);
5327 mutex_unlock(&dev_priv->rps.hw_lock);
5328
5329 if (ret) {
5330 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5331 ret, frequency);
5332 return;
5333 }
5334
5335 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5336 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5337 current_freq = current_freq * 500 + 1000;
5338
5339 /*
5340 * DE PLL has to be disabled when
5341 * - setting to 19.2MHz (bypass, PLL isn't used)
5342 * - before setting to 624MHz (PLL needs toggling)
5343 * - before setting to any frequency from 624MHz (PLL needs toggling)
5344 */
5345 if (frequency == 19200 || frequency == 624000 ||
5346 current_freq == 624000) {
5347 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5348 /* Timeout 200us */
5349 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5350 1))
5351 DRM_ERROR("timout waiting for DE PLL unlock\n");
5352 }
5353
5354 if (frequency != 19200) {
5355 uint32_t val;
5356
5357 val = I915_READ(BXT_DE_PLL_CTL);
5358 val &= ~BXT_DE_PLL_RATIO_MASK;
5359 val |= ratio;
5360 I915_WRITE(BXT_DE_PLL_CTL, val);
5361
5362 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5363 /* Timeout 200us */
5364 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5365 DRM_ERROR("timeout waiting for DE PLL lock\n");
5366
5367 val = I915_READ(CDCLK_CTL);
5368 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5369 val |= divider;
5370 /*
5371 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5372 * enable otherwise.
5373 */
5374 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5375 if (frequency >= 500000)
5376 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5377
5378 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5379 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5380 val |= (frequency - 1000) / 500;
5381 I915_WRITE(CDCLK_CTL, val);
5382 }
5383
5384 mutex_lock(&dev_priv->rps.hw_lock);
5385 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5386 DIV_ROUND_UP(frequency, 25000));
5387 mutex_unlock(&dev_priv->rps.hw_lock);
5388
5389 if (ret) {
5390 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5391 ret, frequency);
5392 return;
5393 }
5394
Damien Lespiaua47871b2015-06-04 18:21:34 +01005395 intel_update_cdclk(dev);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305396}
5397
5398void broxton_init_cdclk(struct drm_device *dev)
5399{
5400 struct drm_i915_private *dev_priv = dev->dev_private;
5401 uint32_t val;
5402
5403 /*
5404 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5405 * or else the reset will hang because there is no PCH to respond.
5406 * Move the handshake programming to initialization sequence.
5407 * Previously was left up to BIOS.
5408 */
5409 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5410 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5411 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5412
5413 /* Enable PG1 for cdclk */
5414 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5415
5416 /* check if cd clock is enabled */
5417 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5418 DRM_DEBUG_KMS("Display already initialized\n");
5419 return;
5420 }
5421
5422 /*
5423 * FIXME:
5424 * - The initial CDCLK needs to be read from VBT.
5425 * Need to make this change after VBT has changes for BXT.
5426 * - check if setting the max (or any) cdclk freq is really necessary
5427 * here, it belongs to modeset time
5428 */
5429 broxton_set_cdclk(dev, 624000);
5430
5431 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005432 POSTING_READ(DBUF_CTL);
5433
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305434 udelay(10);
5435
5436 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5437 DRM_ERROR("DBuf power enable timeout!\n");
5438}
5439
5440void broxton_uninit_cdclk(struct drm_device *dev)
5441{
5442 struct drm_i915_private *dev_priv = dev->dev_private;
5443
5444 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005445 POSTING_READ(DBUF_CTL);
5446
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305447 udelay(10);
5448
5449 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5450 DRM_ERROR("DBuf power disable timeout!\n");
5451
5452 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5453 broxton_set_cdclk(dev, 19200);
5454
5455 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5456}
5457
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005458static const struct skl_cdclk_entry {
5459 unsigned int freq;
5460 unsigned int vco;
5461} skl_cdclk_frequencies[] = {
5462 { .freq = 308570, .vco = 8640 },
5463 { .freq = 337500, .vco = 8100 },
5464 { .freq = 432000, .vco = 8640 },
5465 { .freq = 450000, .vco = 8100 },
5466 { .freq = 540000, .vco = 8100 },
5467 { .freq = 617140, .vco = 8640 },
5468 { .freq = 675000, .vco = 8100 },
5469};
5470
5471static unsigned int skl_cdclk_decimal(unsigned int freq)
5472{
5473 return (freq - 1000) / 500;
5474}
5475
5476static unsigned int skl_cdclk_get_vco(unsigned int freq)
5477{
5478 unsigned int i;
5479
5480 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5481 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5482
5483 if (e->freq == freq)
5484 return e->vco;
5485 }
5486
5487 return 8100;
5488}
5489
5490static void
5491skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5492{
5493 unsigned int min_freq;
5494 u32 val;
5495
5496 /* select the minimum CDCLK before enabling DPLL 0 */
5497 val = I915_READ(CDCLK_CTL);
5498 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5499 val |= CDCLK_FREQ_337_308;
5500
5501 if (required_vco == 8640)
5502 min_freq = 308570;
5503 else
5504 min_freq = 337500;
5505
5506 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5507
5508 I915_WRITE(CDCLK_CTL, val);
5509 POSTING_READ(CDCLK_CTL);
5510
5511 /*
5512 * We always enable DPLL0 with the lowest link rate possible, but still
5513 * taking into account the VCO required to operate the eDP panel at the
5514 * desired frequency. The usual DP link rates operate with a VCO of
5515 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5516 * The modeset code is responsible for the selection of the exact link
5517 * rate later on, with the constraint of choosing a frequency that
5518 * works with required_vco.
5519 */
5520 val = I915_READ(DPLL_CTRL1);
5521
5522 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5523 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5524 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5525 if (required_vco == 8640)
5526 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5527 SKL_DPLL0);
5528 else
5529 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5530 SKL_DPLL0);
5531
5532 I915_WRITE(DPLL_CTRL1, val);
5533 POSTING_READ(DPLL_CTRL1);
5534
5535 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5536
5537 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5538 DRM_ERROR("DPLL0 not locked\n");
5539}
5540
5541static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5542{
5543 int ret;
5544 u32 val;
5545
5546 /* inform PCU we want to change CDCLK */
5547 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5548 mutex_lock(&dev_priv->rps.hw_lock);
5549 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5550 mutex_unlock(&dev_priv->rps.hw_lock);
5551
5552 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5553}
5554
5555static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5556{
5557 unsigned int i;
5558
5559 for (i = 0; i < 15; i++) {
5560 if (skl_cdclk_pcu_ready(dev_priv))
5561 return true;
5562 udelay(10);
5563 }
5564
5565 return false;
5566}
5567
5568static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5569{
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005570 struct drm_device *dev = dev_priv->dev;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005571 u32 freq_select, pcu_ack;
5572
5573 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5574
5575 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5576 DRM_ERROR("failed to inform PCU about cdclk change\n");
5577 return;
5578 }
5579
5580 /* set CDCLK_CTL */
5581 switch(freq) {
5582 case 450000:
5583 case 432000:
5584 freq_select = CDCLK_FREQ_450_432;
5585 pcu_ack = 1;
5586 break;
5587 case 540000:
5588 freq_select = CDCLK_FREQ_540;
5589 pcu_ack = 2;
5590 break;
5591 case 308570:
5592 case 337500:
5593 default:
5594 freq_select = CDCLK_FREQ_337_308;
5595 pcu_ack = 0;
5596 break;
5597 case 617140:
5598 case 675000:
5599 freq_select = CDCLK_FREQ_675_617;
5600 pcu_ack = 3;
5601 break;
5602 }
5603
5604 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5605 POSTING_READ(CDCLK_CTL);
5606
5607 /* inform PCU of the change */
5608 mutex_lock(&dev_priv->rps.hw_lock);
5609 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5610 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005611
5612 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005613}
5614
5615void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5616{
5617 /* disable DBUF power */
5618 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5619 POSTING_READ(DBUF_CTL);
5620
5621 udelay(10);
5622
5623 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5624 DRM_ERROR("DBuf power disable timeout\n");
5625
5626 /* disable DPLL0 */
5627 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5628 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5629 DRM_ERROR("Couldn't disable DPLL0\n");
5630
5631 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5632}
5633
5634void skl_init_cdclk(struct drm_i915_private *dev_priv)
5635{
5636 u32 val;
5637 unsigned int required_vco;
5638
5639 /* enable PCH reset handshake */
5640 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5641 I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
5642
5643 /* enable PG1 and Misc I/O */
5644 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5645
5646 /* DPLL0 already enabed !? */
5647 if (I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE) {
5648 DRM_DEBUG_DRIVER("DPLL0 already running\n");
5649 return;
5650 }
5651
5652 /* enable DPLL0 */
5653 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5654 skl_dpll0_enable(dev_priv, required_vco);
5655
5656 /* set CDCLK to the frequency the BIOS chose */
5657 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5658
5659 /* enable DBUF power */
5660 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5661 POSTING_READ(DBUF_CTL);
5662
5663 udelay(10);
5664
5665 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5666 DRM_ERROR("DBuf power enable timeout\n");
5667}
5668
Ville Syrjälädfcab172014-06-13 13:37:47 +03005669/* returns HPLL frequency in kHz */
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005670static int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005671{
Jesse Barnes586f49d2013-11-04 16:06:59 -08005672 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08005673
Jesse Barnes586f49d2013-11-04 16:06:59 -08005674 /* Obtain SKU information */
Ville Syrjäläa5805162015-05-26 20:42:30 +03005675 mutex_lock(&dev_priv->sb_lock);
Jesse Barnes586f49d2013-11-04 16:06:59 -08005676 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
5677 CCK_FUSE_HPLL_FREQ_MASK;
Ville Syrjäläa5805162015-05-26 20:42:30 +03005678 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005679
Ville Syrjälädfcab172014-06-13 13:37:47 +03005680 return vco_freq[hpll_freq] * 1000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005681}
5682
5683/* Adjust CDclk dividers to allow high res or save power if possible */
5684static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5685{
5686 struct drm_i915_private *dev_priv = dev->dev_private;
5687 u32 val, cmd;
5688
Vandana Kannan164dfd22014-11-24 13:37:41 +05305689 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5690 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005691
Ville Syrjälädfcab172014-06-13 13:37:47 +03005692 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005693 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005694 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005695 cmd = 1;
5696 else
5697 cmd = 0;
5698
5699 mutex_lock(&dev_priv->rps.hw_lock);
5700 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5701 val &= ~DSPFREQGUAR_MASK;
5702 val |= (cmd << DSPFREQGUAR_SHIFT);
5703 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5704 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5705 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5706 50)) {
5707 DRM_ERROR("timed out waiting for CDclk change\n");
5708 }
5709 mutex_unlock(&dev_priv->rps.hw_lock);
5710
Ville Syrjälä54433e92015-05-26 20:42:31 +03005711 mutex_lock(&dev_priv->sb_lock);
5712
Ville Syrjälädfcab172014-06-13 13:37:47 +03005713 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005714 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005715
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005716 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005717
Jesse Barnes30a970c2013-11-04 13:48:12 -08005718 /* adjust cdclk divider */
5719 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjälä9cf33db2014-06-13 13:37:48 +03005720 val &= ~DISPLAY_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005721 val |= divider;
5722 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005723
5724 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5725 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5726 50))
5727 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005728 }
5729
Jesse Barnes30a970c2013-11-04 13:48:12 -08005730 /* adjust self-refresh exit latency value */
5731 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5732 val &= ~0x7f;
5733
5734 /*
5735 * For high bandwidth configs, we set a higher latency in the bunit
5736 * so that the core display fetch happens in time to avoid underruns.
5737 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005738 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005739 val |= 4500 / 250; /* 4.5 usec */
5740 else
5741 val |= 3000 / 250; /* 3.0 usec */
5742 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03005743
Ville Syrjäläa5805162015-05-26 20:42:30 +03005744 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005745
Ville Syrjäläb6283052015-06-03 15:45:07 +03005746 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005747}
5748
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005749static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5750{
5751 struct drm_i915_private *dev_priv = dev->dev_private;
5752 u32 val, cmd;
5753
Vandana Kannan164dfd22014-11-24 13:37:41 +05305754 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5755 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005756
5757 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005758 case 333333:
5759 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005760 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005761 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005762 break;
5763 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005764 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005765 return;
5766 }
5767
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005768 /*
5769 * Specs are full of misinformation, but testing on actual
5770 * hardware has shown that we just need to write the desired
5771 * CCK divider into the Punit register.
5772 */
5773 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5774
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005775 mutex_lock(&dev_priv->rps.hw_lock);
5776 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5777 val &= ~DSPFREQGUAR_MASK_CHV;
5778 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5779 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5780 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5781 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5782 50)) {
5783 DRM_ERROR("timed out waiting for CDclk change\n");
5784 }
5785 mutex_unlock(&dev_priv->rps.hw_lock);
5786
Ville Syrjäläb6283052015-06-03 15:45:07 +03005787 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005788}
5789
Jesse Barnes30a970c2013-11-04 13:48:12 -08005790static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5791 int max_pixclk)
5792{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005793 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005794 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005795
Jesse Barnes30a970c2013-11-04 13:48:12 -08005796 /*
5797 * Really only a few cases to deal with, as only 4 CDclks are supported:
5798 * 200MHz
5799 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005800 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005801 * 400MHz (VLV only)
5802 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5803 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005804 *
5805 * We seem to get an unstable or solid color picture at 200MHz.
5806 * Not sure what's wrong. For now use 200MHz only when all pipes
5807 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005808 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005809 if (!IS_CHERRYVIEW(dev_priv) &&
5810 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005811 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005812 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005813 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005814 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005815 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005816 else
5817 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005818}
5819
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305820static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5821 int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005822{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305823 /*
5824 * FIXME:
5825 * - remove the guardband, it's not needed on BXT
5826 * - set 19.2MHz bypass frequency if there are no active pipes
5827 */
5828 if (max_pixclk > 576000*9/10)
5829 return 624000;
5830 else if (max_pixclk > 384000*9/10)
5831 return 576000;
5832 else if (max_pixclk > 288000*9/10)
5833 return 384000;
5834 else if (max_pixclk > 144000*9/10)
5835 return 288000;
5836 else
5837 return 144000;
5838}
5839
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005840/* Compute the max pixel clock for new configuration. Uses atomic state if
5841 * that's non-NULL, look at current state otherwise. */
5842static int intel_mode_max_pixclk(struct drm_device *dev,
5843 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005844{
Jesse Barnes30a970c2013-11-04 13:48:12 -08005845 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005846 struct intel_crtc_state *crtc_state;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005847 int max_pixclk = 0;
5848
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005849 for_each_intel_crtc(dev, intel_crtc) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005850 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005851 if (IS_ERR(crtc_state))
5852 return PTR_ERR(crtc_state);
5853
5854 if (!crtc_state->base.enable)
5855 continue;
5856
5857 max_pixclk = max(max_pixclk,
5858 crtc_state->base.adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005859 }
5860
5861 return max_pixclk;
5862}
5863
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005864static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005865{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005866 struct drm_device *dev = state->dev;
5867 struct drm_i915_private *dev_priv = dev->dev_private;
5868 int max_pixclk = intel_mode_max_pixclk(dev, state);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005869
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005870 if (max_pixclk < 0)
5871 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005872
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005873 to_intel_atomic_state(state)->cdclk =
5874 valleyview_calc_cdclk(dev_priv, max_pixclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305875
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005876 return 0;
5877}
Jesse Barnes30a970c2013-11-04 13:48:12 -08005878
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005879static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
5880{
5881 struct drm_device *dev = state->dev;
5882 struct drm_i915_private *dev_priv = dev->dev_private;
5883 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005884
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005885 if (max_pixclk < 0)
5886 return max_pixclk;
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005887
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005888 to_intel_atomic_state(state)->cdclk =
5889 broxton_calc_cdclk(dev_priv, max_pixclk);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005890
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005891 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005892}
5893
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005894static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5895{
5896 unsigned int credits, default_credits;
5897
5898 if (IS_CHERRYVIEW(dev_priv))
5899 default_credits = PFI_CREDIT(12);
5900 else
5901 default_credits = PFI_CREDIT(8);
5902
Vandana Kannan164dfd22014-11-24 13:37:41 +05305903 if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005904 /* CHV suggested value is 31 or 63 */
5905 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjäläfcc00082015-05-26 20:22:40 +03005906 credits = PFI_CREDIT_63;
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005907 else
5908 credits = PFI_CREDIT(15);
5909 } else {
5910 credits = default_credits;
5911 }
5912
5913 /*
5914 * WA - write default credits before re-programming
5915 * FIXME: should we also set the resend bit here?
5916 */
5917 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5918 default_credits);
5919
5920 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5921 credits | PFI_CREDIT_RESEND);
5922
5923 /*
5924 * FIXME is this guaranteed to clear
5925 * immediately or should we poll for it?
5926 */
5927 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
5928}
5929
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005930static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005931{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005932 struct drm_device *dev = old_state->dev;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005933 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005934 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005935
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005936 /*
5937 * FIXME: We can end up here with all power domains off, yet
5938 * with a CDCLK frequency other than the minimum. To account
5939 * for this take the PIPE-A power domain, which covers the HW
5940 * blocks needed for the following programming. This can be
5941 * removed once it's guaranteed that we get here either with
5942 * the minimum CDCLK set, or the required power domains
5943 * enabled.
5944 */
5945 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005946
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005947 if (IS_CHERRYVIEW(dev))
5948 cherryview_set_cdclk(dev, req_cdclk);
5949 else
5950 valleyview_set_cdclk(dev, req_cdclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005951
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005952 vlv_program_pfi_credits(dev_priv);
Imre Deak738c05c2014-11-19 16:25:37 +02005953
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005954 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005955}
5956
Jesse Barnes89b667f2013-04-18 14:51:36 -07005957static void valleyview_crtc_enable(struct drm_crtc *crtc)
5958{
5959 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02005960 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005961 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5962 struct intel_encoder *encoder;
5963 int pipe = intel_crtc->pipe;
Jani Nikula23538ef2013-08-27 15:12:22 +03005964 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005965
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02005966 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07005967 return;
5968
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005969 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
Shobhit Kumar8525a232014-06-25 12:20:39 +05305970
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005971 if (!is_dsi) {
5972 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005973 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005974 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005975 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005976 }
Daniel Vetter5b18e572014-04-24 23:55:06 +02005977
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005978 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05305979 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005980
5981 intel_set_pipe_timings(intel_crtc);
5982
Ville Syrjäläc14b0482014-10-16 20:52:34 +03005983 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
5984 struct drm_i915_private *dev_priv = dev->dev_private;
5985
5986 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
5987 I915_WRITE(CHV_CANVAS(pipe), 0);
5988 }
5989
Daniel Vetter5b18e572014-04-24 23:55:06 +02005990 i9xx_set_pipeconf(intel_crtc);
5991
Jesse Barnes89b667f2013-04-18 14:51:36 -07005992 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005993
Daniel Vettera72e4c92014-09-30 10:56:47 +02005994 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005995
Jesse Barnes89b667f2013-04-18 14:51:36 -07005996 for_each_encoder_on_crtc(dev, crtc, encoder)
5997 if (encoder->pre_pll_enable)
5998 encoder->pre_pll_enable(encoder);
5999
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006000 if (!is_dsi) {
6001 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006002 chv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006003 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006004 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006005 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006006
6007 for_each_encoder_on_crtc(dev, crtc, encoder)
6008 if (encoder->pre_enable)
6009 encoder->pre_enable(encoder);
6010
Jesse Barnes2dd24552013-04-25 12:55:01 -07006011 i9xx_pfit_enable(intel_crtc);
6012
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006013 intel_crtc_load_lut(crtc);
6014
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006015 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006016 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006017
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006018 assert_vblank_disabled(crtc);
6019 drm_crtc_vblank_on(crtc);
6020
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006021 for_each_encoder_on_crtc(dev, crtc, encoder)
6022 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006023}
6024
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006025static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6026{
6027 struct drm_device *dev = crtc->base.dev;
6028 struct drm_i915_private *dev_priv = dev->dev_private;
6029
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006030 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6031 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006032}
6033
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006034static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006035{
6036 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006037 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006038 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006039 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006040 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006041
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006042 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006043 return;
6044
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006045 i9xx_set_pll_dividers(intel_crtc);
6046
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006047 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306048 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006049
6050 intel_set_pipe_timings(intel_crtc);
6051
Daniel Vetter5b18e572014-04-24 23:55:06 +02006052 i9xx_set_pipeconf(intel_crtc);
6053
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006054 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006055
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006056 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006057 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006058
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006059 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006060 if (encoder->pre_enable)
6061 encoder->pre_enable(encoder);
6062
Daniel Vetterf6736a12013-06-05 13:34:30 +02006063 i9xx_enable_pll(intel_crtc);
6064
Jesse Barnes2dd24552013-04-25 12:55:01 -07006065 i9xx_pfit_enable(intel_crtc);
6066
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006067 intel_crtc_load_lut(crtc);
6068
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006069 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006070 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006071
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006072 assert_vblank_disabled(crtc);
6073 drm_crtc_vblank_on(crtc);
6074
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006075 for_each_encoder_on_crtc(dev, crtc, encoder)
6076 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006077}
6078
Daniel Vetter87476d62013-04-11 16:29:06 +02006079static void i9xx_pfit_disable(struct intel_crtc *crtc)
6080{
6081 struct drm_device *dev = crtc->base.dev;
6082 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02006083
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006084 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006085 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006086
6087 assert_pipe_disabled(dev_priv, crtc->pipe);
6088
Daniel Vetter328d8e82013-05-08 10:36:31 +02006089 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6090 I915_READ(PFIT_CONTROL));
6091 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006092}
6093
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006094static void i9xx_crtc_disable(struct drm_crtc *crtc)
6095{
6096 struct drm_device *dev = crtc->dev;
6097 struct drm_i915_private *dev_priv = dev->dev_private;
6098 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006099 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006100 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006101
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006102 /*
6103 * On gen2 planes are double buffered but the pipe isn't, so we must
6104 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03006105 * We also need to wait on all gmch platforms because of the
6106 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006107 */
Imre Deak564ed192014-06-13 14:54:21 +03006108 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006109
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006110 for_each_encoder_on_crtc(dev, crtc, encoder)
6111 encoder->disable(encoder);
6112
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006113 drm_crtc_vblank_off(crtc);
6114 assert_vblank_disabled(crtc);
6115
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006116 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006117
Daniel Vetter87476d62013-04-11 16:29:06 +02006118 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006119
Jesse Barnes89b667f2013-04-18 14:51:36 -07006120 for_each_encoder_on_crtc(dev, crtc, encoder)
6121 if (encoder->post_disable)
6122 encoder->post_disable(encoder);
6123
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006124 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006125 if (IS_CHERRYVIEW(dev))
6126 chv_disable_pll(dev_priv, pipe);
6127 else if (IS_VALLEYVIEW(dev))
6128 vlv_disable_pll(dev_priv, pipe);
6129 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006130 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006131 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006132
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006133 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006134 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006135}
6136
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006137static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006138{
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006139 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006140 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006141 enum intel_display_power_domain domain;
6142 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006143
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006144 if (!intel_crtc->active)
6145 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006146
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006147 if (to_intel_plane_state(crtc->primary->state)->visible) {
6148 intel_crtc_wait_for_pending_flips(crtc);
6149 intel_pre_disable_primary(crtc);
6150 }
6151
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02006152 intel_crtc_disable_planes(crtc, crtc->state->plane_mask);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006153 dev_priv->display.crtc_disable(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006154
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006155 domains = intel_crtc->enabled_power_domains;
6156 for_each_power_domain(domain, domains)
6157 intel_display_power_put(dev_priv, domain);
6158 intel_crtc->enabled_power_domains = 0;
6159}
6160
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006161/*
6162 * turn all crtc's off, but do not adjust state
6163 * This has to be paired with a call to intel_modeset_setup_hw_state.
6164 */
Maarten Lankhorst9716c692015-06-10 10:24:19 +02006165void intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006166{
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006167 struct drm_crtc *crtc;
6168
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006169 for_each_crtc(dev, crtc)
6170 intel_crtc_disable_noatomic(crtc);
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006171}
6172
Chris Wilsoncdd59982010-09-08 16:30:16 +01006173/* Master function to enable/disable CRTC and corresponding power wells */
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006174int intel_crtc_control(struct drm_crtc *crtc, bool enable)
Daniel Vetter976f8a22012-07-08 22:34:21 +02006175{
6176 struct drm_device *dev = crtc->dev;
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006177 struct drm_mode_config *config = &dev->mode_config;
6178 struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006179 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006180 struct intel_crtc_state *pipe_config;
6181 struct drm_atomic_state *state;
6182 int ret;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006183
Maarten Lankhorst1b509252015-06-01 12:49:48 +02006184 if (enable == intel_crtc->active)
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006185 return 0;
Maarten Lankhorst1b509252015-06-01 12:49:48 +02006186
6187 if (enable && !crtc->state->enable)
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006188 return 0;
Maarten Lankhorst1b509252015-06-01 12:49:48 +02006189
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006190 /* this function should be called with drm_modeset_lock_all for now */
6191 if (WARN_ON(!ctx))
6192 return -EIO;
6193 lockdep_assert_held(&ctx->ww_ctx);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006194
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006195 state = drm_atomic_state_alloc(dev);
6196 if (WARN_ON(!state))
6197 return -ENOMEM;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006198
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006199 state->acquire_ctx = ctx;
6200 state->allow_modeset = true;
6201
6202 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
6203 if (IS_ERR(pipe_config)) {
6204 ret = PTR_ERR(pipe_config);
6205 goto err;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006206 }
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006207 pipe_config->base.active = enable;
6208
6209 ret = intel_set_mode(state);
6210 if (!ret)
6211 return ret;
6212
6213err:
6214 DRM_ERROR("Updating crtc active failed with %i\n", ret);
6215 drm_atomic_state_free(state);
6216 return ret;
Borun Fub04c5bd2014-07-12 10:02:27 +05306217}
6218
6219/**
6220 * Sets the power management mode of the pipe and plane.
6221 */
6222void intel_crtc_update_dpms(struct drm_crtc *crtc)
6223{
6224 struct drm_device *dev = crtc->dev;
6225 struct intel_encoder *intel_encoder;
6226 bool enable = false;
6227
6228 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
6229 enable |= intel_encoder->connectors_active;
6230
6231 intel_crtc_control(crtc, enable);
Chris Wilsoncdd59982010-09-08 16:30:16 +01006232}
6233
Chris Wilsonea5b2132010-08-04 13:50:23 +01006234void intel_encoder_destroy(struct drm_encoder *encoder)
6235{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006236 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006237
Chris Wilsonea5b2132010-08-04 13:50:23 +01006238 drm_encoder_cleanup(encoder);
6239 kfree(intel_encoder);
6240}
6241
Damien Lespiau92373292013-08-08 22:28:57 +01006242/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006243 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
6244 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01006245static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006246{
6247 if (mode == DRM_MODE_DPMS_ON) {
6248 encoder->connectors_active = true;
6249
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006250 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006251 } else {
6252 encoder->connectors_active = false;
6253
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006254 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006255 }
6256}
6257
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006258/* Cross check the actual hw state with our own modeset state tracking (and it's
6259 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02006260static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006261{
6262 if (connector->get_hw_state(connector)) {
6263 struct intel_encoder *encoder = connector->encoder;
6264 struct drm_crtc *crtc;
6265 bool encoder_enabled;
6266 enum pipe pipe;
6267
6268 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6269 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03006270 connector->base.name);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006271
Dave Airlie0e32b392014-05-02 14:02:48 +10006272 /* there is no real hw state for MST connectors */
6273 if (connector->mst_port)
6274 return;
6275
Rob Clarke2c719b2014-12-15 13:56:32 -05006276 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006277 "wrong connector dpms state\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006278 I915_STATE_WARN(connector->base.encoder != &encoder->base,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006279 "active connector not linked to encoder\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006280
Dave Airlie36cd7442014-05-02 13:44:18 +10006281 if (encoder) {
Rob Clarke2c719b2014-12-15 13:56:32 -05006282 I915_STATE_WARN(!encoder->connectors_active,
Dave Airlie36cd7442014-05-02 13:44:18 +10006283 "encoder->connectors_active not set\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006284
Dave Airlie36cd7442014-05-02 13:44:18 +10006285 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -05006286 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
6287 if (I915_STATE_WARN_ON(!encoder->base.crtc))
Dave Airlie36cd7442014-05-02 13:44:18 +10006288 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006289
Dave Airlie36cd7442014-05-02 13:44:18 +10006290 crtc = encoder->base.crtc;
6291
Matt Roper83d65732015-02-25 13:12:16 -08006292 I915_STATE_WARN(!crtc->state->enable,
6293 "crtc not enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006294 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
6295 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
Dave Airlie36cd7442014-05-02 13:44:18 +10006296 "encoder active on the wrong pipe\n");
6297 }
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006298 }
6299}
6300
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006301int intel_connector_init(struct intel_connector *connector)
6302{
6303 struct drm_connector_state *connector_state;
6304
6305 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6306 if (!connector_state)
6307 return -ENOMEM;
6308
6309 connector->base.state = connector_state;
6310 return 0;
6311}
6312
6313struct intel_connector *intel_connector_alloc(void)
6314{
6315 struct intel_connector *connector;
6316
6317 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6318 if (!connector)
6319 return NULL;
6320
6321 if (intel_connector_init(connector) < 0) {
6322 kfree(connector);
6323 return NULL;
6324 }
6325
6326 return connector;
6327}
6328
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006329/* Even simpler default implementation, if there's really no special case to
6330 * consider. */
6331void intel_connector_dpms(struct drm_connector *connector, int mode)
6332{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006333 /* All the simple cases only support two dpms states. */
6334 if (mode != DRM_MODE_DPMS_ON)
6335 mode = DRM_MODE_DPMS_OFF;
6336
6337 if (mode == connector->dpms)
6338 return;
6339
6340 connector->dpms = mode;
6341
6342 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dc2013-09-29 19:15:07 +01006343 if (connector->encoder)
6344 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006345
Daniel Vetterb9805142012-08-31 17:37:33 +02006346 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006347}
6348
Daniel Vetterf0947c32012-07-02 13:10:34 +02006349/* Simple connector->get_hw_state implementation for encoders that support only
6350 * one connector and no cloning and hence the encoder state determines the state
6351 * of the connector. */
6352bool intel_connector_get_hw_state(struct intel_connector *connector)
6353{
Daniel Vetter24929352012-07-02 20:28:59 +02006354 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006355 struct intel_encoder *encoder = connector->encoder;
6356
6357 return encoder->get_hw_state(encoder, &pipe);
6358}
6359
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006360static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006361{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006362 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6363 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006364
6365 return 0;
6366}
6367
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006368static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006369 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006370{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006371 struct drm_atomic_state *state = pipe_config->base.state;
6372 struct intel_crtc *other_crtc;
6373 struct intel_crtc_state *other_crtc_state;
6374
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006375 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6376 pipe_name(pipe), pipe_config->fdi_lanes);
6377 if (pipe_config->fdi_lanes > 4) {
6378 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6379 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006380 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006381 }
6382
Paulo Zanonibafb6552013-11-02 21:07:44 -07006383 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006384 if (pipe_config->fdi_lanes > 2) {
6385 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6386 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006387 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006388 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006389 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006390 }
6391 }
6392
6393 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006394 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006395
6396 /* Ivybridge 3 pipe is really complicated */
6397 switch (pipe) {
6398 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006399 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006400 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006401 if (pipe_config->fdi_lanes <= 2)
6402 return 0;
6403
6404 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6405 other_crtc_state =
6406 intel_atomic_get_crtc_state(state, other_crtc);
6407 if (IS_ERR(other_crtc_state))
6408 return PTR_ERR(other_crtc_state);
6409
6410 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006411 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6412 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006413 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006414 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006415 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006416 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006417 if (pipe_config->fdi_lanes > 2) {
6418 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6419 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006420 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006421 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006422
6423 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6424 other_crtc_state =
6425 intel_atomic_get_crtc_state(state, other_crtc);
6426 if (IS_ERR(other_crtc_state))
6427 return PTR_ERR(other_crtc_state);
6428
6429 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006430 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006431 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006432 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006433 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006434 default:
6435 BUG();
6436 }
6437}
6438
Daniel Vettere29c22c2013-02-21 00:00:16 +01006439#define RETRY 1
6440static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006441 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006442{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006443 struct drm_device *dev = intel_crtc->base.dev;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006444 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006445 int lane, link_bw, fdi_dotclock, ret;
6446 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006447
Daniel Vettere29c22c2013-02-21 00:00:16 +01006448retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006449 /* FDI is a binary signal running at ~2.7GHz, encoding
6450 * each output octet as 10 bits. The actual frequency
6451 * is stored as a divider into a 100MHz clock, and the
6452 * mode pixel clock is stored in units of 1KHz.
6453 * Hence the bw of each lane in terms of the mode signal
6454 * is:
6455 */
6456 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6457
Damien Lespiau241bfc32013-09-25 16:45:37 +01006458 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006459
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006460 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006461 pipe_config->pipe_bpp);
6462
6463 pipe_config->fdi_lanes = lane;
6464
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006465 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006466 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006467
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006468 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6469 intel_crtc->pipe, pipe_config);
6470 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006471 pipe_config->pipe_bpp -= 2*3;
6472 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6473 pipe_config->pipe_bpp);
6474 needs_recompute = true;
6475 pipe_config->bw_constrained = true;
6476
6477 goto retry;
6478 }
6479
6480 if (needs_recompute)
6481 return RETRY;
6482
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006483 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006484}
6485
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006486static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6487 struct intel_crtc_state *pipe_config)
6488{
6489 if (pipe_config->pipe_bpp > 24)
6490 return false;
6491
6492 /* HSW can handle pixel rate up to cdclk? */
6493 if (IS_HASWELL(dev_priv->dev))
6494 return true;
6495
6496 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006497 * We compare against max which means we must take
6498 * the increased cdclk requirement into account when
6499 * calculating the new cdclk.
6500 *
6501 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006502 */
6503 return ilk_pipe_pixel_rate(pipe_config) <=
6504 dev_priv->max_cdclk_freq * 95 / 100;
6505}
6506
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006507static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006508 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006509{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006510 struct drm_device *dev = crtc->base.dev;
6511 struct drm_i915_private *dev_priv = dev->dev_private;
6512
Jani Nikulad330a952014-01-21 11:24:25 +02006513 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006514 hsw_crtc_supports_ips(crtc) &&
6515 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006516}
6517
Daniel Vettera43f6e02013-06-07 23:10:32 +02006518static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006519 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006520{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006521 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006522 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006523 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01006524
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006525 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006526 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä44913152015-06-03 15:45:10 +03006527 int clock_limit = dev_priv->max_cdclk_freq;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006528
6529 /*
6530 * Enable pixel doubling when the dot clock
6531 * is > 90% of the (display) core speed.
6532 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03006533 * GDG double wide on either pipe,
6534 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006535 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03006536 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01006537 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006538 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006539 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006540 }
6541
Damien Lespiau241bfc32013-09-25 16:45:37 +01006542 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006543 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006544 }
Chris Wilson89749352010-09-12 18:25:19 +01006545
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006546 /*
6547 * Pipe horizontal size must be even in:
6548 * - DVO ganged mode
6549 * - LVDS dual channel mode
6550 * - Double wide pipe
6551 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006552 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006553 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6554 pipe_config->pipe_src_w &= ~1;
6555
Damien Lespiau8693a822013-05-03 18:48:11 +01006556 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6557 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006558 */
6559 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
6560 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006561 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006562
Damien Lespiauf5adf942013-06-24 18:29:34 +01006563 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006564 hsw_compute_ips_config(crtc, pipe_config);
6565
Daniel Vetter877d48d2013-04-19 11:24:43 +02006566 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006567 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006568
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006569 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006570}
6571
Ville Syrjälä1652d192015-03-31 14:12:01 +03006572static int skylake_get_display_clock_speed(struct drm_device *dev)
6573{
6574 struct drm_i915_private *dev_priv = to_i915(dev);
6575 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6576 uint32_t cdctl = I915_READ(CDCLK_CTL);
6577 uint32_t linkrate;
6578
Damien Lespiau414355a2015-06-04 18:21:31 +01006579 if (!(lcpll1 & LCPLL_PLL_ENABLE))
Ville Syrjälä1652d192015-03-31 14:12:01 +03006580 return 24000; /* 24MHz is the cd freq with NSSC ref */
Ville Syrjälä1652d192015-03-31 14:12:01 +03006581
6582 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6583 return 540000;
6584
6585 linkrate = (I915_READ(DPLL_CTRL1) &
Damien Lespiau71cd8422015-04-30 16:39:17 +01006586 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006587
Damien Lespiau71cd8422015-04-30 16:39:17 +01006588 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6589 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006590 /* vco 8640 */
6591 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6592 case CDCLK_FREQ_450_432:
6593 return 432000;
6594 case CDCLK_FREQ_337_308:
6595 return 308570;
6596 case CDCLK_FREQ_675_617:
6597 return 617140;
6598 default:
6599 WARN(1, "Unknown cd freq selection\n");
6600 }
6601 } else {
6602 /* vco 8100 */
6603 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6604 case CDCLK_FREQ_450_432:
6605 return 450000;
6606 case CDCLK_FREQ_337_308:
6607 return 337500;
6608 case CDCLK_FREQ_675_617:
6609 return 675000;
6610 default:
6611 WARN(1, "Unknown cd freq selection\n");
6612 }
6613 }
6614
6615 /* error case, do as if DPLL0 isn't enabled */
6616 return 24000;
6617}
6618
6619static int broadwell_get_display_clock_speed(struct drm_device *dev)
6620{
6621 struct drm_i915_private *dev_priv = dev->dev_private;
6622 uint32_t lcpll = I915_READ(LCPLL_CTL);
6623 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6624
6625 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6626 return 800000;
6627 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6628 return 450000;
6629 else if (freq == LCPLL_CLK_FREQ_450)
6630 return 450000;
6631 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6632 return 540000;
6633 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6634 return 337500;
6635 else
6636 return 675000;
6637}
6638
6639static int haswell_get_display_clock_speed(struct drm_device *dev)
6640{
6641 struct drm_i915_private *dev_priv = dev->dev_private;
6642 uint32_t lcpll = I915_READ(LCPLL_CTL);
6643 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6644
6645 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6646 return 800000;
6647 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6648 return 450000;
6649 else if (freq == LCPLL_CLK_FREQ_450)
6650 return 450000;
6651 else if (IS_HSW_ULT(dev))
6652 return 337500;
6653 else
6654 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006655}
6656
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006657static int valleyview_get_display_clock_speed(struct drm_device *dev)
6658{
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006659 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006660 u32 val;
6661 int divider;
6662
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006663 if (dev_priv->hpll_freq == 0)
6664 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
6665
Ville Syrjäläa5805162015-05-26 20:42:30 +03006666 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006667 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03006668 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006669
6670 divider = val & DISPLAY_FREQUENCY_VALUES;
6671
Ville Syrjälä7d007f42014-06-13 13:37:53 +03006672 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
6673 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
6674 "cdclk change in progress\n");
6675
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006676 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006677}
6678
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006679static int ilk_get_display_clock_speed(struct drm_device *dev)
6680{
6681 return 450000;
6682}
6683
Jesse Barnese70236a2009-09-21 10:42:27 -07006684static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006685{
Jesse Barnese70236a2009-09-21 10:42:27 -07006686 return 400000;
6687}
Jesse Barnes79e53942008-11-07 14:24:08 -08006688
Jesse Barnese70236a2009-09-21 10:42:27 -07006689static int i915_get_display_clock_speed(struct drm_device *dev)
6690{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006691 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006692}
Jesse Barnes79e53942008-11-07 14:24:08 -08006693
Jesse Barnese70236a2009-09-21 10:42:27 -07006694static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6695{
6696 return 200000;
6697}
Jesse Barnes79e53942008-11-07 14:24:08 -08006698
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006699static int pnv_get_display_clock_speed(struct drm_device *dev)
6700{
6701 u16 gcfgc = 0;
6702
6703 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6704
6705 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6706 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006707 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006708 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006709 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006710 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006711 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006712 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6713 return 200000;
6714 default:
6715 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6716 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006717 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006718 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006719 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006720 }
6721}
6722
Jesse Barnese70236a2009-09-21 10:42:27 -07006723static int i915gm_get_display_clock_speed(struct drm_device *dev)
6724{
6725 u16 gcfgc = 0;
6726
6727 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6728
6729 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006730 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006731 else {
6732 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6733 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006734 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006735 default:
6736 case GC_DISPLAY_CLOCK_190_200_MHZ:
6737 return 190000;
6738 }
6739 }
6740}
Jesse Barnes79e53942008-11-07 14:24:08 -08006741
Jesse Barnese70236a2009-09-21 10:42:27 -07006742static int i865_get_display_clock_speed(struct drm_device *dev)
6743{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006744 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006745}
6746
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006747static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07006748{
6749 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006750
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03006751 /*
6752 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6753 * encoding is different :(
6754 * FIXME is this the right way to detect 852GM/852GMV?
6755 */
6756 if (dev->pdev->revision == 0x1)
6757 return 133333;
6758
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006759 pci_bus_read_config_word(dev->pdev->bus,
6760 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6761
Jesse Barnese70236a2009-09-21 10:42:27 -07006762 /* Assume that the hardware is in the high speed state. This
6763 * should be the default.
6764 */
6765 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6766 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006767 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07006768 case GC_CLOCK_100_200:
6769 return 200000;
6770 case GC_CLOCK_166_250:
6771 return 250000;
6772 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006773 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006774 case GC_CLOCK_133_266:
6775 case GC_CLOCK_133_266_2:
6776 case GC_CLOCK_166_266:
6777 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006778 }
6779
6780 /* Shouldn't happen */
6781 return 0;
6782}
6783
6784static int i830_get_display_clock_speed(struct drm_device *dev)
6785{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006786 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006787}
6788
Ville Syrjälä34edce22015-05-22 11:22:33 +03006789static unsigned int intel_hpll_vco(struct drm_device *dev)
6790{
6791 struct drm_i915_private *dev_priv = dev->dev_private;
6792 static const unsigned int blb_vco[8] = {
6793 [0] = 3200000,
6794 [1] = 4000000,
6795 [2] = 5333333,
6796 [3] = 4800000,
6797 [4] = 6400000,
6798 };
6799 static const unsigned int pnv_vco[8] = {
6800 [0] = 3200000,
6801 [1] = 4000000,
6802 [2] = 5333333,
6803 [3] = 4800000,
6804 [4] = 2666667,
6805 };
6806 static const unsigned int cl_vco[8] = {
6807 [0] = 3200000,
6808 [1] = 4000000,
6809 [2] = 5333333,
6810 [3] = 6400000,
6811 [4] = 3333333,
6812 [5] = 3566667,
6813 [6] = 4266667,
6814 };
6815 static const unsigned int elk_vco[8] = {
6816 [0] = 3200000,
6817 [1] = 4000000,
6818 [2] = 5333333,
6819 [3] = 4800000,
6820 };
6821 static const unsigned int ctg_vco[8] = {
6822 [0] = 3200000,
6823 [1] = 4000000,
6824 [2] = 5333333,
6825 [3] = 6400000,
6826 [4] = 2666667,
6827 [5] = 4266667,
6828 };
6829 const unsigned int *vco_table;
6830 unsigned int vco;
6831 uint8_t tmp = 0;
6832
6833 /* FIXME other chipsets? */
6834 if (IS_GM45(dev))
6835 vco_table = ctg_vco;
6836 else if (IS_G4X(dev))
6837 vco_table = elk_vco;
6838 else if (IS_CRESTLINE(dev))
6839 vco_table = cl_vco;
6840 else if (IS_PINEVIEW(dev))
6841 vco_table = pnv_vco;
6842 else if (IS_G33(dev))
6843 vco_table = blb_vco;
6844 else
6845 return 0;
6846
6847 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6848
6849 vco = vco_table[tmp & 0x7];
6850 if (vco == 0)
6851 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6852 else
6853 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6854
6855 return vco;
6856}
6857
6858static int gm45_get_display_clock_speed(struct drm_device *dev)
6859{
6860 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6861 uint16_t tmp = 0;
6862
6863 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6864
6865 cdclk_sel = (tmp >> 12) & 0x1;
6866
6867 switch (vco) {
6868 case 2666667:
6869 case 4000000:
6870 case 5333333:
6871 return cdclk_sel ? 333333 : 222222;
6872 case 3200000:
6873 return cdclk_sel ? 320000 : 228571;
6874 default:
6875 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6876 return 222222;
6877 }
6878}
6879
6880static int i965gm_get_display_clock_speed(struct drm_device *dev)
6881{
6882 static const uint8_t div_3200[] = { 16, 10, 8 };
6883 static const uint8_t div_4000[] = { 20, 12, 10 };
6884 static const uint8_t div_5333[] = { 24, 16, 14 };
6885 const uint8_t *div_table;
6886 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6887 uint16_t tmp = 0;
6888
6889 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6890
6891 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
6892
6893 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6894 goto fail;
6895
6896 switch (vco) {
6897 case 3200000:
6898 div_table = div_3200;
6899 break;
6900 case 4000000:
6901 div_table = div_4000;
6902 break;
6903 case 5333333:
6904 div_table = div_5333;
6905 break;
6906 default:
6907 goto fail;
6908 }
6909
6910 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6911
Damien Lespiaucaf4e252015-06-04 16:56:18 +01006912fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03006913 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
6914 return 200000;
6915}
6916
6917static int g33_get_display_clock_speed(struct drm_device *dev)
6918{
6919 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
6920 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
6921 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
6922 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
6923 const uint8_t *div_table;
6924 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6925 uint16_t tmp = 0;
6926
6927 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6928
6929 cdclk_sel = (tmp >> 4) & 0x7;
6930
6931 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6932 goto fail;
6933
6934 switch (vco) {
6935 case 3200000:
6936 div_table = div_3200;
6937 break;
6938 case 4000000:
6939 div_table = div_4000;
6940 break;
6941 case 4800000:
6942 div_table = div_4800;
6943 break;
6944 case 5333333:
6945 div_table = div_5333;
6946 break;
6947 default:
6948 goto fail;
6949 }
6950
6951 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6952
Damien Lespiaucaf4e252015-06-04 16:56:18 +01006953fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03006954 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
6955 return 190476;
6956}
6957
Zhenyu Wang2c072452009-06-05 15:38:42 +08006958static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006959intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006960{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006961 while (*num > DATA_LINK_M_N_MASK ||
6962 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08006963 *num >>= 1;
6964 *den >>= 1;
6965 }
6966}
6967
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006968static void compute_m_n(unsigned int m, unsigned int n,
6969 uint32_t *ret_m, uint32_t *ret_n)
6970{
6971 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
6972 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6973 intel_reduce_m_n_ratio(ret_m, ret_n);
6974}
6975
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006976void
6977intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6978 int pixel_clock, int link_clock,
6979 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006980{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006981 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006982
6983 compute_m_n(bits_per_pixel * pixel_clock,
6984 link_clock * nlanes * 8,
6985 &m_n->gmch_m, &m_n->gmch_n);
6986
6987 compute_m_n(pixel_clock, link_clock,
6988 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08006989}
6990
Chris Wilsona7615032011-01-12 17:04:08 +00006991static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
6992{
Jani Nikulad330a952014-01-21 11:24:25 +02006993 if (i915.panel_use_ssc >= 0)
6994 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006995 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07006996 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00006997}
6998
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006999static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
7000 int num_connectors)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007001{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007002 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007003 struct drm_i915_private *dev_priv = dev->dev_private;
7004 int refclk;
7005
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007006 WARN_ON(!crtc_state->base.state);
7007
Imre Deak5ab7b0b2015-03-06 03:29:25 +02007008 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02007009 refclk = 100000;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007010 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007011 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007012 refclk = dev_priv->vbt.lvds_ssc_freq;
7013 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007014 } else if (!IS_GEN2(dev)) {
7015 refclk = 96000;
7016 } else {
7017 refclk = 48000;
7018 }
7019
7020 return refclk;
7021}
7022
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007023static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007024{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007025 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007026}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007027
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007028static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7029{
7030 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007031}
7032
Daniel Vetterf47709a2013-03-28 10:42:02 +01007033static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007034 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08007035 intel_clock_t *reduced_clock)
7036{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007037 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007038 u32 fp, fp2 = 0;
7039
7040 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007041 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007042 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007043 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007044 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007045 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007046 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007047 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007048 }
7049
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007050 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007051
Daniel Vetterf47709a2013-03-28 10:42:02 +01007052 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007053 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007054 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007055 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007056 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007057 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007058 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007059 }
7060}
7061
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007062static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7063 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007064{
7065 u32 reg_val;
7066
7067 /*
7068 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7069 * and set it to a reasonable value instead.
7070 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007071 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007072 reg_val &= 0xffffff00;
7073 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007074 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007075
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007076 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007077 reg_val &= 0x8cffffff;
7078 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007079 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007080
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007081 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007082 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007083 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007084
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007085 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007086 reg_val &= 0x00ffffff;
7087 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007088 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007089}
7090
Daniel Vetterb5518422013-05-03 11:49:48 +02007091static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7092 struct intel_link_m_n *m_n)
7093{
7094 struct drm_device *dev = crtc->base.dev;
7095 struct drm_i915_private *dev_priv = dev->dev_private;
7096 int pipe = crtc->pipe;
7097
Daniel Vettere3b95f12013-05-03 11:49:49 +02007098 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7099 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7100 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7101 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007102}
7103
7104static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007105 struct intel_link_m_n *m_n,
7106 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007107{
7108 struct drm_device *dev = crtc->base.dev;
7109 struct drm_i915_private *dev_priv = dev->dev_private;
7110 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007111 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007112
7113 if (INTEL_INFO(dev)->gen >= 5) {
7114 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7115 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7116 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7117 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007118 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7119 * for gen < 8) and if DRRS is supported (to make sure the
7120 * registers are not unnecessarily accessed).
7121 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307122 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007123 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007124 I915_WRITE(PIPE_DATA_M2(transcoder),
7125 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7126 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7127 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7128 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7129 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007130 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007131 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7132 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7133 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7134 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007135 }
7136}
7137
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307138void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007139{
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307140 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7141
7142 if (m_n == M1_N1) {
7143 dp_m_n = &crtc->config->dp_m_n;
7144 dp_m2_n2 = &crtc->config->dp_m2_n2;
7145 } else if (m_n == M2_N2) {
7146
7147 /*
7148 * M2_N2 registers are not supported. Hence m2_n2 divider value
7149 * needs to be programmed into M1_N1.
7150 */
7151 dp_m_n = &crtc->config->dp_m2_n2;
7152 } else {
7153 DRM_ERROR("Unsupported divider value\n");
7154 return;
7155 }
7156
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007157 if (crtc->config->has_pch_encoder)
7158 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007159 else
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307160 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007161}
7162
Ville Syrjäläd288f652014-10-28 13:20:22 +02007163static void vlv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007164 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007165{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007166 u32 dpll, dpll_md;
7167
7168 /*
7169 * Enable DPIO clock input. We should never disable the reference
7170 * clock for pipe B, since VGA hotplug / manual detection depends
7171 * on it.
7172 */
7173 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
7174 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
7175 /* We should never disable this, set it here for state tracking */
7176 if (crtc->pipe == PIPE_B)
7177 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7178 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007179 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007180
Ville Syrjäläd288f652014-10-28 13:20:22 +02007181 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007182 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007183 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007184}
7185
Ville Syrjäläd288f652014-10-28 13:20:22 +02007186static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007187 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007188{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007189 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007190 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007191 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007192 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007193 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007194 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007195
Ville Syrjäläa5805162015-05-26 20:42:30 +03007196 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007197
Ville Syrjäläd288f652014-10-28 13:20:22 +02007198 bestn = pipe_config->dpll.n;
7199 bestm1 = pipe_config->dpll.m1;
7200 bestm2 = pipe_config->dpll.m2;
7201 bestp1 = pipe_config->dpll.p1;
7202 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007203
Jesse Barnes89b667f2013-04-18 14:51:36 -07007204 /* See eDP HDMI DPIO driver vbios notes doc */
7205
7206 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007207 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007208 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007209
7210 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007211 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007212
7213 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007214 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007215 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007216 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007217
7218 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007219 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007220
7221 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007222 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7223 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7224 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007225 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007226
7227 /*
7228 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7229 * but we don't support that).
7230 * Note: don't use the DAC post divider as it seems unstable.
7231 */
7232 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007233 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007234
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007235 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007236 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007237
Jesse Barnes89b667f2013-04-18 14:51:36 -07007238 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007239 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007240 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7241 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007242 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03007243 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007244 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007245 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007246 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007247
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007248 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007249 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007250 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007251 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007252 0x0df40000);
7253 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007254 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007255 0x0df70000);
7256 } else { /* HDMI or VGA */
7257 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007258 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007259 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007260 0x0df70000);
7261 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007262 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007263 0x0df40000);
7264 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007265
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007266 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007267 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007268 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7269 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007270 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007271 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007272
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007273 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007274 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007275}
7276
Ville Syrjäläd288f652014-10-28 13:20:22 +02007277static void chv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007278 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007279{
Ville Syrjäläd288f652014-10-28 13:20:22 +02007280 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007281 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
7282 DPLL_VCO_ENABLE;
7283 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007284 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007285
Ville Syrjäläd288f652014-10-28 13:20:22 +02007286 pipe_config->dpll_hw_state.dpll_md =
7287 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007288}
7289
Ville Syrjäläd288f652014-10-28 13:20:22 +02007290static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007291 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007292{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007293 struct drm_device *dev = crtc->base.dev;
7294 struct drm_i915_private *dev_priv = dev->dev_private;
7295 int pipe = crtc->pipe;
7296 int dpll_reg = DPLL(crtc->pipe);
7297 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307298 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007299 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307300 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307301 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007302
Ville Syrjäläd288f652014-10-28 13:20:22 +02007303 bestn = pipe_config->dpll.n;
7304 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7305 bestm1 = pipe_config->dpll.m1;
7306 bestm2 = pipe_config->dpll.m2 >> 22;
7307 bestp1 = pipe_config->dpll.p1;
7308 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307309 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307310 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307311 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007312
7313 /*
7314 * Enable Refclk and SSC
7315 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007316 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007317 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007318
Ville Syrjäläa5805162015-05-26 20:42:30 +03007319 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007320
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007321 /* p1 and p2 divider */
7322 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7323 5 << DPIO_CHV_S1_DIV_SHIFT |
7324 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7325 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7326 1 << DPIO_CHV_K_DIV_SHIFT);
7327
7328 /* Feedback post-divider - m2 */
7329 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7330
7331 /* Feedback refclk divider - n and m1 */
7332 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7333 DPIO_CHV_M1_DIV_BY_2 |
7334 1 << DPIO_CHV_N_DIV_SHIFT);
7335
7336 /* M2 fraction division */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307337 if (bestm2_frac)
7338 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007339
7340 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307341 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7342 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7343 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7344 if (bestm2_frac)
7345 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7346 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007347
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307348 /* Program digital lock detect threshold */
7349 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7350 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7351 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7352 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7353 if (!bestm2_frac)
7354 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7355 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7356
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007357 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307358 if (vco == 5400000) {
7359 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7360 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7361 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7362 tribuf_calcntr = 0x9;
7363 } else if (vco <= 6200000) {
7364 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7365 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7366 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7367 tribuf_calcntr = 0x9;
7368 } else if (vco <= 6480000) {
7369 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7370 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7371 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7372 tribuf_calcntr = 0x8;
7373 } else {
7374 /* Not supported. Apply the same limits as in the max case */
7375 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7376 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7377 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7378 tribuf_calcntr = 0;
7379 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007380 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7381
Ville Syrjälä968040b2015-03-11 22:52:08 +02007382 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307383 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7384 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7385 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7386
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007387 /* AFC Recal */
7388 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7389 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7390 DPIO_AFC_RECAL);
7391
Ville Syrjäläa5805162015-05-26 20:42:30 +03007392 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007393}
7394
Ville Syrjäläd288f652014-10-28 13:20:22 +02007395/**
7396 * vlv_force_pll_on - forcibly enable just the PLL
7397 * @dev_priv: i915 private structure
7398 * @pipe: pipe PLL to enable
7399 * @dpll: PLL configuration
7400 *
7401 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7402 * in cases where we need the PLL enabled even when @pipe is not going to
7403 * be enabled.
7404 */
7405void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7406 const struct dpll *dpll)
7407{
7408 struct intel_crtc *crtc =
7409 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007410 struct intel_crtc_state pipe_config = {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007411 .base.crtc = &crtc->base,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007412 .pixel_multiplier = 1,
7413 .dpll = *dpll,
7414 };
7415
7416 if (IS_CHERRYVIEW(dev)) {
7417 chv_update_pll(crtc, &pipe_config);
7418 chv_prepare_pll(crtc, &pipe_config);
7419 chv_enable_pll(crtc, &pipe_config);
7420 } else {
7421 vlv_update_pll(crtc, &pipe_config);
7422 vlv_prepare_pll(crtc, &pipe_config);
7423 vlv_enable_pll(crtc, &pipe_config);
7424 }
7425}
7426
7427/**
7428 * vlv_force_pll_off - forcibly disable just the PLL
7429 * @dev_priv: i915 private structure
7430 * @pipe: pipe PLL to disable
7431 *
7432 * Disable the PLL for @pipe. To be used in cases where we need
7433 * the PLL enabled even when @pipe is not going to be enabled.
7434 */
7435void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7436{
7437 if (IS_CHERRYVIEW(dev))
7438 chv_disable_pll(to_i915(dev), pipe);
7439 else
7440 vlv_disable_pll(to_i915(dev), pipe);
7441}
7442
Daniel Vetterf47709a2013-03-28 10:42:02 +01007443static void i9xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007444 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01007445 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007446 int num_connectors)
7447{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007448 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007449 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007450 u32 dpll;
7451 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007452 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007453
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007454 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307455
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007456 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7457 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007458
7459 dpll = DPLL_VGA_MODE_DIS;
7460
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007461 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007462 dpll |= DPLLB_MODE_LVDS;
7463 else
7464 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007465
Daniel Vetteref1b4602013-06-01 17:17:04 +02007466 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007467 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007468 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007469 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007470
7471 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007472 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007473
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007474 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007475 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007476
7477 /* compute bitmask from p1 value */
7478 if (IS_PINEVIEW(dev))
7479 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7480 else {
7481 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7482 if (IS_G4X(dev) && reduced_clock)
7483 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7484 }
7485 switch (clock->p2) {
7486 case 5:
7487 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7488 break;
7489 case 7:
7490 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7491 break;
7492 case 10:
7493 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7494 break;
7495 case 14:
7496 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7497 break;
7498 }
7499 if (INTEL_INFO(dev)->gen >= 4)
7500 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7501
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007502 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007503 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007504 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007505 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7506 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7507 else
7508 dpll |= PLL_REF_INPUT_DREFCLK;
7509
7510 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007511 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007512
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007513 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007514 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007515 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007516 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007517 }
7518}
7519
Daniel Vetterf47709a2013-03-28 10:42:02 +01007520static void i8xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007521 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01007522 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007523 int num_connectors)
7524{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007525 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007526 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007527 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007528 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007529
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007530 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307531
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007532 dpll = DPLL_VGA_MODE_DIS;
7533
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007534 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007535 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7536 } else {
7537 if (clock->p1 == 2)
7538 dpll |= PLL_P1_DIVIDE_BY_TWO;
7539 else
7540 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7541 if (clock->p2 == 4)
7542 dpll |= PLL_P2_DIVIDE_BY_4;
7543 }
7544
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007545 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007546 dpll |= DPLL_DVO_2X_MODE;
7547
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007548 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007549 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7550 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7551 else
7552 dpll |= PLL_REF_INPUT_DREFCLK;
7553
7554 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007555 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007556}
7557
Daniel Vetter8a654f32013-06-01 17:16:22 +02007558static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007559{
7560 struct drm_device *dev = intel_crtc->base.dev;
7561 struct drm_i915_private *dev_priv = dev->dev_private;
7562 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007563 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02007564 struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007565 &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007566 uint32_t crtc_vtotal, crtc_vblank_end;
7567 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007568
7569 /* We need to be careful not to changed the adjusted mode, for otherwise
7570 * the hw state checker will get angry at the mismatch. */
7571 crtc_vtotal = adjusted_mode->crtc_vtotal;
7572 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007573
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007574 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007575 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007576 crtc_vtotal -= 1;
7577 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007578
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007579 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007580 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7581 else
7582 vsyncshift = adjusted_mode->crtc_hsync_start -
7583 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007584 if (vsyncshift < 0)
7585 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007586 }
7587
7588 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007589 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007590
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007591 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007592 (adjusted_mode->crtc_hdisplay - 1) |
7593 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007594 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007595 (adjusted_mode->crtc_hblank_start - 1) |
7596 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007597 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007598 (adjusted_mode->crtc_hsync_start - 1) |
7599 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7600
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007601 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007602 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007603 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007604 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007605 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007606 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007607 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007608 (adjusted_mode->crtc_vsync_start - 1) |
7609 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7610
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007611 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7612 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7613 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7614 * bits. */
7615 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7616 (pipe == PIPE_B || pipe == PIPE_C))
7617 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7618
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007619 /* pipesrc controls the size that is scaled from, which should
7620 * always be the user's requested size.
7621 */
7622 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007623 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7624 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007625}
7626
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007627static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007628 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007629{
7630 struct drm_device *dev = crtc->base.dev;
7631 struct drm_i915_private *dev_priv = dev->dev_private;
7632 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7633 uint32_t tmp;
7634
7635 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007636 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7637 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007638 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007639 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7640 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007641 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007642 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7643 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007644
7645 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007646 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7647 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007648 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007649 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7650 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007651 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007652 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7653 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007654
7655 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007656 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7657 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7658 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007659 }
7660
7661 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007662 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7663 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7664
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007665 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7666 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007667}
7668
Daniel Vetterf6a83282014-02-11 15:28:57 -08007669void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007670 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007671{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007672 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7673 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7674 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7675 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007676
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007677 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7678 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7679 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7680 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007681
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007682 mode->flags = pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007683
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007684 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7685 mode->flags |= pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007686}
7687
Daniel Vetter84b046f2013-02-19 18:48:54 +01007688static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7689{
7690 struct drm_device *dev = intel_crtc->base.dev;
7691 struct drm_i915_private *dev_priv = dev->dev_private;
7692 uint32_t pipeconf;
7693
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007694 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007695
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007696 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7697 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7698 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007699
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007700 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007701 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007702
Daniel Vetterff9ce462013-04-24 14:57:17 +02007703 /* only g4x and later have fancy bpc/dither controls */
7704 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007705 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007706 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007707 pipeconf |= PIPECONF_DITHER_EN |
7708 PIPECONF_DITHER_TYPE_SP;
7709
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007710 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007711 case 18:
7712 pipeconf |= PIPECONF_6BPC;
7713 break;
7714 case 24:
7715 pipeconf |= PIPECONF_8BPC;
7716 break;
7717 case 30:
7718 pipeconf |= PIPECONF_10BPC;
7719 break;
7720 default:
7721 /* Case prevented by intel_choose_pipe_bpp_dither. */
7722 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007723 }
7724 }
7725
7726 if (HAS_PIPE_CXSR(dev)) {
7727 if (intel_crtc->lowfreq_avail) {
7728 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7729 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7730 } else {
7731 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007732 }
7733 }
7734
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007735 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007736 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007737 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007738 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7739 else
7740 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7741 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007742 pipeconf |= PIPECONF_PROGRESSIVE;
7743
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007744 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007745 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007746
Daniel Vetter84b046f2013-02-19 18:48:54 +01007747 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7748 POSTING_READ(PIPECONF(intel_crtc->pipe));
7749}
7750
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007751static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7752 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007753{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007754 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007755 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc751ce42010-03-25 11:48:48 -07007756 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07007757 intel_clock_t clock, reduced_clock;
Daniel Vettera16af722013-04-30 14:01:44 +02007758 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007759 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01007760 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08007761 const intel_limit_t *limit;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007762 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007763 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007764 struct drm_connector_state *connector_state;
7765 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -08007766
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007767 memset(&crtc_state->dpll_hw_state, 0,
7768 sizeof(crtc_state->dpll_hw_state));
7769
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007770 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007771 if (connector_state->crtc != &crtc->base)
7772 continue;
7773
7774 encoder = to_intel_encoder(connector_state->best_encoder);
7775
Chris Wilson5eddb702010-09-11 13:48:45 +01007776 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007777 case INTEL_OUTPUT_LVDS:
7778 is_lvds = true;
7779 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007780 case INTEL_OUTPUT_DSI:
7781 is_dsi = true;
7782 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007783 default:
7784 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007785 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05007786
Eric Anholtc751ce42010-03-25 11:48:48 -07007787 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08007788 }
7789
Jani Nikulaf2335332013-09-13 11:03:09 +03007790 if (is_dsi)
Daniel Vetter5b18e572014-04-24 23:55:06 +02007791 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007792
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007793 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007794 refclk = i9xx_get_refclk(crtc_state, num_connectors);
Jani Nikulaf2335332013-09-13 11:03:09 +03007795
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007796 /*
7797 * Returns a set of divisors for the desired target clock with
7798 * the given refclk, or FALSE. The returned values represent
7799 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7800 * 2) / p1 / p2.
7801 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007802 limit = intel_limit(crtc_state, refclk);
7803 ok = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007804 crtc_state->port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007805 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03007806 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007807 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7808 return -EINVAL;
7809 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007810
Jani Nikulaf2335332013-09-13 11:03:09 +03007811 if (is_lvds && dev_priv->lvds_downclock_avail) {
7812 /*
7813 * Ensure we match the reduced clock's P to the target
7814 * clock. If the clocks don't match, we can't switch
7815 * the display clock by using the FP0/FP1. In such case
7816 * we will disable the LVDS downclock feature.
7817 */
7818 has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007819 dev_priv->display.find_dpll(limit, crtc_state,
Jani Nikulaf2335332013-09-13 11:03:09 +03007820 dev_priv->lvds_downclock,
7821 refclk, &clock,
7822 &reduced_clock);
7823 }
7824 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007825 crtc_state->dpll.n = clock.n;
7826 crtc_state->dpll.m1 = clock.m1;
7827 crtc_state->dpll.m2 = clock.m2;
7828 crtc_state->dpll.p1 = clock.p1;
7829 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007830 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007831
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007832 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007833 i8xx_update_pll(crtc, crtc_state,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307834 has_reduced_clock ? &reduced_clock : NULL,
7835 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007836 } else if (IS_CHERRYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007837 chv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007838 } else if (IS_VALLEYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007839 vlv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007840 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007841 i9xx_update_pll(crtc, crtc_state,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007842 has_reduced_clock ? &reduced_clock : NULL,
Robin Schroereba905b2014-05-18 02:24:50 +02007843 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007844 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007845
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007846 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007847}
7848
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007849static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007850 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007851{
7852 struct drm_device *dev = crtc->base.dev;
7853 struct drm_i915_private *dev_priv = dev->dev_private;
7854 uint32_t tmp;
7855
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007856 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7857 return;
7858
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007859 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007860 if (!(tmp & PFIT_ENABLE))
7861 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007862
Daniel Vetter06922822013-07-11 13:35:40 +02007863 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007864 if (INTEL_INFO(dev)->gen < 4) {
7865 if (crtc->pipe != PIPE_B)
7866 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007867 } else {
7868 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7869 return;
7870 }
7871
Daniel Vetter06922822013-07-11 13:35:40 +02007872 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007873 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7874 if (INTEL_INFO(dev)->gen < 5)
7875 pipe_config->gmch_pfit.lvds_border_bits =
7876 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7877}
7878
Jesse Barnesacbec812013-09-20 11:29:32 -07007879static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007880 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07007881{
7882 struct drm_device *dev = crtc->base.dev;
7883 struct drm_i915_private *dev_priv = dev->dev_private;
7884 int pipe = pipe_config->cpu_transcoder;
7885 intel_clock_t clock;
7886 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07007887 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07007888
Shobhit Kumarf573de52014-07-30 20:32:37 +05307889 /* In case of MIPI DPLL will not even be used */
7890 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7891 return;
7892
Ville Syrjäläa5805162015-05-26 20:42:30 +03007893 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007894 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03007895 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007896
7897 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7898 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7899 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7900 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7901 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7902
Ville Syrjäläf6466282013-10-14 14:50:31 +03007903 vlv_clock(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007904
Ville Syrjäläf6466282013-10-14 14:50:31 +03007905 /* clock.dot is the fast clock */
7906 pipe_config->port_clock = clock.dot / 5;
Jesse Barnesacbec812013-09-20 11:29:32 -07007907}
7908
Damien Lespiau5724dbd2015-01-20 12:51:52 +00007909static void
7910i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7911 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007912{
7913 struct drm_device *dev = crtc->base.dev;
7914 struct drm_i915_private *dev_priv = dev->dev_private;
7915 u32 val, base, offset;
7916 int pipe = crtc->pipe, plane = crtc->plane;
7917 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00007918 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007919 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00007920 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007921
Damien Lespiau42a7b082015-02-05 19:35:13 +00007922 val = I915_READ(DSPCNTR(plane));
7923 if (!(val & DISPLAY_PLANE_ENABLE))
7924 return;
7925
Damien Lespiaud9806c92015-01-21 14:07:19 +00007926 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00007927 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007928 DRM_DEBUG_KMS("failed to alloc fb\n");
7929 return;
7930 }
7931
Damien Lespiau1b842c82015-01-21 13:50:54 +00007932 fb = &intel_fb->base;
7933
Daniel Vetter18c52472015-02-10 17:16:09 +00007934 if (INTEL_INFO(dev)->gen >= 4) {
7935 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007936 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00007937 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
7938 }
7939 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007940
7941 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00007942 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007943 fb->pixel_format = fourcc;
7944 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007945
7946 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007947 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007948 offset = I915_READ(DSPTILEOFF(plane));
7949 else
7950 offset = I915_READ(DSPLINOFF(plane));
7951 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7952 } else {
7953 base = I915_READ(DSPADDR(plane));
7954 }
7955 plane_config->base = base;
7956
7957 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007958 fb->width = ((val >> 16) & 0xfff) + 1;
7959 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007960
7961 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007962 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007963
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007964 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00007965 fb->pixel_format,
7966 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007967
Daniel Vetterf37b5c22015-02-10 23:12:27 +01007968 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007969
Damien Lespiau2844a922015-01-20 12:51:48 +00007970 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7971 pipe_name(pipe), plane, fb->width, fb->height,
7972 fb->bits_per_pixel, base, fb->pitches[0],
7973 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007974
Damien Lespiau2d140302015-02-05 17:22:18 +00007975 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007976}
7977
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007978static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007979 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007980{
7981 struct drm_device *dev = crtc->base.dev;
7982 struct drm_i915_private *dev_priv = dev->dev_private;
7983 int pipe = pipe_config->cpu_transcoder;
7984 enum dpio_channel port = vlv_pipe_to_channel(pipe);
7985 intel_clock_t clock;
7986 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
7987 int refclk = 100000;
7988
Ville Syrjäläa5805162015-05-26 20:42:30 +03007989 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007990 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
7991 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
7992 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
7993 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03007994 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007995
7996 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
7997 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
7998 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
7999 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8000 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8001
8002 chv_clock(refclk, &clock);
8003
8004 /* clock.dot is the fast clock */
8005 pipe_config->port_clock = clock.dot / 5;
8006}
8007
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008008static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008009 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008010{
8011 struct drm_device *dev = crtc->base.dev;
8012 struct drm_i915_private *dev_priv = dev->dev_private;
8013 uint32_t tmp;
8014
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008015 if (!intel_display_power_is_enabled(dev_priv,
8016 POWER_DOMAIN_PIPE(crtc->pipe)))
Imre Deakb5482bd2014-03-05 16:20:55 +02008017 return false;
8018
Daniel Vettere143a212013-07-04 12:01:15 +02008019 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008020 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02008021
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008022 tmp = I915_READ(PIPECONF(crtc->pipe));
8023 if (!(tmp & PIPECONF_ENABLE))
8024 return false;
8025
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008026 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
8027 switch (tmp & PIPECONF_BPC_MASK) {
8028 case PIPECONF_6BPC:
8029 pipe_config->pipe_bpp = 18;
8030 break;
8031 case PIPECONF_8BPC:
8032 pipe_config->pipe_bpp = 24;
8033 break;
8034 case PIPECONF_10BPC:
8035 pipe_config->pipe_bpp = 30;
8036 break;
8037 default:
8038 break;
8039 }
8040 }
8041
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008042 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
8043 pipe_config->limited_color_range = true;
8044
Ville Syrjälä282740f2013-09-04 18:30:03 +03008045 if (INTEL_INFO(dev)->gen < 4)
8046 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8047
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008048 intel_get_pipe_timings(crtc, pipe_config);
8049
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008050 i9xx_get_pfit_config(crtc, pipe_config);
8051
Daniel Vetter6c49f242013-06-06 12:45:25 +02008052 if (INTEL_INFO(dev)->gen >= 4) {
8053 tmp = I915_READ(DPLL_MD(crtc->pipe));
8054 pipe_config->pixel_multiplier =
8055 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8056 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008057 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008058 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8059 tmp = I915_READ(DPLL(crtc->pipe));
8060 pipe_config->pixel_multiplier =
8061 ((tmp & SDVO_MULTIPLIER_MASK)
8062 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8063 } else {
8064 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8065 * port and will be fixed up in the encoder->get_config
8066 * function. */
8067 pipe_config->pixel_multiplier = 1;
8068 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008069 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
8070 if (!IS_VALLEYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008071 /*
8072 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8073 * on 830. Filter it out here so that we don't
8074 * report errors due to that.
8075 */
8076 if (IS_I830(dev))
8077 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8078
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008079 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8080 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008081 } else {
8082 /* Mask out read-only status bits. */
8083 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8084 DPLL_PORTC_READY_MASK |
8085 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008086 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008087
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008088 if (IS_CHERRYVIEW(dev))
8089 chv_crtc_clock_get(crtc, pipe_config);
8090 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008091 vlv_crtc_clock_get(crtc, pipe_config);
8092 else
8093 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008094
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008095 return true;
8096}
8097
Paulo Zanonidde86e22012-12-01 12:04:25 -02008098static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008099{
8100 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008101 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008102 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008103 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008104 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008105 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008106 bool has_ck505 = false;
8107 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008108
8109 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008110 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008111 switch (encoder->type) {
8112 case INTEL_OUTPUT_LVDS:
8113 has_panel = true;
8114 has_lvds = true;
8115 break;
8116 case INTEL_OUTPUT_EDP:
8117 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008118 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008119 has_cpu_edp = true;
8120 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008121 default:
8122 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008123 }
8124 }
8125
Keith Packard99eb6a02011-09-26 14:29:12 -07008126 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008127 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008128 can_ssc = has_ck505;
8129 } else {
8130 has_ck505 = false;
8131 can_ssc = true;
8132 }
8133
Imre Deak2de69052013-05-08 13:14:04 +03008134 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8135 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008136
8137 /* Ironlake: try to setup display ref clock before DPLL
8138 * enabling. This is only under driver's control after
8139 * PCH B stepping, previous chipset stepping should be
8140 * ignoring this setting.
8141 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008142 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008143
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008144 /* As we must carefully and slowly disable/enable each source in turn,
8145 * compute the final state we want first and check if we need to
8146 * make any changes at all.
8147 */
8148 final = val;
8149 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008150 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008151 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008152 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008153 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8154
8155 final &= ~DREF_SSC_SOURCE_MASK;
8156 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8157 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008158
Keith Packard199e5d72011-09-22 12:01:57 -07008159 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008160 final |= DREF_SSC_SOURCE_ENABLE;
8161
8162 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8163 final |= DREF_SSC1_ENABLE;
8164
8165 if (has_cpu_edp) {
8166 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8167 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8168 else
8169 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8170 } else
8171 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8172 } else {
8173 final |= DREF_SSC_SOURCE_DISABLE;
8174 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8175 }
8176
8177 if (final == val)
8178 return;
8179
8180 /* Always enable nonspread source */
8181 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8182
8183 if (has_ck505)
8184 val |= DREF_NONSPREAD_CK505_ENABLE;
8185 else
8186 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8187
8188 if (has_panel) {
8189 val &= ~DREF_SSC_SOURCE_MASK;
8190 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008191
Keith Packard199e5d72011-09-22 12:01:57 -07008192 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008193 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008194 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008195 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008196 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008197 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008198
8199 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008200 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008201 POSTING_READ(PCH_DREF_CONTROL);
8202 udelay(200);
8203
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008204 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008205
8206 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008207 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008208 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008209 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008210 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008211 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008212 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008213 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008214 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008215
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008216 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008217 POSTING_READ(PCH_DREF_CONTROL);
8218 udelay(200);
8219 } else {
8220 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8221
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008222 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008223
8224 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008225 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008226
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008227 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008228 POSTING_READ(PCH_DREF_CONTROL);
8229 udelay(200);
8230
8231 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008232 val &= ~DREF_SSC_SOURCE_MASK;
8233 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008234
8235 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008236 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008237
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008238 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008239 POSTING_READ(PCH_DREF_CONTROL);
8240 udelay(200);
8241 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008242
8243 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008244}
8245
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008246static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008247{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008248 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008249
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008250 tmp = I915_READ(SOUTH_CHICKEN2);
8251 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8252 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008253
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008254 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8255 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8256 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008257
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008258 tmp = I915_READ(SOUTH_CHICKEN2);
8259 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8260 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008261
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008262 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8263 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8264 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008265}
8266
8267/* WaMPhyProgramming:hsw */
8268static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8269{
8270 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008271
8272 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8273 tmp &= ~(0xFF << 24);
8274 tmp |= (0x12 << 24);
8275 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8276
Paulo Zanonidde86e22012-12-01 12:04:25 -02008277 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8278 tmp |= (1 << 11);
8279 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8280
8281 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8282 tmp |= (1 << 11);
8283 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8284
Paulo Zanonidde86e22012-12-01 12:04:25 -02008285 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8286 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8287 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8288
8289 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8290 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8291 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8292
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008293 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8294 tmp &= ~(7 << 13);
8295 tmp |= (5 << 13);
8296 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008297
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008298 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8299 tmp &= ~(7 << 13);
8300 tmp |= (5 << 13);
8301 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008302
8303 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8304 tmp &= ~0xFF;
8305 tmp |= 0x1C;
8306 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8307
8308 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8309 tmp &= ~0xFF;
8310 tmp |= 0x1C;
8311 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8312
8313 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8314 tmp &= ~(0xFF << 16);
8315 tmp |= (0x1C << 16);
8316 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8317
8318 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8319 tmp &= ~(0xFF << 16);
8320 tmp |= (0x1C << 16);
8321 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8322
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008323 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8324 tmp |= (1 << 27);
8325 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008326
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008327 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8328 tmp |= (1 << 27);
8329 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008330
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008331 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8332 tmp &= ~(0xF << 28);
8333 tmp |= (4 << 28);
8334 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008335
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008336 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8337 tmp &= ~(0xF << 28);
8338 tmp |= (4 << 28);
8339 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008340}
8341
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008342/* Implements 3 different sequences from BSpec chapter "Display iCLK
8343 * Programming" based on the parameters passed:
8344 * - Sequence to enable CLKOUT_DP
8345 * - Sequence to enable CLKOUT_DP without spread
8346 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8347 */
8348static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8349 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008350{
8351 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008352 uint32_t reg, tmp;
8353
8354 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8355 with_spread = true;
8356 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
8357 with_fdi, "LP PCH doesn't have FDI\n"))
8358 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008359
Ville Syrjäläa5805162015-05-26 20:42:30 +03008360 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008361
8362 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8363 tmp &= ~SBI_SSCCTL_DISABLE;
8364 tmp |= SBI_SSCCTL_PATHALT;
8365 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8366
8367 udelay(24);
8368
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008369 if (with_spread) {
8370 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8371 tmp &= ~SBI_SSCCTL_PATHALT;
8372 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008373
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008374 if (with_fdi) {
8375 lpt_reset_fdi_mphy(dev_priv);
8376 lpt_program_fdi_mphy(dev_priv);
8377 }
8378 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008379
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008380 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8381 SBI_GEN0 : SBI_DBUFF0;
8382 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8383 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8384 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008385
Ville Syrjäläa5805162015-05-26 20:42:30 +03008386 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008387}
8388
Paulo Zanoni47701c32013-07-23 11:19:25 -03008389/* Sequence to disable CLKOUT_DP */
8390static void lpt_disable_clkout_dp(struct drm_device *dev)
8391{
8392 struct drm_i915_private *dev_priv = dev->dev_private;
8393 uint32_t reg, tmp;
8394
Ville Syrjäläa5805162015-05-26 20:42:30 +03008395 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008396
8397 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8398 SBI_GEN0 : SBI_DBUFF0;
8399 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8400 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8401 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8402
8403 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8404 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8405 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8406 tmp |= SBI_SSCCTL_PATHALT;
8407 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8408 udelay(32);
8409 }
8410 tmp |= SBI_SSCCTL_DISABLE;
8411 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8412 }
8413
Ville Syrjäläa5805162015-05-26 20:42:30 +03008414 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008415}
8416
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008417static void lpt_init_pch_refclk(struct drm_device *dev)
8418{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008419 struct intel_encoder *encoder;
8420 bool has_vga = false;
8421
Damien Lespiaub2784e12014-08-05 11:29:37 +01008422 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008423 switch (encoder->type) {
8424 case INTEL_OUTPUT_ANALOG:
8425 has_vga = true;
8426 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008427 default:
8428 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008429 }
8430 }
8431
Paulo Zanoni47701c32013-07-23 11:19:25 -03008432 if (has_vga)
8433 lpt_enable_clkout_dp(dev, true, true);
8434 else
8435 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008436}
8437
Paulo Zanonidde86e22012-12-01 12:04:25 -02008438/*
8439 * Initialize reference clocks when the driver loads
8440 */
8441void intel_init_pch_refclk(struct drm_device *dev)
8442{
8443 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8444 ironlake_init_pch_refclk(dev);
8445 else if (HAS_PCH_LPT(dev))
8446 lpt_init_pch_refclk(dev);
8447}
8448
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008449static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008450{
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008451 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008452 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008453 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008454 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008455 struct drm_connector_state *connector_state;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008456 struct intel_encoder *encoder;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008457 int num_connectors = 0, i;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008458 bool is_lvds = false;
8459
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008460 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008461 if (connector_state->crtc != crtc_state->base.crtc)
8462 continue;
8463
8464 encoder = to_intel_encoder(connector_state->best_encoder);
8465
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008466 switch (encoder->type) {
8467 case INTEL_OUTPUT_LVDS:
8468 is_lvds = true;
8469 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008470 default:
8471 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008472 }
8473 num_connectors++;
8474 }
8475
8476 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008477 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008478 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008479 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008480 }
8481
8482 return 120000;
8483}
8484
Daniel Vetter6ff93602013-04-19 11:24:36 +02008485static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008486{
8487 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8488 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8489 int pipe = intel_crtc->pipe;
8490 uint32_t val;
8491
Daniel Vetter78114072013-06-13 00:54:57 +02008492 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008493
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008494 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008495 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008496 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008497 break;
8498 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008499 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008500 break;
8501 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008502 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008503 break;
8504 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008505 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008506 break;
8507 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008508 /* Case prevented by intel_choose_pipe_bpp_dither. */
8509 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008510 }
8511
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008512 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008513 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8514
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008515 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008516 val |= PIPECONF_INTERLACED_ILK;
8517 else
8518 val |= PIPECONF_PROGRESSIVE;
8519
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008520 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008521 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008522
Paulo Zanonic8203562012-09-12 10:06:29 -03008523 I915_WRITE(PIPECONF(pipe), val);
8524 POSTING_READ(PIPECONF(pipe));
8525}
8526
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008527/*
8528 * Set up the pipe CSC unit.
8529 *
8530 * Currently only full range RGB to limited range RGB conversion
8531 * is supported, but eventually this should handle various
8532 * RGB<->YCbCr scenarios as well.
8533 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01008534static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008535{
8536 struct drm_device *dev = crtc->dev;
8537 struct drm_i915_private *dev_priv = dev->dev_private;
8538 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8539 int pipe = intel_crtc->pipe;
8540 uint16_t coeff = 0x7800; /* 1.0 */
8541
8542 /*
8543 * TODO: Check what kind of values actually come out of the pipe
8544 * with these coeff/postoff values and adjust to get the best
8545 * accuracy. Perhaps we even need to take the bpc value into
8546 * consideration.
8547 */
8548
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008549 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008550 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8551
8552 /*
8553 * GY/GU and RY/RU should be the other way around according
8554 * to BSpec, but reality doesn't agree. Just set them up in
8555 * a way that results in the correct picture.
8556 */
8557 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8558 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8559
8560 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8561 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8562
8563 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8564 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8565
8566 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8567 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8568 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8569
8570 if (INTEL_INFO(dev)->gen > 6) {
8571 uint16_t postoff = 0;
8572
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008573 if (intel_crtc->config->limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02008574 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008575
8576 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8577 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8578 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8579
8580 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8581 } else {
8582 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8583
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008584 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008585 mode |= CSC_BLACK_SCREEN_OFFSET;
8586
8587 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8588 }
8589}
8590
Daniel Vetter6ff93602013-04-19 11:24:36 +02008591static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008592{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008593 struct drm_device *dev = crtc->dev;
8594 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008595 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008596 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008597 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008598 uint32_t val;
8599
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008600 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008601
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008602 if (IS_HASWELL(dev) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008603 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8604
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008605 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008606 val |= PIPECONF_INTERLACED_ILK;
8607 else
8608 val |= PIPECONF_PROGRESSIVE;
8609
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008610 I915_WRITE(PIPECONF(cpu_transcoder), val);
8611 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008612
8613 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8614 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008615
Satheeshakrishna M3cdf122c2014-04-08 15:46:53 +05308616 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008617 val = 0;
8618
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008619 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008620 case 18:
8621 val |= PIPEMISC_DITHER_6_BPC;
8622 break;
8623 case 24:
8624 val |= PIPEMISC_DITHER_8_BPC;
8625 break;
8626 case 30:
8627 val |= PIPEMISC_DITHER_10_BPC;
8628 break;
8629 case 36:
8630 val |= PIPEMISC_DITHER_12_BPC;
8631 break;
8632 default:
8633 /* Case prevented by pipe_config_set_bpp. */
8634 BUG();
8635 }
8636
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008637 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008638 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8639
8640 I915_WRITE(PIPEMISC(pipe), val);
8641 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008642}
8643
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008644static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008645 struct intel_crtc_state *crtc_state,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008646 intel_clock_t *clock,
8647 bool *has_reduced_clock,
8648 intel_clock_t *reduced_clock)
8649{
8650 struct drm_device *dev = crtc->dev;
8651 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008652 int refclk;
8653 const intel_limit_t *limit;
Daniel Vettera16af722013-04-30 14:01:44 +02008654 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008655
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008656 is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008657
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008658 refclk = ironlake_get_refclk(crtc_state);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008659
8660 /*
8661 * Returns a set of divisors for the desired target clock with the given
8662 * refclk, or FALSE. The returned values represent the clock equation:
8663 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8664 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008665 limit = intel_limit(crtc_state, refclk);
8666 ret = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008667 crtc_state->port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008668 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008669 if (!ret)
8670 return false;
8671
8672 if (is_lvds && dev_priv->lvds_downclock_avail) {
8673 /*
8674 * Ensure we match the reduced clock's P to the target clock.
8675 * If the clocks don't match, we can't switch the display clock
8676 * by using the FP0/FP1. In such case we will disable the LVDS
8677 * downclock feature.
8678 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02008679 *has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008680 dev_priv->display.find_dpll(limit, crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008681 dev_priv->lvds_downclock,
8682 refclk, clock,
8683 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008684 }
8685
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008686 return true;
8687}
8688
Paulo Zanonid4b19312012-11-29 11:29:32 -02008689int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8690{
8691 /*
8692 * Account for spread spectrum to avoid
8693 * oversubscribing the link. Max center spread
8694 * is 2.5%; use 5% for safety's sake.
8695 */
8696 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008697 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008698}
8699
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008700static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008701{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008702 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008703}
8704
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008705static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008706 struct intel_crtc_state *crtc_state,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008707 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008708 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008709{
8710 struct drm_crtc *crtc = &intel_crtc->base;
8711 struct drm_device *dev = crtc->dev;
8712 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008713 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008714 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008715 struct drm_connector_state *connector_state;
8716 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008717 uint32_t dpll;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008718 int factor, num_connectors = 0, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008719 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008720
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008721 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008722 if (connector_state->crtc != crtc_state->base.crtc)
8723 continue;
8724
8725 encoder = to_intel_encoder(connector_state->best_encoder);
8726
8727 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008728 case INTEL_OUTPUT_LVDS:
8729 is_lvds = true;
8730 break;
8731 case INTEL_OUTPUT_SDVO:
8732 case INTEL_OUTPUT_HDMI:
8733 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008734 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008735 default:
8736 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008737 }
8738
8739 num_connectors++;
8740 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008741
Chris Wilsonc1858122010-12-03 21:35:48 +00008742 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008743 factor = 21;
8744 if (is_lvds) {
8745 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008746 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008747 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008748 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008749 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008750 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008751
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008752 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02008753 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00008754
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008755 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8756 *fp2 |= FP_CB_TUNE;
8757
Chris Wilson5eddb702010-09-11 13:48:45 +01008758 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008759
Eric Anholta07d6782011-03-30 13:01:08 -07008760 if (is_lvds)
8761 dpll |= DPLLB_MODE_LVDS;
8762 else
8763 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008764
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008765 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008766 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008767
8768 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008769 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008770 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008771 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008772
Eric Anholta07d6782011-03-30 13:01:08 -07008773 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008774 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008775 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008776 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008777
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008778 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008779 case 5:
8780 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8781 break;
8782 case 7:
8783 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8784 break;
8785 case 10:
8786 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8787 break;
8788 case 14:
8789 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8790 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008791 }
8792
Daniel Vetterb4c09f32013-04-30 14:01:42 +02008793 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008794 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008795 else
8796 dpll |= PLL_REF_INPUT_DREFCLK;
8797
Daniel Vetter959e16d2013-06-05 13:34:21 +02008798 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008799}
8800
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008801static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8802 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008803{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008804 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008805 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008806 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03008807 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01008808 bool is_lvds = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008809 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008810
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008811 memset(&crtc_state->dpll_hw_state, 0,
8812 sizeof(crtc_state->dpll_hw_state));
8813
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03008814 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
Jesse Barnes79e53942008-11-07 14:24:08 -08008815
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008816 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8817 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
8818
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008819 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008820 &has_reduced_clock, &reduced_clock);
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008821 if (!ok && !crtc_state->clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008822 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8823 return -EINVAL;
8824 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01008825 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008826 if (!crtc_state->clock_set) {
8827 crtc_state->dpll.n = clock.n;
8828 crtc_state->dpll.m1 = clock.m1;
8829 crtc_state->dpll.m2 = clock.m2;
8830 crtc_state->dpll.p1 = clock.p1;
8831 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008832 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008833
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008834 /* 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 +02008835 if (crtc_state->has_pch_encoder) {
8836 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008837 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008838 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008839
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008840 dpll = ironlake_compute_dpll(crtc, crtc_state,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008841 &fp, &reduced_clock,
8842 has_reduced_clock ? &fp2 : NULL);
8843
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008844 crtc_state->dpll_hw_state.dpll = dpll;
8845 crtc_state->dpll_hw_state.fp0 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008846 if (has_reduced_clock)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008847 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008848 else
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008849 crtc_state->dpll_hw_state.fp1 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008850
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008851 pll = intel_get_shared_dpll(crtc, crtc_state);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008852 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03008853 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008854 pipe_name(crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07008855 return -EINVAL;
8856 }
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008857 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008858
Rodrigo Viviab585de2015-03-24 12:40:09 -07008859 if (is_lvds && has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008860 crtc->lowfreq_avail = true;
Daniel Vetterbcd644e2013-06-05 13:34:22 +02008861 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008862 crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008863
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008864 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008865}
8866
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008867static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8868 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008869{
8870 struct drm_device *dev = crtc->base.dev;
8871 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008872 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008873
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008874 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8875 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8876 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8877 & ~TU_SIZE_MASK;
8878 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8879 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8880 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8881}
8882
8883static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8884 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008885 struct intel_link_m_n *m_n,
8886 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008887{
8888 struct drm_device *dev = crtc->base.dev;
8889 struct drm_i915_private *dev_priv = dev->dev_private;
8890 enum pipe pipe = crtc->pipe;
8891
8892 if (INTEL_INFO(dev)->gen >= 5) {
8893 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8894 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8895 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8896 & ~TU_SIZE_MASK;
8897 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8898 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8899 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008900 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8901 * gen < 8) and if DRRS is supported (to make sure the
8902 * registers are not unnecessarily read).
8903 */
8904 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008905 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008906 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8907 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8908 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8909 & ~TU_SIZE_MASK;
8910 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8911 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8912 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8913 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008914 } else {
8915 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8916 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8917 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8918 & ~TU_SIZE_MASK;
8919 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8920 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8921 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8922 }
8923}
8924
8925void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008926 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008927{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02008928 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008929 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8930 else
8931 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008932 &pipe_config->dp_m_n,
8933 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008934}
8935
Daniel Vetter72419202013-04-04 13:28:53 +02008936static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008937 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02008938{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008939 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008940 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02008941}
8942
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008943static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008944 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008945{
8946 struct drm_device *dev = crtc->base.dev;
8947 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07008948 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8949 uint32_t ps_ctrl = 0;
8950 int id = -1;
8951 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008952
Chandra Kondurua1b22782015-04-07 15:28:45 -07008953 /* find scaler attached to this pipe */
8954 for (i = 0; i < crtc->num_scalers; i++) {
8955 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8956 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8957 id = i;
8958 pipe_config->pch_pfit.enabled = true;
8959 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8960 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8961 break;
8962 }
8963 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008964
Chandra Kondurua1b22782015-04-07 15:28:45 -07008965 scaler_state->scaler_id = id;
8966 if (id >= 0) {
8967 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8968 } else {
8969 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008970 }
8971}
8972
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008973static void
8974skylake_get_initial_plane_config(struct intel_crtc *crtc,
8975 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008976{
8977 struct drm_device *dev = crtc->base.dev;
8978 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00008979 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008980 int pipe = crtc->pipe;
8981 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008982 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008983 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008984 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008985
Damien Lespiaud9806c92015-01-21 14:07:19 +00008986 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008987 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008988 DRM_DEBUG_KMS("failed to alloc fb\n");
8989 return;
8990 }
8991
Damien Lespiau1b842c82015-01-21 13:50:54 +00008992 fb = &intel_fb->base;
8993
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008994 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00008995 if (!(val & PLANE_CTL_ENABLE))
8996 goto error;
8997
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008998 pixel_format = val & PLANE_CTL_FORMAT_MASK;
8999 fourcc = skl_format_to_fourcc(pixel_format,
9000 val & PLANE_CTL_ORDER_RGBX,
9001 val & PLANE_CTL_ALPHA_MASK);
9002 fb->pixel_format = fourcc;
9003 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9004
Damien Lespiau40f46282015-02-27 11:15:21 +00009005 tiling = val & PLANE_CTL_TILED_MASK;
9006 switch (tiling) {
9007 case PLANE_CTL_TILED_LINEAR:
9008 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9009 break;
9010 case PLANE_CTL_TILED_X:
9011 plane_config->tiling = I915_TILING_X;
9012 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9013 break;
9014 case PLANE_CTL_TILED_Y:
9015 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9016 break;
9017 case PLANE_CTL_TILED_YF:
9018 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9019 break;
9020 default:
9021 MISSING_CASE(tiling);
9022 goto error;
9023 }
9024
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009025 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9026 plane_config->base = base;
9027
9028 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9029
9030 val = I915_READ(PLANE_SIZE(pipe, 0));
9031 fb->height = ((val >> 16) & 0xfff) + 1;
9032 fb->width = ((val >> 0) & 0x1fff) + 1;
9033
9034 val = I915_READ(PLANE_STRIDE(pipe, 0));
Damien Lespiau40f46282015-02-27 11:15:21 +00009035 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
9036 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009037 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9038
9039 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009040 fb->pixel_format,
9041 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009042
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009043 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009044
9045 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9046 pipe_name(pipe), fb->width, fb->height,
9047 fb->bits_per_pixel, base, fb->pitches[0],
9048 plane_config->size);
9049
Damien Lespiau2d140302015-02-05 17:22:18 +00009050 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009051 return;
9052
9053error:
9054 kfree(fb);
9055}
9056
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009057static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009058 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009059{
9060 struct drm_device *dev = crtc->base.dev;
9061 struct drm_i915_private *dev_priv = dev->dev_private;
9062 uint32_t tmp;
9063
9064 tmp = I915_READ(PF_CTL(crtc->pipe));
9065
9066 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009067 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009068 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9069 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009070
9071 /* We currently do not free assignements of panel fitters on
9072 * ivb/hsw (since we don't use the higher upscaling modes which
9073 * differentiates them) so just WARN about this case for now. */
9074 if (IS_GEN7(dev)) {
9075 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9076 PF_PIPE_SEL_IVB(crtc->pipe));
9077 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009078 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009079}
9080
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009081static void
9082ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9083 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009084{
9085 struct drm_device *dev = crtc->base.dev;
9086 struct drm_i915_private *dev_priv = dev->dev_private;
9087 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009088 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009089 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009090 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009091 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009092 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009093
Damien Lespiau42a7b082015-02-05 19:35:13 +00009094 val = I915_READ(DSPCNTR(pipe));
9095 if (!(val & DISPLAY_PLANE_ENABLE))
9096 return;
9097
Damien Lespiaud9806c92015-01-21 14:07:19 +00009098 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009099 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009100 DRM_DEBUG_KMS("failed to alloc fb\n");
9101 return;
9102 }
9103
Damien Lespiau1b842c82015-01-21 13:50:54 +00009104 fb = &intel_fb->base;
9105
Daniel Vetter18c52472015-02-10 17:16:09 +00009106 if (INTEL_INFO(dev)->gen >= 4) {
9107 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009108 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009109 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9110 }
9111 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009112
9113 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009114 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009115 fb->pixel_format = fourcc;
9116 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009117
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009118 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009119 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009120 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009121 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009122 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009123 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009124 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009125 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009126 }
9127 plane_config->base = base;
9128
9129 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009130 fb->width = ((val >> 16) & 0xfff) + 1;
9131 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009132
9133 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009134 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009135
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009136 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009137 fb->pixel_format,
9138 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009139
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009140 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009141
Damien Lespiau2844a922015-01-20 12:51:48 +00009142 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9143 pipe_name(pipe), fb->width, fb->height,
9144 fb->bits_per_pixel, base, fb->pitches[0],
9145 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009146
Damien Lespiau2d140302015-02-05 17:22:18 +00009147 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009148}
9149
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009150static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009151 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009152{
9153 struct drm_device *dev = crtc->base.dev;
9154 struct drm_i915_private *dev_priv = dev->dev_private;
9155 uint32_t tmp;
9156
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009157 if (!intel_display_power_is_enabled(dev_priv,
9158 POWER_DOMAIN_PIPE(crtc->pipe)))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009159 return false;
9160
Daniel Vettere143a212013-07-04 12:01:15 +02009161 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009162 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02009163
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009164 tmp = I915_READ(PIPECONF(crtc->pipe));
9165 if (!(tmp & PIPECONF_ENABLE))
9166 return false;
9167
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009168 switch (tmp & PIPECONF_BPC_MASK) {
9169 case PIPECONF_6BPC:
9170 pipe_config->pipe_bpp = 18;
9171 break;
9172 case PIPECONF_8BPC:
9173 pipe_config->pipe_bpp = 24;
9174 break;
9175 case PIPECONF_10BPC:
9176 pipe_config->pipe_bpp = 30;
9177 break;
9178 case PIPECONF_12BPC:
9179 pipe_config->pipe_bpp = 36;
9180 break;
9181 default:
9182 break;
9183 }
9184
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009185 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9186 pipe_config->limited_color_range = true;
9187
Daniel Vetterab9412b2013-05-03 11:49:46 +02009188 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009189 struct intel_shared_dpll *pll;
9190
Daniel Vetter88adfff2013-03-28 10:42:01 +01009191 pipe_config->has_pch_encoder = true;
9192
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009193 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9194 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9195 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009196
9197 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009198
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009199 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02009200 pipe_config->shared_dpll =
9201 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009202 } else {
9203 tmp = I915_READ(PCH_DPLL_SEL);
9204 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
9205 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
9206 else
9207 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
9208 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009209
9210 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9211
9212 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9213 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009214
9215 tmp = pipe_config->dpll_hw_state.dpll;
9216 pipe_config->pixel_multiplier =
9217 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9218 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009219
9220 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009221 } else {
9222 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009223 }
9224
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009225 intel_get_pipe_timings(crtc, pipe_config);
9226
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009227 ironlake_get_pfit_config(crtc, pipe_config);
9228
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009229 return true;
9230}
9231
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009232static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9233{
9234 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009235 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009236
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009237 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009238 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009239 pipe_name(crtc->pipe));
9240
Rob Clarke2c719b2014-12-15 13:56:32 -05009241 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9242 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
9243 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9244 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
9245 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9246 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009247 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009248 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009249 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009250 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009251 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009252 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009253 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009254 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009255 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009256
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009257 /*
9258 * In theory we can still leave IRQs enabled, as long as only the HPD
9259 * interrupts remain enabled. We used to check for that, but since it's
9260 * gen-specific and since we only disable LCPLL after we fully disable
9261 * the interrupts, the check below should be enough.
9262 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009263 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009264}
9265
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009266static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9267{
9268 struct drm_device *dev = dev_priv->dev;
9269
9270 if (IS_HASWELL(dev))
9271 return I915_READ(D_COMP_HSW);
9272 else
9273 return I915_READ(D_COMP_BDW);
9274}
9275
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009276static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9277{
9278 struct drm_device *dev = dev_priv->dev;
9279
9280 if (IS_HASWELL(dev)) {
9281 mutex_lock(&dev_priv->rps.hw_lock);
9282 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9283 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009284 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009285 mutex_unlock(&dev_priv->rps.hw_lock);
9286 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009287 I915_WRITE(D_COMP_BDW, val);
9288 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009289 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009290}
9291
9292/*
9293 * This function implements pieces of two sequences from BSpec:
9294 * - Sequence for display software to disable LCPLL
9295 * - Sequence for display software to allow package C8+
9296 * The steps implemented here are just the steps that actually touch the LCPLL
9297 * register. Callers should take care of disabling all the display engine
9298 * functions, doing the mode unset, fixing interrupts, etc.
9299 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009300static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9301 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009302{
9303 uint32_t val;
9304
9305 assert_can_disable_lcpll(dev_priv);
9306
9307 val = I915_READ(LCPLL_CTL);
9308
9309 if (switch_to_fclk) {
9310 val |= LCPLL_CD_SOURCE_FCLK;
9311 I915_WRITE(LCPLL_CTL, val);
9312
9313 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9314 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9315 DRM_ERROR("Switching to FCLK failed\n");
9316
9317 val = I915_READ(LCPLL_CTL);
9318 }
9319
9320 val |= LCPLL_PLL_DISABLE;
9321 I915_WRITE(LCPLL_CTL, val);
9322 POSTING_READ(LCPLL_CTL);
9323
9324 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9325 DRM_ERROR("LCPLL still locked\n");
9326
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009327 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009328 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009329 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009330 ndelay(100);
9331
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009332 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9333 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009334 DRM_ERROR("D_COMP RCOMP still in progress\n");
9335
9336 if (allow_power_down) {
9337 val = I915_READ(LCPLL_CTL);
9338 val |= LCPLL_POWER_DOWN_ALLOW;
9339 I915_WRITE(LCPLL_CTL, val);
9340 POSTING_READ(LCPLL_CTL);
9341 }
9342}
9343
9344/*
9345 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9346 * source.
9347 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009348static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009349{
9350 uint32_t val;
9351
9352 val = I915_READ(LCPLL_CTL);
9353
9354 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9355 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9356 return;
9357
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009358 /*
9359 * Make sure we're not on PC8 state before disabling PC8, otherwise
9360 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009361 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009362 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009363
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009364 if (val & LCPLL_POWER_DOWN_ALLOW) {
9365 val &= ~LCPLL_POWER_DOWN_ALLOW;
9366 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009367 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009368 }
9369
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009370 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009371 val |= D_COMP_COMP_FORCE;
9372 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009373 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009374
9375 val = I915_READ(LCPLL_CTL);
9376 val &= ~LCPLL_PLL_DISABLE;
9377 I915_WRITE(LCPLL_CTL, val);
9378
9379 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9380 DRM_ERROR("LCPLL not locked yet\n");
9381
9382 if (val & LCPLL_CD_SOURCE_FCLK) {
9383 val = I915_READ(LCPLL_CTL);
9384 val &= ~LCPLL_CD_SOURCE_FCLK;
9385 I915_WRITE(LCPLL_CTL, val);
9386
9387 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9388 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9389 DRM_ERROR("Switching back to LCPLL failed\n");
9390 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009391
Mika Kuoppala59bad942015-01-16 11:34:40 +02009392 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjäläb6283052015-06-03 15:45:07 +03009393 intel_update_cdclk(dev_priv->dev);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009394}
9395
Paulo Zanoni765dab672014-03-07 20:08:18 -03009396/*
9397 * Package states C8 and deeper are really deep PC states that can only be
9398 * reached when all the devices on the system allow it, so even if the graphics
9399 * device allows PC8+, it doesn't mean the system will actually get to these
9400 * states. Our driver only allows PC8+ when going into runtime PM.
9401 *
9402 * The requirements for PC8+ are that all the outputs are disabled, the power
9403 * well is disabled and most interrupts are disabled, and these are also
9404 * requirements for runtime PM. When these conditions are met, we manually do
9405 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9406 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9407 * hang the machine.
9408 *
9409 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9410 * the state of some registers, so when we come back from PC8+ we need to
9411 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9412 * need to take care of the registers kept by RC6. Notice that this happens even
9413 * if we don't put the device in PCI D3 state (which is what currently happens
9414 * because of the runtime PM support).
9415 *
9416 * For more, read "Display Sequences for Package C8" on the hardware
9417 * documentation.
9418 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009419void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009420{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009421 struct drm_device *dev = dev_priv->dev;
9422 uint32_t val;
9423
Paulo Zanonic67a4702013-08-19 13:18:09 -03009424 DRM_DEBUG_KMS("Enabling package C8+\n");
9425
Paulo Zanonic67a4702013-08-19 13:18:09 -03009426 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9427 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9428 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9429 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9430 }
9431
9432 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009433 hsw_disable_lcpll(dev_priv, true, true);
9434}
9435
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009436void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009437{
9438 struct drm_device *dev = dev_priv->dev;
9439 uint32_t val;
9440
Paulo Zanonic67a4702013-08-19 13:18:09 -03009441 DRM_DEBUG_KMS("Disabling package C8+\n");
9442
9443 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009444 lpt_init_pch_refclk(dev);
9445
9446 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9447 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9448 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9449 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9450 }
9451
9452 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009453}
9454
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009455static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309456{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009457 struct drm_device *dev = old_state->dev;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009458 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309459
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009460 broxton_set_cdclk(dev, req_cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309461}
9462
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009463/* compute the max rate for new configuration */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009464static int ilk_max_pixel_rate(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009465{
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009466 struct intel_crtc *intel_crtc;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009467 struct intel_crtc_state *crtc_state;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009468 int max_pixel_rate = 0;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009469
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009470 for_each_intel_crtc(state->dev, intel_crtc) {
9471 int pixel_rate;
9472
9473 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
9474 if (IS_ERR(crtc_state))
9475 return PTR_ERR(crtc_state);
9476
9477 if (!crtc_state->base.enable)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009478 continue;
9479
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009480 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009481
9482 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009483 if (IS_BROADWELL(state->dev) && crtc_state->ips_enabled)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009484 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9485
9486 max_pixel_rate = max(max_pixel_rate, pixel_rate);
9487 }
9488
9489 return max_pixel_rate;
9490}
9491
9492static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9493{
9494 struct drm_i915_private *dev_priv = dev->dev_private;
9495 uint32_t val, data;
9496 int ret;
9497
9498 if (WARN((I915_READ(LCPLL_CTL) &
9499 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9500 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9501 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9502 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9503 "trying to change cdclk frequency with cdclk not enabled\n"))
9504 return;
9505
9506 mutex_lock(&dev_priv->rps.hw_lock);
9507 ret = sandybridge_pcode_write(dev_priv,
9508 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9509 mutex_unlock(&dev_priv->rps.hw_lock);
9510 if (ret) {
9511 DRM_ERROR("failed to inform pcode about cdclk change\n");
9512 return;
9513 }
9514
9515 val = I915_READ(LCPLL_CTL);
9516 val |= LCPLL_CD_SOURCE_FCLK;
9517 I915_WRITE(LCPLL_CTL, val);
9518
9519 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9520 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9521 DRM_ERROR("Switching to FCLK failed\n");
9522
9523 val = I915_READ(LCPLL_CTL);
9524 val &= ~LCPLL_CLK_FREQ_MASK;
9525
9526 switch (cdclk) {
9527 case 450000:
9528 val |= LCPLL_CLK_FREQ_450;
9529 data = 0;
9530 break;
9531 case 540000:
9532 val |= LCPLL_CLK_FREQ_54O_BDW;
9533 data = 1;
9534 break;
9535 case 337500:
9536 val |= LCPLL_CLK_FREQ_337_5_BDW;
9537 data = 2;
9538 break;
9539 case 675000:
9540 val |= LCPLL_CLK_FREQ_675_BDW;
9541 data = 3;
9542 break;
9543 default:
9544 WARN(1, "invalid cdclk frequency\n");
9545 return;
9546 }
9547
9548 I915_WRITE(LCPLL_CTL, val);
9549
9550 val = I915_READ(LCPLL_CTL);
9551 val &= ~LCPLL_CD_SOURCE_FCLK;
9552 I915_WRITE(LCPLL_CTL, val);
9553
9554 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9555 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9556 DRM_ERROR("Switching back to LCPLL failed\n");
9557
9558 mutex_lock(&dev_priv->rps.hw_lock);
9559 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9560 mutex_unlock(&dev_priv->rps.hw_lock);
9561
9562 intel_update_cdclk(dev);
9563
9564 WARN(cdclk != dev_priv->cdclk_freq,
9565 "cdclk requested %d kHz but got %d kHz\n",
9566 cdclk, dev_priv->cdclk_freq);
9567}
9568
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009569static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009570{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009571 struct drm_i915_private *dev_priv = to_i915(state->dev);
9572 int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009573 int cdclk;
9574
9575 /*
9576 * FIXME should also account for plane ratio
9577 * once 64bpp pixel formats are supported.
9578 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009579 if (max_pixclk > 540000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009580 cdclk = 675000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009581 else if (max_pixclk > 450000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009582 cdclk = 540000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009583 else if (max_pixclk > 337500)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009584 cdclk = 450000;
9585 else
9586 cdclk = 337500;
9587
9588 /*
9589 * FIXME move the cdclk caclulation to
9590 * compute_config() so we can fail gracegully.
9591 */
9592 if (cdclk > dev_priv->max_cdclk_freq) {
9593 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9594 cdclk, dev_priv->max_cdclk_freq);
9595 cdclk = dev_priv->max_cdclk_freq;
9596 }
9597
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009598 to_intel_atomic_state(state)->cdclk = cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009599
9600 return 0;
9601}
9602
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009603static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009604{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009605 struct drm_device *dev = old_state->dev;
9606 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009607
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009608 broadwell_set_cdclk(dev, req_cdclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009609}
9610
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009611static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9612 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009613{
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009614 if (!intel_ddi_pll_select(crtc, crtc_state))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009615 return -EINVAL;
Daniel Vetter716c2e52014-06-25 22:02:02 +03009616
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009617 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009618
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009619 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009620}
9621
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309622static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9623 enum port port,
9624 struct intel_crtc_state *pipe_config)
9625{
9626 switch (port) {
9627 case PORT_A:
9628 pipe_config->ddi_pll_sel = SKL_DPLL0;
9629 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9630 break;
9631 case PORT_B:
9632 pipe_config->ddi_pll_sel = SKL_DPLL1;
9633 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9634 break;
9635 case PORT_C:
9636 pipe_config->ddi_pll_sel = SKL_DPLL2;
9637 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9638 break;
9639 default:
9640 DRM_ERROR("Incorrect port type\n");
9641 }
9642}
9643
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009644static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9645 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009646 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009647{
Damien Lespiau3148ade2014-11-21 16:14:56 +00009648 u32 temp, dpll_ctl1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009649
9650 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9651 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9652
9653 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009654 case SKL_DPLL0:
9655 /*
9656 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9657 * of the shared DPLL framework and thus needs to be read out
9658 * separately
9659 */
9660 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9661 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9662 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009663 case SKL_DPLL1:
9664 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9665 break;
9666 case SKL_DPLL2:
9667 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9668 break;
9669 case SKL_DPLL3:
9670 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9671 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009672 }
9673}
9674
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009675static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9676 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009677 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009678{
9679 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9680
9681 switch (pipe_config->ddi_pll_sel) {
9682 case PORT_CLK_SEL_WRPLL1:
9683 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9684 break;
9685 case PORT_CLK_SEL_WRPLL2:
9686 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9687 break;
9688 }
9689}
9690
Daniel Vetter26804af2014-06-25 22:01:55 +03009691static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009692 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009693{
9694 struct drm_device *dev = crtc->base.dev;
9695 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009696 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009697 enum port port;
9698 uint32_t tmp;
9699
9700 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9701
9702 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9703
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009704 if (IS_SKYLAKE(dev))
9705 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309706 else if (IS_BROXTON(dev))
9707 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009708 else
9709 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009710
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009711 if (pipe_config->shared_dpll >= 0) {
9712 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9713
9714 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9715 &pipe_config->dpll_hw_state));
9716 }
9717
Daniel Vetter26804af2014-06-25 22:01:55 +03009718 /*
9719 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9720 * DDI E. So just check whether this pipe is wired to DDI E and whether
9721 * the PCH transcoder is on.
9722 */
Damien Lespiauca370452013-12-03 13:56:24 +00009723 if (INTEL_INFO(dev)->gen < 9 &&
9724 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009725 pipe_config->has_pch_encoder = true;
9726
9727 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9728 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9729 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9730
9731 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9732 }
9733}
9734
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009735static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009736 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009737{
9738 struct drm_device *dev = crtc->base.dev;
9739 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009740 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009741 uint32_t tmp;
9742
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009743 if (!intel_display_power_is_enabled(dev_priv,
Imre Deakb5482bd2014-03-05 16:20:55 +02009744 POWER_DOMAIN_PIPE(crtc->pipe)))
9745 return false;
9746
Daniel Vettere143a212013-07-04 12:01:15 +02009747 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009748 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9749
Daniel Vettereccb1402013-05-22 00:50:22 +02009750 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9751 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9752 enum pipe trans_edp_pipe;
9753 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9754 default:
9755 WARN(1, "unknown pipe linked to edp transcoder\n");
9756 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9757 case TRANS_DDI_EDP_INPUT_A_ON:
9758 trans_edp_pipe = PIPE_A;
9759 break;
9760 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9761 trans_edp_pipe = PIPE_B;
9762 break;
9763 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9764 trans_edp_pipe = PIPE_C;
9765 break;
9766 }
9767
9768 if (trans_edp_pipe == crtc->pipe)
9769 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9770 }
9771
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009772 if (!intel_display_power_is_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02009773 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03009774 return false;
9775
Daniel Vettereccb1402013-05-22 00:50:22 +02009776 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009777 if (!(tmp & PIPECONF_ENABLE))
9778 return false;
9779
Daniel Vetter26804af2014-06-25 22:01:55 +03009780 haswell_get_ddi_port_state(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009781
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009782 intel_get_pipe_timings(crtc, pipe_config);
9783
Chandra Kondurua1b22782015-04-07 15:28:45 -07009784 if (INTEL_INFO(dev)->gen >= 9) {
9785 skl_init_scalers(dev, crtc, pipe_config);
9786 }
9787
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009788 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Chandra Konduruaf99ceda2015-05-11 14:35:47 -07009789
9790 if (INTEL_INFO(dev)->gen >= 9) {
9791 pipe_config->scaler_state.scaler_id = -1;
9792 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9793 }
9794
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009795 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009796 if (INTEL_INFO(dev)->gen == 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009797 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009798 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009799 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009800 else
9801 MISSING_CASE(INTEL_INFO(dev)->gen);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009802 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01009803
Jesse Barnese59150d2014-01-07 13:30:45 -08009804 if (IS_HASWELL(dev))
9805 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9806 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009807
Clint Taylorebb69c92014-09-30 10:30:22 -07009808 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
9809 pipe_config->pixel_multiplier =
9810 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9811 } else {
9812 pipe_config->pixel_multiplier = 1;
9813 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009814
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009815 return true;
9816}
9817
Chris Wilson560b85b2010-08-07 11:01:38 +01009818static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
9819{
9820 struct drm_device *dev = crtc->dev;
9821 struct drm_i915_private *dev_priv = dev->dev_private;
9822 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +03009823 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01009824
Ville Syrjälädc41c152014-08-13 11:57:05 +03009825 if (base) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009826 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
9827 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009828 unsigned int stride = roundup_pow_of_two(width) * 4;
9829
9830 switch (stride) {
9831 default:
9832 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9833 width, stride);
9834 stride = 256;
9835 /* fallthrough */
9836 case 256:
9837 case 512:
9838 case 1024:
9839 case 2048:
9840 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009841 }
9842
Ville Syrjälädc41c152014-08-13 11:57:05 +03009843 cntl |= CURSOR_ENABLE |
9844 CURSOR_GAMMA_ENABLE |
9845 CURSOR_FORMAT_ARGB |
9846 CURSOR_STRIDE(stride);
9847
9848 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009849 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009850
Ville Syrjälädc41c152014-08-13 11:57:05 +03009851 if (intel_crtc->cursor_cntl != 0 &&
9852 (intel_crtc->cursor_base != base ||
9853 intel_crtc->cursor_size != size ||
9854 intel_crtc->cursor_cntl != cntl)) {
9855 /* On these chipsets we can only modify the base/size/stride
9856 * whilst the cursor is disabled.
9857 */
9858 I915_WRITE(_CURACNTR, 0);
9859 POSTING_READ(_CURACNTR);
9860 intel_crtc->cursor_cntl = 0;
9861 }
9862
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009863 if (intel_crtc->cursor_base != base) {
Ville Syrjälädc41c152014-08-13 11:57:05 +03009864 I915_WRITE(_CURABASE, base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009865 intel_crtc->cursor_base = base;
9866 }
Ville Syrjälädc41c152014-08-13 11:57:05 +03009867
9868 if (intel_crtc->cursor_size != size) {
9869 I915_WRITE(CURSIZE, size);
9870 intel_crtc->cursor_size = size;
9871 }
9872
Chris Wilson4b0e3332014-05-30 16:35:26 +03009873 if (intel_crtc->cursor_cntl != cntl) {
9874 I915_WRITE(_CURACNTR, cntl);
9875 POSTING_READ(_CURACNTR);
9876 intel_crtc->cursor_cntl = cntl;
9877 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009878}
9879
9880static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
9881{
9882 struct drm_device *dev = crtc->dev;
9883 struct drm_i915_private *dev_priv = dev->dev_private;
9884 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9885 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009886 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +01009887
Chris Wilson4b0e3332014-05-30 16:35:26 +03009888 cntl = 0;
9889 if (base) {
9890 cntl = MCURSOR_GAMMA_ENABLE;
Matt Roper3dd512f2015-02-27 10:12:00 -08009891 switch (intel_crtc->base.cursor->state->crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309892 case 64:
9893 cntl |= CURSOR_MODE_64_ARGB_AX;
9894 break;
9895 case 128:
9896 cntl |= CURSOR_MODE_128_ARGB_AX;
9897 break;
9898 case 256:
9899 cntl |= CURSOR_MODE_256_ARGB_AX;
9900 break;
9901 default:
Matt Roper3dd512f2015-02-27 10:12:00 -08009902 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309903 return;
Chris Wilson560b85b2010-08-07 11:01:38 +01009904 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009905 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +03009906
9907 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
9908 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson560b85b2010-08-07 11:01:38 +01009909 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009910
Matt Roper8e7d6882015-01-21 16:35:41 -08009911 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009912 cntl |= CURSOR_ROTATE_180;
9913
Chris Wilson4b0e3332014-05-30 16:35:26 +03009914 if (intel_crtc->cursor_cntl != cntl) {
9915 I915_WRITE(CURCNTR(pipe), cntl);
9916 POSTING_READ(CURCNTR(pipe));
9917 intel_crtc->cursor_cntl = cntl;
9918 }
9919
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009920 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009921 I915_WRITE(CURBASE(pipe), base);
9922 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009923
9924 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009925}
9926
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009927/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01009928static void intel_crtc_update_cursor(struct drm_crtc *crtc,
9929 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009930{
9931 struct drm_device *dev = crtc->dev;
9932 struct drm_i915_private *dev_priv = dev->dev_private;
9933 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9934 int pipe = intel_crtc->pipe;
Matt Roper3d7d6512014-06-10 08:28:13 -07009935 int x = crtc->cursor_x;
9936 int y = crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009937 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009938
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009939 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009940 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009941
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009942 if (x >= intel_crtc->config->pipe_src_w)
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009943 base = 0;
9944
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009945 if (y >= intel_crtc->config->pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009946 base = 0;
9947
9948 if (x < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009949 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009950 base = 0;
9951
9952 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
9953 x = -x;
9954 }
9955 pos |= x << CURSOR_X_SHIFT;
9956
9957 if (y < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009958 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009959 base = 0;
9960
9961 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
9962 y = -y;
9963 }
9964 pos |= y << CURSOR_Y_SHIFT;
9965
Chris Wilson4b0e3332014-05-30 16:35:26 +03009966 if (base == 0 && intel_crtc->cursor_base == 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009967 return;
9968
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009969 I915_WRITE(CURPOS(pipe), pos);
9970
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009971 /* ILK+ do this automagically */
9972 if (HAS_GMCH_DISPLAY(dev) &&
Matt Roper8e7d6882015-01-21 16:35:41 -08009973 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009974 base += (intel_crtc->base.cursor->state->crtc_h *
9975 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009976 }
9977
Ville Syrjälä8ac54662014-08-12 19:39:54 +03009978 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009979 i845_update_cursor(crtc, base);
9980 else
9981 i9xx_update_cursor(crtc, base);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009982}
9983
Ville Syrjälädc41c152014-08-13 11:57:05 +03009984static bool cursor_size_ok(struct drm_device *dev,
9985 uint32_t width, uint32_t height)
9986{
9987 if (width == 0 || height == 0)
9988 return false;
9989
9990 /*
9991 * 845g/865g are special in that they are only limited by
9992 * the width of their cursors, the height is arbitrary up to
9993 * the precision of the register. Everything else requires
9994 * square cursors, limited to a few power-of-two sizes.
9995 */
9996 if (IS_845G(dev) || IS_I865G(dev)) {
9997 if ((width & 63) != 0)
9998 return false;
9999
10000 if (width > (IS_845G(dev) ? 64 : 512))
10001 return false;
10002
10003 if (height > 1023)
10004 return false;
10005 } else {
10006 switch (width | height) {
10007 case 256:
10008 case 128:
10009 if (IS_GEN2(dev))
10010 return false;
10011 case 64:
10012 break;
10013 default:
10014 return false;
10015 }
10016 }
10017
10018 return true;
10019}
10020
Jesse Barnes79e53942008-11-07 14:24:08 -080010021static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +010010022 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -080010023{
James Simmons72034252010-08-03 01:33:19 +010010024 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -080010025 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080010026
James Simmons72034252010-08-03 01:33:19 +010010027 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010028 intel_crtc->lut_r[i] = red[i] >> 8;
10029 intel_crtc->lut_g[i] = green[i] >> 8;
10030 intel_crtc->lut_b[i] = blue[i] >> 8;
10031 }
10032
10033 intel_crtc_load_lut(crtc);
10034}
10035
Jesse Barnes79e53942008-11-07 14:24:08 -080010036/* VESA 640x480x72Hz mode to set on the pipe */
10037static struct drm_display_mode load_detect_mode = {
10038 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10039 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10040};
10041
Daniel Vettera8bb6812014-02-10 18:00:39 +010010042struct drm_framebuffer *
10043__intel_framebuffer_create(struct drm_device *dev,
10044 struct drm_mode_fb_cmd2 *mode_cmd,
10045 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010046{
10047 struct intel_framebuffer *intel_fb;
10048 int ret;
10049
10050 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
10051 if (!intel_fb) {
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +030010052 drm_gem_object_unreference(&obj->base);
Chris Wilsond2dff872011-04-19 08:36:26 +010010053 return ERR_PTR(-ENOMEM);
10054 }
10055
10056 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010057 if (ret)
10058 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010059
10060 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010061err:
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +030010062 drm_gem_object_unreference(&obj->base);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010063 kfree(intel_fb);
10064
10065 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010066}
10067
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010068static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010069intel_framebuffer_create(struct drm_device *dev,
10070 struct drm_mode_fb_cmd2 *mode_cmd,
10071 struct drm_i915_gem_object *obj)
10072{
10073 struct drm_framebuffer *fb;
10074 int ret;
10075
10076 ret = i915_mutex_lock_interruptible(dev);
10077 if (ret)
10078 return ERR_PTR(ret);
10079 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10080 mutex_unlock(&dev->struct_mutex);
10081
10082 return fb;
10083}
10084
Chris Wilsond2dff872011-04-19 08:36:26 +010010085static u32
10086intel_framebuffer_pitch_for_width(int width, int bpp)
10087{
10088 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10089 return ALIGN(pitch, 64);
10090}
10091
10092static u32
10093intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10094{
10095 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010096 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010097}
10098
10099static struct drm_framebuffer *
10100intel_framebuffer_create_for_mode(struct drm_device *dev,
10101 struct drm_display_mode *mode,
10102 int depth, int bpp)
10103{
10104 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010105 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010106
10107 obj = i915_gem_alloc_object(dev,
10108 intel_framebuffer_size_for_mode(mode, bpp));
10109 if (obj == NULL)
10110 return ERR_PTR(-ENOMEM);
10111
10112 mode_cmd.width = mode->hdisplay;
10113 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010114 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10115 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010116 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010117
10118 return intel_framebuffer_create(dev, &mode_cmd, obj);
10119}
10120
10121static struct drm_framebuffer *
10122mode_fits_in_fbdev(struct drm_device *dev,
10123 struct drm_display_mode *mode)
10124{
Daniel Vetter4520f532013-10-09 09:18:51 +020010125#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +010010126 struct drm_i915_private *dev_priv = dev->dev_private;
10127 struct drm_i915_gem_object *obj;
10128 struct drm_framebuffer *fb;
10129
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010130 if (!dev_priv->fbdev)
10131 return NULL;
10132
10133 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010134 return NULL;
10135
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010136 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010137 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010138
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010139 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010140 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10141 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010142 return NULL;
10143
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010144 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010145 return NULL;
10146
10147 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010148#else
10149 return NULL;
10150#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010151}
10152
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010153static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10154 struct drm_crtc *crtc,
10155 struct drm_display_mode *mode,
10156 struct drm_framebuffer *fb,
10157 int x, int y)
10158{
10159 struct drm_plane_state *plane_state;
10160 int hdisplay, vdisplay;
10161 int ret;
10162
10163 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10164 if (IS_ERR(plane_state))
10165 return PTR_ERR(plane_state);
10166
10167 if (mode)
10168 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10169 else
10170 hdisplay = vdisplay = 0;
10171
10172 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10173 if (ret)
10174 return ret;
10175 drm_atomic_set_fb_for_plane(plane_state, fb);
10176 plane_state->crtc_x = 0;
10177 plane_state->crtc_y = 0;
10178 plane_state->crtc_w = hdisplay;
10179 plane_state->crtc_h = vdisplay;
10180 plane_state->src_x = x << 16;
10181 plane_state->src_y = y << 16;
10182 plane_state->src_w = hdisplay << 16;
10183 plane_state->src_h = vdisplay << 16;
10184
10185 return 0;
10186}
10187
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010188bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010189 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010190 struct intel_load_detect_pipe *old,
10191 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010192{
10193 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010194 struct intel_encoder *intel_encoder =
10195 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010196 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010197 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010198 struct drm_crtc *crtc = NULL;
10199 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010200 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010201 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010202 struct drm_atomic_state *state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010203 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010204 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010205 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010206
Chris Wilsond2dff872011-04-19 08:36:26 +010010207 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010208 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010209 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010210
Rob Clark51fd3712013-11-19 12:10:12 -050010211retry:
10212 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10213 if (ret)
10214 goto fail_unlock;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010215
Jesse Barnes79e53942008-11-07 14:24:08 -080010216 /*
10217 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010218 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010219 * - if the connector already has an assigned crtc, use it (but make
10220 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010221 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010222 * - try to find the first unused crtc that can drive this connector,
10223 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010224 */
10225
10226 /* See if we already have a CRTC for this connector */
10227 if (encoder->crtc) {
10228 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010229
Rob Clark51fd3712013-11-19 12:10:12 -050010230 ret = drm_modeset_lock(&crtc->mutex, ctx);
10231 if (ret)
10232 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010233 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10234 if (ret)
10235 goto fail_unlock;
Daniel Vetter7b240562012-12-12 00:35:33 +010010236
Daniel Vetter24218aa2012-08-12 19:27:11 +020010237 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +010010238 old->load_detect_temp = false;
10239
10240 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +020010241 if (connector->dpms != DRM_MODE_DPMS_ON)
10242 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +010010243
Chris Wilson71731882011-04-19 23:10:58 +010010244 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -080010245 }
10246
10247 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010248 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010249 i++;
10250 if (!(encoder->possible_crtcs & (1 << i)))
10251 continue;
Matt Roper83d65732015-02-25 13:12:16 -080010252 if (possible_crtc->state->enable)
Ville Syrjäläa4592492014-08-11 13:15:36 +030010253 continue;
10254 /* This can occur when applying the pipe A quirk on resume. */
10255 if (to_intel_crtc(possible_crtc)->new_enabled)
10256 continue;
10257
10258 crtc = possible_crtc;
10259 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010260 }
10261
10262 /*
10263 * If we didn't find an unused CRTC, don't use any.
10264 */
10265 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010266 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Rob Clark51fd3712013-11-19 12:10:12 -050010267 goto fail_unlock;
Jesse Barnes79e53942008-11-07 14:24:08 -080010268 }
10269
Rob Clark51fd3712013-11-19 12:10:12 -050010270 ret = drm_modeset_lock(&crtc->mutex, ctx);
10271 if (ret)
10272 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010273 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10274 if (ret)
10275 goto fail_unlock;
Daniel Vetterfc303102012-07-09 10:40:58 +020010276 intel_encoder->new_crtc = to_intel_crtc(crtc);
10277 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010278
10279 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010280 intel_crtc->new_enabled = true;
Daniel Vetter24218aa2012-08-12 19:27:11 +020010281 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +010010282 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +010010283 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -080010284
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010285 state = drm_atomic_state_alloc(dev);
10286 if (!state)
10287 return false;
10288
10289 state->acquire_ctx = ctx;
10290
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010291 connector_state = drm_atomic_get_connector_state(state, connector);
10292 if (IS_ERR(connector_state)) {
10293 ret = PTR_ERR(connector_state);
10294 goto fail;
10295 }
10296
10297 connector_state->crtc = crtc;
10298 connector_state->best_encoder = &intel_encoder->base;
10299
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010300 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10301 if (IS_ERR(crtc_state)) {
10302 ret = PTR_ERR(crtc_state);
10303 goto fail;
10304 }
10305
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010306 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010307
Chris Wilson64927112011-04-20 07:25:26 +010010308 if (!mode)
10309 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010310
Chris Wilsond2dff872011-04-19 08:36:26 +010010311 /* We need a framebuffer large enough to accommodate all accesses
10312 * that the plane may generate whilst we perform load detection.
10313 * We can not rely on the fbcon either being present (we get called
10314 * during its initialisation to detect all boot displays, or it may
10315 * not even exist) or that it is large enough to satisfy the
10316 * requested mode.
10317 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010318 fb = mode_fits_in_fbdev(dev, mode);
10319 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010320 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010321 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
10322 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010323 } else
10324 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010325 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010326 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010327 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010328 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010329
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010330 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10331 if (ret)
10332 goto fail;
10333
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010334 drm_mode_copy(&crtc_state->base.mode, mode);
10335
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020010336 if (intel_set_mode(state)) {
Chris Wilson64927112011-04-20 07:25:26 +010010337 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +010010338 if (old->release_fb)
10339 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010340 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010341 }
Daniel Vetter9128b042015-03-03 17:31:21 +010010342 crtc->primary->crtc = crtc;
Chris Wilson71731882011-04-19 23:10:58 +010010343
Jesse Barnes79e53942008-11-07 14:24:08 -080010344 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010345 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010346 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010347
10348 fail:
Matt Roper83d65732015-02-25 13:12:16 -080010349 intel_crtc->new_enabled = crtc->state->enable;
Rob Clark51fd3712013-11-19 12:10:12 -050010350fail_unlock:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010351 drm_atomic_state_free(state);
10352 state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010353
Rob Clark51fd3712013-11-19 12:10:12 -050010354 if (ret == -EDEADLK) {
10355 drm_modeset_backoff(ctx);
10356 goto retry;
10357 }
10358
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010359 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010360}
10361
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010362void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010363 struct intel_load_detect_pipe *old,
10364 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010365{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010366 struct drm_device *dev = connector->dev;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010367 struct intel_encoder *intel_encoder =
10368 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010369 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +010010370 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010371 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010372 struct drm_atomic_state *state;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010373 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010374 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010375 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010376
Chris Wilsond2dff872011-04-19 08:36:26 +010010377 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010378 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010379 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010380
Chris Wilson8261b192011-04-19 23:18:09 +010010381 if (old->load_detect_temp) {
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010382 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010383 if (!state)
10384 goto fail;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010385
10386 state->acquire_ctx = ctx;
10387
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010388 connector_state = drm_atomic_get_connector_state(state, connector);
10389 if (IS_ERR(connector_state))
10390 goto fail;
10391
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010392 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10393 if (IS_ERR(crtc_state))
10394 goto fail;
10395
Daniel Vetterfc303102012-07-09 10:40:58 +020010396 to_intel_connector(connector)->new_encoder = NULL;
10397 intel_encoder->new_crtc = NULL;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010398 intel_crtc->new_enabled = false;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010399
10400 connector_state->best_encoder = NULL;
10401 connector_state->crtc = NULL;
10402
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010403 crtc_state->base.enable = crtc_state->base.active = false;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010404
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010405 ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
10406 0, 0);
10407 if (ret)
10408 goto fail;
10409
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020010410 ret = intel_set_mode(state);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030010411 if (ret)
10412 goto fail;
Chris Wilsond2dff872011-04-19 08:36:26 +010010413
Daniel Vetter36206362012-12-10 20:42:17 +010010414 if (old->release_fb) {
10415 drm_framebuffer_unregister_private(old->release_fb);
10416 drm_framebuffer_unreference(old->release_fb);
10417 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010418
Chris Wilson0622a532011-04-21 09:32:11 +010010419 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010420 }
10421
Eric Anholtc751ce42010-03-25 11:48:48 -070010422 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +020010423 if (old->dpms_mode != DRM_MODE_DPMS_ON)
10424 connector->funcs->dpms(connector, old->dpms_mode);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010425
10426 return;
10427fail:
10428 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
10429 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010430}
10431
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010432static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010433 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010434{
10435 struct drm_i915_private *dev_priv = dev->dev_private;
10436 u32 dpll = pipe_config->dpll_hw_state.dpll;
10437
10438 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010439 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010440 else if (HAS_PCH_SPLIT(dev))
10441 return 120000;
10442 else if (!IS_GEN2(dev))
10443 return 96000;
10444 else
10445 return 48000;
10446}
10447
Jesse Barnes79e53942008-11-07 14:24:08 -080010448/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010449static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010450 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010451{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010452 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010453 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010454 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010455 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010456 u32 fp;
10457 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010458 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010459
10460 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010461 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010462 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010463 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010464
10465 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010466 if (IS_PINEVIEW(dev)) {
10467 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10468 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010469 } else {
10470 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10471 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10472 }
10473
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010474 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010475 if (IS_PINEVIEW(dev))
10476 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10477 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010478 else
10479 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010480 DPLL_FPA01_P1_POST_DIV_SHIFT);
10481
10482 switch (dpll & DPLL_MODE_MASK) {
10483 case DPLLB_MODE_DAC_SERIAL:
10484 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10485 5 : 10;
10486 break;
10487 case DPLLB_MODE_LVDS:
10488 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10489 7 : 14;
10490 break;
10491 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010492 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010493 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010494 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010495 }
10496
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010497 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010498 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010499 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010500 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010501 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010502 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010503 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010504
10505 if (is_lvds) {
10506 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10507 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010508
10509 if (lvds & LVDS_CLKB_POWER_UP)
10510 clock.p2 = 7;
10511 else
10512 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010513 } else {
10514 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10515 clock.p1 = 2;
10516 else {
10517 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10518 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10519 }
10520 if (dpll & PLL_P2_DIVIDE_BY_4)
10521 clock.p2 = 4;
10522 else
10523 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010524 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010525
10526 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010527 }
10528
Ville Syrjälä18442d02013-09-13 16:00:08 +030010529 /*
10530 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010531 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010532 * encoder's get_config() function.
10533 */
10534 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010535}
10536
Ville Syrjälä6878da02013-09-13 15:59:11 +030010537int intel_dotclock_calculate(int link_freq,
10538 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010539{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010540 /*
10541 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010542 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010543 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010544 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010545 *
10546 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010547 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010548 */
10549
Ville Syrjälä6878da02013-09-13 15:59:11 +030010550 if (!m_n->link_n)
10551 return 0;
10552
10553 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10554}
10555
Ville Syrjälä18442d02013-09-13 16:00:08 +030010556static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010557 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010558{
10559 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +030010560
10561 /* read out port_clock from the DPLL */
10562 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010563
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010564 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +030010565 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +010010566 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +030010567 * agree once we know their relationship in the encoder's
10568 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010569 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010570 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +030010571 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10572 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010573}
10574
10575/** Returns the currently programmed mode of the given pipe. */
10576struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10577 struct drm_crtc *crtc)
10578{
Jesse Barnes548f2452011-02-17 10:40:53 -080010579 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010580 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010581 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010582 struct drm_display_mode *mode;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010583 struct intel_crtc_state pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010584 int htot = I915_READ(HTOTAL(cpu_transcoder));
10585 int hsync = I915_READ(HSYNC(cpu_transcoder));
10586 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10587 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010588 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010589
10590 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10591 if (!mode)
10592 return NULL;
10593
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010594 /*
10595 * Construct a pipe_config sufficient for getting the clock info
10596 * back out of crtc_clock_get.
10597 *
10598 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10599 * to use a real value here instead.
10600 */
Ville Syrjälä293623f2013-09-13 16:18:46 +030010601 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010602 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010603 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10604 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10605 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010606 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10607
Ville Syrjälä773ae032013-09-23 17:48:20 +030010608 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010609 mode->hdisplay = (htot & 0xffff) + 1;
10610 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10611 mode->hsync_start = (hsync & 0xffff) + 1;
10612 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10613 mode->vdisplay = (vtot & 0xffff) + 1;
10614 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10615 mode->vsync_start = (vsync & 0xffff) + 1;
10616 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10617
10618 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010619
10620 return mode;
10621}
10622
Jesse Barnes652c3932009-08-17 13:31:43 -070010623static void intel_decrease_pllclock(struct drm_crtc *crtc)
10624{
10625 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030010626 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -070010627 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -070010628
Sonika Jindalbaff2962014-07-22 11:16:35 +053010629 if (!HAS_GMCH_DISPLAY(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -070010630 return;
10631
10632 if (!dev_priv->lvds_downclock_avail)
10633 return;
10634
10635 /*
10636 * Since this is called by a timer, we should never get here in
10637 * the manual case.
10638 */
10639 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +010010640 int pipe = intel_crtc->pipe;
10641 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +020010642 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +010010643
Zhao Yakui44d98a62009-10-09 11:39:40 +080010644 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -070010645
Sean Paul8ac5a6d2012-02-13 13:14:51 -050010646 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -070010647
Chris Wilson074b5e12012-05-02 12:07:06 +010010648 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -070010649 dpll |= DISPLAY_RATE_SELECT_FPA1;
10650 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010651 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -070010652 dpll = I915_READ(dpll_reg);
10653 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +080010654 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -070010655 }
10656
10657}
10658
Chris Wilsonf047e392012-07-21 12:31:41 +010010659void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010660{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010661 struct drm_i915_private *dev_priv = dev->dev_private;
10662
Chris Wilsonf62a0072014-02-21 17:55:39 +000010663 if (dev_priv->mm.busy)
10664 return;
10665
Paulo Zanoni43694d62014-03-07 20:08:08 -030010666 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010667 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010668 if (INTEL_INFO(dev)->gen >= 6)
10669 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010670 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010671}
10672
10673void intel_mark_idle(struct drm_device *dev)
10674{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010675 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010676 struct drm_crtc *crtc;
10677
Chris Wilsonf62a0072014-02-21 17:55:39 +000010678 if (!dev_priv->mm.busy)
10679 return;
10680
10681 dev_priv->mm.busy = false;
10682
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010683 for_each_crtc(dev, crtc) {
Matt Roperf4510a22014-04-01 15:22:40 -070010684 if (!crtc->primary->fb)
Chris Wilson725a5b52013-01-08 11:02:57 +000010685 continue;
10686
10687 intel_decrease_pllclock(crtc);
10688 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010689
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010690 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010691 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010692
Paulo Zanoni43694d62014-03-07 20:08:08 -030010693 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010694}
10695
Jesse Barnes79e53942008-11-07 14:24:08 -080010696static void intel_crtc_destroy(struct drm_crtc *crtc)
10697{
10698 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010699 struct drm_device *dev = crtc->dev;
10700 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010701
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010702 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010703 work = intel_crtc->unpin_work;
10704 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010705 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010706
10707 if (work) {
10708 cancel_work_sync(&work->work);
10709 kfree(work);
10710 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010711
10712 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010713
Jesse Barnes79e53942008-11-07 14:24:08 -080010714 kfree(intel_crtc);
10715}
10716
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010717static void intel_unpin_work_fn(struct work_struct *__work)
10718{
10719 struct intel_unpin_work *work =
10720 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010721 struct drm_device *dev = work->crtc->dev;
Daniel Vetterf99d7062014-06-19 16:01:59 +020010722 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010723
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010724 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000010725 intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
Chris Wilson05394f32010-11-08 19:18:58 +000010726 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010727
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020010728 intel_fbc_update(dev);
John Harrisonf06cc1b2014-11-24 18:49:37 +000010729
10730 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010731 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010732 mutex_unlock(&dev->struct_mutex);
10733
Daniel Vetterf99d7062014-06-19 16:01:59 +020010734 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Chris Wilson89ed88b2015-02-16 14:31:49 +000010735 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010736
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010737 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
10738 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
10739
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010740 kfree(work);
10741}
10742
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010743static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010010744 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010745{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010746 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10747 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010748 unsigned long flags;
10749
10750 /* Ignore early vblank irqs */
10751 if (intel_crtc == NULL)
10752 return;
10753
Daniel Vetterf3260382014-09-15 14:55:23 +020010754 /*
10755 * This is called both by irq handlers and the reset code (to complete
10756 * lost pageflips) so needs the full irqsave spinlocks.
10757 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010758 spin_lock_irqsave(&dev->event_lock, flags);
10759 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000010760
10761 /* Ensure we don't miss a work->pending update ... */
10762 smp_rmb();
10763
10764 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010765 spin_unlock_irqrestore(&dev->event_lock, flags);
10766 return;
10767 }
10768
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010769 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010010770
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010771 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010772}
10773
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010774void intel_finish_page_flip(struct drm_device *dev, int pipe)
10775{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010776 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010777 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10778
Mario Kleiner49b14a52010-12-09 07:00:07 +010010779 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010780}
10781
10782void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10783{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010784 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010785 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10786
Mario Kleiner49b14a52010-12-09 07:00:07 +010010787 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010788}
10789
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010790/* Is 'a' after or equal to 'b'? */
10791static bool g4x_flip_count_after_eq(u32 a, u32 b)
10792{
10793 return !((a - b) & 0x80000000);
10794}
10795
10796static bool page_flip_finished(struct intel_crtc *crtc)
10797{
10798 struct drm_device *dev = crtc->base.dev;
10799 struct drm_i915_private *dev_priv = dev->dev_private;
10800
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030010801 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10802 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10803 return true;
10804
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010805 /*
10806 * The relevant registers doen't exist on pre-ctg.
10807 * As the flip done interrupt doesn't trigger for mmio
10808 * flips on gmch platforms, a flip count check isn't
10809 * really needed there. But since ctg has the registers,
10810 * include it in the check anyway.
10811 */
10812 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10813 return true;
10814
10815 /*
10816 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10817 * used the same base address. In that case the mmio flip might
10818 * have completed, but the CS hasn't even executed the flip yet.
10819 *
10820 * A flip count check isn't enough as the CS might have updated
10821 * the base address just after start of vblank, but before we
10822 * managed to process the interrupt. This means we'd complete the
10823 * CS flip too soon.
10824 *
10825 * Combining both checks should get us a good enough result. It may
10826 * still happen that the CS flip has been executed, but has not
10827 * yet actually completed. But in case the base address is the same
10828 * anyway, we don't really care.
10829 */
10830 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10831 crtc->unpin_work->gtt_offset &&
10832 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
10833 crtc->unpin_work->flip_count);
10834}
10835
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010836void intel_prepare_page_flip(struct drm_device *dev, int plane)
10837{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010838 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010839 struct intel_crtc *intel_crtc =
10840 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10841 unsigned long flags;
10842
Daniel Vetterf3260382014-09-15 14:55:23 +020010843
10844 /*
10845 * This is called both by irq handlers and the reset code (to complete
10846 * lost pageflips) so needs the full irqsave spinlocks.
10847 *
10848 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000010849 * generate a page-flip completion irq, i.e. every modeset
10850 * is also accompanied by a spurious intel_prepare_page_flip().
10851 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010852 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010853 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000010854 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010855 spin_unlock_irqrestore(&dev->event_lock, flags);
10856}
10857
Robin Schroereba905b2014-05-18 02:24:50 +020010858static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
Chris Wilsone7d841c2012-12-03 11:36:30 +000010859{
10860 /* Ensure that the work item is consistent when activating it ... */
10861 smp_wmb();
10862 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
10863 /* and that it is marked active as soon as the irq could fire. */
10864 smp_wmb();
10865}
10866
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010867static int intel_gen2_queue_flip(struct drm_device *dev,
10868 struct drm_crtc *crtc,
10869 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010870 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010871 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010872 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010873{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010874 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010875 u32 flip_mask;
10876 int ret;
10877
Daniel Vetter6d90c952012-04-26 23:28:05 +020010878 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010879 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010880 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010881
10882 /* Can't queue multiple flips, so wait for the previous
10883 * one to finish before executing the next.
10884 */
10885 if (intel_crtc->plane)
10886 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10887 else
10888 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010889 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10890 intel_ring_emit(ring, MI_NOOP);
10891 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10892 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10893 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010894 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010895 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000010896
10897 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010898 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010899 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010900}
10901
10902static int intel_gen3_queue_flip(struct drm_device *dev,
10903 struct drm_crtc *crtc,
10904 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010905 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010906 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010907 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010908{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010909 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010910 u32 flip_mask;
10911 int ret;
10912
Daniel Vetter6d90c952012-04-26 23:28:05 +020010913 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010914 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010915 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010916
10917 if (intel_crtc->plane)
10918 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10919 else
10920 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010921 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10922 intel_ring_emit(ring, MI_NOOP);
10923 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
10924 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10925 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010926 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010927 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010928
Chris Wilsone7d841c2012-12-03 11:36:30 +000010929 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010930 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010931 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010932}
10933
10934static int intel_gen4_queue_flip(struct drm_device *dev,
10935 struct drm_crtc *crtc,
10936 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010937 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010938 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010939 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010940{
10941 struct drm_i915_private *dev_priv = dev->dev_private;
10942 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10943 uint32_t pf, pipesrc;
10944 int ret;
10945
Daniel Vetter6d90c952012-04-26 23:28:05 +020010946 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010947 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010948 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010949
10950 /* i965+ uses the linear or tiled offsets from the
10951 * Display Registers (which do not change across a page-flip)
10952 * so we need only reprogram the base address.
10953 */
Daniel Vetter6d90c952012-04-26 23:28:05 +020010954 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10955 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10956 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010957 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020010958 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010959
10960 /* XXX Enabling the panel-fitter across page-flip is so far
10961 * untested on non-native modes, so ignore it for now.
10962 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
10963 */
10964 pf = 0;
10965 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010966 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010967
10968 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010969 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010970 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010971}
10972
10973static int intel_gen6_queue_flip(struct drm_device *dev,
10974 struct drm_crtc *crtc,
10975 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010976 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010977 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010978 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010979{
10980 struct drm_i915_private *dev_priv = dev->dev_private;
10981 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10982 uint32_t pf, pipesrc;
10983 int ret;
10984
Daniel Vetter6d90c952012-04-26 23:28:05 +020010985 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010986 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010987 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010988
Daniel Vetter6d90c952012-04-26 23:28:05 +020010989 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10990 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10991 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010992 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010993
Chris Wilson99d9acd2012-04-17 20:37:00 +010010994 /* Contrary to the suggestions in the documentation,
10995 * "Enable Panel Fitter" does not seem to be required when page
10996 * flipping with a non-native mode, and worse causes a normal
10997 * modeset to fail.
10998 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
10999 */
11000 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011001 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020011002 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011003
11004 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010011005 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010011006 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011007}
11008
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011009static int intel_gen7_queue_flip(struct drm_device *dev,
11010 struct drm_crtc *crtc,
11011 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011012 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011013 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070011014 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011015{
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011016 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011017 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010011018 int len, ret;
11019
Robin Schroereba905b2014-05-18 02:24:50 +020011020 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011021 case PLANE_A:
11022 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11023 break;
11024 case PLANE_B:
11025 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11026 break;
11027 case PLANE_C:
11028 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11029 break;
11030 default:
11031 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011032 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011033 }
11034
Chris Wilsonffe74d72013-08-26 20:58:12 +010011035 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +010011036 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010011037 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010011038 /*
11039 * On Gen 8, SRM is now taking an extra dword to accommodate
11040 * 48bits addresses, and we need a NOOP for the batch size to
11041 * stay even.
11042 */
11043 if (IS_GEN8(dev))
11044 len += 2;
11045 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011046
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011047 /*
11048 * BSpec MI_DISPLAY_FLIP for IVB:
11049 * "The full packet must be contained within the same cache line."
11050 *
11051 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11052 * cacheline, if we ever start emitting more commands before
11053 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11054 * then do the cacheline alignment, and finally emit the
11055 * MI_DISPLAY_FLIP.
11056 */
11057 ret = intel_ring_cacheline_align(ring);
11058 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011059 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011060
Chris Wilsonffe74d72013-08-26 20:58:12 +010011061 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011062 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011063 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011064
Chris Wilsonffe74d72013-08-26 20:58:12 +010011065 /* Unmask the flip-done completion message. Note that the bspec says that
11066 * we should do this for both the BCS and RCS, and that we must not unmask
11067 * more than one flip event at any time (or ensure that one flip message
11068 * can be sent by waiting for flip-done prior to queueing new flips).
11069 * Experimentation says that BCS works despite DERRMR masking all
11070 * flip-done completion events and that unmasking all planes at once
11071 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11072 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11073 */
11074 if (ring->id == RCS) {
11075 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
11076 intel_ring_emit(ring, DERRMR);
11077 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11078 DERRMR_PIPEB_PRI_FLIP_DONE |
11079 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010011080 if (IS_GEN8(dev))
11081 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
11082 MI_SRM_LRM_GLOBAL_GTT);
11083 else
11084 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
11085 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +010011086 intel_ring_emit(ring, DERRMR);
11087 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010011088 if (IS_GEN8(dev)) {
11089 intel_ring_emit(ring, 0);
11090 intel_ring_emit(ring, MI_NOOP);
11091 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011092 }
11093
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011094 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +020011095 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011096 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011097 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000011098
11099 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010011100 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010011101 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011102}
11103
Sourab Gupta84c33a62014-06-02 16:47:17 +053011104static bool use_mmio_flip(struct intel_engine_cs *ring,
11105 struct drm_i915_gem_object *obj)
11106{
11107 /*
11108 * This is not being used for older platforms, because
11109 * non-availability of flip done interrupt forces us to use
11110 * CS flips. Older platforms derive flip done using some clever
11111 * tricks involving the flip_pending status bits and vblank irqs.
11112 * So using MMIO flips there would disrupt this mechanism.
11113 */
11114
Chris Wilson8e09bf82014-07-08 10:40:30 +010011115 if (ring == NULL)
11116 return true;
11117
Sourab Gupta84c33a62014-06-02 16:47:17 +053011118 if (INTEL_INFO(ring->dev)->gen < 5)
11119 return false;
11120
11121 if (i915.use_mmio_flip < 0)
11122 return false;
11123 else if (i915.use_mmio_flip > 0)
11124 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010011125 else if (i915.enable_execlists)
11126 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011127 else
Chris Wilsonb4716182015-04-27 13:41:17 +010011128 return ring != i915_gem_request_get_ring(obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011129}
11130
Damien Lespiauff944562014-11-20 14:58:16 +000011131static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
11132{
11133 struct drm_device *dev = intel_crtc->base.dev;
11134 struct drm_i915_private *dev_priv = dev->dev_private;
11135 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000011136 const enum pipe pipe = intel_crtc->pipe;
11137 u32 ctl, stride;
11138
11139 ctl = I915_READ(PLANE_CTL(pipe, 0));
11140 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011141 switch (fb->modifier[0]) {
11142 case DRM_FORMAT_MOD_NONE:
11143 break;
11144 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000011145 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011146 break;
11147 case I915_FORMAT_MOD_Y_TILED:
11148 ctl |= PLANE_CTL_TILED_Y;
11149 break;
11150 case I915_FORMAT_MOD_Yf_TILED:
11151 ctl |= PLANE_CTL_TILED_YF;
11152 break;
11153 default:
11154 MISSING_CASE(fb->modifier[0]);
11155 }
Damien Lespiauff944562014-11-20 14:58:16 +000011156
11157 /*
11158 * The stride is either expressed as a multiple of 64 bytes chunks for
11159 * linear buffers or in number of tiles for tiled buffers.
11160 */
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011161 stride = fb->pitches[0] /
11162 intel_fb_stride_alignment(dev, fb->modifier[0],
11163 fb->pixel_format);
Damien Lespiauff944562014-11-20 14:58:16 +000011164
11165 /*
11166 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11167 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11168 */
11169 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11170 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11171
11172 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
11173 POSTING_READ(PLANE_SURF(pipe, 0));
11174}
11175
11176static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011177{
11178 struct drm_device *dev = intel_crtc->base.dev;
11179 struct drm_i915_private *dev_priv = dev->dev_private;
11180 struct intel_framebuffer *intel_fb =
11181 to_intel_framebuffer(intel_crtc->base.primary->fb);
11182 struct drm_i915_gem_object *obj = intel_fb->obj;
11183 u32 dspcntr;
11184 u32 reg;
11185
Sourab Gupta84c33a62014-06-02 16:47:17 +053011186 reg = DSPCNTR(intel_crtc->plane);
11187 dspcntr = I915_READ(reg);
11188
Damien Lespiauc5d97472014-10-25 00:11:11 +010011189 if (obj->tiling_mode != I915_TILING_NONE)
11190 dspcntr |= DISPPLANE_TILED;
11191 else
11192 dspcntr &= ~DISPPLANE_TILED;
11193
Sourab Gupta84c33a62014-06-02 16:47:17 +053011194 I915_WRITE(reg, dspcntr);
11195
11196 I915_WRITE(DSPSURF(intel_crtc->plane),
11197 intel_crtc->unpin_work->gtt_offset);
11198 POSTING_READ(DSPSURF(intel_crtc->plane));
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011199
Damien Lespiauff944562014-11-20 14:58:16 +000011200}
11201
11202/*
11203 * XXX: This is the temporary way to update the plane registers until we get
11204 * around to using the usual plane update functions for MMIO flips
11205 */
11206static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
11207{
11208 struct drm_device *dev = intel_crtc->base.dev;
11209 bool atomic_update;
11210 u32 start_vbl_count;
11211
11212 intel_mark_page_flip_active(intel_crtc);
11213
11214 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
11215
11216 if (INTEL_INFO(dev)->gen >= 9)
11217 skl_do_mmio_flip(intel_crtc);
11218 else
11219 /* use_mmio_flip() retricts MMIO flips to ilk+ */
11220 ilk_do_mmio_flip(intel_crtc);
11221
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011222 if (atomic_update)
11223 intel_pipe_update_end(intel_crtc, start_vbl_count);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011224}
11225
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011226static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011227{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011228 struct intel_mmio_flip *mmio_flip =
11229 container_of(work, struct intel_mmio_flip, work);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011230
Daniel Vettereed29a52015-05-21 14:21:25 +020011231 if (mmio_flip->req)
11232 WARN_ON(__i915_wait_request(mmio_flip->req,
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011233 mmio_flip->crtc->reset_counter,
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011234 false, NULL,
11235 &mmio_flip->i915->rps.mmioflips));
Sourab Gupta84c33a62014-06-02 16:47:17 +053011236
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011237 intel_do_mmio_flip(mmio_flip->crtc);
11238
Daniel Vettereed29a52015-05-21 14:21:25 +020011239 i915_gem_request_unreference__unlocked(mmio_flip->req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011240 kfree(mmio_flip);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011241}
11242
11243static int intel_queue_mmio_flip(struct drm_device *dev,
11244 struct drm_crtc *crtc,
11245 struct drm_framebuffer *fb,
11246 struct drm_i915_gem_object *obj,
11247 struct intel_engine_cs *ring,
11248 uint32_t flags)
11249{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011250 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011251
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011252 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11253 if (mmio_flip == NULL)
11254 return -ENOMEM;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011255
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011256 mmio_flip->i915 = to_i915(dev);
Daniel Vettereed29a52015-05-21 14:21:25 +020011257 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011258 mmio_flip->crtc = to_intel_crtc(crtc);
11259
11260 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11261 schedule_work(&mmio_flip->work);
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020011262
Sourab Gupta84c33a62014-06-02 16:47:17 +053011263 return 0;
11264}
11265
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011266static int intel_default_queue_flip(struct drm_device *dev,
11267 struct drm_crtc *crtc,
11268 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011269 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011270 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070011271 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011272{
11273 return -ENODEV;
11274}
11275
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011276static bool __intel_pageflip_stall_check(struct drm_device *dev,
11277 struct drm_crtc *crtc)
11278{
11279 struct drm_i915_private *dev_priv = dev->dev_private;
11280 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11281 struct intel_unpin_work *work = intel_crtc->unpin_work;
11282 u32 addr;
11283
11284 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11285 return true;
11286
11287 if (!work->enable_stall_check)
11288 return false;
11289
11290 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010011291 if (work->flip_queued_req &&
11292 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011293 return false;
11294
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011295 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011296 }
11297
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011298 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011299 return false;
11300
11301 /* Potential stall - if we see that the flip has happened,
11302 * assume a missed interrupt. */
11303 if (INTEL_INFO(dev)->gen >= 4)
11304 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11305 else
11306 addr = I915_READ(DSPADDR(intel_crtc->plane));
11307
11308 /* There is a potential issue here with a false positive after a flip
11309 * to the same address. We could address this by checking for a
11310 * non-incrementing frame counter.
11311 */
11312 return addr == work->gtt_offset;
11313}
11314
11315void intel_check_page_flip(struct drm_device *dev, int pipe)
11316{
11317 struct drm_i915_private *dev_priv = dev->dev_private;
11318 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11319 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011320 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020011321
Dave Gordon6c51d462015-03-06 15:34:26 +000011322 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011323
11324 if (crtc == NULL)
11325 return;
11326
Daniel Vetterf3260382014-09-15 14:55:23 +020011327 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011328 work = intel_crtc->unpin_work;
11329 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011330 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010011331 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011332 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011333 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011334 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010011335 if (work != NULL &&
11336 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11337 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020011338 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011339}
11340
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011341static int intel_crtc_page_flip(struct drm_crtc *crtc,
11342 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011343 struct drm_pending_vblank_event *event,
11344 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011345{
11346 struct drm_device *dev = crtc->dev;
11347 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070011348 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070011349 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011350 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080011351 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020011352 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011353 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011354 struct intel_engine_cs *ring;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011355 bool mmio_flip;
John Harrison91af1272015-06-18 13:14:56 +010011356 struct drm_i915_gem_request *request = NULL;
Chris Wilson52e68632010-08-08 10:15:59 +010011357 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011358
Matt Roper2ff8fde2014-07-08 07:50:07 -070011359 /*
11360 * drm_mode_page_flip_ioctl() should already catch this, but double
11361 * check to be safe. In the future we may enable pageflipping from
11362 * a disabled primary plane.
11363 */
11364 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11365 return -EBUSY;
11366
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011367 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070011368 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011369 return -EINVAL;
11370
11371 /*
11372 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11373 * Note that pitch changes could also affect these register.
11374 */
11375 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070011376 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11377 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011378 return -EINVAL;
11379
Chris Wilsonf900db42014-02-20 09:26:13 +000011380 if (i915_terminally_wedged(&dev_priv->gpu_error))
11381 goto out_hang;
11382
Daniel Vetterb14c5672013-09-19 12:18:32 +020011383 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011384 if (work == NULL)
11385 return -ENOMEM;
11386
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011387 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011388 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011389 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011390 INIT_WORK(&work->work, intel_unpin_work_fn);
11391
Daniel Vetter87b6b102014-05-15 15:33:46 +020011392 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011393 if (ret)
11394 goto free_work;
11395
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011396 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011397 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011398 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011399 /* Before declaring the flip queue wedged, check if
11400 * the hardware completed the operation behind our backs.
11401 */
11402 if (__intel_pageflip_stall_check(dev, crtc)) {
11403 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11404 page_flip_completed(intel_crtc);
11405 } else {
11406 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011407 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010011408
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011409 drm_crtc_vblank_put(crtc);
11410 kfree(work);
11411 return -EBUSY;
11412 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011413 }
11414 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011415 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011416
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011417 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11418 flush_workqueue(dev_priv->wq);
11419
Jesse Barnes75dfca82010-02-10 15:09:44 -080011420 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011421 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000011422 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011423
Matt Roperf4510a22014-04-01 15:22:40 -070011424 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011425 update_state_fb(crtc->primary);
Matt Roper1ed1f962015-01-30 16:22:36 -080011426
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011427 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011428
Chris Wilson89ed88b2015-02-16 14:31:49 +000011429 ret = i915_mutex_lock_interruptible(dev);
11430 if (ret)
11431 goto cleanup;
11432
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011433 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020011434 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011435
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011436 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Daniel Vettera071fa02014-06-18 23:28:09 +020011437 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011438
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011439 if (IS_VALLEYVIEW(dev)) {
11440 ring = &dev_priv->ring[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011441 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011442 /* vlv: DISPLAY_FLIP fails to change tiling */
11443 ring = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000011444 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Chris Wilson2a92d5b2014-07-08 10:40:29 +010011445 ring = &dev_priv->ring[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011446 } else if (INTEL_INFO(dev)->gen >= 7) {
Chris Wilsonb4716182015-04-27 13:41:17 +010011447 ring = i915_gem_request_get_ring(obj->last_write_req);
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011448 if (ring == NULL || ring->id != RCS)
11449 ring = &dev_priv->ring[BCS];
11450 } else {
11451 ring = &dev_priv->ring[RCS];
11452 }
11453
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011454 mmio_flip = use_mmio_flip(ring, obj);
11455
11456 /* When using CS flips, we want to emit semaphores between rings.
11457 * However, when using mmio flips we will create a task to do the
11458 * synchronisation, so all we want here is to pin the framebuffer
11459 * into the display plane and skip any waits.
11460 */
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011461 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011462 crtc->primary->state,
John Harrison91af1272015-06-18 13:14:56 +010011463 mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring, &request);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011464 if (ret)
11465 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011466
Tvrtko Ursulin121920f2015-03-23 11:10:37 +000011467 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
11468 + intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011469
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011470 if (mmio_flip) {
Sourab Gupta84c33a62014-06-02 16:47:17 +053011471 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
11472 page_flip_flags);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011473 if (ret)
11474 goto cleanup_unpin;
11475
John Harrisonf06cc1b2014-11-24 18:49:37 +000011476 i915_gem_request_assign(&work->flip_queued_req,
11477 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011478 } else {
Chris Wilsond94b5032015-04-27 13:41:15 +010011479 if (obj->last_write_req) {
11480 ret = i915_gem_check_olr(obj->last_write_req);
11481 if (ret)
11482 goto cleanup_unpin;
11483 }
11484
Sourab Gupta84c33a62014-06-02 16:47:17 +053011485 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011486 page_flip_flags);
11487 if (ret)
11488 goto cleanup_unpin;
11489
John Harrisonf06cc1b2014-11-24 18:49:37 +000011490 i915_gem_request_assign(&work->flip_queued_req,
11491 intel_ring_get_request(ring));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011492 }
11493
John Harrison91af1272015-06-18 13:14:56 +010011494 if (request)
11495 i915_add_request_no_flush(request->ring);
11496
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011497 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011498 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011499
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011500 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Daniel Vettera071fa02014-06-18 23:28:09 +020011501 INTEL_FRONTBUFFER_PRIMARY(pipe));
11502
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020011503 intel_fbc_disable(dev);
Daniel Vetterf99d7062014-06-19 16:01:59 +020011504 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011505 mutex_unlock(&dev->struct_mutex);
11506
Jesse Barnese5510fa2010-07-01 16:48:37 -070011507 trace_i915_flip_request(intel_crtc->plane, obj);
11508
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011509 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010011510
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011511cleanup_unpin:
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011512 intel_unpin_fb_obj(fb, crtc->primary->state);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011513cleanup_pending:
John Harrison91af1272015-06-18 13:14:56 +010011514 if (request)
11515 i915_gem_request_cancel(request);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011516 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000011517 mutex_unlock(&dev->struct_mutex);
11518cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070011519 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011520 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010011521
Chris Wilson89ed88b2015-02-16 14:31:49 +000011522 drm_gem_object_unreference_unlocked(&obj->base);
11523 drm_framebuffer_unreference(work->old_fb);
11524
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011525 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011526 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011527 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011528
Daniel Vetter87b6b102014-05-15 15:33:46 +020011529 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011530free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010011531 kfree(work);
11532
Chris Wilsonf900db42014-02-20 09:26:13 +000011533 if (ret == -EIO) {
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011534 struct drm_atomic_state *state;
11535 struct drm_plane_state *plane_state;
11536
Chris Wilsonf900db42014-02-20 09:26:13 +000011537out_hang:
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011538 state = drm_atomic_state_alloc(dev);
11539 if (!state)
11540 return -ENOMEM;
11541 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11542
11543retry:
11544 plane_state = drm_atomic_get_plane_state(state, primary);
11545 ret = PTR_ERR_OR_ZERO(plane_state);
11546 if (!ret) {
11547 drm_atomic_set_fb_for_plane(plane_state, fb);
11548
11549 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11550 if (!ret)
11551 ret = drm_atomic_commit(state);
11552 }
11553
11554 if (ret == -EDEADLK) {
11555 drm_modeset_backoff(state->acquire_ctx);
11556 drm_atomic_state_clear(state);
11557 goto retry;
11558 }
11559
11560 if (ret)
11561 drm_atomic_state_free(state);
11562
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011563 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011564 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020011565 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011566 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011567 }
Chris Wilsonf900db42014-02-20 09:26:13 +000011568 }
Chris Wilson96b099f2010-06-07 14:03:04 +010011569 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011570}
11571
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011572
11573/**
11574 * intel_wm_need_update - Check whether watermarks need updating
11575 * @plane: drm plane
11576 * @state: new plane state
11577 *
11578 * Check current plane state versus the new one to determine whether
11579 * watermarks need to be recalculated.
11580 *
11581 * Returns true or false.
11582 */
11583static bool intel_wm_need_update(struct drm_plane *plane,
11584 struct drm_plane_state *state)
11585{
11586 /* Update watermarks on tiling changes. */
11587 if (!plane->state->fb || !state->fb ||
11588 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
11589 plane->state->rotation != state->rotation)
11590 return true;
11591
11592 if (plane->state->crtc_w != state->crtc_w)
11593 return true;
11594
11595 return false;
11596}
11597
11598int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11599 struct drm_plane_state *plane_state)
11600{
11601 struct drm_crtc *crtc = crtc_state->crtc;
11602 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11603 struct drm_plane *plane = plane_state->plane;
11604 struct drm_device *dev = crtc->dev;
11605 struct drm_i915_private *dev_priv = dev->dev_private;
11606 struct intel_plane_state *old_plane_state =
11607 to_intel_plane_state(plane->state);
11608 int idx = intel_crtc->base.base.id, ret;
11609 int i = drm_plane_index(plane);
11610 bool mode_changed = needs_modeset(crtc_state);
11611 bool was_crtc_enabled = crtc->state->active;
11612 bool is_crtc_enabled = crtc_state->active;
11613
11614 bool turn_off, turn_on, visible, was_visible;
11615 struct drm_framebuffer *fb = plane_state->fb;
11616
11617 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11618 plane->type != DRM_PLANE_TYPE_CURSOR) {
11619 ret = skl_update_scaler_plane(
11620 to_intel_crtc_state(crtc_state),
11621 to_intel_plane_state(plane_state));
11622 if (ret)
11623 return ret;
11624 }
11625
11626 /*
11627 * Disabling a plane is always okay; we just need to update
11628 * fb tracking in a special way since cleanup_fb() won't
11629 * get called by the plane helpers.
11630 */
11631 if (old_plane_state->base.fb && !fb)
11632 intel_crtc->atomic.disabled_planes |= 1 << i;
11633
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011634 was_visible = old_plane_state->visible;
11635 visible = to_intel_plane_state(plane_state)->visible;
11636
11637 if (!was_crtc_enabled && WARN_ON(was_visible))
11638 was_visible = false;
11639
11640 if (!is_crtc_enabled && WARN_ON(visible))
11641 visible = false;
11642
11643 if (!was_visible && !visible)
11644 return 0;
11645
11646 turn_off = was_visible && (!visible || mode_changed);
11647 turn_on = visible && (!was_visible || mode_changed);
11648
11649 DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
11650 plane->base.id, fb ? fb->base.id : -1);
11651
11652 DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11653 plane->base.id, was_visible, visible,
11654 turn_off, turn_on, mode_changed);
11655
11656 if (intel_wm_need_update(plane, plane_state))
11657 intel_crtc->atomic.update_wm = true;
11658
11659 switch (plane->type) {
11660 case DRM_PLANE_TYPE_PRIMARY:
11661 if (visible)
11662 intel_crtc->atomic.fb_bits |=
11663 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
11664
11665 intel_crtc->atomic.wait_for_flips = true;
11666 intel_crtc->atomic.pre_disable_primary = turn_off;
11667 intel_crtc->atomic.post_enable_primary = turn_on;
11668
11669 if (turn_off)
11670 intel_crtc->atomic.disable_fbc = true;
11671
11672 /*
11673 * FBC does not work on some platforms for rotated
11674 * planes, so disable it when rotation is not 0 and
11675 * update it when rotation is set back to 0.
11676 *
11677 * FIXME: This is redundant with the fbc update done in
11678 * the primary plane enable function except that that
11679 * one is done too late. We eventually need to unify
11680 * this.
11681 */
11682
11683 if (visible &&
11684 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
11685 dev_priv->fbc.crtc == intel_crtc &&
11686 plane_state->rotation != BIT(DRM_ROTATE_0))
11687 intel_crtc->atomic.disable_fbc = true;
11688
11689 /*
11690 * BDW signals flip done immediately if the plane
11691 * is disabled, even if the plane enable is already
11692 * armed to occur at the next vblank :(
11693 */
11694 if (turn_on && IS_BROADWELL(dev))
11695 intel_crtc->atomic.wait_vblank = true;
11696
11697 intel_crtc->atomic.update_fbc |= visible || mode_changed;
11698 break;
11699 case DRM_PLANE_TYPE_CURSOR:
11700 if (visible)
11701 intel_crtc->atomic.fb_bits |=
11702 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
11703 break;
11704 case DRM_PLANE_TYPE_OVERLAY:
11705 /*
11706 * 'prepare' is never called when plane is being disabled, so
11707 * we need to handle frontbuffer tracking as a special case
11708 */
11709 if (visible)
11710 intel_crtc->atomic.fb_bits |=
11711 INTEL_FRONTBUFFER_SPRITE(intel_crtc->pipe);
11712
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020011713 if (turn_off && !mode_changed) {
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011714 intel_crtc->atomic.wait_vblank = true;
11715 intel_crtc->atomic.update_sprite_watermarks |=
11716 1 << i;
11717 }
11718 break;
11719 }
11720 return 0;
11721}
11722
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011723static bool encoders_cloneable(const struct intel_encoder *a,
11724 const struct intel_encoder *b)
11725{
11726 /* masks could be asymmetric, so check both ways */
11727 return a == b || (a->cloneable & (1 << b->type) &&
11728 b->cloneable & (1 << a->type));
11729}
11730
11731static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11732 struct intel_crtc *crtc,
11733 struct intel_encoder *encoder)
11734{
11735 struct intel_encoder *source_encoder;
11736 struct drm_connector *connector;
11737 struct drm_connector_state *connector_state;
11738 int i;
11739
11740 for_each_connector_in_state(state, connector, connector_state, i) {
11741 if (connector_state->crtc != &crtc->base)
11742 continue;
11743
11744 source_encoder =
11745 to_intel_encoder(connector_state->best_encoder);
11746 if (!encoders_cloneable(encoder, source_encoder))
11747 return false;
11748 }
11749
11750 return true;
11751}
11752
11753static bool check_encoder_cloning(struct drm_atomic_state *state,
11754 struct intel_crtc *crtc)
11755{
11756 struct intel_encoder *encoder;
11757 struct drm_connector *connector;
11758 struct drm_connector_state *connector_state;
11759 int i;
11760
11761 for_each_connector_in_state(state, connector, connector_state, i) {
11762 if (connector_state->crtc != &crtc->base)
11763 continue;
11764
11765 encoder = to_intel_encoder(connector_state->best_encoder);
11766 if (!check_single_encoder_cloning(state, crtc, encoder))
11767 return false;
11768 }
11769
11770 return true;
11771}
11772
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020011773static void intel_crtc_check_initial_planes(struct drm_crtc *crtc,
11774 struct drm_crtc_state *crtc_state)
11775{
11776 struct intel_crtc_state *pipe_config =
11777 to_intel_crtc_state(crtc_state);
11778 struct drm_plane *p;
11779 unsigned visible_mask = 0;
11780
11781 drm_for_each_plane_mask(p, crtc->dev, crtc_state->plane_mask) {
11782 struct drm_plane_state *plane_state =
11783 drm_atomic_get_existing_plane_state(crtc_state->state, p);
11784
11785 if (WARN_ON(!plane_state))
11786 continue;
11787
11788 if (!plane_state->fb)
11789 crtc_state->plane_mask &=
11790 ~(1 << drm_plane_index(p));
11791 else if (to_intel_plane_state(plane_state)->visible)
11792 visible_mask |= 1 << drm_plane_index(p);
11793 }
11794
11795 if (!visible_mask)
11796 return;
11797
11798 pipe_config->quirks &= ~PIPE_CONFIG_QUIRK_INITIAL_PLANES;
11799}
11800
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011801static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11802 struct drm_crtc_state *crtc_state)
11803{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011804 struct drm_device *dev = crtc->dev;
Maarten Lankhorstad421372015-06-15 12:33:42 +020011805 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011806 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011807 struct intel_crtc_state *pipe_config =
11808 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011809 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorstad421372015-06-15 12:33:42 +020011810 int ret, idx = crtc->base.id;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011811 bool mode_changed = needs_modeset(crtc_state);
11812
11813 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
11814 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11815 return -EINVAL;
11816 }
11817
11818 I915_STATE_WARN(crtc->state->active != intel_crtc->active,
11819 "[CRTC:%i] mismatch between state->active(%i) and crtc->active(%i)\n",
11820 idx, crtc->state->active, intel_crtc->active);
11821
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020011822 /* plane mask is fixed up after all initial planes are calculated */
11823 if (pipe_config->quirks & PIPE_CONFIG_QUIRK_INITIAL_PLANES)
11824 intel_crtc_check_initial_planes(crtc, crtc_state);
11825
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020011826 if (mode_changed)
11827 intel_crtc->atomic.update_wm = !crtc_state->active;
11828
Maarten Lankhorstad421372015-06-15 12:33:42 +020011829 if (mode_changed && crtc_state->enable &&
11830 dev_priv->display.crtc_compute_clock &&
11831 !WARN_ON(pipe_config->shared_dpll != DPLL_ID_PRIVATE)) {
11832 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11833 pipe_config);
11834 if (ret)
11835 return ret;
11836 }
11837
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011838 return intel_atomic_setup_scalers(dev, intel_crtc, pipe_config);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011839}
11840
Jani Nikula65b38e02015-04-13 11:26:56 +030011841static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011842 .mode_set_base_atomic = intel_pipe_set_base_atomic,
11843 .load_lut = intel_crtc_load_lut,
Matt Roperea2c67b2014-12-23 10:41:52 -080011844 .atomic_begin = intel_begin_crtc_commit,
11845 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011846 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011847};
11848
Daniel Vetter9a935852012-07-05 22:34:27 +020011849/**
11850 * intel_modeset_update_staged_output_state
11851 *
11852 * Updates the staged output configuration state, e.g. after we've read out the
11853 * current hw state.
11854 */
11855static void intel_modeset_update_staged_output_state(struct drm_device *dev)
11856{
Ville Syrjälä76688512014-01-10 11:28:06 +020011857 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011858 struct intel_encoder *encoder;
11859 struct intel_connector *connector;
11860
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011861 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011862 connector->new_encoder =
11863 to_intel_encoder(connector->base.encoder);
11864 }
11865
Damien Lespiaub2784e12014-08-05 11:29:37 +010011866 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011867 encoder->new_crtc =
11868 to_intel_crtc(encoder->base.crtc);
11869 }
Ville Syrjälä76688512014-01-10 11:28:06 +020011870
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011871 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011872 crtc->new_enabled = crtc->base.state->enable;
Ville Syrjälä76688512014-01-10 11:28:06 +020011873 }
Daniel Vetter9a935852012-07-05 22:34:27 +020011874}
11875
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011876/* Transitional helper to copy current connector/encoder state to
11877 * connector->state. This is needed so that code that is partially
11878 * converted to atomic does the right thing.
11879 */
11880static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11881{
11882 struct intel_connector *connector;
11883
11884 for_each_intel_connector(dev, connector) {
11885 if (connector->base.encoder) {
11886 connector->base.state->best_encoder =
11887 connector->base.encoder;
11888 connector->base.state->crtc =
11889 connector->base.encoder->crtc;
11890 } else {
11891 connector->base.state->best_encoder = NULL;
11892 connector->base.state->crtc = NULL;
11893 }
11894 }
11895}
11896
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011897static void
Robin Schroereba905b2014-05-18 02:24:50 +020011898connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011899 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011900{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011901 int bpp = pipe_config->pipe_bpp;
11902
11903 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11904 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011905 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011906
11907 /* Don't use an invalid EDID bpc value */
11908 if (connector->base.display_info.bpc &&
11909 connector->base.display_info.bpc * 3 < bpp) {
11910 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11911 bpp, connector->base.display_info.bpc*3);
11912 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11913 }
11914
11915 /* Clamp bpp to 8 on screens without EDID 1.4 */
11916 if (connector->base.display_info.bpc == 0 && bpp > 24) {
11917 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11918 bpp);
11919 pipe_config->pipe_bpp = 24;
11920 }
11921}
11922
11923static int
11924compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011925 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011926{
11927 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011928 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011929 struct drm_connector *connector;
11930 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011931 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011932
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011933 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011934 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011935 else if (INTEL_INFO(dev)->gen >= 5)
11936 bpp = 12*3;
11937 else
11938 bpp = 8*3;
11939
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011940
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011941 pipe_config->pipe_bpp = bpp;
11942
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011943 state = pipe_config->base.state;
11944
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011945 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011946 for_each_connector_in_state(state, connector, connector_state, i) {
11947 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011948 continue;
11949
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011950 connected_sink_compute_bpp(to_intel_connector(connector),
11951 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011952 }
11953
11954 return bpp;
11955}
11956
Daniel Vetter644db712013-09-19 14:53:58 +020011957static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11958{
11959 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11960 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010011961 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020011962 mode->crtc_hdisplay, mode->crtc_hsync_start,
11963 mode->crtc_hsync_end, mode->crtc_htotal,
11964 mode->crtc_vdisplay, mode->crtc_vsync_start,
11965 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11966}
11967
Daniel Vetterc0b03412013-05-28 12:05:54 +020011968static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011969 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020011970 const char *context)
11971{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011972 struct drm_device *dev = crtc->base.dev;
11973 struct drm_plane *plane;
11974 struct intel_plane *intel_plane;
11975 struct intel_plane_state *state;
11976 struct drm_framebuffer *fb;
11977
11978 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
11979 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020011980
11981 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
11982 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
11983 pipe_config->pipe_bpp, pipe_config->dither);
11984 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11985 pipe_config->has_pch_encoder,
11986 pipe_config->fdi_lanes,
11987 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
11988 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
11989 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011990 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11991 pipe_config->has_dp_encoder,
11992 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
11993 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
11994 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011995
11996 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
11997 pipe_config->has_dp_encoder,
11998 pipe_config->dp_m2_n2.gmch_m,
11999 pipe_config->dp_m2_n2.gmch_n,
12000 pipe_config->dp_m2_n2.link_m,
12001 pipe_config->dp_m2_n2.link_n,
12002 pipe_config->dp_m2_n2.tu);
12003
Daniel Vetter55072d12014-11-20 16:10:28 +010012004 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12005 pipe_config->has_audio,
12006 pipe_config->has_infoframe);
12007
Daniel Vetterc0b03412013-05-28 12:05:54 +020012008 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012009 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012010 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012011 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12012 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030012013 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012014 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12015 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010012016 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12017 crtc->num_scalers,
12018 pipe_config->scaler_state.scaler_users,
12019 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012020 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12021 pipe_config->gmch_pfit.control,
12022 pipe_config->gmch_pfit.pgm_ratios,
12023 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012024 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020012025 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012026 pipe_config->pch_pfit.size,
12027 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012028 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030012029 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012030
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012031 if (IS_BROXTON(dev)) {
12032 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, "
12033 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
12034 "pll6: 0x%x, pll8: 0x%x, pcsdw12: 0x%x\n",
12035 pipe_config->ddi_pll_sel,
12036 pipe_config->dpll_hw_state.ebb0,
12037 pipe_config->dpll_hw_state.pll0,
12038 pipe_config->dpll_hw_state.pll1,
12039 pipe_config->dpll_hw_state.pll2,
12040 pipe_config->dpll_hw_state.pll3,
12041 pipe_config->dpll_hw_state.pll6,
12042 pipe_config->dpll_hw_state.pll8,
12043 pipe_config->dpll_hw_state.pcsdw12);
12044 } else if (IS_SKYLAKE(dev)) {
12045 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12046 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12047 pipe_config->ddi_pll_sel,
12048 pipe_config->dpll_hw_state.ctrl1,
12049 pipe_config->dpll_hw_state.cfgcr1,
12050 pipe_config->dpll_hw_state.cfgcr2);
12051 } else if (HAS_DDI(dev)) {
12052 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
12053 pipe_config->ddi_pll_sel,
12054 pipe_config->dpll_hw_state.wrpll);
12055 } else {
12056 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12057 "fp0: 0x%x, fp1: 0x%x\n",
12058 pipe_config->dpll_hw_state.dpll,
12059 pipe_config->dpll_hw_state.dpll_md,
12060 pipe_config->dpll_hw_state.fp0,
12061 pipe_config->dpll_hw_state.fp1);
12062 }
12063
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012064 DRM_DEBUG_KMS("planes on this crtc\n");
12065 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12066 intel_plane = to_intel_plane(plane);
12067 if (intel_plane->pipe != crtc->pipe)
12068 continue;
12069
12070 state = to_intel_plane_state(plane->state);
12071 fb = state->base.fb;
12072 if (!fb) {
12073 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
12074 "disabled, scaler_id = %d\n",
12075 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12076 plane->base.id, intel_plane->pipe,
12077 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
12078 drm_plane_index(plane), state->scaler_id);
12079 continue;
12080 }
12081
12082 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
12083 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12084 plane->base.id, intel_plane->pipe,
12085 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
12086 drm_plane_index(plane));
12087 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
12088 fb->base.id, fb->width, fb->height, fb->pixel_format);
12089 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
12090 state->scaler_id,
12091 state->src.x1 >> 16, state->src.y1 >> 16,
12092 drm_rect_width(&state->src) >> 16,
12093 drm_rect_height(&state->src) >> 16,
12094 state->dst.x1, state->dst.y1,
12095 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
12096 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020012097}
12098
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012099static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012100{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012101 struct drm_device *dev = state->dev;
12102 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012103 struct drm_connector *connector;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012104 struct drm_connector_state *connector_state;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012105 unsigned int used_ports = 0;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012106 int i;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012107
12108 /*
12109 * Walk the connector list instead of the encoder
12110 * list to detect the problem on ddi platforms
12111 * where there's just one encoder per digital port.
12112 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012113 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012114 if (!connector_state->best_encoder)
12115 continue;
12116
12117 encoder = to_intel_encoder(connector_state->best_encoder);
12118
12119 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012120
12121 switch (encoder->type) {
12122 unsigned int port_mask;
12123 case INTEL_OUTPUT_UNKNOWN:
12124 if (WARN_ON(!HAS_DDI(dev)))
12125 break;
12126 case INTEL_OUTPUT_DISPLAYPORT:
12127 case INTEL_OUTPUT_HDMI:
12128 case INTEL_OUTPUT_EDP:
12129 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12130
12131 /* the same port mustn't appear more than once */
12132 if (used_ports & port_mask)
12133 return false;
12134
12135 used_ports |= port_mask;
12136 default:
12137 break;
12138 }
12139 }
12140
12141 return true;
12142}
12143
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012144static void
12145clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12146{
12147 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012148 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012149 struct intel_dpll_hw_state dpll_hw_state;
12150 enum intel_dpll_id shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012151 uint32_t ddi_pll_sel;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012152
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012153 /* FIXME: before the switch to atomic started, a new pipe_config was
12154 * kzalloc'd. Code that depends on any field being zero should be
12155 * fixed, so that the crtc_state can be safely duplicated. For now,
12156 * only fields that are know to not cause problems are preserved. */
12157
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012158 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012159 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012160 shared_dpll = crtc_state->shared_dpll;
12161 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012162 ddi_pll_sel = crtc_state->ddi_pll_sel;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012163
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012164 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012165
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012166 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012167 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012168 crtc_state->shared_dpll = shared_dpll;
12169 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012170 crtc_state->ddi_pll_sel = ddi_pll_sel;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012171}
12172
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012173static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012174intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012175 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012176{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012177 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020012178 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012179 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012180 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012181 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012182 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012183 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012184
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012185 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012186
Daniel Vettere143a212013-07-04 12:01:15 +020012187 pipe_config->cpu_transcoder =
12188 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012189
Imre Deak2960bc92013-07-30 13:36:32 +030012190 /*
12191 * Sanitize sync polarity flags based on requested ones. If neither
12192 * positive or negative polarity is requested, treat this as meaning
12193 * negative polarity.
12194 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012195 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012196 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012197 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012198
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012199 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012200 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012201 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012202
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012203 /* Compute a starting value for pipe_config->pipe_bpp taking the source
12204 * plane pixel format and any sink constraints into account. Returns the
12205 * source plane bpp so that dithering can be selected on mismatches
12206 * after encoders and crtc also have had their say. */
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012207 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12208 pipe_config);
12209 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012210 goto fail;
12211
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012212 /*
12213 * Determine the real pipe dimensions. Note that stereo modes can
12214 * increase the actual pipe size due to the frame doubling and
12215 * insertion of additional space for blanks between the frame. This
12216 * is stored in the crtc timings. We use the requested mode to do this
12217 * computation to clearly distinguish it from the adjusted mode, which
12218 * can be changed by the connectors in the below retry loop.
12219 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012220 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080012221 &pipe_config->pipe_src_w,
12222 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012223
Daniel Vettere29c22c2013-02-21 00:00:16 +010012224encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020012225 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020012226 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020012227 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012228
Daniel Vetter135c81b2013-07-21 21:37:09 +020012229 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012230 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12231 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020012232
Daniel Vetter7758a112012-07-08 19:40:39 +020012233 /* Pass our mode to the connectors and the CRTC to give them a chance to
12234 * adjust it according to limitations or connector properties, and also
12235 * a chance to reject the mode entirely.
12236 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012237 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012238 if (connector_state->crtc != crtc)
12239 continue;
12240
12241 encoder = to_intel_encoder(connector_state->best_encoder);
12242
Daniel Vetterefea6e82013-07-21 21:36:59 +020012243 if (!(encoder->compute_config(encoder, pipe_config))) {
12244 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020012245 goto fail;
12246 }
12247 }
12248
Daniel Vetterff9a6752013-06-01 17:16:21 +020012249 /* Set default port clock if not overwritten by the encoder. Needs to be
12250 * done afterwards in case the encoder adjusts the mode. */
12251 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012252 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010012253 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012254
Daniel Vettera43f6e02013-06-07 23:10:32 +020012255 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010012256 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020012257 DRM_DEBUG_KMS("CRTC fixup failed\n");
12258 goto fail;
12259 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010012260
12261 if (ret == RETRY) {
12262 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12263 ret = -EINVAL;
12264 goto fail;
12265 }
12266
12267 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12268 retry = false;
12269 goto encoder_retry;
12270 }
12271
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012272 pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012273 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012274 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012275
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +020012276 /* Check if we need to force a modeset */
12277 if (pipe_config->has_audio !=
Maarten Lankhorst85a96e72015-06-01 12:49:53 +020012278 to_intel_crtc_state(crtc->state)->has_audio) {
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +020012279 pipe_config->base.mode_changed = true;
Maarten Lankhorst85a96e72015-06-01 12:49:53 +020012280 ret = drm_atomic_add_affected_planes(state, crtc);
12281 }
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +020012282
12283 /*
12284 * Note we have an issue here with infoframes: current code
12285 * only updates them on the full mode set path per hw
12286 * requirements. So here we should be checking for any
12287 * required changes and forcing a mode set.
12288 */
Daniel Vetter7758a112012-07-08 19:40:39 +020012289fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012290 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020012291}
12292
Daniel Vetterea9d7582012-07-10 10:42:52 +020012293static bool intel_crtc_in_use(struct drm_crtc *crtc)
12294{
12295 struct drm_encoder *encoder;
12296 struct drm_device *dev = crtc->dev;
12297
12298 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
12299 if (encoder->crtc == crtc)
12300 return true;
12301
12302 return false;
12303}
12304
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012305static void
12306intel_modeset_update_state(struct drm_atomic_state *state)
12307{
12308 struct drm_device *dev = state->dev;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012309 struct intel_encoder *intel_encoder;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012310 struct drm_crtc *crtc;
12311 struct drm_crtc_state *crtc_state;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012312 struct drm_connector *connector;
12313
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020012314 intel_shared_dpll_commit(state);
Daniel Vetterba41c0de2014-11-03 15:04:55 +010012315
Damien Lespiaub2784e12014-08-05 11:29:37 +010012316 for_each_intel_encoder(dev, intel_encoder) {
Daniel Vetterea9d7582012-07-10 10:42:52 +020012317 if (!intel_encoder->base.crtc)
12318 continue;
12319
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012320 crtc = intel_encoder->base.crtc;
12321 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
12322 if (!crtc_state || !needs_modeset(crtc->state))
12323 continue;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012324
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012325 intel_encoder->connectors_active = false;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012326 }
12327
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012328 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorstf7217902015-06-10 10:24:20 +020012329 intel_modeset_update_staged_output_state(state->dev);
Daniel Vetterea9d7582012-07-10 10:42:52 +020012330
Ville Syrjälä76688512014-01-10 11:28:06 +020012331 /* Double check state. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012332 for_each_crtc(dev, crtc) {
12333 WARN_ON(crtc->state->enable != intel_crtc_in_use(crtc));
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012334
12335 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020012336
12337 /* Update hwmode for vblank functions */
12338 if (crtc->state->active)
12339 crtc->hwmode = crtc->state->adjusted_mode;
12340 else
12341 crtc->hwmode.crtc_clock = 0;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012342 }
12343
12344 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
12345 if (!connector->encoder || !connector->encoder->crtc)
12346 continue;
12347
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012348 crtc = connector->encoder->crtc;
12349 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
12350 if (!crtc_state || !needs_modeset(crtc->state))
12351 continue;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012352
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020012353 if (crtc->state->active) {
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012354 struct drm_property *dpms_property =
12355 dev->mode_config.dpms_property;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012356
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012357 connector->dpms = DRM_MODE_DPMS_ON;
12358 drm_object_property_set_value(&connector->base, dpms_property, DRM_MODE_DPMS_ON);
Daniel Vetter68d34722012-09-06 22:08:35 +020012359
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012360 intel_encoder = to_intel_encoder(connector->encoder);
12361 intel_encoder->connectors_active = true;
12362 } else
12363 connector->dpms = DRM_MODE_DPMS_OFF;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012364 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012365}
12366
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012367static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012368{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012369 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012370
12371 if (clock1 == clock2)
12372 return true;
12373
12374 if (!clock1 || !clock2)
12375 return false;
12376
12377 diff = abs(clock1 - clock2);
12378
12379 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12380 return true;
12381
12382 return false;
12383}
12384
Daniel Vetter25c5b262012-07-08 22:08:04 +020012385#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12386 list_for_each_entry((intel_crtc), \
12387 &(dev)->mode_config.crtc_list, \
12388 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +020012389 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020012390
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012391static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012392intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012393 struct intel_crtc_state *current_config,
12394 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012395{
Daniel Vetter66e985c2013-06-05 13:34:20 +020012396#define PIPE_CONF_CHECK_X(name) \
12397 if (current_config->name != pipe_config->name) { \
12398 DRM_ERROR("mismatch in " #name " " \
12399 "(expected 0x%08x, found 0x%08x)\n", \
12400 current_config->name, \
12401 pipe_config->name); \
12402 return false; \
12403 }
12404
Daniel Vetter08a24032013-04-19 11:25:34 +020012405#define PIPE_CONF_CHECK_I(name) \
12406 if (current_config->name != pipe_config->name) { \
12407 DRM_ERROR("mismatch in " #name " " \
12408 "(expected %i, found %i)\n", \
12409 current_config->name, \
12410 pipe_config->name); \
12411 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010012412 }
12413
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012414/* This is required for BDW+ where there is only one set of registers for
12415 * switching between high and low RR.
12416 * This macro can be used whenever a comparison has to be made between one
12417 * hw state and multiple sw state variables.
12418 */
12419#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
12420 if ((current_config->name != pipe_config->name) && \
12421 (current_config->alt_name != pipe_config->name)) { \
12422 DRM_ERROR("mismatch in " #name " " \
12423 "(expected %i or %i, found %i)\n", \
12424 current_config->name, \
12425 current_config->alt_name, \
12426 pipe_config->name); \
12427 return false; \
12428 }
12429
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012430#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12431 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -070012432 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012433 "(expected %i, found %i)\n", \
12434 current_config->name & (mask), \
12435 pipe_config->name & (mask)); \
12436 return false; \
12437 }
12438
Ville Syrjälä5e550652013-09-06 23:29:07 +030012439#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12440 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
12441 DRM_ERROR("mismatch in " #name " " \
12442 "(expected %i, found %i)\n", \
12443 current_config->name, \
12444 pipe_config->name); \
12445 return false; \
12446 }
12447
Daniel Vetterbb760062013-06-06 14:55:52 +020012448#define PIPE_CONF_QUIRK(quirk) \
12449 ((current_config->quirks | pipe_config->quirks) & (quirk))
12450
Daniel Vettereccb1402013-05-22 00:50:22 +020012451 PIPE_CONF_CHECK_I(cpu_transcoder);
12452
Daniel Vetter08a24032013-04-19 11:25:34 +020012453 PIPE_CONF_CHECK_I(has_pch_encoder);
12454 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +020012455 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
12456 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
12457 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
12458 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
12459 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +020012460
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012461 PIPE_CONF_CHECK_I(has_dp_encoder);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012462
12463 if (INTEL_INFO(dev)->gen < 8) {
12464 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
12465 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
12466 PIPE_CONF_CHECK_I(dp_m_n.link_m);
12467 PIPE_CONF_CHECK_I(dp_m_n.link_n);
12468 PIPE_CONF_CHECK_I(dp_m_n.tu);
12469
12470 if (current_config->has_drrs) {
12471 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
12472 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
12473 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
12474 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
12475 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
12476 }
12477 } else {
12478 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
12479 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
12480 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
12481 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
12482 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
12483 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012484
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012485 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12486 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12487 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12488 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12489 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12490 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012491
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012492 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12493 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12494 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12495 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12496 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12497 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012498
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012499 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b52014-04-24 23:54:47 +020012500 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012501 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
12502 IS_VALLEYVIEW(dev))
12503 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012504 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012505
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012506 PIPE_CONF_CHECK_I(has_audio);
12507
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012508 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012509 DRM_MODE_FLAG_INTERLACE);
12510
Daniel Vetterbb760062013-06-06 14:55:52 +020012511 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012512 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012513 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012514 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012515 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012516 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012517 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012518 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012519 DRM_MODE_FLAG_NVSYNC);
12520 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012521
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012522 PIPE_CONF_CHECK_I(pipe_src_w);
12523 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012524
Daniel Vetter99535992014-04-13 12:00:33 +020012525 /*
12526 * FIXME: BIOS likes to set up a cloned config with lvds+external
12527 * screen. Since we don't yet re-compute the pipe config when moving
12528 * just the lvds port away to another pipe the sw tracking won't match.
12529 *
12530 * Proper atomic modesets with recomputed global state will fix this.
12531 * Until then just don't check gmch state for inherited modes.
12532 */
12533 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
12534 PIPE_CONF_CHECK_I(gmch_pfit.control);
12535 /* pfit ratios are autocomputed by the hw on gen4+ */
12536 if (INTEL_INFO(dev)->gen < 4)
12537 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
12538 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
12539 }
12540
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012541 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12542 if (current_config->pch_pfit.enabled) {
12543 PIPE_CONF_CHECK_I(pch_pfit.pos);
12544 PIPE_CONF_CHECK_I(pch_pfit.size);
12545 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012546
Chandra Kondurua1b22782015-04-07 15:28:45 -070012547 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12548
Jesse Barnese59150d2014-01-07 13:30:45 -080012549 /* BDW+ don't expose a synchronous way to read the state */
12550 if (IS_HASWELL(dev))
12551 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012552
Ville Syrjälä282740f2013-09-04 18:30:03 +030012553 PIPE_CONF_CHECK_I(double_wide);
12554
Daniel Vetter26804af2014-06-25 22:01:55 +030012555 PIPE_CONF_CHECK_X(ddi_pll_sel);
12556
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012557 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012558 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012559 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012560 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12561 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012562 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012563 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12564 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12565 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012566
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012567 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12568 PIPE_CONF_CHECK_I(pipe_bpp);
12569
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012570 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012571 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012572
Daniel Vetter66e985c2013-06-05 13:34:20 +020012573#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012574#undef PIPE_CONF_CHECK_I
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012575#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012576#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012577#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012578#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012579
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012580 return true;
12581}
12582
Damien Lespiau08db6652014-11-04 17:06:52 +000012583static void check_wm_state(struct drm_device *dev)
12584{
12585 struct drm_i915_private *dev_priv = dev->dev_private;
12586 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12587 struct intel_crtc *intel_crtc;
12588 int plane;
12589
12590 if (INTEL_INFO(dev)->gen < 9)
12591 return;
12592
12593 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12594 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12595
12596 for_each_intel_crtc(dev, intel_crtc) {
12597 struct skl_ddb_entry *hw_entry, *sw_entry;
12598 const enum pipe pipe = intel_crtc->pipe;
12599
12600 if (!intel_crtc->active)
12601 continue;
12602
12603 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000012604 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012605 hw_entry = &hw_ddb.plane[pipe][plane];
12606 sw_entry = &sw_ddb->plane[pipe][plane];
12607
12608 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12609 continue;
12610
12611 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12612 "(expected (%u,%u), found (%u,%u))\n",
12613 pipe_name(pipe), plane + 1,
12614 sw_entry->start, sw_entry->end,
12615 hw_entry->start, hw_entry->end);
12616 }
12617
12618 /* cursor */
12619 hw_entry = &hw_ddb.cursor[pipe];
12620 sw_entry = &sw_ddb->cursor[pipe];
12621
12622 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12623 continue;
12624
12625 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12626 "(expected (%u,%u), found (%u,%u))\n",
12627 pipe_name(pipe),
12628 sw_entry->start, sw_entry->end,
12629 hw_entry->start, hw_entry->end);
12630 }
12631}
12632
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012633static void
12634check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012635{
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012636 struct intel_connector *connector;
12637
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012638 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012639 /* This also checks the encoder/connector hw state with the
12640 * ->get_hw_state callbacks. */
12641 intel_connector_check_state(connector);
12642
Rob Clarke2c719b2014-12-15 13:56:32 -050012643 I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012644 "connector's staged encoder doesn't match current encoder\n");
12645 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012646}
12647
12648static void
12649check_encoder_state(struct drm_device *dev)
12650{
12651 struct intel_encoder *encoder;
12652 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012653
Damien Lespiaub2784e12014-08-05 11:29:37 +010012654 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012655 bool enabled = false;
12656 bool active = false;
12657 enum pipe pipe, tracked_pipe;
12658
12659 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12660 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012661 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012662
Rob Clarke2c719b2014-12-15 13:56:32 -050012663 I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012664 "encoder's stage crtc doesn't match current crtc\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012665 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012666 "encoder's active_connectors set, but no crtc\n");
12667
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012668 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012669 if (connector->base.encoder != &encoder->base)
12670 continue;
12671 enabled = true;
12672 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
12673 active = true;
12674 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012675 /*
12676 * for MST connectors if we unplug the connector is gone
12677 * away but the encoder is still connected to a crtc
12678 * until a modeset happens in response to the hotplug.
12679 */
12680 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
12681 continue;
12682
Rob Clarke2c719b2014-12-15 13:56:32 -050012683 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012684 "encoder's enabled state mismatch "
12685 "(expected %i, found %i)\n",
12686 !!encoder->base.crtc, enabled);
Rob Clarke2c719b2014-12-15 13:56:32 -050012687 I915_STATE_WARN(active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012688 "active encoder with no crtc\n");
12689
Rob Clarke2c719b2014-12-15 13:56:32 -050012690 I915_STATE_WARN(encoder->connectors_active != active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012691 "encoder's computed active state doesn't match tracked active state "
12692 "(expected %i, found %i)\n", active, encoder->connectors_active);
12693
12694 active = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -050012695 I915_STATE_WARN(active != encoder->connectors_active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012696 "encoder's hw state doesn't match sw tracking "
12697 "(expected %i, found %i)\n",
12698 encoder->connectors_active, active);
12699
12700 if (!encoder->base.crtc)
12701 continue;
12702
12703 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
Rob Clarke2c719b2014-12-15 13:56:32 -050012704 I915_STATE_WARN(active && pipe != tracked_pipe,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012705 "active encoder's pipe doesn't match"
12706 "(expected %i, found %i)\n",
12707 tracked_pipe, pipe);
12708
12709 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012710}
12711
12712static void
12713check_crtc_state(struct drm_device *dev)
12714{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012715 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012716 struct intel_crtc *crtc;
12717 struct intel_encoder *encoder;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012718 struct intel_crtc_state pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012719
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012720 for_each_intel_crtc(dev, crtc) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012721 bool enabled = false;
12722 bool active = false;
12723
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012724 memset(&pipe_config, 0, sizeof(pipe_config));
12725
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012726 DRM_DEBUG_KMS("[CRTC:%d]\n",
12727 crtc->base.base.id);
12728
Matt Roper83d65732015-02-25 13:12:16 -080012729 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012730 "active crtc, but not enabled in sw tracking\n");
12731
Damien Lespiaub2784e12014-08-05 11:29:37 +010012732 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012733 if (encoder->base.crtc != &crtc->base)
12734 continue;
12735 enabled = true;
12736 if (encoder->connectors_active)
12737 active = true;
12738 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020012739
Rob Clarke2c719b2014-12-15 13:56:32 -050012740 I915_STATE_WARN(active != crtc->active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012741 "crtc's computed active state doesn't match tracked active state "
12742 "(expected %i, found %i)\n", active, crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080012743 I915_STATE_WARN(enabled != crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012744 "crtc's computed enabled state doesn't match tracked enabled state "
Matt Roper83d65732015-02-25 13:12:16 -080012745 "(expected %i, found %i)\n", enabled,
12746 crtc->base.state->enable);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012747
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012748 active = dev_priv->display.get_pipe_config(crtc,
12749 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +020012750
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030012751 /* hw state is inconsistent with the pipe quirk */
12752 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12753 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetterd62cf622013-05-29 10:41:29 +020012754 active = crtc->active;
12755
Damien Lespiaub2784e12014-08-05 11:29:37 +010012756 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +030012757 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +020012758 if (encoder->base.crtc != &crtc->base)
12759 continue;
Daniel Vetter1d37b682013-11-18 09:00:59 +010012760 if (encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +020012761 encoder->get_config(encoder, &pipe_config);
12762 }
12763
Rob Clarke2c719b2014-12-15 13:56:32 -050012764 I915_STATE_WARN(crtc->active != active,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012765 "crtc active state doesn't match with hw state "
12766 "(expected %i, found %i)\n", crtc->active, active);
12767
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020012768 I915_STATE_WARN(crtc->active != crtc->base.state->active,
12769 "transitional active state does not match atomic hw state "
12770 "(expected %i, found %i)\n", crtc->base.state->active, crtc->active);
12771
Daniel Vetterc0b03412013-05-28 12:05:54 +020012772 if (active &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012773 !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050012774 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Daniel Vetterc0b03412013-05-28 12:05:54 +020012775 intel_dump_pipe_config(crtc, &pipe_config,
12776 "[hw state]");
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012777 intel_dump_pipe_config(crtc, crtc->config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012778 "[sw state]");
12779 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012780 }
12781}
12782
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012783static void
12784check_shared_dpll_state(struct drm_device *dev)
12785{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012786 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012787 struct intel_crtc *crtc;
12788 struct intel_dpll_hw_state dpll_hw_state;
12789 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020012790
12791 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12792 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12793 int enabled_crtcs = 0, active_crtcs = 0;
12794 bool active;
12795
12796 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12797
12798 DRM_DEBUG_KMS("%s\n", pll->name);
12799
12800 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
12801
Rob Clarke2c719b2014-12-15 13:56:32 -050012802 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
Daniel Vetter53589012013-06-05 13:34:16 +020012803 "more active pll users than references: %i vs %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012804 pll->active, hweight32(pll->config.crtc_mask));
Rob Clarke2c719b2014-12-15 13:56:32 -050012805 I915_STATE_WARN(pll->active && !pll->on,
Daniel Vetter53589012013-06-05 13:34:16 +020012806 "pll in active use but not on in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012807 I915_STATE_WARN(pll->on && !pll->active,
Daniel Vetter35c95372013-07-17 06:55:04 +020012808 "pll in on but not on in use in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012809 I915_STATE_WARN(pll->on != active,
Daniel Vetter53589012013-06-05 13:34:16 +020012810 "pll on state mismatch (expected %i, found %i)\n",
12811 pll->on, active);
12812
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012813 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080012814 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
Daniel Vetter53589012013-06-05 13:34:16 +020012815 enabled_crtcs++;
12816 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12817 active_crtcs++;
12818 }
Rob Clarke2c719b2014-12-15 13:56:32 -050012819 I915_STATE_WARN(pll->active != active_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012820 "pll active crtcs mismatch (expected %i, found %i)\n",
12821 pll->active, active_crtcs);
Rob Clarke2c719b2014-12-15 13:56:32 -050012822 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012823 "pll enabled crtcs mismatch (expected %i, found %i)\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012824 hweight32(pll->config.crtc_mask), enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012825
Rob Clarke2c719b2014-12-15 13:56:32 -050012826 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020012827 sizeof(dpll_hw_state)),
12828 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020012829 }
Daniel Vettere2e1ed42012-07-08 21:14:38 +020012830}
12831
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012832void
12833intel_modeset_check_state(struct drm_device *dev)
12834{
Damien Lespiau08db6652014-11-04 17:06:52 +000012835 check_wm_state(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012836 check_connector_state(dev);
12837 check_encoder_state(dev);
12838 check_crtc_state(dev);
12839 check_shared_dpll_state(dev);
12840}
12841
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012842void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
Ville Syrjälä18442d02013-09-13 16:00:08 +030012843 int dotclock)
12844{
12845 /*
12846 * FDI already provided one idea for the dotclock.
12847 * Yell if the encoder disagrees.
12848 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012849 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030012850 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012851 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030012852}
12853
Ville Syrjälä80715b22014-05-15 20:23:23 +030012854static void update_scanline_offset(struct intel_crtc *crtc)
12855{
12856 struct drm_device *dev = crtc->base.dev;
12857
12858 /*
12859 * The scanline counter increments at the leading edge of hsync.
12860 *
12861 * On most platforms it starts counting from vtotal-1 on the
12862 * first active line. That means the scanline counter value is
12863 * always one less than what we would expect. Ie. just after
12864 * start of vblank, which also occurs at start of hsync (on the
12865 * last active line), the scanline counter will read vblank_start-1.
12866 *
12867 * On gen2 the scanline counter starts counting from 1 instead
12868 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12869 * to keep the value positive), instead of adding one.
12870 *
12871 * On HSW+ the behaviour of the scanline counter depends on the output
12872 * type. For DP ports it behaves like most other platforms, but on HDMI
12873 * there's an extra 1 line difference. So we need to add two instead of
12874 * one to the value.
12875 */
12876 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012877 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012878 int vtotal;
12879
12880 vtotal = mode->crtc_vtotal;
12881 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
12882 vtotal /= 2;
12883
12884 crtc->scanline_offset = vtotal - 1;
12885 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030012886 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030012887 crtc->scanline_offset = 2;
12888 } else
12889 crtc->scanline_offset = 1;
12890}
12891
Maarten Lankhorstad421372015-06-15 12:33:42 +020012892static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012893{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012894 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012895 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstad421372015-06-15 12:33:42 +020012896 struct intel_shared_dpll_config *shared_dpll = NULL;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012897 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012898 struct intel_crtc_state *intel_crtc_state;
12899 struct drm_crtc *crtc;
12900 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012901 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012902
12903 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020012904 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012905
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012906 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020012907 int dpll;
12908
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012909 intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012910 intel_crtc_state = to_intel_crtc_state(crtc_state);
Maarten Lankhorstad421372015-06-15 12:33:42 +020012911 dpll = intel_crtc_state->shared_dpll;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012912
Maarten Lankhorstad421372015-06-15 12:33:42 +020012913 if (!needs_modeset(crtc_state) || dpll == DPLL_ID_PRIVATE)
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012914 continue;
12915
Maarten Lankhorstad421372015-06-15 12:33:42 +020012916 intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012917
Maarten Lankhorstad421372015-06-15 12:33:42 +020012918 if (!shared_dpll)
12919 shared_dpll = intel_atomic_get_shared_dpll_state(state);
12920
12921 shared_dpll[dpll].crtc_mask &= ~(1 << intel_crtc->pipe);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012922 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012923}
12924
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020012925/*
12926 * This implements the workaround described in the "notes" section of the mode
12927 * set sequence documentation. When going from no pipes or single pipe to
12928 * multiple pipes, and planes are enabled after the pipe, we need to wait at
12929 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
12930 */
12931static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
12932{
12933 struct drm_crtc_state *crtc_state;
12934 struct intel_crtc *intel_crtc;
12935 struct drm_crtc *crtc;
12936 struct intel_crtc_state *first_crtc_state = NULL;
12937 struct intel_crtc_state *other_crtc_state = NULL;
12938 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
12939 int i;
12940
12941 /* look at all crtc's that are going to be enabled in during modeset */
12942 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12943 intel_crtc = to_intel_crtc(crtc);
12944
12945 if (!crtc_state->active || !needs_modeset(crtc_state))
12946 continue;
12947
12948 if (first_crtc_state) {
12949 other_crtc_state = to_intel_crtc_state(crtc_state);
12950 break;
12951 } else {
12952 first_crtc_state = to_intel_crtc_state(crtc_state);
12953 first_pipe = intel_crtc->pipe;
12954 }
12955 }
12956
12957 /* No workaround needed? */
12958 if (!first_crtc_state)
12959 return 0;
12960
12961 /* w/a possibly needed, check how many crtc's are already enabled. */
12962 for_each_intel_crtc(state->dev, intel_crtc) {
12963 struct intel_crtc_state *pipe_config;
12964
12965 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
12966 if (IS_ERR(pipe_config))
12967 return PTR_ERR(pipe_config);
12968
12969 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
12970
12971 if (!pipe_config->base.active ||
12972 needs_modeset(&pipe_config->base))
12973 continue;
12974
12975 /* 2 or more enabled crtcs means no need for w/a */
12976 if (enabled_pipe != INVALID_PIPE)
12977 return 0;
12978
12979 enabled_pipe = intel_crtc->pipe;
12980 }
12981
12982 if (enabled_pipe != INVALID_PIPE)
12983 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
12984 else if (other_crtc_state)
12985 other_crtc_state->hsw_workaround_pipe = first_pipe;
12986
12987 return 0;
12988}
12989
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012990static int intel_modeset_all_pipes(struct drm_atomic_state *state)
12991{
12992 struct drm_crtc *crtc;
12993 struct drm_crtc_state *crtc_state;
12994 int ret = 0;
12995
12996 /* add all active pipes to the state */
12997 for_each_crtc(state->dev, crtc) {
12998 crtc_state = drm_atomic_get_crtc_state(state, crtc);
12999 if (IS_ERR(crtc_state))
13000 return PTR_ERR(crtc_state);
13001
13002 if (!crtc_state->active || needs_modeset(crtc_state))
13003 continue;
13004
13005 crtc_state->mode_changed = true;
13006
13007 ret = drm_atomic_add_affected_connectors(state, crtc);
13008 if (ret)
13009 break;
13010
13011 ret = drm_atomic_add_affected_planes(state, crtc);
13012 if (ret)
13013 break;
13014 }
13015
13016 return ret;
13017}
13018
13019
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013020/* Code that should eventually be part of atomic_check() */
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013021static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013022{
13023 struct drm_device *dev = state->dev;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013024 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013025 int ret;
13026
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013027 if (!check_digital_port_conflicts(state)) {
13028 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13029 return -EINVAL;
13030 }
13031
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013032 /*
13033 * See if the config requires any additional preparation, e.g.
13034 * to adjust global state with pipes off. We need to do this
13035 * here so we can get the modeset_pipe updated config for the new
13036 * mode set on this crtc. For other crtcs we need to use the
13037 * adjusted_mode bits in the crtc directly.
13038 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013039 if (dev_priv->display.modeset_calc_cdclk) {
13040 unsigned int cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030013041
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013042 ret = dev_priv->display.modeset_calc_cdclk(state);
13043
13044 cdclk = to_intel_atomic_state(state)->cdclk;
13045 if (!ret && cdclk != dev_priv->cdclk_freq)
13046 ret = intel_modeset_all_pipes(state);
13047
13048 if (ret < 0)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013049 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013050 } else
13051 to_intel_atomic_state(state)->cdclk = dev_priv->cdclk_freq;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013052
Maarten Lankhorstad421372015-06-15 12:33:42 +020013053 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013054
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013055 if (IS_HASWELL(dev))
Maarten Lankhorstad421372015-06-15 12:33:42 +020013056 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013057
Maarten Lankhorstad421372015-06-15 12:33:42 +020013058 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013059}
13060
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013061static int
13062intel_modeset_compute_config(struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013063{
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013064 struct drm_crtc *crtc;
13065 struct drm_crtc_state *crtc_state;
13066 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013067 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013068
13069 ret = drm_atomic_helper_check_modeset(state->dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013070 if (ret)
13071 return ret;
13072
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013073 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013074 if (!crtc_state->enable) {
13075 if (needs_modeset(crtc_state))
13076 any_ms = true;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013077 continue;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013078 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013079
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020013080 if (to_intel_crtc_state(crtc_state)->quirks &
13081 PIPE_CONFIG_QUIRK_INITIAL_PLANES) {
13082 ret = drm_atomic_add_affected_planes(state, crtc);
13083 if (ret)
13084 return ret;
13085
13086 /*
13087 * We ought to handle i915.fastboot here.
13088 * If no modeset is required and the primary plane has
13089 * a fb, update the members of crtc_state as needed,
13090 * and run the necessary updates during vblank evasion.
13091 */
13092 }
13093
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013094 if (!needs_modeset(crtc_state)) {
13095 ret = drm_atomic_add_affected_connectors(state, crtc);
13096 if (ret)
13097 return ret;
13098 }
13099
13100 ret = intel_modeset_pipe_config(crtc,
13101 to_intel_crtc_state(crtc_state));
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013102 if (ret)
13103 return ret;
13104
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013105 if (needs_modeset(crtc_state))
13106 any_ms = true;
13107
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013108 intel_dump_pipe_config(to_intel_crtc(crtc),
13109 to_intel_crtc_state(crtc_state),
13110 "[modeset]");
13111 }
13112
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013113 if (any_ms) {
13114 ret = intel_modeset_checks(state);
13115
13116 if (ret)
13117 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013118 } else
13119 to_intel_atomic_state(state)->cdclk =
13120 to_i915(state->dev)->cdclk_freq;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013121
13122 return drm_atomic_helper_check_planes(state->dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013123}
13124
Ander Conselvan de Oliveirac72d9692015-06-01 12:49:50 +020013125static int __intel_set_mode(struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013126{
Ander Conselvan de Oliveirac72d9692015-06-01 12:49:50 +020013127 struct drm_device *dev = state->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030013128 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013129 struct drm_crtc *crtc;
13130 struct drm_crtc_state *crtc_state;
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013131 int ret = 0;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013132 int i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013133 bool any_ms = false;
Daniel Vettera6778b32012-07-02 09:56:42 +020013134
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013135 ret = drm_atomic_helper_prepare_planes(dev, state);
13136 if (ret)
13137 return ret;
13138
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013139 drm_atomic_helper_swap_state(dev, state);
13140
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013141 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013142 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13143
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013144 if (!needs_modeset(crtc->state))
13145 continue;
13146
13147 any_ms = true;
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013148 intel_pre_plane_update(intel_crtc);
Daniel Vetter460da9162013-03-27 00:44:51 +010013149
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013150 if (crtc_state->active) {
13151 intel_crtc_disable_planes(crtc, crtc_state->plane_mask);
13152 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013153 intel_crtc->active = false;
13154 intel_disable_shared_dpll(intel_crtc);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013155 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010013156 }
Daniel Vetter7758a112012-07-08 19:40:39 +020013157
Daniel Vetterea9d7582012-07-10 10:42:52 +020013158 /* Only after disabling all output pipelines that will be changed can we
13159 * update the the output configuration. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013160 intel_modeset_update_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020013161
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030013162 /* The state has been swaped above, so state actually contains the
13163 * old state now. */
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013164 if (any_ms)
13165 modeset_update_crtc_power_domains(state);
Daniel Vetter47fab732012-10-26 10:58:18 +020013166
Daniel Vettera6778b32012-07-02 09:56:42 +020013167 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013168 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013169 if (needs_modeset(crtc->state) && crtc->state->active) {
13170 update_scanline_offset(to_intel_crtc(crtc));
13171 dev_priv->display.crtc_enable(crtc);
13172 }
13173
Maarten Lankhorst5ac1c4b2015-06-01 12:50:01 +020013174 drm_atomic_helper_commit_planes_on_crtc(crtc_state);
Ville Syrjälä80715b22014-05-15 20:23:23 +030013175 }
Daniel Vettera6778b32012-07-02 09:56:42 +020013176
Daniel Vettera6778b32012-07-02 09:56:42 +020013177 /* FIXME: add subpixel order */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013178
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013179 drm_atomic_helper_cleanup_planes(dev, state);
13180
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013181 drm_atomic_state_free(state);
13182
Ander Conselvan de Oliveira9eb45f22015-04-21 17:13:07 +030013183 return 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020013184}
13185
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013186static int intel_set_mode_checked(struct drm_atomic_state *state)
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013187{
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013188 struct drm_device *dev = state->dev;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013189 int ret;
13190
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013191 ret = __intel_set_mode(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013192 if (ret == 0)
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013193 intel_modeset_check_state(dev);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013194
13195 return ret;
13196}
13197
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013198static int intel_set_mode(struct drm_atomic_state *state)
Daniel Vetterf30da182013-04-11 20:22:50 +020013199{
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013200 int ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020013201
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013202 ret = intel_modeset_compute_config(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013203 if (ret)
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013204 return ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013205
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013206 return intel_set_mode_checked(state);
Daniel Vetterf30da182013-04-11 20:22:50 +020013207}
13208
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013209void intel_crtc_restore_mode(struct drm_crtc *crtc)
13210{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013211 struct drm_device *dev = crtc->dev;
13212 struct drm_atomic_state *state;
13213 struct intel_encoder *encoder;
13214 struct intel_connector *connector;
13215 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013216 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013217 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013218
13219 state = drm_atomic_state_alloc(dev);
13220 if (!state) {
13221 DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
13222 crtc->base.id);
13223 return;
13224 }
13225
13226 state->acquire_ctx = dev->mode_config.acquire_ctx;
13227
13228 /* The force restore path in the HW readout code relies on the staged
13229 * config still keeping the user requested config while the actual
13230 * state has been overwritten by the configuration read from HW. We
13231 * need to copy the staged config to the atomic state, otherwise the
13232 * mode set will just reapply the state the HW is already in. */
13233 for_each_intel_encoder(dev, encoder) {
13234 if (&encoder->new_crtc->base != crtc)
13235 continue;
13236
13237 for_each_intel_connector(dev, connector) {
13238 if (connector->new_encoder != encoder)
13239 continue;
13240
13241 connector_state = drm_atomic_get_connector_state(state, &connector->base);
13242 if (IS_ERR(connector_state)) {
13243 DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
13244 connector->base.base.id,
13245 connector->base.name,
13246 PTR_ERR(connector_state));
13247 continue;
13248 }
13249
13250 connector_state->crtc = crtc;
13251 connector_state->best_encoder = &encoder->base;
13252 }
13253 }
13254
Ander Conselvan de Oliveira4ed9fb32015-06-16 11:49:45 +030013255 crtc_state = intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));
13256 if (IS_ERR(crtc_state)) {
13257 DRM_DEBUG_KMS("Failed to add [CRTC:%d] to state: %ld\n",
13258 crtc->base.id, PTR_ERR(crtc_state));
13259 drm_atomic_state_free(state);
13260 return;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013261 }
13262
Ander Conselvan de Oliveira4ed9fb32015-06-16 11:49:45 +030013263 crtc_state->base.active = crtc_state->base.enable =
13264 to_intel_crtc(crtc)->new_enabled;
13265
13266 drm_mode_copy(&crtc_state->base.mode, &crtc->mode);
13267
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030013268 intel_modeset_setup_plane_state(state, crtc, &crtc->mode,
13269 crtc->primary->fb, crtc->x, crtc->y);
13270
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013271 ret = intel_set_mode(state);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013272 if (ret)
13273 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013274}
13275
Daniel Vetter25c5b262012-07-08 22:08:04 +020013276#undef for_each_intel_crtc_masked
13277
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013278static bool intel_connector_in_mode_set(struct intel_connector *connector,
13279 struct drm_mode_set *set)
13280{
13281 int ro;
13282
13283 for (ro = 0; ro < set->num_connectors; ro++)
13284 if (set->connectors[ro] == &connector->base)
13285 return true;
13286
13287 return false;
13288}
13289
Daniel Vetter2e431052012-07-04 22:42:15 +020013290static int
Daniel Vetter9a935852012-07-05 22:34:27 +020013291intel_modeset_stage_output_state(struct drm_device *dev,
13292 struct drm_mode_set *set,
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020013293 struct drm_atomic_state *state)
Daniel Vetter50f56112012-07-02 09:35:43 +020013294{
Daniel Vetter9a935852012-07-05 22:34:27 +020013295 struct intel_connector *connector;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013296 struct drm_connector *drm_connector;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020013297 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013298 struct drm_crtc *crtc;
13299 struct drm_crtc_state *crtc_state;
13300 int i, ret;
Daniel Vetter50f56112012-07-02 09:35:43 +020013301
Damien Lespiau9abdda72013-02-13 13:29:23 +000013302 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +020013303 * of connectors. For paranoia, double-check this. */
13304 WARN_ON(!set->fb && (set->num_connectors != 0));
13305 WARN_ON(set->fb && (set->num_connectors == 0));
13306
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020013307 for_each_intel_connector(dev, connector) {
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013308 bool in_mode_set = intel_connector_in_mode_set(connector, set);
13309
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013310 if (!in_mode_set && connector->base.state->crtc != set->crtc)
13311 continue;
13312
13313 connector_state =
13314 drm_atomic_get_connector_state(state, &connector->base);
13315 if (IS_ERR(connector_state))
13316 return PTR_ERR(connector_state);
13317
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013318 if (in_mode_set) {
13319 int pipe = to_intel_crtc(set->crtc)->pipe;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013320 connector_state->best_encoder =
13321 &intel_find_encoder(connector, pipe)->base;
Daniel Vetter50f56112012-07-02 09:35:43 +020013322 }
13323
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013324 if (connector->base.state->crtc != set->crtc)
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013325 continue;
13326
Daniel Vetter9a935852012-07-05 22:34:27 +020013327 /* If we disable the crtc, disable all its connectors. Also, if
13328 * the connector is on the changing crtc but not on the new
13329 * connector list, disable it. */
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013330 if (!set->fb || !in_mode_set) {
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013331 connector_state->best_encoder = NULL;
Daniel Vetter9a935852012-07-05 22:34:27 +020013332
13333 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
13334 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030013335 connector->base.name);
Daniel Vetter9a935852012-07-05 22:34:27 +020013336 }
Daniel Vetter9a935852012-07-05 22:34:27 +020013337 }
13338 /* connector->new_encoder is now updated for all connectors. */
13339
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013340 for_each_connector_in_state(state, drm_connector, connector_state, i) {
13341 connector = to_intel_connector(drm_connector);
Ville Syrjälä76688512014-01-10 11:28:06 +020013342
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013343 if (!connector_state->best_encoder) {
13344 ret = drm_atomic_set_crtc_for_connector(connector_state,
13345 NULL);
13346 if (ret)
13347 return ret;
13348
Daniel Vetter50f56112012-07-02 09:35:43 +020013349 continue;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013350 }
Daniel Vetter50f56112012-07-02 09:35:43 +020013351
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013352 if (intel_connector_in_mode_set(connector, set)) {
13353 struct drm_crtc *crtc = connector->base.state->crtc;
13354
13355 /* If this connector was in a previous crtc, add it
13356 * to the state. We might need to disable it. */
13357 if (crtc) {
13358 crtc_state =
13359 drm_atomic_get_crtc_state(state, crtc);
13360 if (IS_ERR(crtc_state))
13361 return PTR_ERR(crtc_state);
13362 }
13363
13364 ret = drm_atomic_set_crtc_for_connector(connector_state,
13365 set->crtc);
13366 if (ret)
13367 return ret;
13368 }
Daniel Vetter50f56112012-07-02 09:35:43 +020013369
13370 /* Make sure the new CRTC will work with the encoder */
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013371 if (!drm_encoder_crtc_ok(connector_state->best_encoder,
13372 connector_state->crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020013373 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +020013374 }
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020013375
Daniel Vetter9a935852012-07-05 22:34:27 +020013376 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
13377 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030013378 connector->base.name,
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013379 connector_state->crtc->base.id);
13380
13381 if (connector_state->best_encoder != &connector->encoder->base)
13382 connector->encoder =
13383 to_intel_encoder(connector_state->best_encoder);
Daniel Vetter9a935852012-07-05 22:34:27 +020013384 }
13385
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013386 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020013387 bool has_connectors;
13388
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013389 ret = drm_atomic_add_affected_connectors(state, crtc);
13390 if (ret)
13391 return ret;
Paulo Zanoni5a65f352014-01-07 14:55:53 -020013392
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020013393 has_connectors = !!drm_atomic_connectors_for_crtc(state, crtc);
13394 if (has_connectors != crtc_state->enable)
13395 crtc_state->enable =
13396 crtc_state->active = has_connectors;
Ville Syrjälä76688512014-01-10 11:28:06 +020013397 }
13398
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030013399 ret = intel_modeset_setup_plane_state(state, set->crtc, set->mode,
13400 set->fb, set->x, set->y);
13401 if (ret)
13402 return ret;
13403
13404 crtc_state = drm_atomic_get_crtc_state(state, set->crtc);
13405 if (IS_ERR(crtc_state))
13406 return PTR_ERR(crtc_state);
13407
Matt Roperce522992015-06-05 15:08:24 -070013408 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
13409 if (ret)
13410 return ret;
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030013411
13412 if (set->num_connectors)
13413 crtc_state->active = true;
13414
Daniel Vetter2e431052012-07-04 22:42:15 +020013415 return 0;
13416}
13417
13418static int intel_crtc_set_config(struct drm_mode_set *set)
13419{
13420 struct drm_device *dev;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013421 struct drm_atomic_state *state = NULL;
Daniel Vetter2e431052012-07-04 22:42:15 +020013422 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +020013423
Daniel Vetter8d3e3752012-07-05 16:09:09 +020013424 BUG_ON(!set);
13425 BUG_ON(!set->crtc);
13426 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +020013427
Daniel Vetter7e53f3a2013-01-21 10:52:17 +010013428 /* Enforce sane interface api - has been abused by the fb helper. */
13429 BUG_ON(!set->mode && set->fb);
13430 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +020013431
Daniel Vetter2e431052012-07-04 22:42:15 +020013432 if (set->fb) {
13433 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
13434 set->crtc->base.id, set->fb->base.id,
13435 (int)set->num_connectors, set->x, set->y);
13436 } else {
13437 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +020013438 }
13439
13440 dev = set->crtc->dev;
13441
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013442 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013443 if (!state)
13444 return -ENOMEM;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013445
13446 state->acquire_ctx = dev->mode_config.acquire_ctx;
13447
Ander Conselvan de Oliveira462a4252015-04-21 17:13:00 +030013448 ret = intel_modeset_stage_output_state(dev, set, state);
Daniel Vetter2e431052012-07-04 22:42:15 +020013449 if (ret)
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013450 goto out;
Daniel Vetter2e431052012-07-04 22:42:15 +020013451
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013452 ret = intel_modeset_compute_config(state);
13453 if (ret)
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013454 goto out;
Jesse Barnes50f52752014-11-07 13:11:00 -080013455
Jesse Barnes1f9954d2014-11-05 14:26:10 -080013456 intel_update_pipe_size(to_intel_crtc(set->crtc));
13457
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013458 ret = intel_set_mode_checked(state);
Chris Wilson2d05eae2013-05-03 17:36:25 +010013459 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +020013460 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
13461 set->crtc->base.id, ret);
Chris Wilson2d05eae2013-05-03 17:36:25 +010013462 }
Daniel Vetter50f56112012-07-02 09:35:43 +020013463
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013464out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013465 if (ret)
13466 drm_atomic_state_free(state);
Daniel Vetter50f56112012-07-02 09:35:43 +020013467 return ret;
13468}
13469
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013470static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013471 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +020013472 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013473 .destroy = intel_crtc_destroy,
13474 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013475 .atomic_duplicate_state = intel_crtc_duplicate_state,
13476 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013477};
13478
Daniel Vetter53589012013-06-05 13:34:16 +020013479static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
13480 struct intel_shared_dpll *pll,
13481 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013482{
Daniel Vetter53589012013-06-05 13:34:16 +020013483 uint32_t val;
13484
Daniel Vetterf458ebb2014-09-30 10:56:39 +020013485 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030013486 return false;
13487
Daniel Vetter53589012013-06-05 13:34:16 +020013488 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020013489 hw_state->dpll = val;
13490 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
13491 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020013492
13493 return val & DPLL_VCO_ENABLE;
13494}
13495
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013496static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
13497 struct intel_shared_dpll *pll)
13498{
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013499 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
13500 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013501}
13502
Daniel Vettere7b903d2013-06-05 13:34:14 +020013503static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
13504 struct intel_shared_dpll *pll)
13505{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013506 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020013507 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020013508
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013509 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013510
13511 /* Wait for the clocks to stabilize. */
13512 POSTING_READ(PCH_DPLL(pll->id));
13513 udelay(150);
13514
13515 /* The pixel multiplier can only be updated once the
13516 * DPLL is enabled and the clocks are stable.
13517 *
13518 * So write it again.
13519 */
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013520 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013521 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013522 udelay(200);
13523}
13524
13525static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
13526 struct intel_shared_dpll *pll)
13527{
13528 struct drm_device *dev = dev_priv->dev;
13529 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013530
13531 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013532 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020013533 if (intel_crtc_to_shared_dpll(crtc) == pll)
13534 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
13535 }
13536
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013537 I915_WRITE(PCH_DPLL(pll->id), 0);
13538 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013539 udelay(200);
13540}
13541
Daniel Vetter46edb022013-06-05 13:34:12 +020013542static char *ibx_pch_dpll_names[] = {
13543 "PCH DPLL A",
13544 "PCH DPLL B",
13545};
13546
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013547static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013548{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013549 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013550 int i;
13551
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013552 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013553
Daniel Vettere72f9fb2013-06-05 13:34:06 +020013554 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020013555 dev_priv->shared_dplls[i].id = i;
13556 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013557 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013558 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
13559 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020013560 dev_priv->shared_dplls[i].get_hw_state =
13561 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013562 }
13563}
13564
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013565static void intel_shared_dpll_init(struct drm_device *dev)
13566{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013567 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013568
Ville Syrjäläb6283052015-06-03 15:45:07 +030013569 intel_update_cdclk(dev);
13570
Daniel Vetter9cd86932014-06-25 22:01:57 +030013571 if (HAS_DDI(dev))
13572 intel_ddi_pll_init(dev);
13573 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013574 ibx_pch_dpll_init(dev);
13575 else
13576 dev_priv->num_shared_dpll = 0;
13577
13578 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013579}
13580
Matt Roper6beb8c232014-12-01 15:40:14 -080013581/**
13582 * intel_prepare_plane_fb - Prepare fb for usage on plane
13583 * @plane: drm plane to prepare for
13584 * @fb: framebuffer to prepare for presentation
13585 *
13586 * Prepares a framebuffer for usage on a display plane. Generally this
13587 * involves pinning the underlying object and updating the frontbuffer tracking
13588 * bits. Some older platforms need special physical address handling for
13589 * cursor planes.
13590 *
13591 * Returns 0 on success, negative error code on failure.
13592 */
13593int
13594intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013595 struct drm_framebuffer *fb,
13596 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013597{
13598 struct drm_device *dev = plane->dev;
Matt Roper6beb8c232014-12-01 15:40:14 -080013599 struct intel_plane *intel_plane = to_intel_plane(plane);
13600 enum pipe pipe = intel_plane->pipe;
13601 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13602 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
13603 unsigned frontbuffer_bits = 0;
13604 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013605
Matt Roperea2c67b2014-12-23 10:41:52 -080013606 if (!obj)
Matt Roper465c1202014-05-29 08:06:54 -070013607 return 0;
13608
Matt Roper6beb8c232014-12-01 15:40:14 -080013609 switch (plane->type) {
13610 case DRM_PLANE_TYPE_PRIMARY:
13611 frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
13612 break;
13613 case DRM_PLANE_TYPE_CURSOR:
13614 frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
13615 break;
13616 case DRM_PLANE_TYPE_OVERLAY:
13617 frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
13618 break;
13619 }
Matt Roper465c1202014-05-29 08:06:54 -070013620
Matt Roper4c345742014-07-09 16:22:10 -070013621 mutex_lock(&dev->struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070013622
Matt Roper6beb8c232014-12-01 15:40:14 -080013623 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13624 INTEL_INFO(dev)->cursor_needs_physical) {
13625 int align = IS_I830(dev) ? 16 * 1024 : 256;
13626 ret = i915_gem_object_attach_phys(obj, align);
13627 if (ret)
13628 DRM_DEBUG_KMS("failed to attach phys object\n");
13629 } else {
John Harrison91af1272015-06-18 13:14:56 +010013630 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL, NULL);
Matt Roper6beb8c232014-12-01 15:40:14 -080013631 }
13632
13633 if (ret == 0)
13634 i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
13635
13636 mutex_unlock(&dev->struct_mutex);
13637
13638 return ret;
13639}
13640
Matt Roper38f3ce32014-12-02 07:45:25 -080013641/**
13642 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13643 * @plane: drm plane to clean up for
13644 * @fb: old framebuffer that was on plane
13645 *
13646 * Cleans up a framebuffer that has just been removed from a plane.
13647 */
13648void
13649intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013650 struct drm_framebuffer *fb,
13651 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013652{
13653 struct drm_device *dev = plane->dev;
13654 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13655
13656 if (WARN_ON(!obj))
13657 return;
13658
13659 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
13660 !INTEL_INFO(dev)->cursor_needs_physical) {
13661 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000013662 intel_unpin_fb_obj(fb, old_state);
Matt Roper38f3ce32014-12-02 07:45:25 -080013663 mutex_unlock(&dev->struct_mutex);
13664 }
Matt Roper465c1202014-05-29 08:06:54 -070013665}
13666
Chandra Konduru6156a452015-04-27 13:48:39 -070013667int
13668skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13669{
13670 int max_scale;
13671 struct drm_device *dev;
13672 struct drm_i915_private *dev_priv;
13673 int crtc_clock, cdclk;
13674
13675 if (!intel_crtc || !crtc_state)
13676 return DRM_PLANE_HELPER_NO_SCALING;
13677
13678 dev = intel_crtc->base.dev;
13679 dev_priv = dev->dev_private;
13680 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013681 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
Chandra Konduru6156a452015-04-27 13:48:39 -070013682
13683 if (!crtc_clock || !cdclk)
13684 return DRM_PLANE_HELPER_NO_SCALING;
13685
13686 /*
13687 * skl max scale is lower of:
13688 * close to 3 but not 3, -1 is for that purpose
13689 * or
13690 * cdclk/crtc_clock
13691 */
13692 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13693
13694 return max_scale;
13695}
13696
Matt Roper465c1202014-05-29 08:06:54 -070013697static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013698intel_check_primary_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013699 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013700 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013701{
Matt Roper2b875c22014-12-01 15:40:13 -080013702 struct drm_crtc *crtc = state->base.crtc;
13703 struct drm_framebuffer *fb = state->base.fb;
Chandra Konduru6156a452015-04-27 13:48:39 -070013704 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013705 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13706 bool can_position = false;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013707
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013708 /* use scaler when colorkey is not required */
13709 if (INTEL_INFO(plane->dev)->gen >= 9 &&
Maarten Lankhorst818ed962015-06-15 12:33:54 +020013710 state->ckey.flags == I915_SET_COLORKEY_NONE) {
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013711 min_scale = 1;
13712 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
Sonika Jindald8106362015-04-10 14:37:28 +053013713 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070013714 }
Sonika Jindald8106362015-04-10 14:37:28 +053013715
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013716 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13717 &state->dst, &state->clip,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013718 min_scale, max_scale,
13719 can_position, true,
13720 &state->visible);
Matt Roper465c1202014-05-29 08:06:54 -070013721}
13722
Gustavo Padovan14af2932014-10-24 14:51:31 +010013723static void
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013724intel_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;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013732 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;
Matt Roper9dc806f2014-11-17 18:10:38 -080013738 crtc->x = src->x1 >> 16;
13739 crtc->y = src->y1 >> 16;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013740
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013741 if (!crtc->state->active)
Maarten Lankhorst302d19a2015-06-15 12:33:45 +020013742 return;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013743
Maarten Lankhorst302d19a2015-06-15 12:33:45 +020013744 if (state->visible)
13745 /* FIXME: kill this fastboot hack */
13746 intel_update_pipe_size(intel_crtc);
13747
13748 dev_priv->display.update_primary_plane(crtc, fb, crtc->x, crtc->y);
Matt Roper32b7eee2014-12-24 07:59:06 -080013749}
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,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020013753 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013754{
13755 struct drm_device *dev = plane->dev;
13756 struct drm_i915_private *dev_priv = dev->dev_private;
13757
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013758 dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
13759}
13760
Matt Roper32b7eee2014-12-24 07:59:06 -080013761static void intel_begin_crtc_commit(struct drm_crtc *crtc)
13762{
13763 struct drm_device *dev = crtc->dev;
13764 struct drm_i915_private *dev_priv = dev->dev_private;
13765 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013766
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013767 if (!needs_modeset(crtc->state))
13768 intel_pre_plane_update(intel_crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013769
13770 if (intel_crtc->atomic.update_wm)
13771 intel_update_watermarks(crtc);
13772
13773 intel_runtime_pm_get(dev_priv);
Matt Roperc34c9ee2014-12-23 10:41:50 -080013774
13775 /* Perform vblank evasion around commit operation */
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013776 if (crtc->state->active)
Matt Roperc34c9ee2014-12-23 10:41:50 -080013777 intel_crtc->atomic.evade =
13778 intel_pipe_update_start(intel_crtc,
13779 &intel_crtc->atomic.start_vbl_count);
Maarten Lankhorst05832362015-06-15 12:33:48 +020013780
13781 if (!needs_modeset(crtc->state) && INTEL_INFO(dev)->gen >= 9)
13782 skl_detach_scalers(intel_crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013783}
13784
13785static void intel_finish_crtc_commit(struct drm_crtc *crtc)
13786{
13787 struct drm_device *dev = crtc->dev;
13788 struct drm_i915_private *dev_priv = dev->dev_private;
13789 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013790
Matt Roperc34c9ee2014-12-23 10:41:50 -080013791 if (intel_crtc->atomic.evade)
13792 intel_pipe_update_end(intel_crtc,
13793 intel_crtc->atomic.start_vbl_count);
13794
Matt Roper32b7eee2014-12-24 07:59:06 -080013795 intel_runtime_pm_put(dev_priv);
13796
Maarten Lankhorstac21b222015-06-15 12:33:49 +020013797 intel_post_plane_update(intel_crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013798}
13799
Matt Ropercf4c7c12014-12-04 10:27:42 -080013800/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013801 * intel_plane_destroy - destroy a plane
13802 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013803 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013804 * Common destruction function for all types of planes (primary, cursor,
13805 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013806 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013807void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013808{
13809 struct intel_plane *intel_plane = to_intel_plane(plane);
13810 drm_plane_cleanup(plane);
13811 kfree(intel_plane);
13812}
13813
Matt Roper65a3fea2015-01-21 16:35:42 -080013814const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013815 .update_plane = drm_atomic_helper_update_plane,
13816 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013817 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080013818 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080013819 .atomic_get_property = intel_plane_atomic_get_property,
13820 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013821 .atomic_duplicate_state = intel_plane_duplicate_state,
13822 .atomic_destroy_state = intel_plane_destroy_state,
13823
Matt Roper465c1202014-05-29 08:06:54 -070013824};
13825
13826static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13827 int pipe)
13828{
13829 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080013830 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070013831 const uint32_t *intel_primary_formats;
13832 int num_formats;
13833
13834 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13835 if (primary == NULL)
13836 return NULL;
13837
Matt Roper8e7d6882015-01-21 16:35:41 -080013838 state = intel_create_plane_state(&primary->base);
13839 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013840 kfree(primary);
13841 return NULL;
13842 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013843 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013844
Matt Roper465c1202014-05-29 08:06:54 -070013845 primary->can_scale = false;
13846 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013847 if (INTEL_INFO(dev)->gen >= 9) {
13848 primary->can_scale = true;
Chandra Konduruaf99ceda2015-05-11 14:35:47 -070013849 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013850 }
Matt Roper465c1202014-05-29 08:06:54 -070013851 primary->pipe = pipe;
13852 primary->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080013853 primary->check_plane = intel_check_primary_plane;
13854 primary->commit_plane = intel_commit_primary_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013855 primary->disable_plane = intel_disable_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070013856 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13857 primary->plane = !pipe;
13858
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013859 if (INTEL_INFO(dev)->gen >= 9) {
13860 intel_primary_formats = skl_primary_formats;
13861 num_formats = ARRAY_SIZE(skl_primary_formats);
13862 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010013863 intel_primary_formats = i965_primary_formats;
13864 num_formats = ARRAY_SIZE(i965_primary_formats);
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013865 } else {
13866 intel_primary_formats = i8xx_primary_formats;
13867 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Matt Roper465c1202014-05-29 08:06:54 -070013868 }
13869
13870 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013871 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070013872 intel_primary_formats, num_formats,
13873 DRM_PLANE_TYPE_PRIMARY);
Sonika Jindal48404c12014-08-22 14:06:04 +053013874
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013875 if (INTEL_INFO(dev)->gen >= 4)
13876 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053013877
Matt Roperea2c67b2014-12-23 10:41:52 -080013878 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13879
Matt Roper465c1202014-05-29 08:06:54 -070013880 return &primary->base;
13881}
13882
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013883void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13884{
13885 if (!dev->mode_config.rotation_property) {
13886 unsigned long flags = BIT(DRM_ROTATE_0) |
13887 BIT(DRM_ROTATE_180);
13888
13889 if (INTEL_INFO(dev)->gen >= 9)
13890 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13891
13892 dev->mode_config.rotation_property =
13893 drm_mode_create_rotation_property(dev, flags);
13894 }
13895 if (dev->mode_config.rotation_property)
13896 drm_object_attach_property(&plane->base.base,
13897 dev->mode_config.rotation_property,
13898 plane->base.state->rotation);
13899}
13900
Matt Roper3d7d6512014-06-10 08:28:13 -070013901static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030013902intel_check_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013903 struct intel_crtc_state *crtc_state,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013904 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070013905{
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013906 struct drm_crtc *crtc = crtc_state->base.crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080013907 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013908 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013909 unsigned stride;
13910 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013911
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013912 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13913 &state->dst, &state->clip,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013914 DRM_PLANE_HELPER_NO_SCALING,
13915 DRM_PLANE_HELPER_NO_SCALING,
13916 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013917 if (ret)
13918 return ret;
13919
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013920 /* if we want to turn off the cursor ignore width and height */
13921 if (!obj)
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013922 return 0;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013923
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013924 /* Check for which cursor types we support */
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013925 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013926 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13927 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013928 return -EINVAL;
13929 }
13930
Matt Roperea2c67b2014-12-23 10:41:52 -080013931 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13932 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013933 DRM_DEBUG_KMS("buffer is too small\n");
13934 return -ENOMEM;
13935 }
13936
Ville Syrjälä3a656b52015-03-09 21:08:37 +020013937 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013938 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013939 return -EINVAL;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013940 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013941
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013942 return 0;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013943}
13944
Matt Roperf4a2cf22014-12-01 15:40:12 -080013945static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013946intel_disable_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020013947 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013948{
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013949 intel_crtc_update_cursor(crtc, false);
13950}
13951
13952static void
Gustavo Padovan852e7872014-09-05 17:22:31 -030013953intel_commit_cursor_plane(struct drm_plane *plane,
13954 struct intel_plane_state *state)
13955{
Matt Roper2b875c22014-12-01 15:40:13 -080013956 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013957 struct drm_device *dev = plane->dev;
13958 struct intel_crtc *intel_crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080013959 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080013960 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070013961
Matt Roperea2c67b2014-12-23 10:41:52 -080013962 crtc = crtc ? crtc : plane->crtc;
13963 intel_crtc = to_intel_crtc(crtc);
Sonika Jindala919db92014-10-23 07:41:33 -070013964
Matt Roperea2c67b2014-12-23 10:41:52 -080013965 plane->fb = state->base.fb;
13966 crtc->cursor_x = state->base.crtc_x;
13967 crtc->cursor_y = state->base.crtc_y;
13968
Gustavo Padovana912f122014-12-01 15:40:10 -080013969 if (intel_crtc->cursor_bo == obj)
13970 goto update;
13971
Matt Roperf4a2cf22014-12-01 15:40:12 -080013972 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080013973 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080013974 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080013975 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080013976 else
Gustavo Padovana912f122014-12-01 15:40:10 -080013977 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080013978
Gustavo Padovana912f122014-12-01 15:40:10 -080013979 intel_crtc->cursor_addr = addr;
13980 intel_crtc->cursor_bo = obj;
Gustavo Padovana912f122014-12-01 15:40:10 -080013981
Maarten Lankhorst302d19a2015-06-15 12:33:45 +020013982update:
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013983 if (crtc->state->active)
Gustavo Padovan852e7872014-09-05 17:22:31 -030013984 intel_crtc_update_cursor(crtc, state->visible);
Matt Roper3d7d6512014-06-10 08:28:13 -070013985}
Gustavo Padovan852e7872014-09-05 17:22:31 -030013986
Matt Roper3d7d6512014-06-10 08:28:13 -070013987static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
13988 int pipe)
13989{
13990 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080013991 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070013992
13993 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
13994 if (cursor == NULL)
13995 return NULL;
13996
Matt Roper8e7d6882015-01-21 16:35:41 -080013997 state = intel_create_plane_state(&cursor->base);
13998 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013999 kfree(cursor);
14000 return NULL;
14001 }
Matt Roper8e7d6882015-01-21 16:35:41 -080014002 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014003
Matt Roper3d7d6512014-06-10 08:28:13 -070014004 cursor->can_scale = false;
14005 cursor->max_downscale = 1;
14006 cursor->pipe = pipe;
14007 cursor->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080014008 cursor->check_plane = intel_check_cursor_plane;
14009 cursor->commit_plane = intel_commit_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014010 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070014011
14012 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080014013 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070014014 intel_cursor_formats,
14015 ARRAY_SIZE(intel_cursor_formats),
14016 DRM_PLANE_TYPE_CURSOR);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014017
14018 if (INTEL_INFO(dev)->gen >= 4) {
14019 if (!dev->mode_config.rotation_property)
14020 dev->mode_config.rotation_property =
14021 drm_mode_create_rotation_property(dev,
14022 BIT(DRM_ROTATE_0) |
14023 BIT(DRM_ROTATE_180));
14024 if (dev->mode_config.rotation_property)
14025 drm_object_attach_property(&cursor->base.base,
14026 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080014027 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014028 }
14029
Chandra Konduruaf99ceda2015-05-11 14:35:47 -070014030 if (INTEL_INFO(dev)->gen >=9)
14031 state->scaler_id = -1;
14032
Matt Roperea2c67b2014-12-23 10:41:52 -080014033 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14034
Matt Roper3d7d6512014-06-10 08:28:13 -070014035 return &cursor->base;
14036}
14037
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014038static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14039 struct intel_crtc_state *crtc_state)
14040{
14041 int i;
14042 struct intel_scaler *intel_scaler;
14043 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14044
14045 for (i = 0; i < intel_crtc->num_scalers; i++) {
14046 intel_scaler = &scaler_state->scalers[i];
14047 intel_scaler->in_use = 0;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014048 intel_scaler->mode = PS_SCALER_MODE_DYN;
14049 }
14050
14051 scaler_state->scaler_id = -1;
14052}
14053
Hannes Ederb358d0a2008-12-18 21:18:47 +010014054static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080014055{
Jani Nikulafbee40d2014-03-31 14:27:18 +030014056 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080014057 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014058 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014059 struct drm_plane *primary = NULL;
14060 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014061 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014062
Daniel Vetter955382f2013-09-19 14:05:45 +020014063 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080014064 if (intel_crtc == NULL)
14065 return;
14066
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014067 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14068 if (!crtc_state)
14069 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030014070 intel_crtc->config = crtc_state;
14071 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080014072 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014073
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014074 /* initialize shared scalers */
14075 if (INTEL_INFO(dev)->gen >= 9) {
14076 if (pipe == PIPE_C)
14077 intel_crtc->num_scalers = 1;
14078 else
14079 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14080
14081 skl_init_scalers(dev, intel_crtc, crtc_state);
14082 }
14083
Matt Roper465c1202014-05-29 08:06:54 -070014084 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014085 if (!primary)
14086 goto fail;
14087
14088 cursor = intel_cursor_plane_create(dev, pipe);
14089 if (!cursor)
14090 goto fail;
14091
Matt Roper465c1202014-05-29 08:06:54 -070014092 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Matt Roper3d7d6512014-06-10 08:28:13 -070014093 cursor, &intel_crtc_funcs);
14094 if (ret)
14095 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080014096
14097 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080014098 for (i = 0; i < 256; i++) {
14099 intel_crtc->lut_r[i] = i;
14100 intel_crtc->lut_g[i] = i;
14101 intel_crtc->lut_b[i] = i;
14102 }
14103
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014104 /*
14105 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020014106 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014107 */
Jesse Barnes80824002009-09-10 15:28:06 -070014108 intel_crtc->pipe = pipe;
14109 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010014110 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080014111 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010014112 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070014113 }
14114
Chris Wilson4b0e3332014-05-30 16:35:26 +030014115 intel_crtc->cursor_base = ~0;
14116 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030014117 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030014118
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080014119 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14120 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14121 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14122 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14123
Jesse Barnes79e53942008-11-07 14:24:08 -080014124 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020014125
14126 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014127 return;
14128
14129fail:
14130 if (primary)
14131 drm_plane_cleanup(primary);
14132 if (cursor)
14133 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014134 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014135 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080014136}
14137
Jesse Barnes752aa882013-10-31 18:55:49 +020014138enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14139{
14140 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014141 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020014142
Rob Clark51fd3712013-11-19 12:10:12 -050014143 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020014144
Ville Syrjäläd3babd32014-11-07 11:16:01 +020014145 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020014146 return INVALID_PIPE;
14147
14148 return to_intel_crtc(encoder->crtc)->pipe;
14149}
14150
Carl Worth08d7b3d2009-04-29 14:43:54 -070014151int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000014152 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070014153{
Carl Worth08d7b3d2009-04-29 14:43:54 -070014154 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040014155 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020014156 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014157
Rob Clark7707e652014-07-17 23:30:04 -040014158 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070014159
Rob Clark7707e652014-07-17 23:30:04 -040014160 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070014161 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030014162 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014163 }
14164
Rob Clark7707e652014-07-17 23:30:04 -040014165 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020014166 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014167
Daniel Vetterc05422d2009-08-11 16:05:30 +020014168 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014169}
14170
Daniel Vetter66a92782012-07-12 20:08:18 +020014171static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080014172{
Daniel Vetter66a92782012-07-12 20:08:18 +020014173 struct drm_device *dev = encoder->base.dev;
14174 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080014175 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080014176 int entry = 0;
14177
Damien Lespiaub2784e12014-08-05 11:29:37 +010014178 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020014179 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020014180 index_mask |= (1 << entry);
14181
Jesse Barnes79e53942008-11-07 14:24:08 -080014182 entry++;
14183 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010014184
Jesse Barnes79e53942008-11-07 14:24:08 -080014185 return index_mask;
14186}
14187
Chris Wilson4d302442010-12-14 19:21:29 +000014188static bool has_edp_a(struct drm_device *dev)
14189{
14190 struct drm_i915_private *dev_priv = dev->dev_private;
14191
14192 if (!IS_MOBILE(dev))
14193 return false;
14194
14195 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14196 return false;
14197
Damien Lespiaue3589902014-02-07 19:12:50 +000014198 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000014199 return false;
14200
14201 return true;
14202}
14203
Jesse Barnes84b4e042014-06-25 08:24:29 -070014204static bool intel_crt_present(struct drm_device *dev)
14205{
14206 struct drm_i915_private *dev_priv = dev->dev_private;
14207
Damien Lespiau884497e2013-12-03 13:56:23 +000014208 if (INTEL_INFO(dev)->gen >= 9)
14209 return false;
14210
Damien Lespiaucf404ce2014-10-01 20:04:15 +010014211 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014212 return false;
14213
14214 if (IS_CHERRYVIEW(dev))
14215 return false;
14216
14217 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
14218 return false;
14219
14220 return true;
14221}
14222
Jesse Barnes79e53942008-11-07 14:24:08 -080014223static void intel_setup_outputs(struct drm_device *dev)
14224{
Eric Anholt725e30a2009-01-22 13:01:02 -080014225 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010014226 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014227 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080014228
Daniel Vetterc9093352013-06-06 22:22:47 +020014229 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014230
Jesse Barnes84b4e042014-06-25 08:24:29 -070014231 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020014232 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014233
Vandana Kannanc776eb22014-08-19 12:05:01 +053014234 if (IS_BROXTON(dev)) {
14235 /*
14236 * FIXME: Broxton doesn't support port detection via the
14237 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14238 * detect the ports.
14239 */
14240 intel_ddi_init(dev, PORT_A);
14241 intel_ddi_init(dev, PORT_B);
14242 intel_ddi_init(dev, PORT_C);
14243 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014244 int found;
14245
Jesse Barnesde31fac2015-03-06 15:53:32 -080014246 /*
14247 * Haswell uses DDI functions to detect digital outputs.
14248 * On SKL pre-D0 the strap isn't connected, so we assume
14249 * it's there.
14250 */
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014251 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014252 /* WaIgnoreDDIAStrap: skl */
14253 if (found ||
14254 (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014255 intel_ddi_init(dev, PORT_A);
14256
14257 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14258 * register */
14259 found = I915_READ(SFUSE_STRAP);
14260
14261 if (found & SFUSE_STRAP_DDIB_DETECTED)
14262 intel_ddi_init(dev, PORT_B);
14263 if (found & SFUSE_STRAP_DDIC_DETECTED)
14264 intel_ddi_init(dev, PORT_C);
14265 if (found & SFUSE_STRAP_DDID_DETECTED)
14266 intel_ddi_init(dev, PORT_D);
14267 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014268 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020014269 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014270
14271 if (has_edp_a(dev))
14272 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014273
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014274 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014275 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010014276 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014277 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014278 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014279 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014280 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014281 }
14282
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014283 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014284 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014285
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014286 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014287 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014288
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014289 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014290 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014291
Daniel Vetter270b3042012-10-27 15:52:05 +020014292 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014293 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070014294 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014295 /*
14296 * The DP_DETECTED bit is the latched state of the DDC
14297 * SDA pin at boot. However since eDP doesn't require DDC
14298 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14299 * eDP ports may have been muxed to an alternate function.
14300 * Thus we can't rely on the DP_DETECTED bit alone to detect
14301 * eDP ports. Consult the VBT as well as DP_DETECTED to
14302 * detect eDP ports.
14303 */
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014304 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
14305 !intel_dp_is_edp(dev, PORT_B))
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014306 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
14307 PORT_B);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014308 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
14309 intel_dp_is_edp(dev, PORT_B))
14310 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014311
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014312 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
14313 !intel_dp_is_edp(dev, PORT_C))
Jesse Barnes6f6005a2013-08-09 09:34:35 -070014314 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
14315 PORT_C);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014316 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
14317 intel_dp_is_edp(dev, PORT_C))
14318 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014319
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014320 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014321 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014322 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
14323 PORT_D);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014324 /* eDP not supported on port D, so don't check VBT */
14325 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
14326 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014327 }
14328
Jani Nikula3cfca972013-08-27 15:12:26 +030014329 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +080014330 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014331 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014332
Paulo Zanonie2debe92013-02-18 19:00:27 -030014333 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014334 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014335 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014336 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
14337 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014338 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014339 }
Ma Ling27185ae2009-08-24 13:50:23 +080014340
Imre Deake7281ea2013-05-08 13:14:08 +030014341 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014342 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014343 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014344
14345 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014346
Paulo Zanonie2debe92013-02-18 19:00:27 -030014347 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014348 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014349 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014350 }
Ma Ling27185ae2009-08-24 13:50:23 +080014351
Paulo Zanonie2debe92013-02-18 19:00:27 -030014352 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014353
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014354 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
14355 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014356 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014357 }
Imre Deake7281ea2013-05-08 13:14:08 +030014358 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014359 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014360 }
Ma Ling27185ae2009-08-24 13:50:23 +080014361
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014362 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014363 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014364 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014365 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014366 intel_dvo_init(dev);
14367
Zhenyu Wang103a1962009-11-27 11:44:36 +080014368 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014369 intel_tv_init(dev);
14370
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014371 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014372
Damien Lespiaub2784e12014-08-05 11:29:37 +010014373 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014374 encoder->base.possible_crtcs = encoder->crtc_mask;
14375 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014376 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014377 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014378
Paulo Zanonidde86e22012-12-01 12:04:25 -020014379 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014380
14381 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014382}
14383
14384static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14385{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014386 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014387 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014388
Daniel Vetteref2d6332014-02-10 18:00:38 +010014389 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014390 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014391 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014392 drm_gem_object_unreference(&intel_fb->obj->base);
14393 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014394 kfree(intel_fb);
14395}
14396
14397static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014398 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014399 unsigned int *handle)
14400{
14401 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014402 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014403
Chris Wilson05394f32010-11-08 19:18:58 +000014404 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014405}
14406
14407static const struct drm_framebuffer_funcs intel_fb_funcs = {
14408 .destroy = intel_user_framebuffer_destroy,
14409 .create_handle = intel_user_framebuffer_create_handle,
14410};
14411
Damien Lespiaub3218032015-02-27 11:15:18 +000014412static
14413u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14414 uint32_t pixel_format)
14415{
14416 u32 gen = INTEL_INFO(dev)->gen;
14417
14418 if (gen >= 9) {
14419 /* "The stride in bytes must not exceed the of the size of 8K
14420 * pixels and 32K bytes."
14421 */
14422 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
14423 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
14424 return 32*1024;
14425 } else if (gen >= 4) {
14426 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14427 return 16*1024;
14428 else
14429 return 32*1024;
14430 } else if (gen >= 3) {
14431 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14432 return 8*1024;
14433 else
14434 return 16*1024;
14435 } else {
14436 /* XXX DSPC is limited to 4k tiled */
14437 return 8*1024;
14438 }
14439}
14440
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014441static int intel_framebuffer_init(struct drm_device *dev,
14442 struct intel_framebuffer *intel_fb,
14443 struct drm_mode_fb_cmd2 *mode_cmd,
14444 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014445{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014446 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014447 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014448 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014449
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014450 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14451
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014452 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14453 /* Enforce that fb modifier and tiling mode match, but only for
14454 * X-tiled. This is needed for FBC. */
14455 if (!!(obj->tiling_mode == I915_TILING_X) !=
14456 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14457 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14458 return -EINVAL;
14459 }
14460 } else {
14461 if (obj->tiling_mode == I915_TILING_X)
14462 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14463 else if (obj->tiling_mode == I915_TILING_Y) {
14464 DRM_DEBUG("No Y tiling for legacy addfb\n");
14465 return -EINVAL;
14466 }
14467 }
14468
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014469 /* Passed in modifier sanity checking. */
14470 switch (mode_cmd->modifier[0]) {
14471 case I915_FORMAT_MOD_Y_TILED:
14472 case I915_FORMAT_MOD_Yf_TILED:
14473 if (INTEL_INFO(dev)->gen < 9) {
14474 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14475 mode_cmd->modifier[0]);
14476 return -EINVAL;
14477 }
14478 case DRM_FORMAT_MOD_NONE:
14479 case I915_FORMAT_MOD_X_TILED:
14480 break;
14481 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014482 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14483 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014484 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014485 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014486
Damien Lespiaub3218032015-02-27 11:15:18 +000014487 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
14488 mode_cmd->pixel_format);
14489 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14490 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14491 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014492 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014493 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014494
Damien Lespiaub3218032015-02-27 11:15:18 +000014495 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14496 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014497 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014498 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14499 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014500 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014501 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014502 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014503 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014504
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014505 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014506 mode_cmd->pitches[0] != obj->stride) {
14507 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14508 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014509 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014510 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014511
Ville Syrjälä57779d02012-10-31 17:50:14 +020014512 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014513 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014514 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014515 case DRM_FORMAT_RGB565:
14516 case DRM_FORMAT_XRGB8888:
14517 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014518 break;
14519 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014520 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014521 DRM_DEBUG("unsupported pixel format: %s\n",
14522 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014523 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014524 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014525 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014526 case DRM_FORMAT_ABGR8888:
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014527 if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
14528 DRM_DEBUG("unsupported pixel format: %s\n",
14529 drm_get_format_name(mode_cmd->pixel_format));
14530 return -EINVAL;
14531 }
14532 break;
14533 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014534 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014535 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014536 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014537 DRM_DEBUG("unsupported pixel format: %s\n",
14538 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014539 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014540 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014541 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014542 case DRM_FORMAT_ABGR2101010:
14543 if (!IS_VALLEYVIEW(dev)) {
14544 DRM_DEBUG("unsupported pixel format: %s\n",
14545 drm_get_format_name(mode_cmd->pixel_format));
14546 return -EINVAL;
14547 }
14548 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014549 case DRM_FORMAT_YUYV:
14550 case DRM_FORMAT_UYVY:
14551 case DRM_FORMAT_YVYU:
14552 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014553 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014554 DRM_DEBUG("unsupported pixel format: %s\n",
14555 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014556 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014557 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014558 break;
14559 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014560 DRM_DEBUG("unsupported pixel format: %s\n",
14561 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014562 return -EINVAL;
14563 }
14564
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014565 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14566 if (mode_cmd->offsets[0] != 0)
14567 return -EINVAL;
14568
Damien Lespiauec2c9812015-01-20 12:51:45 +000014569 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014570 mode_cmd->pixel_format,
14571 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014572 /* FIXME drm helper for size checks (especially planar formats)? */
14573 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14574 return -EINVAL;
14575
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014576 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14577 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020014578 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014579
Jesse Barnes79e53942008-11-07 14:24:08 -080014580 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14581 if (ret) {
14582 DRM_ERROR("framebuffer init failed %d\n", ret);
14583 return ret;
14584 }
14585
Jesse Barnes79e53942008-11-07 14:24:08 -080014586 return 0;
14587}
14588
Jesse Barnes79e53942008-11-07 14:24:08 -080014589static struct drm_framebuffer *
14590intel_user_framebuffer_create(struct drm_device *dev,
14591 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014592 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014593{
Chris Wilson05394f32010-11-08 19:18:58 +000014594 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014595
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014596 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
14597 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014598 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014599 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014600
Chris Wilsond2dff872011-04-19 08:36:26 +010014601 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080014602}
14603
Daniel Vetter4520f532013-10-09 09:18:51 +020014604#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020014605static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014606{
14607}
14608#endif
14609
Jesse Barnes79e53942008-11-07 14:24:08 -080014610static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014611 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014612 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014613 .atomic_check = intel_atomic_check,
14614 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020014615 .atomic_state_alloc = intel_atomic_state_alloc,
14616 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080014617};
14618
Jesse Barnese70236a2009-09-21 10:42:27 -070014619/* Set up chip specific display functions */
14620static void intel_init_display(struct drm_device *dev)
14621{
14622 struct drm_i915_private *dev_priv = dev->dev_private;
14623
Daniel Vetteree9300b2013-06-03 22:40:22 +020014624 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14625 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030014626 else if (IS_CHERRYVIEW(dev))
14627 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020014628 else if (IS_VALLEYVIEW(dev))
14629 dev_priv->display.find_dpll = vlv_find_best_dpll;
14630 else if (IS_PINEVIEW(dev))
14631 dev_priv->display.find_dpll = pnv_find_best_dpll;
14632 else
14633 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14634
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014635 if (INTEL_INFO(dev)->gen >= 9) {
14636 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014637 dev_priv->display.get_initial_plane_config =
14638 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014639 dev_priv->display.crtc_compute_clock =
14640 haswell_crtc_compute_clock;
14641 dev_priv->display.crtc_enable = haswell_crtc_enable;
14642 dev_priv->display.crtc_disable = haswell_crtc_disable;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014643 dev_priv->display.update_primary_plane =
14644 skylake_update_primary_plane;
14645 } else if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014646 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014647 dev_priv->display.get_initial_plane_config =
14648 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014649 dev_priv->display.crtc_compute_clock =
14650 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014651 dev_priv->display.crtc_enable = haswell_crtc_enable;
14652 dev_priv->display.crtc_disable = haswell_crtc_disable;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014653 dev_priv->display.update_primary_plane =
14654 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030014655 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014656 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014657 dev_priv->display.get_initial_plane_config =
14658 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014659 dev_priv->display.crtc_compute_clock =
14660 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014661 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14662 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014663 dev_priv->display.update_primary_plane =
14664 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014665 } else if (IS_VALLEYVIEW(dev)) {
14666 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014667 dev_priv->display.get_initial_plane_config =
14668 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014669 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014670 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14671 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014672 dev_priv->display.update_primary_plane =
14673 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014674 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014675 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014676 dev_priv->display.get_initial_plane_config =
14677 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014678 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014679 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14680 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014681 dev_priv->display.update_primary_plane =
14682 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014683 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014684
Jesse Barnese70236a2009-09-21 10:42:27 -070014685 /* Returns the core display clock speed */
Ville Syrjälä1652d192015-03-31 14:12:01 +030014686 if (IS_SKYLAKE(dev))
14687 dev_priv->display.get_display_clock_speed =
14688 skylake_get_display_clock_speed;
14689 else if (IS_BROADWELL(dev))
14690 dev_priv->display.get_display_clock_speed =
14691 broadwell_get_display_clock_speed;
14692 else if (IS_HASWELL(dev))
14693 dev_priv->display.get_display_clock_speed =
14694 haswell_get_display_clock_speed;
14695 else if (IS_VALLEYVIEW(dev))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070014696 dev_priv->display.get_display_clock_speed =
14697 valleyview_get_display_clock_speed;
Ville Syrjäläb37a6432015-03-31 14:11:54 +030014698 else if (IS_GEN5(dev))
14699 dev_priv->display.get_display_clock_speed =
14700 ilk_get_display_clock_speed;
Ville Syrjäläa7c66cd2015-03-31 14:11:56 +030014701 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
Ville Syrjälä34edce22015-05-22 11:22:33 +030014702 IS_GEN6(dev) || IS_IVYBRIDGE(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014703 dev_priv->display.get_display_clock_speed =
14704 i945_get_display_clock_speed;
Ville Syrjälä34edce22015-05-22 11:22:33 +030014705 else if (IS_GM45(dev))
14706 dev_priv->display.get_display_clock_speed =
14707 gm45_get_display_clock_speed;
14708 else if (IS_CRESTLINE(dev))
14709 dev_priv->display.get_display_clock_speed =
14710 i965gm_get_display_clock_speed;
14711 else if (IS_PINEVIEW(dev))
14712 dev_priv->display.get_display_clock_speed =
14713 pnv_get_display_clock_speed;
14714 else if (IS_G33(dev) || IS_G4X(dev))
14715 dev_priv->display.get_display_clock_speed =
14716 g33_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014717 else if (IS_I915G(dev))
14718 dev_priv->display.get_display_clock_speed =
14719 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014720 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014721 dev_priv->display.get_display_clock_speed =
14722 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014723 else if (IS_PINEVIEW(dev))
14724 dev_priv->display.get_display_clock_speed =
14725 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014726 else if (IS_I915GM(dev))
14727 dev_priv->display.get_display_clock_speed =
14728 i915gm_get_display_clock_speed;
14729 else if (IS_I865G(dev))
14730 dev_priv->display.get_display_clock_speed =
14731 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020014732 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014733 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030014734 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014735 else { /* 830 */
14736 WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070014737 dev_priv->display.get_display_clock_speed =
14738 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014739 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014740
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014741 if (IS_GEN5(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014742 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014743 } else if (IS_GEN6(dev)) {
14744 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014745 } else if (IS_IVYBRIDGE(dev)) {
14746 /* FIXME: detect B0+ stepping and use auto training */
14747 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Paulo Zanoni059b2fe2014-09-02 16:53:57 -030014748 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014749 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014750 if (IS_BROADWELL(dev)) {
14751 dev_priv->display.modeset_commit_cdclk =
14752 broadwell_modeset_commit_cdclk;
14753 dev_priv->display.modeset_calc_cdclk =
14754 broadwell_modeset_calc_cdclk;
14755 }
Jesse Barnes30a970c2013-11-04 13:48:12 -080014756 } else if (IS_VALLEYVIEW(dev)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014757 dev_priv->display.modeset_commit_cdclk =
14758 valleyview_modeset_commit_cdclk;
14759 dev_priv->display.modeset_calc_cdclk =
14760 valleyview_modeset_calc_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +053014761 } else if (IS_BROXTON(dev)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014762 dev_priv->display.modeset_commit_cdclk =
14763 broxton_modeset_commit_cdclk;
14764 dev_priv->display.modeset_calc_cdclk =
14765 broxton_modeset_calc_cdclk;
Jesse Barnese70236a2009-09-21 10:42:27 -070014766 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014767
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014768 switch (INTEL_INFO(dev)->gen) {
14769 case 2:
14770 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14771 break;
14772
14773 case 3:
14774 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14775 break;
14776
14777 case 4:
14778 case 5:
14779 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14780 break;
14781
14782 case 6:
14783 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14784 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014785 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070014786 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014787 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14788 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000014789 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000014790 /* Drop through - unsupported since execlist only. */
14791 default:
14792 /* Default just returns -ENODEV to indicate unsupported */
14793 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014794 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020014795
14796 intel_panel_init_backlight_funcs(dev);
Ville Syrjäläe39b9992014-09-04 14:53:14 +030014797
14798 mutex_init(&dev_priv->pps_mutex);
Jesse Barnese70236a2009-09-21 10:42:27 -070014799}
14800
Jesse Barnesb690e962010-07-19 13:53:12 -070014801/*
14802 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14803 * resume, or other times. This quirk makes sure that's the case for
14804 * affected systems.
14805 */
Akshay Joshi0206e352011-08-16 15:34:10 -040014806static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070014807{
14808 struct drm_i915_private *dev_priv = dev->dev_private;
14809
14810 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014811 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014812}
14813
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014814static void quirk_pipeb_force(struct drm_device *dev)
14815{
14816 struct drm_i915_private *dev_priv = dev->dev_private;
14817
14818 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14819 DRM_INFO("applying pipe b force quirk\n");
14820}
14821
Keith Packard435793d2011-07-12 14:56:22 -070014822/*
14823 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14824 */
14825static void quirk_ssc_force_disable(struct drm_device *dev)
14826{
14827 struct drm_i915_private *dev_priv = dev->dev_private;
14828 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014829 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070014830}
14831
Carsten Emde4dca20e2012-03-15 15:56:26 +010014832/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010014833 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14834 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010014835 */
14836static void quirk_invert_brightness(struct drm_device *dev)
14837{
14838 struct drm_i915_private *dev_priv = dev->dev_private;
14839 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014840 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014841}
14842
Scot Doyle9c72cc62014-07-03 23:27:50 +000014843/* Some VBT's incorrectly indicate no backlight is present */
14844static void quirk_backlight_present(struct drm_device *dev)
14845{
14846 struct drm_i915_private *dev_priv = dev->dev_private;
14847 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14848 DRM_INFO("applying backlight present quirk\n");
14849}
14850
Jesse Barnesb690e962010-07-19 13:53:12 -070014851struct intel_quirk {
14852 int device;
14853 int subsystem_vendor;
14854 int subsystem_device;
14855 void (*hook)(struct drm_device *dev);
14856};
14857
Egbert Eich5f85f172012-10-14 15:46:38 +020014858/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14859struct intel_dmi_quirk {
14860 void (*hook)(struct drm_device *dev);
14861 const struct dmi_system_id (*dmi_id_list)[];
14862};
14863
14864static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14865{
14866 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14867 return 1;
14868}
14869
14870static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14871 {
14872 .dmi_id_list = &(const struct dmi_system_id[]) {
14873 {
14874 .callback = intel_dmi_reverse_brightness,
14875 .ident = "NCR Corporation",
14876 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14877 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14878 },
14879 },
14880 { } /* terminating entry */
14881 },
14882 .hook = quirk_invert_brightness,
14883 },
14884};
14885
Ben Widawskyc43b5632012-04-16 14:07:40 -070014886static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070014887 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14888 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14889
Jesse Barnesb690e962010-07-19 13:53:12 -070014890 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14891 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14892
Ville Syrjälä5f080c02014-08-15 01:22:06 +030014893 /* 830 needs to leave pipe A & dpll A up */
14894 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14895
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014896 /* 830 needs to leave pipe B & dpll B up */
14897 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14898
Keith Packard435793d2011-07-12 14:56:22 -070014899 /* Lenovo U160 cannot use SSC on LVDS */
14900 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020014901
14902 /* Sony Vaio Y cannot use SSC on LVDS */
14903 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010014904
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010014905 /* Acer Aspire 5734Z must invert backlight brightness */
14906 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14907
14908 /* Acer/eMachines G725 */
14909 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14910
14911 /* Acer/eMachines e725 */
14912 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14913
14914 /* Acer/Packard Bell NCL20 */
14915 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14916
14917 /* Acer Aspire 4736Z */
14918 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020014919
14920 /* Acer Aspire 5336 */
14921 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000014922
14923 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14924 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000014925
Scot Doyledfb3d47b2014-08-21 16:08:02 +000014926 /* Acer C720 Chromebook (Core i3 4005U) */
14927 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14928
jens steinb2a96012014-10-28 20:25:53 +010014929 /* Apple Macbook 2,1 (Core 2 T7400) */
14930 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14931
Scot Doyled4967d82014-07-03 23:27:52 +000014932 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14933 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000014934
14935 /* HP Chromebook 14 (Celeron 2955U) */
14936 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020014937
14938 /* Dell Chromebook 11 */
14939 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070014940};
14941
14942static void intel_init_quirks(struct drm_device *dev)
14943{
14944 struct pci_dev *d = dev->pdev;
14945 int i;
14946
14947 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14948 struct intel_quirk *q = &intel_quirks[i];
14949
14950 if (d->device == q->device &&
14951 (d->subsystem_vendor == q->subsystem_vendor ||
14952 q->subsystem_vendor == PCI_ANY_ID) &&
14953 (d->subsystem_device == q->subsystem_device ||
14954 q->subsystem_device == PCI_ANY_ID))
14955 q->hook(dev);
14956 }
Egbert Eich5f85f172012-10-14 15:46:38 +020014957 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14958 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14959 intel_dmi_quirks[i].hook(dev);
14960 }
Jesse Barnesb690e962010-07-19 13:53:12 -070014961}
14962
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014963/* Disable the VGA plane that we never use */
14964static void i915_disable_vga(struct drm_device *dev)
14965{
14966 struct drm_i915_private *dev_priv = dev->dev_private;
14967 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020014968 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014969
Ville Syrjälä2b37c612014-01-22 21:32:38 +020014970 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014971 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070014972 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014973 sr1 = inb(VGA_SR_DATA);
14974 outb(sr1 | 1<<5, VGA_SR_DATA);
14975 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
14976 udelay(300);
14977
Ville Syrjälä01f5a622014-12-16 18:38:37 +020014978 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014979 POSTING_READ(vga_reg);
14980}
14981
Daniel Vetterf8175862012-04-10 15:50:11 +020014982void intel_modeset_init_hw(struct drm_device *dev)
14983{
Ville Syrjäläb6283052015-06-03 15:45:07 +030014984 intel_update_cdclk(dev);
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030014985 intel_prepare_ddi(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020014986 intel_init_clock_gating(dev);
Daniel Vetter8090c6b2012-06-24 16:42:32 +020014987 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020014988}
14989
Jesse Barnes79e53942008-11-07 14:24:08 -080014990void intel_modeset_init(struct drm_device *dev)
14991{
Jesse Barnes652c3932009-08-17 13:31:43 -070014992 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000014993 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014994 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080014995 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080014996
14997 drm_mode_config_init(dev);
14998
14999 dev->mode_config.min_width = 0;
15000 dev->mode_config.min_height = 0;
15001
Dave Airlie019d96c2011-09-29 16:20:42 +010015002 dev->mode_config.preferred_depth = 24;
15003 dev->mode_config.prefer_shadow = 1;
15004
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000015005 dev->mode_config.allow_fb_modifiers = true;
15006
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020015007 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080015008
Jesse Barnesb690e962010-07-19 13:53:12 -070015009 intel_init_quirks(dev);
15010
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030015011 intel_init_pm(dev);
15012
Ben Widawskye3c74752013-04-05 13:12:39 -070015013 if (INTEL_INFO(dev)->num_pipes == 0)
15014 return;
15015
Jesse Barnese70236a2009-09-21 10:42:27 -070015016 intel_init_display(dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +020015017 intel_init_audio(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070015018
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015019 if (IS_GEN2(dev)) {
15020 dev->mode_config.max_width = 2048;
15021 dev->mode_config.max_height = 2048;
15022 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070015023 dev->mode_config.max_width = 4096;
15024 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080015025 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015026 dev->mode_config.max_width = 8192;
15027 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080015028 }
Damien Lespiau068be562014-03-28 14:17:49 +000015029
Ville Syrjälädc41c152014-08-13 11:57:05 +030015030 if (IS_845G(dev) || IS_I865G(dev)) {
15031 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15032 dev->mode_config.cursor_height = 1023;
15033 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000015034 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15035 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15036 } else {
15037 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15038 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15039 }
15040
Ben Widawsky5d4545a2013-01-17 12:45:15 -080015041 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080015042
Zhao Yakui28c97732009-10-09 11:39:41 +080015043 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015044 INTEL_INFO(dev)->num_pipes,
15045 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080015046
Damien Lespiau055e3932014-08-18 13:49:10 +010015047 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015048 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000015049 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000015050 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015051 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030015052 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000015053 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015054 }
Jesse Barnes79e53942008-11-07 14:24:08 -080015055 }
15056
Jesse Barnesf42bb702013-12-16 16:34:23 -080015057 intel_init_dpio(dev);
15058
Daniel Vettere72f9fb2013-06-05 13:34:06 +020015059 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010015060
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015061 /* Just disable it once at startup */
15062 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015063 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000015064
15065 /* Just in case the BIOS is doing something questionable. */
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020015066 intel_fbc_disable(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080015067
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015068 drm_modeset_lock_all(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080015069 intel_modeset_setup_hw_state(dev, false);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015070 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015071
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015072 for_each_intel_crtc(dev, crtc) {
Jesse Barnes46f297f2014-03-07 08:57:48 -080015073 if (!crtc->active)
15074 continue;
15075
Jesse Barnes46f297f2014-03-07 08:57:48 -080015076 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080015077 * Note that reserving the BIOS fb up front prevents us
15078 * from stuffing other stolen allocations like the ring
15079 * on top. This prevents some ugliness at boot time, and
15080 * can even allow for smooth boot transitions if the BIOS
15081 * fb is large enough for the active pipe configuration.
15082 */
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015083 if (dev_priv->display.get_initial_plane_config) {
15084 dev_priv->display.get_initial_plane_config(crtc,
Jesse Barnes46f297f2014-03-07 08:57:48 -080015085 &crtc->plane_config);
15086 /*
15087 * If the fb is shared between multiple heads, we'll
15088 * just get the first one.
15089 */
Daniel Vetterf6936e22015-03-26 12:17:05 +010015090 intel_find_initial_plane_obj(crtc, &crtc->plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015091 }
Jesse Barnes46f297f2014-03-07 08:57:48 -080015092 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010015093}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015094
Daniel Vetter7fad7982012-07-04 17:51:47 +020015095static void intel_enable_pipe_a(struct drm_device *dev)
15096{
15097 struct intel_connector *connector;
15098 struct drm_connector *crt = NULL;
15099 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015100 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020015101
15102 /* We can't just switch on the pipe A, we need to set things up with a
15103 * proper mode and output configuration. As a gross hack, enable pipe A
15104 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015105 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020015106 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15107 crt = &connector->base;
15108 break;
15109 }
15110 }
15111
15112 if (!crt)
15113 return;
15114
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015115 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020015116 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020015117}
15118
Daniel Vetterfa555832012-10-10 23:14:00 +020015119static bool
15120intel_check_plane_mapping(struct intel_crtc *crtc)
15121{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015122 struct drm_device *dev = crtc->base.dev;
15123 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020015124 u32 reg, val;
15125
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015126 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020015127 return true;
15128
15129 reg = DSPCNTR(!crtc->plane);
15130 val = I915_READ(reg);
15131
15132 if ((val & DISPLAY_PLANE_ENABLE) &&
15133 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15134 return false;
15135
15136 return true;
15137}
15138
Daniel Vetter24929352012-07-02 20:28:59 +020015139static void intel_sanitize_crtc(struct intel_crtc *crtc)
15140{
15141 struct drm_device *dev = crtc->base.dev;
15142 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015143 struct intel_encoder *encoder;
Daniel Vetterfa555832012-10-10 23:14:00 +020015144 u32 reg;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015145 bool enable;
Daniel Vetter24929352012-07-02 20:28:59 +020015146
Daniel Vetter24929352012-07-02 20:28:59 +020015147 /* Clear any frame start delays used for debugging left by the BIOS */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015148 reg = PIPECONF(crtc->config->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015149 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15150
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015151 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010015152 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015153 if (crtc->active) {
15154 update_scanline_offset(crtc);
Daniel Vetter96256042015-02-13 21:03:42 +010015155 drm_crtc_vblank_on(&crtc->base);
15156 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015157
Daniel Vetter24929352012-07-02 20:28:59 +020015158 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020015159 * disable the crtc (and hence change the state) if it is wrong. Note
15160 * that gen4+ has a fixed plane -> pipe mapping. */
15161 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020015162 bool plane;
15163
Daniel Vetter24929352012-07-02 20:28:59 +020015164 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15165 crtc->base.base.id);
15166
15167 /* Pipe has the wrong plane attached and the plane is active.
15168 * Temporarily change the plane mapping and disable everything
15169 * ... */
15170 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015171 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020015172 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015173 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015174 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020015175 }
Daniel Vetter24929352012-07-02 20:28:59 +020015176
Daniel Vetter7fad7982012-07-04 17:51:47 +020015177 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15178 crtc->pipe == PIPE_A && !crtc->active) {
15179 /* BIOS forgot to enable pipe A, this mostly happens after
15180 * resume. Force-enable the pipe to fix this, the update_dpms
15181 * call below we restore the pipe to the right state, but leave
15182 * the required bits on. */
15183 intel_enable_pipe_a(dev);
15184 }
15185
Daniel Vetter24929352012-07-02 20:28:59 +020015186 /* Adjust the state of the output pipe according to whether we
15187 * have active connectors/encoders. */
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015188 enable = false;
15189 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15190 enable |= encoder->connectors_active;
Daniel Vetter24929352012-07-02 20:28:59 +020015191
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015192 if (!enable)
15193 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015194
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020015195 if (crtc->active != crtc->base.state->active) {
Daniel Vetter24929352012-07-02 20:28:59 +020015196
15197 /* This can happen either due to bugs in the get_hw_state
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015198 * functions or because of calls to intel_crtc_disable_noatomic,
15199 * or because the pipe is force-enabled due to the
Daniel Vetter24929352012-07-02 20:28:59 +020015200 * pipe A quirk. */
15201 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
15202 crtc->base.base.id,
Matt Roper83d65732015-02-25 13:12:16 -080015203 crtc->base.state->enable ? "enabled" : "disabled",
Daniel Vetter24929352012-07-02 20:28:59 +020015204 crtc->active ? "enabled" : "disabled");
15205
Matt Roper83d65732015-02-25 13:12:16 -080015206 crtc->base.state->enable = crtc->active;
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020015207 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020015208 crtc->base.enabled = crtc->active;
15209
15210 /* Because we only establish the connector -> encoder ->
15211 * crtc links if something is active, this means the
15212 * crtc is now deactivated. Break the links. connector
15213 * -> encoder links are only establish when things are
15214 * actually up, hence no need to break them. */
15215 WARN_ON(crtc->active);
15216
15217 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
15218 WARN_ON(encoder->connectors_active);
15219 encoder->base.crtc = NULL;
15220 }
15221 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015222
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030015223 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015224 /*
15225 * We start out with underrun reporting disabled to avoid races.
15226 * For correct bookkeeping mark this on active crtcs.
15227 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015228 * Also on gmch platforms we dont have any hardware bits to
15229 * disable the underrun reporting. Which means we need to start
15230 * out with underrun reporting disabled also on inactive pipes,
15231 * since otherwise we'll complain about the garbage we read when
15232 * e.g. coming up after runtime pm.
15233 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015234 * No protection against concurrent access is required - at
15235 * worst a fifo underrun happens which also sets this to false.
15236 */
15237 crtc->cpu_fifo_underrun_disabled = true;
15238 crtc->pch_fifo_underrun_disabled = true;
15239 }
Daniel Vetter24929352012-07-02 20:28:59 +020015240}
15241
15242static void intel_sanitize_encoder(struct intel_encoder *encoder)
15243{
15244 struct intel_connector *connector;
15245 struct drm_device *dev = encoder->base.dev;
15246
15247 /* We need to check both for a crtc link (meaning that the
15248 * encoder is active and trying to read from a pipe) and the
15249 * pipe itself being active. */
15250 bool has_active_crtc = encoder->base.crtc &&
15251 to_intel_crtc(encoder->base.crtc)->active;
15252
15253 if (encoder->connectors_active && !has_active_crtc) {
15254 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15255 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015256 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015257
15258 /* Connector is active, but has no active pipe. This is
15259 * fallout from our resume register restoring. Disable
15260 * the encoder manually again. */
15261 if (encoder->base.crtc) {
15262 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15263 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015264 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015265 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015266 if (encoder->post_disable)
15267 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015268 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015269 encoder->base.crtc = NULL;
15270 encoder->connectors_active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020015271
15272 /* Inconsistent output/port/pipe state happens presumably due to
15273 * a bug in one of the get_hw_state functions. Or someplace else
15274 * in our code, like the register restore mess on resume. Clamp
15275 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015276 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015277 if (connector->encoder != encoder)
15278 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020015279 connector->base.dpms = DRM_MODE_DPMS_OFF;
15280 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015281 }
15282 }
15283 /* Enabled encoders without active connectors will be fixed in
15284 * the crtc fixup. */
15285}
15286
Imre Deak04098752014-02-18 00:02:16 +020015287void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015288{
15289 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020015290 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015291
Imre Deak04098752014-02-18 00:02:16 +020015292 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15293 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15294 i915_disable_vga(dev);
15295 }
15296}
15297
15298void i915_redisable_vga(struct drm_device *dev)
15299{
15300 struct drm_i915_private *dev_priv = dev->dev_private;
15301
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015302 /* This function can be called both from intel_modeset_setup_hw_state or
15303 * at a very early point in our resume sequence, where the power well
15304 * structures are not yet restored. Since this function is at a very
15305 * paranoid "someone might have enabled VGA while we were not looking"
15306 * level, just check if the power well is enabled instead of trying to
15307 * follow the "don't touch the power well if we don't need it" policy
15308 * the rest of the driver uses. */
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015309 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015310 return;
15311
Imre Deak04098752014-02-18 00:02:16 +020015312 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015313}
15314
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015315static bool primary_get_hw_state(struct intel_crtc *crtc)
15316{
15317 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
15318
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015319 return !!(I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE);
15320}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015321
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015322static void readout_plane_state(struct intel_crtc *crtc,
15323 struct intel_crtc_state *crtc_state)
15324{
15325 struct intel_plane *p;
15326 struct drm_plane_state *drm_plane_state;
15327 bool active = crtc_state->base.active;
15328
15329 if (active) {
15330 crtc_state->quirks |= PIPE_CONFIG_QUIRK_INITIAL_PLANES;
15331
15332 /* apply to previous sw state too */
15333 to_intel_crtc_state(crtc->base.state)->quirks |=
15334 PIPE_CONFIG_QUIRK_INITIAL_PLANES;
15335 }
15336
15337 for_each_intel_plane(crtc->base.dev, p) {
15338 bool visible = active;
15339
15340 if (crtc->pipe != p->pipe)
15341 continue;
15342
15343 drm_plane_state = p->base.state;
15344 if (active && p->base.type == DRM_PLANE_TYPE_PRIMARY) {
15345 visible = primary_get_hw_state(crtc);
15346 to_intel_plane_state(drm_plane_state)->visible = visible;
15347 } else {
15348 /*
15349 * unknown state, assume it's off to force a transition
15350 * to on when calculating state changes.
15351 */
15352 to_intel_plane_state(drm_plane_state)->visible = false;
15353 }
15354
15355 if (visible) {
15356 crtc_state->base.plane_mask |=
15357 1 << drm_plane_index(&p->base);
15358 } else if (crtc_state->base.state) {
15359 /* Make this unconditional for atomic hw readout. */
15360 crtc_state->base.plane_mask &=
15361 ~(1 << drm_plane_index(&p->base));
15362 }
15363 }
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015364}
15365
Daniel Vetter30e984d2013-06-05 13:34:17 +020015366static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015367{
15368 struct drm_i915_private *dev_priv = dev->dev_private;
15369 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015370 struct intel_crtc *crtc;
15371 struct intel_encoder *encoder;
15372 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020015373 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015374
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015375 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015376 memset(crtc->config, 0, sizeof(*crtc->config));
Maarten Lankhorstf7217902015-06-10 10:24:20 +020015377 crtc->config->base.crtc = &crtc->base;
Daniel Vetter3b117c82013-04-17 20:15:07 +020015378
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015379 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
Daniel Vetter99535992014-04-13 12:00:33 +020015380
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015381 crtc->active = dev_priv->display.get_pipe_config(crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015382 crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015383
Matt Roper83d65732015-02-25 13:12:16 -080015384 crtc->base.state->enable = crtc->active;
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020015385 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020015386 crtc->base.enabled = crtc->active;
Maarten Lankhorstb8b7fad2015-06-12 11:15:41 +020015387 crtc->base.hwmode = crtc->config->base.adjusted_mode;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015388
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015389 readout_plane_state(crtc, to_intel_crtc_state(crtc->base.state));
Daniel Vetter24929352012-07-02 20:28:59 +020015390
15391 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15392 crtc->base.base.id,
15393 crtc->active ? "enabled" : "disabled");
15394 }
15395
Daniel Vetter53589012013-06-05 13:34:16 +020015396 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15397 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15398
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015399 pll->on = pll->get_hw_state(dev_priv, pll,
15400 &pll->config.hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020015401 pll->active = 0;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015402 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015403 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015404 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
Daniel Vetter53589012013-06-05 13:34:16 +020015405 pll->active++;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015406 pll->config.crtc_mask |= 1 << crtc->pipe;
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015407 }
Daniel Vetter53589012013-06-05 13:34:16 +020015408 }
Daniel Vetter53589012013-06-05 13:34:16 +020015409
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015410 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015411 pll->name, pll->config.crtc_mask, pll->on);
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015412
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015413 if (pll->config.crtc_mask)
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015414 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
Daniel Vetter53589012013-06-05 13:34:16 +020015415 }
15416
Damien Lespiaub2784e12014-08-05 11:29:37 +010015417 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015418 pipe = 0;
15419
15420 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015421 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15422 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015423 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015424 } else {
15425 encoder->base.crtc = NULL;
15426 }
15427
15428 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015429 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020015430 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015431 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015432 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015433 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015434 }
15435
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015436 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015437 if (connector->get_hw_state(connector)) {
15438 connector->base.dpms = DRM_MODE_DPMS_ON;
15439 connector->encoder->connectors_active = true;
15440 connector->base.encoder = &connector->encoder->base;
15441 } else {
15442 connector->base.dpms = DRM_MODE_DPMS_OFF;
15443 connector->base.encoder = NULL;
15444 }
15445 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15446 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030015447 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015448 connector->base.encoder ? "enabled" : "disabled");
15449 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015450}
15451
15452/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
15453 * and i915 state tracking structures. */
15454void intel_modeset_setup_hw_state(struct drm_device *dev,
15455 bool force_restore)
15456{
15457 struct drm_i915_private *dev_priv = dev->dev_private;
15458 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015459 struct intel_crtc *crtc;
15460 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015461 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015462
15463 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015464
Jesse Barnesbabea612013-06-26 18:57:38 +030015465 /*
15466 * Now that we have the config, copy it to each CRTC struct
15467 * Note that this could go away if we move to using crtc_config
15468 * checking everywhere.
15469 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015470 for_each_intel_crtc(dev, crtc) {
Jani Nikulad330a952014-01-21 11:24:25 +020015471 if (crtc->active && i915.fastboot) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015472 intel_mode_from_pipe_config(&crtc->base.mode,
15473 crtc->config);
Jesse Barnesbabea612013-06-26 18:57:38 +030015474 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
15475 crtc->base.base.id);
15476 drm_mode_debug_printmodeline(&crtc->base.mode);
15477 }
15478 }
15479
Daniel Vetter24929352012-07-02 20:28:59 +020015480 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010015481 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015482 intel_sanitize_encoder(encoder);
15483 }
15484
Damien Lespiau055e3932014-08-18 13:49:10 +010015485 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020015486 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15487 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015488 intel_dump_pipe_config(crtc, crtc->config,
15489 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015490 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015491
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015492 intel_modeset_update_connector_atomic_state(dev);
15493
Daniel Vetter35c95372013-07-17 06:55:04 +020015494 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15495 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15496
15497 if (!pll->on || pll->active)
15498 continue;
15499
15500 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15501
15502 pll->disable(dev_priv, pll);
15503 pll->on = false;
15504 }
15505
Pradeep Bhat30789992014-11-04 17:06:45 +000015506 if (IS_GEN9(dev))
15507 skl_wm_get_hw_state(dev);
15508 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015509 ilk_wm_get_hw_state(dev);
15510
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015511 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015512 i915_redisable_vga(dev);
15513
Daniel Vetterf30da182013-04-11 20:22:50 +020015514 /*
15515 * We need to use raw interfaces for restoring state to avoid
15516 * checking (bogus) intermediate states.
15517 */
Damien Lespiau055e3932014-08-18 13:49:10 +010015518 for_each_pipe(dev_priv, pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070015519 struct drm_crtc *crtc =
15520 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020015521
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020015522 intel_crtc_restore_mode(crtc);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015523 }
15524 } else {
15525 intel_modeset_update_staged_output_state(dev);
15526 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020015527
15528 intel_modeset_check_state(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015529}
15530
15531void intel_modeset_gem_init(struct drm_device *dev)
15532{
Jesse Barnes92122782014-10-09 12:57:42 -070015533 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015534 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070015535 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015536 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015537
Imre Deakae484342014-03-31 15:10:44 +030015538 mutex_lock(&dev->struct_mutex);
15539 intel_init_gt_powersave(dev);
15540 mutex_unlock(&dev->struct_mutex);
15541
Jesse Barnes92122782014-10-09 12:57:42 -070015542 /*
15543 * There may be no VBT; and if the BIOS enabled SSC we can
15544 * just keep using it to avoid unnecessary flicker. Whereas if the
15545 * BIOS isn't using it, don't assume it will work even if the VBT
15546 * indicates as much.
15547 */
15548 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
15549 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15550 DREF_SSC1_ENABLE);
15551
Chris Wilson1833b132012-05-09 11:56:28 +010015552 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020015553
15554 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015555
15556 /*
15557 * Make sure any fbs we allocated at startup are properly
15558 * pinned & fenced. When we do the allocation it's too early
15559 * for this.
15560 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010015561 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070015562 obj = intel_fb_obj(c->primary->fb);
15563 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080015564 continue;
15565
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015566 mutex_lock(&dev->struct_mutex);
15567 ret = intel_pin_and_fence_fb_obj(c->primary,
15568 c->primary->fb,
15569 c->primary->state,
John Harrison91af1272015-06-18 13:14:56 +010015570 NULL, NULL);
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015571 mutex_unlock(&dev->struct_mutex);
15572 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080015573 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15574 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100015575 drm_framebuffer_unreference(c->primary->fb);
15576 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015577 c->primary->crtc = c->primary->state->crtc = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080015578 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015579 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080015580 }
15581 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015582
15583 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015584}
15585
Imre Deak4932e2c2014-02-11 17:12:48 +020015586void intel_connector_unregister(struct intel_connector *intel_connector)
15587{
15588 struct drm_connector *connector = &intel_connector->base;
15589
15590 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010015591 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015592}
15593
Jesse Barnes79e53942008-11-07 14:24:08 -080015594void intel_modeset_cleanup(struct drm_device *dev)
15595{
Jesse Barnes652c3932009-08-17 13:31:43 -070015596 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid9255d52013-09-26 20:05:59 -030015597 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070015598
Imre Deak2eb52522014-11-19 15:30:05 +020015599 intel_disable_gt_powersave(dev);
15600
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015601 intel_backlight_unregister(dev);
15602
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015603 /*
15604 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020015605 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015606 * experience fancy races otherwise.
15607 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020015608 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070015609
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015610 /*
15611 * Due to the hpd irq storm handling the hotplug work can re-arm the
15612 * poll handlers. Hence disable polling after hpd handling is shut down.
15613 */
Keith Packardf87ea762010-10-03 19:36:26 -070015614 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015615
Jesse Barnes652c3932009-08-17 13:31:43 -070015616 mutex_lock(&dev->struct_mutex);
15617
Jesse Barnes723bfd72010-10-07 16:01:13 -070015618 intel_unregister_dsm_handler();
15619
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020015620 intel_fbc_disable(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070015621
Kristian Høgsberg69341a52009-11-11 12:19:17 -050015622 mutex_unlock(&dev->struct_mutex);
15623
Chris Wilson1630fe72011-07-08 12:22:42 +010015624 /* flush any delayed tasks or pending work */
15625 flush_scheduled_work();
15626
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015627 /* destroy the backlight and sysfs files before encoders/connectors */
15628 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020015629 struct intel_connector *intel_connector;
15630
15631 intel_connector = to_intel_connector(connector);
15632 intel_connector->unregister(intel_connector);
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015633 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030015634
Jesse Barnes79e53942008-11-07 14:24:08 -080015635 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010015636
15637 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030015638
15639 mutex_lock(&dev->struct_mutex);
15640 intel_cleanup_gt_powersave(dev);
15641 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080015642}
15643
Dave Airlie28d52042009-09-21 14:33:58 +100015644/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080015645 * Return which encoder is currently attached for connector.
15646 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010015647struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080015648{
Chris Wilsondf0e9242010-09-09 16:20:55 +010015649 return &intel_attached_encoder(connector)->base;
15650}
Jesse Barnes79e53942008-11-07 14:24:08 -080015651
Chris Wilsondf0e9242010-09-09 16:20:55 +010015652void intel_connector_attach_encoder(struct intel_connector *connector,
15653 struct intel_encoder *encoder)
15654{
15655 connector->encoder = encoder;
15656 drm_mode_connector_attach_encoder(&connector->base,
15657 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080015658}
Dave Airlie28d52042009-09-21 14:33:58 +100015659
15660/*
15661 * set vga decode state - true == enable VGA decode
15662 */
15663int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15664{
15665 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000015666 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100015667 u16 gmch_ctrl;
15668
Chris Wilson75fa0412014-02-07 18:37:02 -020015669 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15670 DRM_ERROR("failed to read control word\n");
15671 return -EIO;
15672 }
15673
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020015674 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15675 return 0;
15676
Dave Airlie28d52042009-09-21 14:33:58 +100015677 if (state)
15678 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15679 else
15680 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020015681
15682 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15683 DRM_ERROR("failed to write control word\n");
15684 return -EIO;
15685 }
15686
Dave Airlie28d52042009-09-21 14:33:58 +100015687 return 0;
15688}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015689
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015690struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015691
15692 u32 power_well_driver;
15693
Chris Wilson63b66e52013-08-08 15:12:06 +020015694 int num_transcoders;
15695
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015696 struct intel_cursor_error_state {
15697 u32 control;
15698 u32 position;
15699 u32 base;
15700 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010015701 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015702
15703 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015704 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015705 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030015706 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010015707 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015708
15709 struct intel_plane_error_state {
15710 u32 control;
15711 u32 stride;
15712 u32 size;
15713 u32 pos;
15714 u32 addr;
15715 u32 surface;
15716 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010015717 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020015718
15719 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015720 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020015721 enum transcoder cpu_transcoder;
15722
15723 u32 conf;
15724
15725 u32 htotal;
15726 u32 hblank;
15727 u32 hsync;
15728 u32 vtotal;
15729 u32 vblank;
15730 u32 vsync;
15731 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015732};
15733
15734struct intel_display_error_state *
15735intel_display_capture_error_state(struct drm_device *dev)
15736{
Jani Nikulafbee40d2014-03-31 14:27:18 +030015737 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015738 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020015739 int transcoders[] = {
15740 TRANSCODER_A,
15741 TRANSCODER_B,
15742 TRANSCODER_C,
15743 TRANSCODER_EDP,
15744 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015745 int i;
15746
Chris Wilson63b66e52013-08-08 15:12:06 +020015747 if (INTEL_INFO(dev)->num_pipes == 0)
15748 return NULL;
15749
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015750 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015751 if (error == NULL)
15752 return NULL;
15753
Imre Deak190be112013-11-25 17:15:31 +020015754 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015755 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15756
Damien Lespiau055e3932014-08-18 13:49:10 +010015757 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020015758 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015759 __intel_display_power_is_enabled(dev_priv,
15760 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020015761 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015762 continue;
15763
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030015764 error->cursor[i].control = I915_READ(CURCNTR(i));
15765 error->cursor[i].position = I915_READ(CURPOS(i));
15766 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015767
15768 error->plane[i].control = I915_READ(DSPCNTR(i));
15769 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015770 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030015771 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015772 error->plane[i].pos = I915_READ(DSPPOS(i));
15773 }
Paulo Zanonica291362013-03-06 20:03:14 -030015774 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15775 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015776 if (INTEL_INFO(dev)->gen >= 4) {
15777 error->plane[i].surface = I915_READ(DSPSURF(i));
15778 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15779 }
15780
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015781 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030015782
Sonika Jindal3abfce72014-07-21 15:23:43 +053015783 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e2014-04-18 15:55:04 +030015784 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020015785 }
15786
15787 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
15788 if (HAS_DDI(dev_priv->dev))
15789 error->num_transcoders++; /* Account for eDP. */
15790
15791 for (i = 0; i < error->num_transcoders; i++) {
15792 enum transcoder cpu_transcoder = transcoders[i];
15793
Imre Deakddf9c532013-11-27 22:02:02 +020015794 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015795 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020015796 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015797 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015798 continue;
15799
Chris Wilson63b66e52013-08-08 15:12:06 +020015800 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15801
15802 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15803 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15804 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15805 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15806 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15807 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15808 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015809 }
15810
15811 return error;
15812}
15813
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015814#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15815
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015816void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015817intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015818 struct drm_device *dev,
15819 struct intel_display_error_state *error)
15820{
Damien Lespiau055e3932014-08-18 13:49:10 +010015821 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015822 int i;
15823
Chris Wilson63b66e52013-08-08 15:12:06 +020015824 if (!error)
15825 return;
15826
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015827 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020015828 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015829 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015830 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010015831 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015832 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020015833 err_printf(m, " Power: %s\n",
15834 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015835 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030015836 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015837
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015838 err_printf(m, "Plane [%d]:\n", i);
15839 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15840 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015841 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015842 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15843 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015844 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030015845 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015846 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015847 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015848 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15849 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015850 }
15851
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015852 err_printf(m, "Cursor [%d]:\n", i);
15853 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15854 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15855 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015856 }
Chris Wilson63b66e52013-08-08 15:12:06 +020015857
15858 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010015859 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020015860 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015861 err_printf(m, " Power: %s\n",
15862 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020015863 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15864 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15865 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15866 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15867 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15868 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15869 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15870 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015871}
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015872
15873void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
15874{
15875 struct intel_crtc *crtc;
15876
15877 for_each_intel_crtc(dev, crtc) {
15878 struct intel_unpin_work *work;
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015879
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015880 spin_lock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015881
15882 work = crtc->unpin_work;
15883
15884 if (work && work->event &&
15885 work->event->base.file_priv == file) {
15886 kfree(work->event);
15887 work->event = NULL;
15888 }
15889
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015890 spin_unlock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015891 }
15892}