blob: 30ffca865e1d7f31ef9dc13860e42c83230c5210 [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
Ma Lingd4906092009-03-18 20:13:27 +0800644static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200645i9xx_find_best_dpll(const intel_limit_t *limit,
646 struct intel_crtc_state *crtc_state,
Sean Paulcec2f352012-01-10 15:09:36 -0800647 int target, int refclk, intel_clock_t *match_clock,
648 intel_clock_t *best_clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800649{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200650 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300651 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800652 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800653 int err = target;
654
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200655 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800656 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100657 * For LVDS just rely on its current settings for dual-channel.
658 * We haven't figured out how to reliably set up different
659 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800660 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100661 if (intel_is_dual_link_lvds(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800662 clock.p2 = limit->p2.p2_fast;
663 else
664 clock.p2 = limit->p2.p2_slow;
665 } else {
666 if (target < limit->p2.dot_limit)
667 clock.p2 = limit->p2.p2_slow;
668 else
669 clock.p2 = limit->p2.p2_fast;
670 }
671
Akshay Joshi0206e352011-08-16 15:34:10 -0400672 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800673
Zhao Yakui42158662009-11-20 11:24:18 +0800674 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
675 clock.m1++) {
676 for (clock.m2 = limit->m2.min;
677 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200678 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800679 break;
680 for (clock.n = limit->n.min;
681 clock.n <= limit->n.max; clock.n++) {
682 for (clock.p1 = limit->p1.min;
683 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800684 int this_err;
685
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200686 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000687 if (!intel_PLL_is_valid(dev, limit,
688 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800689 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800690 if (match_clock &&
691 clock.p != match_clock->p)
692 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800693
694 this_err = abs(clock.dot - target);
695 if (this_err < err) {
696 *best_clock = clock;
697 err = this_err;
698 }
699 }
700 }
701 }
702 }
703
704 return (err != target);
705}
706
Ma Lingd4906092009-03-18 20:13:27 +0800707static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200708pnv_find_best_dpll(const intel_limit_t *limit,
709 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200710 int target, int refclk, intel_clock_t *match_clock,
711 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200712{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200713 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300714 struct drm_device *dev = crtc->base.dev;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200715 intel_clock_t clock;
716 int err = target;
717
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200718 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200719 /*
720 * For LVDS just rely on its current settings for dual-channel.
721 * We haven't figured out how to reliably set up different
722 * single/dual channel state, if we even can.
723 */
724 if (intel_is_dual_link_lvds(dev))
725 clock.p2 = limit->p2.p2_fast;
726 else
727 clock.p2 = limit->p2.p2_slow;
728 } else {
729 if (target < limit->p2.dot_limit)
730 clock.p2 = limit->p2.p2_slow;
731 else
732 clock.p2 = limit->p2.p2_fast;
733 }
734
735 memset(best_clock, 0, sizeof(*best_clock));
736
737 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
738 clock.m1++) {
739 for (clock.m2 = limit->m2.min;
740 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200741 for (clock.n = limit->n.min;
742 clock.n <= limit->n.max; clock.n++) {
743 for (clock.p1 = limit->p1.min;
744 clock.p1 <= limit->p1.max; clock.p1++) {
745 int this_err;
746
747 pineview_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800748 if (!intel_PLL_is_valid(dev, limit,
749 &clock))
750 continue;
751 if (match_clock &&
752 clock.p != match_clock->p)
753 continue;
754
755 this_err = abs(clock.dot - target);
756 if (this_err < err) {
757 *best_clock = clock;
758 err = this_err;
759 }
760 }
761 }
762 }
763 }
764
765 return (err != target);
766}
767
Ma Lingd4906092009-03-18 20:13:27 +0800768static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200769g4x_find_best_dpll(const intel_limit_t *limit,
770 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200771 int target, int refclk, intel_clock_t *match_clock,
772 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800773{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200774 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300775 struct drm_device *dev = crtc->base.dev;
Ma Lingd4906092009-03-18 20:13:27 +0800776 intel_clock_t clock;
777 int max_n;
778 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400779 /* approximately equals target * 0.00585 */
780 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800781 found = false;
782
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200783 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100784 if (intel_is_dual_link_lvds(dev))
Ma Lingd4906092009-03-18 20:13:27 +0800785 clock.p2 = limit->p2.p2_fast;
786 else
787 clock.p2 = limit->p2.p2_slow;
788 } else {
789 if (target < limit->p2.dot_limit)
790 clock.p2 = limit->p2.p2_slow;
791 else
792 clock.p2 = limit->p2.p2_fast;
793 }
794
795 memset(best_clock, 0, sizeof(*best_clock));
796 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200797 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800798 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200799 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800800 for (clock.m1 = limit->m1.max;
801 clock.m1 >= limit->m1.min; clock.m1--) {
802 for (clock.m2 = limit->m2.max;
803 clock.m2 >= limit->m2.min; clock.m2--) {
804 for (clock.p1 = limit->p1.max;
805 clock.p1 >= limit->p1.min; clock.p1--) {
806 int this_err;
807
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200808 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000809 if (!intel_PLL_is_valid(dev, limit,
810 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800811 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000812
813 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800814 if (this_err < err_most) {
815 *best_clock = clock;
816 err_most = this_err;
817 max_n = clock.n;
818 found = true;
819 }
820 }
821 }
822 }
823 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800824 return found;
825}
Ma Lingd4906092009-03-18 20:13:27 +0800826
Imre Deakd5dd62b2015-03-17 11:40:03 +0200827/*
828 * Check if the calculated PLL configuration is more optimal compared to the
829 * best configuration and error found so far. Return the calculated error.
830 */
831static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
832 const intel_clock_t *calculated_clock,
833 const intel_clock_t *best_clock,
834 unsigned int best_error_ppm,
835 unsigned int *error_ppm)
836{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200837 /*
838 * For CHV ignore the error and consider only the P value.
839 * Prefer a bigger P value based on HW requirements.
840 */
841 if (IS_CHERRYVIEW(dev)) {
842 *error_ppm = 0;
843
844 return calculated_clock->p > best_clock->p;
845 }
846
Imre Deak24be4e42015-03-17 11:40:04 +0200847 if (WARN_ON_ONCE(!target_freq))
848 return false;
849
Imre Deakd5dd62b2015-03-17 11:40:03 +0200850 *error_ppm = div_u64(1000000ULL *
851 abs(target_freq - calculated_clock->dot),
852 target_freq);
853 /*
854 * Prefer a better P value over a better (smaller) error if the error
855 * is small. Ensure this preference for future configurations too by
856 * setting the error to 0.
857 */
858 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
859 *error_ppm = 0;
860
861 return true;
862 }
863
864 return *error_ppm + 10 < best_error_ppm;
865}
866
Zhenyu Wang2c072452009-06-05 15:38:42 +0800867static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200868vlv_find_best_dpll(const intel_limit_t *limit,
869 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200870 int target, int refclk, intel_clock_t *match_clock,
871 intel_clock_t *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700872{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200873 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300874 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300875 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300876 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300877 /* min update 19.2 MHz */
878 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300879 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700880
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300881 target *= 5; /* fast clock */
882
883 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700884
885 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300886 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300887 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300888 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300889 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300890 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700891 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300892 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200893 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300894
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300895 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
896 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300897
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300898 vlv_clock(refclk, &clock);
899
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300900 if (!intel_PLL_is_valid(dev, limit,
901 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300902 continue;
903
Imre Deakd5dd62b2015-03-17 11:40:03 +0200904 if (!vlv_PLL_is_optimal(dev, target,
905 &clock,
906 best_clock,
907 bestppm, &ppm))
908 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300909
Imre Deakd5dd62b2015-03-17 11:40:03 +0200910 *best_clock = clock;
911 bestppm = ppm;
912 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700913 }
914 }
915 }
916 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700917
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300918 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700919}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700920
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300921static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200922chv_find_best_dpll(const intel_limit_t *limit,
923 struct intel_crtc_state *crtc_state,
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300924 int target, int refclk, intel_clock_t *match_clock,
925 intel_clock_t *best_clock)
926{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200927 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300928 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200929 unsigned int best_error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300930 intel_clock_t clock;
931 uint64_t m2;
932 int found = false;
933
934 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200935 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300936
937 /*
938 * Based on hardware doc, the n always set to 1, and m1 always
939 * set to 2. If requires to support 200Mhz refclk, we need to
940 * revisit this because n may not 1 anymore.
941 */
942 clock.n = 1, clock.m1 = 2;
943 target *= 5; /* fast clock */
944
945 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
946 for (clock.p2 = limit->p2.p2_fast;
947 clock.p2 >= limit->p2.p2_slow;
948 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +0200949 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300950
951 clock.p = clock.p1 * clock.p2;
952
953 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
954 clock.n) << 22, refclk * clock.m1);
955
956 if (m2 > INT_MAX/clock.m1)
957 continue;
958
959 clock.m2 = m2;
960
961 chv_clock(refclk, &clock);
962
963 if (!intel_PLL_is_valid(dev, limit, &clock))
964 continue;
965
Imre Deak9ca3ba02015-03-17 11:40:05 +0200966 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
967 best_error_ppm, &error_ppm))
968 continue;
969
970 *best_clock = clock;
971 best_error_ppm = error_ppm;
972 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300973 }
974 }
975
976 return found;
977}
978
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200979bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
980 intel_clock_t *best_clock)
981{
982 int refclk = i9xx_get_refclk(crtc_state, 0);
983
984 return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
985 target_clock, refclk, NULL, best_clock);
986}
987
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300988bool intel_crtc_active(struct drm_crtc *crtc)
989{
990 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
991
992 /* Be paranoid as we can arrive here with only partial
993 * state retrieved from the hardware during setup.
994 *
Damien Lespiau241bfc32013-09-25 16:45:37 +0100995 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300996 * as Haswell has gained clock readout/fastboot support.
997 *
Dave Airlie66e514c2014-04-03 07:51:54 +1000998 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300999 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -07001000 *
1001 * FIXME: The intel_crtc->active here should be switched to
1002 * crtc->state->active once we have proper CRTC states wired up
1003 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001004 */
Matt Roperc3d1f432015-03-09 10:19:23 -07001005 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001006 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001007}
1008
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001009enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1010 enum pipe pipe)
1011{
1012 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1013 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1014
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001015 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001016}
1017
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001018static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1019{
1020 struct drm_i915_private *dev_priv = dev->dev_private;
1021 u32 reg = PIPEDSL(pipe);
1022 u32 line1, line2;
1023 u32 line_mask;
1024
1025 if (IS_GEN2(dev))
1026 line_mask = DSL_LINEMASK_GEN2;
1027 else
1028 line_mask = DSL_LINEMASK_GEN3;
1029
1030 line1 = I915_READ(reg) & line_mask;
1031 mdelay(5);
1032 line2 = I915_READ(reg) & line_mask;
1033
1034 return line1 == line2;
1035}
1036
Keith Packardab7ad7f2010-10-03 00:33:06 -07001037/*
1038 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001039 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001040 *
1041 * After disabling a pipe, we can't wait for vblank in the usual way,
1042 * spinning on the vblank interrupt status bit, since we won't actually
1043 * see an interrupt when the pipe is disabled.
1044 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001045 * On Gen4 and above:
1046 * wait for the pipe register state bit to turn off
1047 *
1048 * Otherwise:
1049 * wait for the display line value to settle (it usually
1050 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001051 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001052 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001053static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001054{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001055 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001056 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001057 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001058 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001059
Keith Packardab7ad7f2010-10-03 00:33:06 -07001060 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001061 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001062
Keith Packardab7ad7f2010-10-03 00:33:06 -07001063 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +01001064 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1065 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001066 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001067 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001068 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001069 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001070 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001071 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001072}
1073
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001074/*
1075 * ibx_digital_port_connected - is the specified port connected?
1076 * @dev_priv: i915 private structure
1077 * @port: the port to test
1078 *
1079 * Returns true if @port is connected, false otherwise.
1080 */
1081bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
1082 struct intel_digital_port *port)
1083{
1084 u32 bit;
1085
Damien Lespiauc36346e2012-12-13 16:09:03 +00001086 if (HAS_PCH_IBX(dev_priv->dev)) {
Robin Schroereba905b2014-05-18 02:24:50 +02001087 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +00001088 case PORT_B:
1089 bit = SDE_PORTB_HOTPLUG;
1090 break;
1091 case PORT_C:
1092 bit = SDE_PORTC_HOTPLUG;
1093 break;
1094 case PORT_D:
1095 bit = SDE_PORTD_HOTPLUG;
1096 break;
1097 default:
1098 return true;
1099 }
1100 } else {
Robin Schroereba905b2014-05-18 02:24:50 +02001101 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +00001102 case PORT_B:
1103 bit = SDE_PORTB_HOTPLUG_CPT;
1104 break;
1105 case PORT_C:
1106 bit = SDE_PORTC_HOTPLUG_CPT;
1107 break;
1108 case PORT_D:
1109 bit = SDE_PORTD_HOTPLUG_CPT;
1110 break;
1111 default:
1112 return true;
1113 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001114 }
1115
1116 return I915_READ(SDEISR) & bit;
1117}
1118
Jesse Barnesb24e7172011-01-04 15:09:30 -08001119static const char *state_string(bool enabled)
1120{
1121 return enabled ? "on" : "off";
1122}
1123
1124/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001125void assert_pll(struct drm_i915_private *dev_priv,
1126 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001127{
1128 int reg;
1129 u32 val;
1130 bool cur_state;
1131
1132 reg = DPLL(pipe);
1133 val = I915_READ(reg);
1134 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001135 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001136 "PLL state assertion failure (expected %s, current %s)\n",
1137 state_string(state), state_string(cur_state));
1138}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001139
Jani Nikula23538ef2013-08-27 15:12:22 +03001140/* XXX: the dsi pll is shared between MIPI DSI ports */
1141static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1142{
1143 u32 val;
1144 bool cur_state;
1145
Ville Syrjäläa5805162015-05-26 20:42:30 +03001146 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001147 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001148 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001149
1150 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001151 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001152 "DSI PLL state assertion failure (expected %s, current %s)\n",
1153 state_string(state), state_string(cur_state));
1154}
1155#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1156#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1157
Daniel Vetter55607e82013-06-16 21:42:39 +02001158struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +02001159intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -08001160{
Daniel Vettere2b78262013-06-07 23:10:03 +02001161 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1162
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001163 if (crtc->config->shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +02001164 return NULL;
1165
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001166 return &dev_priv->shared_dplls[crtc->config->shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +02001167}
1168
Jesse Barnesb24e7172011-01-04 15:09:30 -08001169/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +02001170void assert_shared_dpll(struct drm_i915_private *dev_priv,
1171 struct intel_shared_dpll *pll,
1172 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001173{
Jesse Barnes040484a2011-01-03 12:14:26 -08001174 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +02001175 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001176
Chris Wilson92b27b02012-05-20 18:10:50 +01001177 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +02001178 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001179 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001180
Daniel Vetter53589012013-06-05 13:34:16 +02001181 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Rob Clarke2c719b2014-12-15 13:56:32 -05001182 I915_STATE_WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +02001183 "%s assertion failure (expected %s, current %s)\n",
1184 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001185}
Jesse Barnes040484a2011-01-03 12:14:26 -08001186
1187static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1188 enum pipe pipe, bool state)
1189{
1190 int reg;
1191 u32 val;
1192 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001193 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1194 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001195
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001196 if (HAS_DDI(dev_priv->dev)) {
1197 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -02001198 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001199 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -02001200 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001201 } else {
1202 reg = FDI_TX_CTL(pipe);
1203 val = I915_READ(reg);
1204 cur_state = !!(val & FDI_TX_ENABLE);
1205 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001206 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001207 "FDI TX state assertion failure (expected %s, current %s)\n",
1208 state_string(state), state_string(cur_state));
1209}
1210#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1211#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1212
1213static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1214 enum pipe pipe, bool state)
1215{
1216 int reg;
1217 u32 val;
1218 bool cur_state;
1219
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001220 reg = FDI_RX_CTL(pipe);
1221 val = I915_READ(reg);
1222 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001223 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001224 "FDI RX state assertion failure (expected %s, current %s)\n",
1225 state_string(state), state_string(cur_state));
1226}
1227#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1228#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1229
1230static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1231 enum pipe pipe)
1232{
1233 int reg;
1234 u32 val;
1235
1236 /* ILK FDI PLL is always enabled */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001237 if (INTEL_INFO(dev_priv->dev)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001238 return;
1239
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001240 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001241 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001242 return;
1243
Jesse Barnes040484a2011-01-03 12:14:26 -08001244 reg = FDI_TX_CTL(pipe);
1245 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001246 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 -08001247}
1248
Daniel Vetter55607e82013-06-16 21:42:39 +02001249void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1250 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001251{
1252 int reg;
1253 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001254 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001255
1256 reg = FDI_RX_CTL(pipe);
1257 val = I915_READ(reg);
Daniel Vetter55607e82013-06-16 21:42:39 +02001258 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001259 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001260 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1261 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001262}
1263
Daniel Vetterb680c372014-09-19 18:27:27 +02001264void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1265 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001266{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001267 struct drm_device *dev = dev_priv->dev;
1268 int pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001269 u32 val;
1270 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001271 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001272
Jani Nikulabedd4db2014-08-22 15:04:13 +03001273 if (WARN_ON(HAS_DDI(dev)))
1274 return;
1275
1276 if (HAS_PCH_SPLIT(dev)) {
1277 u32 port_sel;
1278
Jesse Barnesea0760c2011-01-04 15:09:32 -08001279 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001280 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1281
1282 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1283 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1284 panel_pipe = PIPE_B;
1285 /* XXX: else fix for eDP */
1286 } else if (IS_VALLEYVIEW(dev)) {
1287 /* presumably write lock depends on pipe, not port select */
1288 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1289 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001290 } else {
1291 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001292 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1293 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001294 }
1295
1296 val = I915_READ(pp_reg);
1297 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001298 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001299 locked = false;
1300
Rob Clarke2c719b2014-12-15 13:56:32 -05001301 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001302 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001303 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001304}
1305
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001306static void assert_cursor(struct drm_i915_private *dev_priv,
1307 enum pipe pipe, bool state)
1308{
1309 struct drm_device *dev = dev_priv->dev;
1310 bool cur_state;
1311
Paulo Zanonid9d82082014-02-27 16:30:56 -03001312 if (IS_845G(dev) || IS_I865G(dev))
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001313 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001314 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001315 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001316
Rob Clarke2c719b2014-12-15 13:56:32 -05001317 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001318 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1319 pipe_name(pipe), state_string(state), state_string(cur_state));
1320}
1321#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1322#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1323
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001324void assert_pipe(struct drm_i915_private *dev_priv,
1325 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001326{
1327 int reg;
1328 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001329 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001330 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1331 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001332
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001333 /* if we need the pipe quirk it must be always on */
1334 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1335 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001336 state = true;
1337
Daniel Vetterf458ebb2014-09-30 10:56:39 +02001338 if (!intel_display_power_is_enabled(dev_priv,
Paulo Zanonib97186f2013-05-03 12:15:36 -03001339 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001340 cur_state = false;
1341 } else {
1342 reg = PIPECONF(cpu_transcoder);
1343 val = I915_READ(reg);
1344 cur_state = !!(val & PIPECONF_ENABLE);
1345 }
1346
Rob Clarke2c719b2014-12-15 13:56:32 -05001347 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001348 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001349 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001350}
1351
Chris Wilson931872f2012-01-16 23:01:13 +00001352static void assert_plane(struct drm_i915_private *dev_priv,
1353 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001354{
1355 int reg;
1356 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001357 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001358
1359 reg = DSPCNTR(plane);
1360 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001361 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001362 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001363 "plane %c assertion failure (expected %s, current %s)\n",
1364 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001365}
1366
Chris Wilson931872f2012-01-16 23:01:13 +00001367#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1368#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1369
Jesse Barnesb24e7172011-01-04 15:09:30 -08001370static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1371 enum pipe pipe)
1372{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001373 struct drm_device *dev = dev_priv->dev;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001374 int reg, i;
1375 u32 val;
1376 int cur_pipe;
1377
Ville Syrjälä653e1022013-06-04 13:49:05 +03001378 /* Primary planes are fixed to pipes on gen4+ */
1379 if (INTEL_INFO(dev)->gen >= 4) {
Adam Jackson28c057942011-10-07 14:38:42 -04001380 reg = DSPCNTR(pipe);
1381 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001382 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001383 "plane %c assertion failure, should be disabled but not\n",
1384 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001385 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001386 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001387
Jesse Barnesb24e7172011-01-04 15:09:30 -08001388 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001389 for_each_pipe(dev_priv, i) {
Jesse Barnesb24e7172011-01-04 15:09:30 -08001390 reg = DSPCNTR(i);
1391 val = I915_READ(reg);
1392 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1393 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001394 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001395 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1396 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001397 }
1398}
1399
Jesse Barnes19332d72013-03-28 09:55:38 -07001400static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1401 enum pipe pipe)
1402{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001403 struct drm_device *dev = dev_priv->dev;
Damien Lespiau1fe47782014-03-03 17:31:47 +00001404 int reg, sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001405 u32 val;
1406
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001407 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001408 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001409 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001410 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001411 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1412 sprite, pipe_name(pipe));
1413 }
1414 } else if (IS_VALLEYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001415 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +00001416 reg = SPCNTR(pipe, sprite);
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001417 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001418 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001419 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001420 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001421 }
1422 } else if (INTEL_INFO(dev)->gen >= 7) {
1423 reg = SPRCTL(pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001424 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001425 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001426 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001427 plane_name(pipe), pipe_name(pipe));
1428 } else if (INTEL_INFO(dev)->gen >= 5) {
1429 reg = DVSCNTR(pipe);
1430 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001431 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001432 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1433 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001434 }
1435}
1436
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001437static void assert_vblank_disabled(struct drm_crtc *crtc)
1438{
Rob Clarke2c719b2014-12-15 13:56:32 -05001439 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001440 drm_crtc_vblank_put(crtc);
1441}
1442
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001443static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
Jesse Barnes92f25842011-01-04 15:09:34 -08001444{
1445 u32 val;
1446 bool enabled;
1447
Rob Clarke2c719b2014-12-15 13:56:32 -05001448 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001449
Jesse Barnes92f25842011-01-04 15:09:34 -08001450 val = I915_READ(PCH_DREF_CONTROL);
1451 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1452 DREF_SUPERSPREAD_SOURCE_MASK));
Rob Clarke2c719b2014-12-15 13:56:32 -05001453 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
Jesse Barnes92f25842011-01-04 15:09:34 -08001454}
1455
Daniel Vetterab9412b2013-05-03 11:49:46 +02001456static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1457 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001458{
1459 int reg;
1460 u32 val;
1461 bool enabled;
1462
Daniel Vetterab9412b2013-05-03 11:49:46 +02001463 reg = PCH_TRANSCONF(pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001464 val = I915_READ(reg);
1465 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001466 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001467 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1468 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001469}
1470
Keith Packard4e634382011-08-06 10:39:45 -07001471static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1472 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001473{
1474 if ((val & DP_PORT_EN) == 0)
1475 return false;
1476
1477 if (HAS_PCH_CPT(dev_priv->dev)) {
1478 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1479 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1480 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1481 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001482 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1483 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1484 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001485 } else {
1486 if ((val & DP_PIPE_MASK) != (pipe << 30))
1487 return false;
1488 }
1489 return true;
1490}
1491
Keith Packard1519b992011-08-06 10:35:34 -07001492static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1493 enum pipe pipe, u32 val)
1494{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001495 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001496 return false;
1497
1498 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001499 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001500 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001501 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1502 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1503 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001504 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001505 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001506 return false;
1507 }
1508 return true;
1509}
1510
1511static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1512 enum pipe pipe, u32 val)
1513{
1514 if ((val & LVDS_PORT_EN) == 0)
1515 return false;
1516
1517 if (HAS_PCH_CPT(dev_priv->dev)) {
1518 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1519 return false;
1520 } else {
1521 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1522 return false;
1523 }
1524 return true;
1525}
1526
1527static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1528 enum pipe pipe, u32 val)
1529{
1530 if ((val & ADPA_DAC_ENABLE) == 0)
1531 return false;
1532 if (HAS_PCH_CPT(dev_priv->dev)) {
1533 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1534 return false;
1535 } else {
1536 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1537 return false;
1538 }
1539 return true;
1540}
1541
Jesse Barnes291906f2011-02-02 12:28:03 -08001542static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001543 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001544{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001545 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001546 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001547 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001548 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001549
Rob Clarke2c719b2014-12-15 13:56:32 -05001550 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001551 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001552 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001553}
1554
1555static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1556 enum pipe pipe, int reg)
1557{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001558 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001559 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001560 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001561 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001562
Rob Clarke2c719b2014-12-15 13:56:32 -05001563 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001564 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001565 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001566}
1567
1568static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1569 enum pipe pipe)
1570{
1571 int reg;
1572 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001573
Keith Packardf0575e92011-07-25 22:12:43 -07001574 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1575 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1576 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001577
1578 reg = PCH_ADPA;
1579 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001580 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001581 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001582 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001583
1584 reg = PCH_LVDS;
1585 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001586 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001587 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001588 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001589
Paulo Zanonie2debe92013-02-18 19:00:27 -03001590 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1591 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1592 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001593}
1594
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001595static void intel_init_dpio(struct drm_device *dev)
1596{
1597 struct drm_i915_private *dev_priv = dev->dev_private;
1598
1599 if (!IS_VALLEYVIEW(dev))
1600 return;
1601
Chon Ming Leea09cadd2014-04-09 13:28:14 +03001602 /*
1603 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1604 * CHV x1 PHY (DP/HDMI D)
1605 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1606 */
1607 if (IS_CHERRYVIEW(dev)) {
1608 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1609 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1610 } else {
1611 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1612 }
Jesse Barnes5382f5f352013-12-16 16:34:24 -08001613}
1614
Ville Syrjäläd288f652014-10-28 13:20:22 +02001615static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001616 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001617{
Daniel Vetter426115c2013-07-11 22:13:42 +02001618 struct drm_device *dev = crtc->base.dev;
1619 struct drm_i915_private *dev_priv = dev->dev_private;
1620 int reg = DPLL(crtc->pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02001621 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001622
Daniel Vetter426115c2013-07-11 22:13:42 +02001623 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001624
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001625 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001626 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1627
1628 /* PLL is protected by panel, make sure we can write it */
Jani Nikula6a9e7362014-08-22 15:06:35 +03001629 if (IS_MOBILE(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001630 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001631
Daniel Vetter426115c2013-07-11 22:13:42 +02001632 I915_WRITE(reg, dpll);
1633 POSTING_READ(reg);
1634 udelay(150);
1635
1636 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1637 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1638
Ville Syrjäläd288f652014-10-28 13:20:22 +02001639 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
Daniel Vetter426115c2013-07-11 22:13:42 +02001640 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001641
1642 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001643 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001644 POSTING_READ(reg);
1645 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001646 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001647 POSTING_READ(reg);
1648 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001649 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001650 POSTING_READ(reg);
1651 udelay(150); /* wait for warmup */
1652}
1653
Ville Syrjäläd288f652014-10-28 13:20:22 +02001654static void chv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001655 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001656{
1657 struct drm_device *dev = crtc->base.dev;
1658 struct drm_i915_private *dev_priv = dev->dev_private;
1659 int pipe = crtc->pipe;
1660 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001661 u32 tmp;
1662
1663 assert_pipe_disabled(dev_priv, crtc->pipe);
1664
1665 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1666
Ville Syrjäläa5805162015-05-26 20:42:30 +03001667 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001668
1669 /* Enable back the 10bit clock to display controller */
1670 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1671 tmp |= DPIO_DCLKP_EN;
1672 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1673
Ville Syrjälä54433e92015-05-26 20:42:31 +03001674 mutex_unlock(&dev_priv->sb_lock);
1675
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001676 /*
1677 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1678 */
1679 udelay(1);
1680
1681 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001682 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001683
1684 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001685 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001686 DRM_ERROR("PLL %d failed to lock\n", pipe);
1687
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001688 /* not sure when this should be written */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001689 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001690 POSTING_READ(DPLL_MD(pipe));
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001691}
1692
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001693static int intel_num_dvo_pipes(struct drm_device *dev)
1694{
1695 struct intel_crtc *crtc;
1696 int count = 0;
1697
1698 for_each_intel_crtc(dev, crtc)
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001699 count += crtc->base.state->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001700 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001701
1702 return count;
1703}
1704
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001705static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001706{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001707 struct drm_device *dev = crtc->base.dev;
1708 struct drm_i915_private *dev_priv = dev->dev_private;
1709 int reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001710 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001711
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001712 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001713
1714 /* No really, not for ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001715 BUG_ON(INTEL_INFO(dev)->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001716
1717 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001718 if (IS_MOBILE(dev) && !IS_I830(dev))
1719 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001720
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001721 /* Enable DVO 2x clock on both PLLs if necessary */
1722 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1723 /*
1724 * It appears to be important that we don't enable this
1725 * for the current pipe before otherwise configuring the
1726 * PLL. No idea how this should be handled if multiple
1727 * DVO outputs are enabled simultaneosly.
1728 */
1729 dpll |= DPLL_DVO_2X_MODE;
1730 I915_WRITE(DPLL(!crtc->pipe),
1731 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1732 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001733
1734 /* Wait for the clocks to stabilize. */
1735 POSTING_READ(reg);
1736 udelay(150);
1737
1738 if (INTEL_INFO(dev)->gen >= 4) {
1739 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001740 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001741 } else {
1742 /* The pixel multiplier can only be updated once the
1743 * DPLL is enabled and the clocks are stable.
1744 *
1745 * So write it again.
1746 */
1747 I915_WRITE(reg, dpll);
1748 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001749
1750 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001751 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001752 POSTING_READ(reg);
1753 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001754 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001755 POSTING_READ(reg);
1756 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001757 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001758 POSTING_READ(reg);
1759 udelay(150); /* wait for warmup */
1760}
1761
1762/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001763 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001764 * @dev_priv: i915 private structure
1765 * @pipe: pipe PLL to disable
1766 *
1767 * Disable the PLL for @pipe, making sure the pipe is off first.
1768 *
1769 * Note! This is for pre-ILK only.
1770 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001771static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001772{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001773 struct drm_device *dev = crtc->base.dev;
1774 struct drm_i915_private *dev_priv = dev->dev_private;
1775 enum pipe pipe = crtc->pipe;
1776
1777 /* Disable DVO 2x clock on both PLLs if necessary */
1778 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001779 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001780 !intel_num_dvo_pipes(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001781 I915_WRITE(DPLL(PIPE_B),
1782 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1783 I915_WRITE(DPLL(PIPE_A),
1784 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1785 }
1786
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001787 /* Don't disable pipe or pipe PLLs if needed */
1788 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1789 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001790 return;
1791
1792 /* Make sure the pipe isn't still relying on us */
1793 assert_pipe_disabled(dev_priv, pipe);
1794
Daniel Vetter50b44a42013-06-05 13:34:33 +02001795 I915_WRITE(DPLL(pipe), 0);
1796 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001797}
1798
Jesse Barnesf6071162013-10-01 10:41:38 -07001799static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1800{
1801 u32 val = 0;
1802
1803 /* Make sure the pipe isn't still relying on us */
1804 assert_pipe_disabled(dev_priv, pipe);
1805
Imre Deake5cbfbf2014-01-09 17:08:16 +02001806 /*
1807 * Leave integrated clock source and reference clock enabled for pipe B.
1808 * The latter is needed for VGA hotplug / manual detection.
1809 */
Jesse Barnesf6071162013-10-01 10:41:38 -07001810 if (pipe == PIPE_B)
Imre Deake5cbfbf2014-01-09 17:08:16 +02001811 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07001812 I915_WRITE(DPLL(pipe), val);
1813 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001814
1815}
1816
1817static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1818{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001819 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001820 u32 val;
1821
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001822 /* Make sure the pipe isn't still relying on us */
1823 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001824
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001825 /* Set PLL en = 0 */
Ville Syrjäläd17ec4c2014-06-28 02:03:59 +03001826 val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001827 if (pipe != PIPE_A)
1828 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1829 I915_WRITE(DPLL(pipe), val);
1830 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001831
Ville Syrjäläa5805162015-05-26 20:42:30 +03001832 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001833
1834 /* Disable 10bit clock to display controller */
1835 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1836 val &= ~DPIO_DCLKP_EN;
1837 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1838
Ville Syrjälä61407f62014-05-27 16:32:55 +03001839 /* disable left/right clock distribution */
1840 if (pipe != PIPE_B) {
1841 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1842 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1843 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1844 } else {
1845 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1846 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1847 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1848 }
1849
Ville Syrjäläa5805162015-05-26 20:42:30 +03001850 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001851}
1852
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001853void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001854 struct intel_digital_port *dport,
1855 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001856{
1857 u32 port_mask;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001858 int dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001859
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001860 switch (dport->port) {
1861 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001862 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001863 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001864 break;
1865 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001866 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001867 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001868 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001869 break;
1870 case PORT_D:
1871 port_mask = DPLL_PORTD_READY_MASK;
1872 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001873 break;
1874 default:
1875 BUG();
1876 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001877
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001878 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1879 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1880 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001881}
1882
Daniel Vetterb14b1052014-04-24 23:55:13 +02001883static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1884{
1885 struct drm_device *dev = crtc->base.dev;
1886 struct drm_i915_private *dev_priv = dev->dev_private;
1887 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1888
Chris Wilsonbe19f0f2014-05-28 16:16:42 +01001889 if (WARN_ON(pll == NULL))
1890 return;
1891
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001892 WARN_ON(!pll->config.crtc_mask);
Daniel Vetterb14b1052014-04-24 23:55:13 +02001893 if (pll->active == 0) {
1894 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1895 WARN_ON(pll->on);
1896 assert_shared_dpll_disabled(dev_priv, pll);
1897
1898 pll->mode_set(dev_priv, pll);
1899 }
1900}
1901
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001902/**
Daniel Vetter85b38942014-04-24 23:55:14 +02001903 * intel_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001904 * @dev_priv: i915 private structure
1905 * @pipe: pipe PLL to enable
1906 *
1907 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1908 * drives the transcoder clock.
1909 */
Daniel Vetter85b38942014-04-24 23:55:14 +02001910static void intel_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001911{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001912 struct drm_device *dev = crtc->base.dev;
1913 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001914 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001915
Daniel Vetter87a875b2013-06-05 13:34:19 +02001916 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001917 return;
1918
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001919 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001920 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001921
Damien Lespiau74dd6922014-07-29 18:06:17 +01001922 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
Daniel Vetter46edb022013-06-05 13:34:12 +02001923 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001924 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001925
Daniel Vettercdbd2312013-06-05 13:34:03 +02001926 if (pll->active++) {
1927 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001928 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001929 return;
1930 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001931 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001932
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001933 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1934
Daniel Vetter46edb022013-06-05 13:34:12 +02001935 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001936 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001937 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001938}
1939
Damien Lespiauf6daaec2014-08-09 23:00:56 +01001940static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001941{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001942 struct drm_device *dev = crtc->base.dev;
1943 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001944 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001945
Jesse Barnes92f25842011-01-04 15:09:34 -08001946 /* PCH only available on ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001947 BUG_ON(INTEL_INFO(dev)->gen < 5);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02001948 if (pll == NULL)
1949 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001950
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02001951 if (WARN_ON(!(pll->config.crtc_mask & (1 << drm_crtc_index(&crtc->base)))))
Chris Wilson48da64a2012-05-13 20:16:12 +01001952 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001953
Daniel Vetter46edb022013-06-05 13:34:12 +02001954 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1955 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001956 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001957
Chris Wilson48da64a2012-05-13 20:16:12 +01001958 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001959 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001960 return;
1961 }
1962
Daniel Vettere9d69442013-06-05 13:34:15 +02001963 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001964 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001965 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001966 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001967
Daniel Vetter46edb022013-06-05 13:34:12 +02001968 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001969 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001970 pll->on = false;
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001971
1972 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
Jesse Barnes92f25842011-01-04 15:09:34 -08001973}
1974
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001975static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1976 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001977{
Daniel Vetter23670b322012-11-01 09:15:30 +01001978 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001979 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001980 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001981 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001982
1983 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001984 BUG_ON(!HAS_PCH_SPLIT(dev));
Jesse Barnes040484a2011-01-03 12:14:26 -08001985
1986 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001987 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001988 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001989
1990 /* FDI must be feeding us bits for PCH ports */
1991 assert_fdi_tx_enabled(dev_priv, pipe);
1992 assert_fdi_rx_enabled(dev_priv, pipe);
1993
Daniel Vetter23670b322012-11-01 09:15:30 +01001994 if (HAS_PCH_CPT(dev)) {
1995 /* Workaround: Set the timing override bit before enabling the
1996 * pch transcoder. */
1997 reg = TRANS_CHICKEN2(pipe);
1998 val = I915_READ(reg);
1999 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
2000 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03002001 }
Daniel Vetter23670b322012-11-01 09:15:30 +01002002
Daniel Vetterab9412b2013-05-03 11:49:46 +02002003 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002004 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002005 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07002006
2007 if (HAS_PCH_IBX(dev_priv->dev)) {
2008 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03002009 * Make the BPC in transcoder be consistent with
2010 * that in pipeconf reg. For HDMI we must use 8bpc
2011 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07002012 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002013 val &= ~PIPECONF_BPC_MASK;
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03002014 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
2015 val |= PIPECONF_8BPC;
2016 else
2017 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07002018 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002019
2020 val &= ~TRANS_INTERLACE_MASK;
2021 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002022 if (HAS_PCH_IBX(dev_priv->dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002023 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002024 val |= TRANS_LEGACY_INTERLACED_ILK;
2025 else
2026 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002027 else
2028 val |= TRANS_PROGRESSIVE;
2029
Jesse Barnes040484a2011-01-03 12:14:26 -08002030 I915_WRITE(reg, val | TRANS_ENABLE);
2031 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002032 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08002033}
2034
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002035static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02002036 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08002037{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002038 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002039
2040 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03002041 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002042
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002043 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01002044 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02002045 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002046
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002047 /* Workaround: set timing override bit. */
2048 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002049 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002050 I915_WRITE(_TRANSA_CHICKEN2, val);
2051
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02002052 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02002053 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002054
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02002055 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
2056 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02002057 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002058 else
2059 val |= TRANS_PROGRESSIVE;
2060
Daniel Vetterab9412b2013-05-03 11:49:46 +02002061 I915_WRITE(LPT_TRANSCONF, val);
2062 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02002063 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002064}
2065
Paulo Zanonib8a4f402012-10-31 18:12:42 -02002066static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
2067 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08002068{
Daniel Vetter23670b322012-11-01 09:15:30 +01002069 struct drm_device *dev = dev_priv->dev;
2070 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08002071
2072 /* FDI relies on the transcoder */
2073 assert_fdi_tx_disabled(dev_priv, pipe);
2074 assert_fdi_rx_disabled(dev_priv, pipe);
2075
Jesse Barnes291906f2011-02-02 12:28:03 -08002076 /* Ports must be off as well */
2077 assert_pch_ports_disabled(dev_priv, pipe);
2078
Daniel Vetterab9412b2013-05-03 11:49:46 +02002079 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002080 val = I915_READ(reg);
2081 val &= ~TRANS_ENABLE;
2082 I915_WRITE(reg, val);
2083 /* wait for PCH transcoder off, transcoder state */
2084 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002085 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01002086
2087 if (!HAS_PCH_IBX(dev)) {
2088 /* Workaround: Clear the timing override chicken bit again. */
2089 reg = TRANS_CHICKEN2(pipe);
2090 val = I915_READ(reg);
2091 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2092 I915_WRITE(reg, val);
2093 }
Jesse Barnes040484a2011-01-03 12:14:26 -08002094}
2095
Paulo Zanoniab4d9662012-10-31 18:12:55 -02002096static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002097{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002098 u32 val;
2099
Daniel Vetterab9412b2013-05-03 11:49:46 +02002100 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002101 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02002102 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002103 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02002104 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02002105 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002106
2107 /* Workaround: clear timing override bit. */
2108 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002109 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002110 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08002111}
2112
2113/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002114 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02002115 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08002116 *
Paulo Zanoni03722642014-01-17 13:51:09 -02002117 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08002118 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002119 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02002120static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002121{
Paulo Zanoni03722642014-01-17 13:51:09 -02002122 struct drm_device *dev = crtc->base.dev;
2123 struct drm_i915_private *dev_priv = dev->dev_private;
2124 enum pipe pipe = crtc->pipe;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002125 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2126 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002127 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002128 int reg;
2129 u32 val;
2130
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002131 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002132 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002133 assert_sprites_disabled(dev_priv, pipe);
2134
Paulo Zanoni681e5812012-12-06 11:12:38 -02002135 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002136 pch_transcoder = TRANSCODER_A;
2137 else
2138 pch_transcoder = pipe;
2139
Jesse Barnesb24e7172011-01-04 15:09:30 -08002140 /*
2141 * A pipe without a PLL won't actually be able to drive bits from
2142 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2143 * need the check.
2144 */
Imre Deak50360402015-01-16 00:55:16 -08002145 if (HAS_GMCH_DISPLAY(dev_priv->dev))
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002146 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03002147 assert_dsi_pll_enabled(dev_priv);
2148 else
2149 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002150 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002151 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002152 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002153 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002154 assert_fdi_tx_pll_enabled(dev_priv,
2155 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08002156 }
2157 /* FIXME: assert CPU port conditions for SNB+ */
2158 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08002159
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002160 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002161 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002162 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002163 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2164 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002165 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002166 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002167
2168 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002169 POSTING_READ(reg);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002170}
2171
2172/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002173 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002174 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002175 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002176 * Disable the pipe of @crtc, making sure that various hardware
2177 * specific requirements are met, if applicable, e.g. plane
2178 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002179 *
2180 * Will wait until the pipe has shut down before returning.
2181 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002182static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002183{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002184 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002185 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002186 enum pipe pipe = crtc->pipe;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002187 int reg;
2188 u32 val;
2189
2190 /*
2191 * Make sure planes won't keep trying to pump pixels to us,
2192 * or we might hang the display.
2193 */
2194 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002195 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002196 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002197
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002198 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002199 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002200 if ((val & PIPECONF_ENABLE) == 0)
2201 return;
2202
Ville Syrjälä67adc642014-08-15 01:21:57 +03002203 /*
2204 * Double wide has implications for planes
2205 * so best keep it disabled when not needed.
2206 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002207 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002208 val &= ~PIPECONF_DOUBLE_WIDE;
2209
2210 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002211 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2212 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002213 val &= ~PIPECONF_ENABLE;
2214
2215 I915_WRITE(reg, val);
2216 if ((val & PIPECONF_ENABLE) == 0)
2217 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002218}
2219
Chris Wilson693db182013-03-05 14:52:39 +00002220static bool need_vtd_wa(struct drm_device *dev)
2221{
2222#ifdef CONFIG_INTEL_IOMMU
2223 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2224 return true;
2225#endif
2226 return false;
2227}
2228
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002229unsigned int
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002230intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
2231 uint64_t fb_format_modifier)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002232{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002233 unsigned int tile_height;
2234 uint32_t pixel_bytes;
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002235
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002236 switch (fb_format_modifier) {
2237 case DRM_FORMAT_MOD_NONE:
2238 tile_height = 1;
2239 break;
2240 case I915_FORMAT_MOD_X_TILED:
2241 tile_height = IS_GEN2(dev) ? 16 : 8;
2242 break;
2243 case I915_FORMAT_MOD_Y_TILED:
2244 tile_height = 32;
2245 break;
2246 case I915_FORMAT_MOD_Yf_TILED:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002247 pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
2248 switch (pixel_bytes) {
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002249 default:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002250 case 1:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002251 tile_height = 64;
2252 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002253 case 2:
2254 case 4:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002255 tile_height = 32;
2256 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002257 case 8:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002258 tile_height = 16;
2259 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002260 case 16:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002261 WARN_ONCE(1,
2262 "128-bit pixels are not supported for display!");
2263 tile_height = 16;
2264 break;
2265 }
2266 break;
2267 default:
2268 MISSING_CASE(fb_format_modifier);
2269 tile_height = 1;
2270 break;
2271 }
Daniel Vetter091df6c2015-02-10 17:16:10 +00002272
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002273 return tile_height;
2274}
2275
2276unsigned int
2277intel_fb_align_height(struct drm_device *dev, unsigned int height,
2278 uint32_t pixel_format, uint64_t fb_format_modifier)
2279{
2280 return ALIGN(height, intel_tile_height(dev, pixel_format,
2281 fb_format_modifier));
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002282}
2283
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002284static int
2285intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
2286 const struct drm_plane_state *plane_state)
2287{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002288 struct intel_rotation_info *info = &view->rotation_info;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002289
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002290 *view = i915_ggtt_view_normal;
2291
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002292 if (!plane_state)
2293 return 0;
2294
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002295 if (!intel_rotation_90_or_270(plane_state->rotation))
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002296 return 0;
2297
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002298 *view = i915_ggtt_view_rotated;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002299
2300 info->height = fb->height;
2301 info->pixel_format = fb->pixel_format;
2302 info->pitch = fb->pitches[0];
2303 info->fb_modifier = fb->modifier[0];
2304
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002305 return 0;
2306}
2307
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002308static unsigned int intel_linear_alignment(struct drm_i915_private *dev_priv)
2309{
2310 if (INTEL_INFO(dev_priv)->gen >= 9)
2311 return 256 * 1024;
Ville Syrjälä985b8bb2015-06-11 16:31:15 +03002312 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
2313 IS_VALLEYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002314 return 128 * 1024;
2315 else if (INTEL_INFO(dev_priv)->gen >= 4)
2316 return 4 * 1024;
2317 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03002318 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002319}
2320
Chris Wilson127bd2a2010-07-23 23:32:05 +01002321int
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002322intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2323 struct drm_framebuffer *fb,
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002324 const struct drm_plane_state *plane_state,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002325 struct intel_engine_cs *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002326{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002327 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002328 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002329 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002330 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002331 u32 alignment;
2332 int ret;
2333
Matt Roperebcdd392014-07-09 16:22:11 -07002334 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2335
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002336 switch (fb->modifier[0]) {
2337 case DRM_FORMAT_MOD_NONE:
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002338 alignment = intel_linear_alignment(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002339 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002340 case I915_FORMAT_MOD_X_TILED:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002341 if (INTEL_INFO(dev)->gen >= 9)
2342 alignment = 256 * 1024;
2343 else {
2344 /* pin() will align the object as required by fence */
2345 alignment = 0;
2346 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002347 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002348 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiau1327b9a2015-02-27 11:15:20 +00002349 case I915_FORMAT_MOD_Yf_TILED:
2350 if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2351 "Y tiling bo slipped through, driver bug!\n"))
2352 return -EINVAL;
2353 alignment = 1 * 1024 * 1024;
2354 break;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002355 default:
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002356 MISSING_CASE(fb->modifier[0]);
2357 return -EINVAL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002358 }
2359
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002360 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2361 if (ret)
2362 return ret;
2363
Chris Wilson693db182013-03-05 14:52:39 +00002364 /* Note that the w/a also requires 64 PTE of padding following the
2365 * bo. We currently fill all unused PTE with the shadow page and so
2366 * we should always have valid PTE following the scanout preventing
2367 * the VT-d warning.
2368 */
2369 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2370 alignment = 256 * 1024;
2371
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002372 /*
2373 * Global gtt pte registers are special registers which actually forward
2374 * writes to a chunk of system memory. Which means that there is no risk
2375 * that the register values disappear as soon as we call
2376 * intel_runtime_pm_put(), so it is correct to wrap only the
2377 * pin/unpin/fence and not more.
2378 */
2379 intel_runtime_pm_get(dev_priv);
2380
Chris Wilsonce453d82011-02-21 14:43:56 +00002381 dev_priv->mm.interruptible = false;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00002382 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002383 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002384 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00002385 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002386
2387 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2388 * fence, whereas 965+ only requires a fence if using
2389 * framebuffer compression. For simplicity, we always install
2390 * a fence as the cost is not that onerous.
2391 */
Chris Wilson06d98132012-04-17 15:31:24 +01002392 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002393 if (ret)
2394 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002395
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002396 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002397
Chris Wilsonce453d82011-02-21 14:43:56 +00002398 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002399 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002400 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002401
2402err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002403 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilsonce453d82011-02-21 14:43:56 +00002404err_interruptible:
2405 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002406 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002407 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002408}
2409
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002410static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
2411 const struct drm_plane_state *plane_state)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002412{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002413 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002414 struct i915_ggtt_view view;
2415 int ret;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002416
Matt Roperebcdd392014-07-09 16:22:11 -07002417 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2418
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002419 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2420 WARN_ONCE(ret, "Couldn't get view from plane state!");
2421
Chris Wilson1690e1e2011-12-14 13:57:08 +01002422 i915_gem_object_unpin_fence(obj);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002423 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002424}
2425
Daniel Vetterc2c75132012-07-05 12:17:30 +02002426/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2427 * is assumed to be a power-of-two. */
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002428unsigned long intel_gen4_compute_page_offset(struct drm_i915_private *dev_priv,
2429 int *x, int *y,
Chris Wilsonbc752862013-02-21 20:04:31 +00002430 unsigned int tiling_mode,
2431 unsigned int cpp,
2432 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002433{
Chris Wilsonbc752862013-02-21 20:04:31 +00002434 if (tiling_mode != I915_TILING_NONE) {
2435 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002436
Chris Wilsonbc752862013-02-21 20:04:31 +00002437 tile_rows = *y / 8;
2438 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002439
Chris Wilsonbc752862013-02-21 20:04:31 +00002440 tiles = *x / (512/cpp);
2441 *x %= 512/cpp;
2442
2443 return tile_rows * pitch * 8 + tiles * 4096;
2444 } else {
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002445 unsigned int alignment = intel_linear_alignment(dev_priv) - 1;
Chris Wilsonbc752862013-02-21 20:04:31 +00002446 unsigned int offset;
2447
2448 offset = *y * pitch + *x * cpp;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002449 *y = (offset & alignment) / pitch;
2450 *x = ((offset & alignment) - *y * pitch) / cpp;
2451 return offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002452 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002453}
2454
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002455static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002456{
2457 switch (format) {
2458 case DISPPLANE_8BPP:
2459 return DRM_FORMAT_C8;
2460 case DISPPLANE_BGRX555:
2461 return DRM_FORMAT_XRGB1555;
2462 case DISPPLANE_BGRX565:
2463 return DRM_FORMAT_RGB565;
2464 default:
2465 case DISPPLANE_BGRX888:
2466 return DRM_FORMAT_XRGB8888;
2467 case DISPPLANE_RGBX888:
2468 return DRM_FORMAT_XBGR8888;
2469 case DISPPLANE_BGRX101010:
2470 return DRM_FORMAT_XRGB2101010;
2471 case DISPPLANE_RGBX101010:
2472 return DRM_FORMAT_XBGR2101010;
2473 }
2474}
2475
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002476static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2477{
2478 switch (format) {
2479 case PLANE_CTL_FORMAT_RGB_565:
2480 return DRM_FORMAT_RGB565;
2481 default:
2482 case PLANE_CTL_FORMAT_XRGB_8888:
2483 if (rgb_order) {
2484 if (alpha)
2485 return DRM_FORMAT_ABGR8888;
2486 else
2487 return DRM_FORMAT_XBGR8888;
2488 } else {
2489 if (alpha)
2490 return DRM_FORMAT_ARGB8888;
2491 else
2492 return DRM_FORMAT_XRGB8888;
2493 }
2494 case PLANE_CTL_FORMAT_XRGB_2101010:
2495 if (rgb_order)
2496 return DRM_FORMAT_XBGR2101010;
2497 else
2498 return DRM_FORMAT_XRGB2101010;
2499 }
2500}
2501
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002502static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002503intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2504 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002505{
2506 struct drm_device *dev = crtc->base.dev;
2507 struct drm_i915_gem_object *obj = NULL;
2508 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002509 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002510 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2511 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2512 PAGE_SIZE);
2513
2514 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002515
Chris Wilsonff2652e2014-03-10 08:07:02 +00002516 if (plane_config->size == 0)
2517 return false;
2518
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002519 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2520 base_aligned,
2521 base_aligned,
2522 size_aligned);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002523 if (!obj)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002524 return false;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002525
Damien Lespiau49af4492015-01-20 12:51:44 +00002526 obj->tiling_mode = plane_config->tiling;
2527 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002528 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002529
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002530 mode_cmd.pixel_format = fb->pixel_format;
2531 mode_cmd.width = fb->width;
2532 mode_cmd.height = fb->height;
2533 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002534 mode_cmd.modifier[0] = fb->modifier[0];
2535 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002536
2537 mutex_lock(&dev->struct_mutex);
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002538 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002539 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002540 DRM_DEBUG_KMS("intel fb init failed\n");
2541 goto out_unref_obj;
2542 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002543 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002544
Daniel Vetterf6936e22015-03-26 12:17:05 +01002545 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002546 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002547
2548out_unref_obj:
2549 drm_gem_object_unreference(&obj->base);
2550 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002551 return false;
2552}
2553
Matt Roperafd65eb2015-02-03 13:10:04 -08002554/* Update plane->state->fb to match plane->fb after driver-internal updates */
2555static void
2556update_state_fb(struct drm_plane *plane)
2557{
2558 if (plane->fb == plane->state->fb)
2559 return;
2560
2561 if (plane->state->fb)
2562 drm_framebuffer_unreference(plane->state->fb);
2563 plane->state->fb = plane->fb;
2564 if (plane->state->fb)
2565 drm_framebuffer_reference(plane->state->fb);
2566}
2567
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002568static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002569intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2570 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002571{
2572 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002573 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002574 struct drm_crtc *c;
2575 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002576 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002577 struct drm_plane *primary = intel_crtc->base.primary;
2578 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002579
Damien Lespiau2d140302015-02-05 17:22:18 +00002580 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002581 return;
2582
Daniel Vetterf6936e22015-03-26 12:17:05 +01002583 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002584 fb = &plane_config->fb->base;
2585 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002586 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002587
Damien Lespiau2d140302015-02-05 17:22:18 +00002588 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002589
2590 /*
2591 * Failed to alloc the obj, check to see if we should share
2592 * an fb with another CRTC instead
2593 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002594 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002595 i = to_intel_crtc(c);
2596
2597 if (c == &intel_crtc->base)
2598 continue;
2599
Matt Roper2ff8fde2014-07-08 07:50:07 -07002600 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002601 continue;
2602
Daniel Vetter88595ac2015-03-26 12:42:24 +01002603 fb = c->primary->fb;
2604 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002605 continue;
2606
Daniel Vetter88595ac2015-03-26 12:42:24 +01002607 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002608 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002609 drm_framebuffer_reference(fb);
2610 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002611 }
2612 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002613
2614 return;
2615
2616valid_fb:
2617 obj = intel_fb_obj(fb);
2618 if (obj->tiling_mode != I915_TILING_NONE)
2619 dev_priv->preserve_bios_swizzle = true;
2620
2621 primary->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002622 primary->crtc = primary->state->crtc = &intel_crtc->base;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002623 update_state_fb(primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002624 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
Daniel Vetter88595ac2015-03-26 12:42:24 +01002625 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002626}
2627
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002628static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2629 struct drm_framebuffer *fb,
2630 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002631{
2632 struct drm_device *dev = crtc->dev;
2633 struct drm_i915_private *dev_priv = dev->dev_private;
2634 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002635 struct drm_plane *primary = crtc->primary;
2636 bool visible = to_intel_plane_state(primary->state)->visible;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002637 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002638 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002639 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002640 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002641 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302642 int pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002643
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002644 if (!visible || !fb) {
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002645 I915_WRITE(reg, 0);
2646 if (INTEL_INFO(dev)->gen >= 4)
2647 I915_WRITE(DSPSURF(plane), 0);
2648 else
2649 I915_WRITE(DSPADDR(plane), 0);
2650 POSTING_READ(reg);
2651 return;
2652 }
2653
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002654 obj = intel_fb_obj(fb);
2655 if (WARN_ON(obj == NULL))
2656 return;
2657
2658 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2659
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002660 dspcntr = DISPPLANE_GAMMA_ENABLE;
2661
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002662 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002663
2664 if (INTEL_INFO(dev)->gen < 4) {
2665 if (intel_crtc->pipe == PIPE_B)
2666 dspcntr |= DISPPLANE_SEL_PIPE_B;
2667
2668 /* pipesrc and dspsize control the size that is scaled from,
2669 * which should always be the user's requested size.
2670 */
2671 I915_WRITE(DSPSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002672 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2673 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002674 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002675 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2676 I915_WRITE(PRIMSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002677 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2678 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002679 I915_WRITE(PRIMPOS(plane), 0);
2680 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002681 }
2682
Ville Syrjälä57779d02012-10-31 17:50:14 +02002683 switch (fb->pixel_format) {
2684 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002685 dspcntr |= DISPPLANE_8BPP;
2686 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002687 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002688 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002689 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002690 case DRM_FORMAT_RGB565:
2691 dspcntr |= DISPPLANE_BGRX565;
2692 break;
2693 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002694 dspcntr |= DISPPLANE_BGRX888;
2695 break;
2696 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002697 dspcntr |= DISPPLANE_RGBX888;
2698 break;
2699 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002700 dspcntr |= DISPPLANE_BGRX101010;
2701 break;
2702 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002703 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002704 break;
2705 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002706 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002707 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002708
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002709 if (INTEL_INFO(dev)->gen >= 4 &&
2710 obj->tiling_mode != I915_TILING_NONE)
2711 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002712
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002713 if (IS_G4X(dev))
2714 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2715
Ville Syrjäläb98971272014-08-27 16:51:22 +03002716 linear_offset = y * fb->pitches[0] + x * pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002717
Daniel Vetterc2c75132012-07-05 12:17:30 +02002718 if (INTEL_INFO(dev)->gen >= 4) {
2719 intel_crtc->dspaddr_offset =
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002720 intel_gen4_compute_page_offset(dev_priv,
2721 &x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002722 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002723 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002724 linear_offset -= intel_crtc->dspaddr_offset;
2725 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002726 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002727 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002728
Matt Roper8e7d6882015-01-21 16:35:41 -08002729 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302730 dspcntr |= DISPPLANE_ROTATE_180;
2731
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002732 x += (intel_crtc->config->pipe_src_w - 1);
2733 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302734
2735 /* Finding the last pixel of the last line of the display
2736 data and adding to linear_offset*/
2737 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002738 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2739 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302740 }
2741
2742 I915_WRITE(reg, dspcntr);
2743
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002744 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002745 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002746 I915_WRITE(DSPSURF(plane),
2747 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002748 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002749 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002750 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002751 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002752 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002753}
2754
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002755static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2756 struct drm_framebuffer *fb,
2757 int x, int y)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002758{
2759 struct drm_device *dev = crtc->dev;
2760 struct drm_i915_private *dev_priv = dev->dev_private;
2761 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002762 struct drm_plane *primary = crtc->primary;
2763 bool visible = to_intel_plane_state(primary->state)->visible;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002764 struct drm_i915_gem_object *obj;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002765 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002766 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002767 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002768 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302769 int pixel_size;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002770
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002771 if (!visible || !fb) {
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002772 I915_WRITE(reg, 0);
2773 I915_WRITE(DSPSURF(plane), 0);
2774 POSTING_READ(reg);
2775 return;
2776 }
2777
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002778 obj = intel_fb_obj(fb);
2779 if (WARN_ON(obj == NULL))
2780 return;
2781
2782 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2783
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002784 dspcntr = DISPPLANE_GAMMA_ENABLE;
2785
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002786 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002787
2788 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2789 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2790
Ville Syrjälä57779d02012-10-31 17:50:14 +02002791 switch (fb->pixel_format) {
2792 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002793 dspcntr |= DISPPLANE_8BPP;
2794 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002795 case DRM_FORMAT_RGB565:
2796 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002797 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002798 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002799 dspcntr |= DISPPLANE_BGRX888;
2800 break;
2801 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002802 dspcntr |= DISPPLANE_RGBX888;
2803 break;
2804 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002805 dspcntr |= DISPPLANE_BGRX101010;
2806 break;
2807 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002808 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002809 break;
2810 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002811 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002812 }
2813
2814 if (obj->tiling_mode != I915_TILING_NONE)
2815 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002816
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002817 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002818 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002819
Ville Syrjäläb98971272014-08-27 16:51:22 +03002820 linear_offset = y * fb->pitches[0] + x * pixel_size;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002821 intel_crtc->dspaddr_offset =
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002822 intel_gen4_compute_page_offset(dev_priv,
2823 &x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002824 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002825 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002826 linear_offset -= intel_crtc->dspaddr_offset;
Matt Roper8e7d6882015-01-21 16:35:41 -08002827 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302828 dspcntr |= DISPPLANE_ROTATE_180;
2829
2830 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002831 x += (intel_crtc->config->pipe_src_w - 1);
2832 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302833
2834 /* Finding the last pixel of the last line of the display
2835 data and adding to linear_offset*/
2836 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002837 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2838 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302839 }
2840 }
2841
2842 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002843
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002844 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002845 I915_WRITE(DSPSURF(plane),
2846 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002847 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002848 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2849 } else {
2850 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2851 I915_WRITE(DSPLINOFF(plane), linear_offset);
2852 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002853 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002854}
2855
Damien Lespiaub3218032015-02-27 11:15:18 +00002856u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2857 uint32_t pixel_format)
2858{
2859 u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2860
2861 /*
2862 * The stride is either expressed as a multiple of 64 bytes
2863 * chunks for linear buffers or in number of tiles for tiled
2864 * buffers.
2865 */
2866 switch (fb_modifier) {
2867 case DRM_FORMAT_MOD_NONE:
2868 return 64;
2869 case I915_FORMAT_MOD_X_TILED:
2870 if (INTEL_INFO(dev)->gen == 2)
2871 return 128;
2872 return 512;
2873 case I915_FORMAT_MOD_Y_TILED:
2874 /* No need to check for old gens and Y tiling since this is
2875 * about the display engine and those will be blocked before
2876 * we get here.
2877 */
2878 return 128;
2879 case I915_FORMAT_MOD_Yf_TILED:
2880 if (bits_per_pixel == 8)
2881 return 64;
2882 else
2883 return 128;
2884 default:
2885 MISSING_CASE(fb_modifier);
2886 return 64;
2887 }
2888}
2889
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002890unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
2891 struct drm_i915_gem_object *obj)
2892{
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002893 const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002894
2895 if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002896 view = &i915_ggtt_view_rotated;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002897
2898 return i915_gem_obj_ggtt_offset_view(obj, view);
2899}
2900
Chandra Kondurua1b22782015-04-07 15:28:45 -07002901/*
2902 * This function detaches (aka. unbinds) unused scalers in hardware
2903 */
Maarten Lankhorst05832362015-06-15 12:33:48 +02002904static void skl_detach_scalers(struct intel_crtc *intel_crtc)
Chandra Kondurua1b22782015-04-07 15:28:45 -07002905{
2906 struct drm_device *dev;
2907 struct drm_i915_private *dev_priv;
2908 struct intel_crtc_scaler_state *scaler_state;
2909 int i;
2910
Chandra Kondurua1b22782015-04-07 15:28:45 -07002911 dev = intel_crtc->base.dev;
2912 dev_priv = dev->dev_private;
2913 scaler_state = &intel_crtc->config->scaler_state;
2914
2915 /* loop through and disable scalers that aren't in use */
2916 for (i = 0; i < intel_crtc->num_scalers; i++) {
2917 if (!scaler_state->scalers[i].in_use) {
2918 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, i), 0);
2919 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, i), 0);
2920 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, i), 0);
2921 DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
2922 intel_crtc->base.base.id, intel_crtc->pipe, i);
2923 }
2924 }
2925}
2926
Chandra Konduru6156a452015-04-27 13:48:39 -07002927u32 skl_plane_ctl_format(uint32_t pixel_format)
2928{
Chandra Konduru6156a452015-04-27 13:48:39 -07002929 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01002930 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002931 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07002932 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002933 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07002934 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002935 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07002936 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002937 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07002938 /*
2939 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2940 * to be already pre-multiplied. We need to add a knob (or a different
2941 * DRM_FORMAT) for user-space to configure that.
2942 */
2943 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002944 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07002945 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002946 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002947 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07002948 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002949 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002950 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002951 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002952 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002953 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002954 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07002955 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002956 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07002957 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002958 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002959 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002960 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002961 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01002962 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07002963 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002964
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002965 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002966}
2967
2968u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2969{
Chandra Konduru6156a452015-04-27 13:48:39 -07002970 switch (fb_modifier) {
2971 case DRM_FORMAT_MOD_NONE:
2972 break;
2973 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002974 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07002975 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002976 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07002977 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002978 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07002979 default:
2980 MISSING_CASE(fb_modifier);
2981 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002982
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002983 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002984}
2985
2986u32 skl_plane_ctl_rotation(unsigned int rotation)
2987{
Chandra Konduru6156a452015-04-27 13:48:39 -07002988 switch (rotation) {
2989 case BIT(DRM_ROTATE_0):
2990 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05302991 /*
2992 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
2993 * while i915 HW rotation is clockwise, thats why this swapping.
2994 */
Chandra Konduru6156a452015-04-27 13:48:39 -07002995 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05302996 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07002997 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002998 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07002999 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303000 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07003001 default:
3002 MISSING_CASE(rotation);
3003 }
3004
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003005 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003006}
3007
Damien Lespiau70d21f02013-07-03 21:06:04 +01003008static void skylake_update_primary_plane(struct drm_crtc *crtc,
3009 struct drm_framebuffer *fb,
3010 int x, int y)
3011{
3012 struct drm_device *dev = crtc->dev;
3013 struct drm_i915_private *dev_priv = dev->dev_private;
3014 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03003015 struct drm_plane *plane = crtc->primary;
3016 bool visible = to_intel_plane_state(plane->state)->visible;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003017 struct drm_i915_gem_object *obj;
3018 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303019 u32 plane_ctl, stride_div, stride;
3020 u32 tile_height, plane_offset, plane_size;
3021 unsigned int rotation;
3022 int x_offset, y_offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003023 unsigned long surf_addr;
Chandra Konduru6156a452015-04-27 13:48:39 -07003024 struct intel_crtc_state *crtc_state = intel_crtc->config;
3025 struct intel_plane_state *plane_state;
3026 int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
3027 int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
3028 int scaler_id = -1;
3029
Chandra Konduru6156a452015-04-27 13:48:39 -07003030 plane_state = to_intel_plane_state(plane->state);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003031
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03003032 if (!visible || !fb) {
Damien Lespiau70d21f02013-07-03 21:06:04 +01003033 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3034 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3035 POSTING_READ(PLANE_CTL(pipe, 0));
3036 return;
3037 }
3038
3039 plane_ctl = PLANE_CTL_ENABLE |
3040 PLANE_CTL_PIPE_GAMMA_ENABLE |
3041 PLANE_CTL_PIPE_CSC_ENABLE;
3042
Chandra Konduru6156a452015-04-27 13:48:39 -07003043 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3044 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003045 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303046
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303047 rotation = plane->state->rotation;
Chandra Konduru6156a452015-04-27 13:48:39 -07003048 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003049
Damien Lespiaub3218032015-02-27 11:15:18 +00003050 obj = intel_fb_obj(fb);
3051 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3052 fb->pixel_format);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303053 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
3054
Chandra Konduru6156a452015-04-27 13:48:39 -07003055 /*
3056 * FIXME: intel_plane_state->src, dst aren't set when transitional
3057 * update_plane helpers are called from legacy paths.
3058 * Once full atomic crtc is available, below check can be avoided.
3059 */
3060 if (drm_rect_width(&plane_state->src)) {
3061 scaler_id = plane_state->scaler_id;
3062 src_x = plane_state->src.x1 >> 16;
3063 src_y = plane_state->src.y1 >> 16;
3064 src_w = drm_rect_width(&plane_state->src) >> 16;
3065 src_h = drm_rect_height(&plane_state->src) >> 16;
3066 dst_x = plane_state->dst.x1;
3067 dst_y = plane_state->dst.y1;
3068 dst_w = drm_rect_width(&plane_state->dst);
3069 dst_h = drm_rect_height(&plane_state->dst);
3070
3071 WARN_ON(x != src_x || y != src_y);
3072 } else {
3073 src_w = intel_crtc->config->pipe_src_w;
3074 src_h = intel_crtc->config->pipe_src_h;
3075 }
3076
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303077 if (intel_rotation_90_or_270(rotation)) {
3078 /* stride = Surface height in tiles */
Chandra Konduru2614f172015-05-08 20:22:46 -07003079 tile_height = intel_tile_height(dev, fb->pixel_format,
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303080 fb->modifier[0]);
3081 stride = DIV_ROUND_UP(fb->height, tile_height);
Chandra Konduru6156a452015-04-27 13:48:39 -07003082 x_offset = stride * tile_height - y - src_h;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303083 y_offset = x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003084 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303085 } else {
3086 stride = fb->pitches[0] / stride_div;
3087 x_offset = x;
3088 y_offset = y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003089 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303090 }
3091 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003092
Damien Lespiau70d21f02013-07-03 21:06:04 +01003093 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303094 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3095 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3096 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003097
3098 if (scaler_id >= 0) {
3099 uint32_t ps_ctrl = 0;
3100
3101 WARN_ON(!dst_w || !dst_h);
3102 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3103 crtc_state->scaler_state.scalers[scaler_id].mode;
3104 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3105 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3106 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3107 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3108 I915_WRITE(PLANE_POS(pipe, 0), 0);
3109 } else {
3110 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3111 }
3112
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003113 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003114
3115 POSTING_READ(PLANE_SURF(pipe, 0));
3116}
3117
Jesse Barnes17638cd2011-06-24 12:19:23 -07003118/* Assume fb object is pinned & idle & fenced and just update base pointers */
3119static int
3120intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3121 int x, int y, enum mode_set_atomic state)
3122{
3123 struct drm_device *dev = crtc->dev;
3124 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003125
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01003126 if (dev_priv->display.disable_fbc)
3127 dev_priv->display.disable_fbc(dev);
Jesse Barnes81255562010-08-02 12:07:50 -07003128
Daniel Vetter29b9bde2014-04-24 23:55:01 +02003129 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3130
3131 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07003132}
3133
Ville Syrjälä75147472014-11-24 18:28:11 +02003134static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003135{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003136 struct drm_crtc *crtc;
3137
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003138 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003139 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3140 enum plane plane = intel_crtc->plane;
3141
3142 intel_prepare_page_flip(dev, plane);
3143 intel_finish_page_flip_plane(dev, plane);
3144 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003145}
3146
3147static void intel_update_primary_planes(struct drm_device *dev)
3148{
3149 struct drm_i915_private *dev_priv = dev->dev_private;
3150 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003151
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003152 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003153 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3154
Rob Clark51fd3712013-11-19 12:10:12 -05003155 drm_modeset_lock(&crtc->mutex, NULL);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003156 /*
3157 * FIXME: Once we have proper support for primary planes (and
3158 * disabling them without disabling the entire crtc) allow again
Dave Airlie66e514c2014-04-03 07:51:54 +10003159 * a NULL crtc->primary->fb.
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003160 */
Matt Roperf4510a22014-04-01 15:22:40 -07003161 if (intel_crtc->active && crtc->primary->fb)
Matt Roper262ca2b2014-03-18 17:22:55 -07003162 dev_priv->display.update_primary_plane(crtc,
Dave Airlie66e514c2014-04-03 07:51:54 +10003163 crtc->primary->fb,
Matt Roper262ca2b2014-03-18 17:22:55 -07003164 crtc->x,
3165 crtc->y);
Rob Clark51fd3712013-11-19 12:10:12 -05003166 drm_modeset_unlock(&crtc->mutex);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003167 }
3168}
3169
Ville Syrjälä75147472014-11-24 18:28:11 +02003170void intel_prepare_reset(struct drm_device *dev)
3171{
3172 /* no reset support for gen2 */
3173 if (IS_GEN2(dev))
3174 return;
3175
3176 /* reset doesn't touch the display */
3177 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3178 return;
3179
3180 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003181 /*
3182 * Disabling the crtcs gracefully seems nicer. Also the
3183 * g33 docs say we should at least disable all the planes.
3184 */
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02003185 intel_display_suspend(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003186}
3187
3188void intel_finish_reset(struct drm_device *dev)
3189{
3190 struct drm_i915_private *dev_priv = to_i915(dev);
3191
3192 /*
3193 * Flips in the rings will be nuked by the reset,
3194 * so complete all pending flips so that user space
3195 * will get its events and not get stuck.
3196 */
3197 intel_complete_page_flips(dev);
3198
3199 /* no reset support for gen2 */
3200 if (IS_GEN2(dev))
3201 return;
3202
3203 /* reset doesn't touch the display */
3204 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3205 /*
3206 * Flips in the rings have been nuked by the reset,
3207 * so update the base address of all primary
3208 * planes to the the last fb to make sure we're
3209 * showing the correct fb after a reset.
3210 */
3211 intel_update_primary_planes(dev);
3212 return;
3213 }
3214
3215 /*
3216 * The display has been reset as well,
3217 * so need a full re-initialization.
3218 */
3219 intel_runtime_pm_disable_interrupts(dev_priv);
3220 intel_runtime_pm_enable_interrupts(dev_priv);
3221
3222 intel_modeset_init_hw(dev);
3223
3224 spin_lock_irq(&dev_priv->irq_lock);
3225 if (dev_priv->display.hpd_irq_setup)
3226 dev_priv->display.hpd_irq_setup(dev);
3227 spin_unlock_irq(&dev_priv->irq_lock);
3228
3229 intel_modeset_setup_hw_state(dev, true);
3230
3231 intel_hpd_init(dev_priv);
3232
3233 drm_modeset_unlock_all(dev);
3234}
3235
Chris Wilson2e2f3512015-04-27 13:41:14 +01003236static void
Chris Wilson14667a42012-04-03 17:58:35 +01003237intel_finish_fb(struct drm_framebuffer *old_fb)
3238{
Matt Roper2ff8fde2014-07-08 07:50:07 -07003239 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
Chris Wilson2e2f3512015-04-27 13:41:14 +01003240 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilson14667a42012-04-03 17:58:35 +01003241 bool was_interruptible = dev_priv->mm.interruptible;
3242 int ret;
3243
Chris Wilson14667a42012-04-03 17:58:35 +01003244 /* Big Hammer, we also need to ensure that any pending
3245 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3246 * current scanout is retired before unpinning the old
Chris Wilson2e2f3512015-04-27 13:41:14 +01003247 * framebuffer. Note that we rely on userspace rendering
3248 * into the buffer attached to the pipe they are waiting
3249 * on. If not, userspace generates a GPU hang with IPEHR
3250 * point to the MI_WAIT_FOR_EVENT.
Chris Wilson14667a42012-04-03 17:58:35 +01003251 *
3252 * This should only fail upon a hung GPU, in which case we
3253 * can safely continue.
3254 */
3255 dev_priv->mm.interruptible = false;
Chris Wilson2e2f3512015-04-27 13:41:14 +01003256 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson14667a42012-04-03 17:58:35 +01003257 dev_priv->mm.interruptible = was_interruptible;
3258
Chris Wilson2e2f3512015-04-27 13:41:14 +01003259 WARN_ON(ret);
Chris Wilson14667a42012-04-03 17:58:35 +01003260}
3261
Chris Wilson7d5e3792014-03-04 13:15:08 +00003262static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3263{
3264 struct drm_device *dev = crtc->dev;
3265 struct drm_i915_private *dev_priv = dev->dev_private;
3266 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003267 bool pending;
3268
3269 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3270 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3271 return false;
3272
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003273 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003274 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003275 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003276
3277 return pending;
3278}
3279
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003280static void intel_update_pipe_size(struct intel_crtc *crtc)
3281{
3282 struct drm_device *dev = crtc->base.dev;
3283 struct drm_i915_private *dev_priv = dev->dev_private;
3284 const struct drm_display_mode *adjusted_mode;
3285
3286 if (!i915.fastboot)
3287 return;
3288
3289 /*
3290 * Update pipe size and adjust fitter if needed: the reason for this is
3291 * that in compute_mode_changes we check the native mode (not the pfit
3292 * mode) to see if we can flip rather than do a full mode set. In the
3293 * fastboot case, we'll flip, but if we don't update the pipesrc and
3294 * pfit state, we'll end up with a big fb scanned out into the wrong
3295 * sized surface.
3296 *
3297 * To fix this properly, we need to hoist the checks up into
3298 * compute_mode_changes (or above), check the actual pfit state and
3299 * whether the platform allows pfit disable with pipe active, and only
3300 * then update the pipesrc and pfit state, even on the flip path.
3301 */
3302
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003303 adjusted_mode = &crtc->config->base.adjusted_mode;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003304
3305 I915_WRITE(PIPESRC(crtc->pipe),
3306 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3307 (adjusted_mode->crtc_vdisplay - 1));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003308 if (!crtc->config->pch_pfit.enabled &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03003309 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3310 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003311 I915_WRITE(PF_CTL(crtc->pipe), 0);
3312 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3313 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3314 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003315 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3316 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003317}
3318
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003319static void intel_fdi_normal_train(struct drm_crtc *crtc)
3320{
3321 struct drm_device *dev = crtc->dev;
3322 struct drm_i915_private *dev_priv = dev->dev_private;
3323 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3324 int pipe = intel_crtc->pipe;
3325 u32 reg, temp;
3326
3327 /* enable normal train */
3328 reg = FDI_TX_CTL(pipe);
3329 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003330 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003331 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3332 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003333 } else {
3334 temp &= ~FDI_LINK_TRAIN_NONE;
3335 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003336 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003337 I915_WRITE(reg, temp);
3338
3339 reg = FDI_RX_CTL(pipe);
3340 temp = I915_READ(reg);
3341 if (HAS_PCH_CPT(dev)) {
3342 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3343 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3344 } else {
3345 temp &= ~FDI_LINK_TRAIN_NONE;
3346 temp |= FDI_LINK_TRAIN_NONE;
3347 }
3348 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3349
3350 /* wait one idle pattern time */
3351 POSTING_READ(reg);
3352 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003353
3354 /* IVB wants error correction enabled */
3355 if (IS_IVYBRIDGE(dev))
3356 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3357 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003358}
3359
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003360/* The FDI link training functions for ILK/Ibexpeak. */
3361static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3362{
3363 struct drm_device *dev = crtc->dev;
3364 struct drm_i915_private *dev_priv = dev->dev_private;
3365 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3366 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003367 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003368
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003369 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003370 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003371
Adam Jacksone1a44742010-06-25 15:32:14 -04003372 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3373 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003374 reg = FDI_RX_IMR(pipe);
3375 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003376 temp &= ~FDI_RX_SYMBOL_LOCK;
3377 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003378 I915_WRITE(reg, temp);
3379 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003380 udelay(150);
3381
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003382 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003383 reg = FDI_TX_CTL(pipe);
3384 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003385 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003386 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003387 temp &= ~FDI_LINK_TRAIN_NONE;
3388 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003389 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003390
Chris Wilson5eddb702010-09-11 13:48:45 +01003391 reg = FDI_RX_CTL(pipe);
3392 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003393 temp &= ~FDI_LINK_TRAIN_NONE;
3394 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003395 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3396
3397 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003398 udelay(150);
3399
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003400 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003401 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3402 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3403 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003404
Chris Wilson5eddb702010-09-11 13:48:45 +01003405 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003406 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003407 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003408 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3409
3410 if ((temp & FDI_RX_BIT_LOCK)) {
3411 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003412 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003413 break;
3414 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003415 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003416 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003417 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003418
3419 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003420 reg = FDI_TX_CTL(pipe);
3421 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003422 temp &= ~FDI_LINK_TRAIN_NONE;
3423 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003424 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003425
Chris Wilson5eddb702010-09-11 13:48:45 +01003426 reg = FDI_RX_CTL(pipe);
3427 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003428 temp &= ~FDI_LINK_TRAIN_NONE;
3429 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003430 I915_WRITE(reg, temp);
3431
3432 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003433 udelay(150);
3434
Chris Wilson5eddb702010-09-11 13:48:45 +01003435 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003436 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003437 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003438 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3439
3440 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003441 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003442 DRM_DEBUG_KMS("FDI train 2 done.\n");
3443 break;
3444 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003445 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003446 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003447 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003448
3449 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003450
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003451}
3452
Akshay Joshi0206e352011-08-16 15:34:10 -04003453static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003454 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3455 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3456 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3457 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3458};
3459
3460/* The FDI link training functions for SNB/Cougarpoint. */
3461static void gen6_fdi_link_train(struct drm_crtc *crtc)
3462{
3463 struct drm_device *dev = crtc->dev;
3464 struct drm_i915_private *dev_priv = dev->dev_private;
3465 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3466 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05003467 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003468
Adam Jacksone1a44742010-06-25 15:32:14 -04003469 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3470 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003471 reg = FDI_RX_IMR(pipe);
3472 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003473 temp &= ~FDI_RX_SYMBOL_LOCK;
3474 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003475 I915_WRITE(reg, temp);
3476
3477 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003478 udelay(150);
3479
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003480 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003481 reg = FDI_TX_CTL(pipe);
3482 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003483 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003484 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003485 temp &= ~FDI_LINK_TRAIN_NONE;
3486 temp |= FDI_LINK_TRAIN_PATTERN_1;
3487 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3488 /* SNB-B */
3489 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003490 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003491
Daniel Vetterd74cf322012-10-26 10:58:13 +02003492 I915_WRITE(FDI_RX_MISC(pipe),
3493 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3494
Chris Wilson5eddb702010-09-11 13:48:45 +01003495 reg = FDI_RX_CTL(pipe);
3496 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003497 if (HAS_PCH_CPT(dev)) {
3498 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3499 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3500 } else {
3501 temp &= ~FDI_LINK_TRAIN_NONE;
3502 temp |= FDI_LINK_TRAIN_PATTERN_1;
3503 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003504 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3505
3506 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003507 udelay(150);
3508
Akshay Joshi0206e352011-08-16 15:34:10 -04003509 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003510 reg = FDI_TX_CTL(pipe);
3511 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003512 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3513 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003514 I915_WRITE(reg, temp);
3515
3516 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003517 udelay(500);
3518
Sean Paulfa37d392012-03-02 12:53:39 -05003519 for (retry = 0; retry < 5; retry++) {
3520 reg = FDI_RX_IIR(pipe);
3521 temp = I915_READ(reg);
3522 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3523 if (temp & FDI_RX_BIT_LOCK) {
3524 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3525 DRM_DEBUG_KMS("FDI train 1 done.\n");
3526 break;
3527 }
3528 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003529 }
Sean Paulfa37d392012-03-02 12:53:39 -05003530 if (retry < 5)
3531 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003532 }
3533 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003534 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003535
3536 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003537 reg = FDI_TX_CTL(pipe);
3538 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003539 temp &= ~FDI_LINK_TRAIN_NONE;
3540 temp |= FDI_LINK_TRAIN_PATTERN_2;
3541 if (IS_GEN6(dev)) {
3542 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3543 /* SNB-B */
3544 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3545 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003546 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003547
Chris Wilson5eddb702010-09-11 13:48:45 +01003548 reg = FDI_RX_CTL(pipe);
3549 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003550 if (HAS_PCH_CPT(dev)) {
3551 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3552 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3553 } else {
3554 temp &= ~FDI_LINK_TRAIN_NONE;
3555 temp |= FDI_LINK_TRAIN_PATTERN_2;
3556 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003557 I915_WRITE(reg, temp);
3558
3559 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003560 udelay(150);
3561
Akshay Joshi0206e352011-08-16 15:34:10 -04003562 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003563 reg = FDI_TX_CTL(pipe);
3564 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003565 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3566 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003567 I915_WRITE(reg, temp);
3568
3569 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003570 udelay(500);
3571
Sean Paulfa37d392012-03-02 12:53:39 -05003572 for (retry = 0; retry < 5; retry++) {
3573 reg = FDI_RX_IIR(pipe);
3574 temp = I915_READ(reg);
3575 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3576 if (temp & FDI_RX_SYMBOL_LOCK) {
3577 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3578 DRM_DEBUG_KMS("FDI train 2 done.\n");
3579 break;
3580 }
3581 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003582 }
Sean Paulfa37d392012-03-02 12:53:39 -05003583 if (retry < 5)
3584 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003585 }
3586 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003587 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003588
3589 DRM_DEBUG_KMS("FDI train done.\n");
3590}
3591
Jesse Barnes357555c2011-04-28 15:09:55 -07003592/* Manual link training for Ivy Bridge A0 parts */
3593static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3594{
3595 struct drm_device *dev = crtc->dev;
3596 struct drm_i915_private *dev_priv = dev->dev_private;
3597 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3598 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003599 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003600
3601 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3602 for train result */
3603 reg = FDI_RX_IMR(pipe);
3604 temp = I915_READ(reg);
3605 temp &= ~FDI_RX_SYMBOL_LOCK;
3606 temp &= ~FDI_RX_BIT_LOCK;
3607 I915_WRITE(reg, temp);
3608
3609 POSTING_READ(reg);
3610 udelay(150);
3611
Daniel Vetter01a415f2012-10-27 15:58:40 +02003612 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3613 I915_READ(FDI_RX_IIR(pipe)));
3614
Jesse Barnes139ccd32013-08-19 11:04:55 -07003615 /* Try each vswing and preemphasis setting twice before moving on */
3616 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3617 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003618 reg = FDI_TX_CTL(pipe);
3619 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003620 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3621 temp &= ~FDI_TX_ENABLE;
3622 I915_WRITE(reg, temp);
3623
3624 reg = FDI_RX_CTL(pipe);
3625 temp = I915_READ(reg);
3626 temp &= ~FDI_LINK_TRAIN_AUTO;
3627 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3628 temp &= ~FDI_RX_ENABLE;
3629 I915_WRITE(reg, temp);
3630
3631 /* enable CPU FDI TX and PCH FDI RX */
3632 reg = FDI_TX_CTL(pipe);
3633 temp = I915_READ(reg);
3634 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003635 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003636 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003637 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003638 temp |= snb_b_fdi_train_param[j/2];
3639 temp |= FDI_COMPOSITE_SYNC;
3640 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3641
3642 I915_WRITE(FDI_RX_MISC(pipe),
3643 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3644
3645 reg = FDI_RX_CTL(pipe);
3646 temp = I915_READ(reg);
3647 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3648 temp |= FDI_COMPOSITE_SYNC;
3649 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3650
3651 POSTING_READ(reg);
3652 udelay(1); /* should be 0.5us */
3653
3654 for (i = 0; i < 4; i++) {
3655 reg = FDI_RX_IIR(pipe);
3656 temp = I915_READ(reg);
3657 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3658
3659 if (temp & FDI_RX_BIT_LOCK ||
3660 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3661 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3662 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3663 i);
3664 break;
3665 }
3666 udelay(1); /* should be 0.5us */
3667 }
3668 if (i == 4) {
3669 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3670 continue;
3671 }
3672
3673 /* Train 2 */
3674 reg = FDI_TX_CTL(pipe);
3675 temp = I915_READ(reg);
3676 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3677 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3678 I915_WRITE(reg, temp);
3679
3680 reg = FDI_RX_CTL(pipe);
3681 temp = I915_READ(reg);
3682 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3683 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003684 I915_WRITE(reg, temp);
3685
3686 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003687 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003688
Jesse Barnes139ccd32013-08-19 11:04:55 -07003689 for (i = 0; i < 4; i++) {
3690 reg = FDI_RX_IIR(pipe);
3691 temp = I915_READ(reg);
3692 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003693
Jesse Barnes139ccd32013-08-19 11:04:55 -07003694 if (temp & FDI_RX_SYMBOL_LOCK ||
3695 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3696 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3697 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3698 i);
3699 goto train_done;
3700 }
3701 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003702 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003703 if (i == 4)
3704 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003705 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003706
Jesse Barnes139ccd32013-08-19 11:04:55 -07003707train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003708 DRM_DEBUG_KMS("FDI train done.\n");
3709}
3710
Daniel Vetter88cefb62012-08-12 19:27:14 +02003711static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003712{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003713 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003714 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003715 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003716 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003717
Jesse Barnesc64e3112010-09-10 11:27:03 -07003718
Jesse Barnes0e23b992010-09-10 11:10:00 -07003719 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003720 reg = FDI_RX_CTL(pipe);
3721 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003722 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003723 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003724 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003725 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3726
3727 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003728 udelay(200);
3729
3730 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003731 temp = I915_READ(reg);
3732 I915_WRITE(reg, temp | FDI_PCDCLK);
3733
3734 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003735 udelay(200);
3736
Paulo Zanoni20749732012-11-23 15:30:38 -02003737 /* Enable CPU FDI TX PLL, always on for Ironlake */
3738 reg = FDI_TX_CTL(pipe);
3739 temp = I915_READ(reg);
3740 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3741 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003742
Paulo Zanoni20749732012-11-23 15:30:38 -02003743 POSTING_READ(reg);
3744 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003745 }
3746}
3747
Daniel Vetter88cefb62012-08-12 19:27:14 +02003748static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3749{
3750 struct drm_device *dev = intel_crtc->base.dev;
3751 struct drm_i915_private *dev_priv = dev->dev_private;
3752 int pipe = intel_crtc->pipe;
3753 u32 reg, temp;
3754
3755 /* Switch from PCDclk to Rawclk */
3756 reg = FDI_RX_CTL(pipe);
3757 temp = I915_READ(reg);
3758 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3759
3760 /* Disable CPU FDI TX PLL */
3761 reg = FDI_TX_CTL(pipe);
3762 temp = I915_READ(reg);
3763 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3764
3765 POSTING_READ(reg);
3766 udelay(100);
3767
3768 reg = FDI_RX_CTL(pipe);
3769 temp = I915_READ(reg);
3770 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3771
3772 /* Wait for the clocks to turn off. */
3773 POSTING_READ(reg);
3774 udelay(100);
3775}
3776
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003777static void ironlake_fdi_disable(struct drm_crtc *crtc)
3778{
3779 struct drm_device *dev = crtc->dev;
3780 struct drm_i915_private *dev_priv = dev->dev_private;
3781 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3782 int pipe = intel_crtc->pipe;
3783 u32 reg, temp;
3784
3785 /* disable CPU FDI tx and PCH FDI rx */
3786 reg = FDI_TX_CTL(pipe);
3787 temp = I915_READ(reg);
3788 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3789 POSTING_READ(reg);
3790
3791 reg = FDI_RX_CTL(pipe);
3792 temp = I915_READ(reg);
3793 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003794 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003795 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3796
3797 POSTING_READ(reg);
3798 udelay(100);
3799
3800 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003801 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003802 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003803
3804 /* still set train pattern 1 */
3805 reg = FDI_TX_CTL(pipe);
3806 temp = I915_READ(reg);
3807 temp &= ~FDI_LINK_TRAIN_NONE;
3808 temp |= FDI_LINK_TRAIN_PATTERN_1;
3809 I915_WRITE(reg, temp);
3810
3811 reg = FDI_RX_CTL(pipe);
3812 temp = I915_READ(reg);
3813 if (HAS_PCH_CPT(dev)) {
3814 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3815 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3816 } else {
3817 temp &= ~FDI_LINK_TRAIN_NONE;
3818 temp |= FDI_LINK_TRAIN_PATTERN_1;
3819 }
3820 /* BPC in FDI rx is consistent with that in PIPECONF */
3821 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003822 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003823 I915_WRITE(reg, temp);
3824
3825 POSTING_READ(reg);
3826 udelay(100);
3827}
3828
Chris Wilson5dce5b932014-01-20 10:17:36 +00003829bool intel_has_pending_fb_unpin(struct drm_device *dev)
3830{
3831 struct intel_crtc *crtc;
3832
3833 /* Note that we don't need to be called with mode_config.lock here
3834 * as our list of CRTC objects is static for the lifetime of the
3835 * device and so cannot disappear as we iterate. Similarly, we can
3836 * happily treat the predicates as racy, atomic checks as userspace
3837 * cannot claim and pin a new fb without at least acquring the
3838 * struct_mutex and so serialising with us.
3839 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003840 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003841 if (atomic_read(&crtc->unpin_work_count) == 0)
3842 continue;
3843
3844 if (crtc->unpin_work)
3845 intel_wait_for_vblank(dev, crtc->pipe);
3846
3847 return true;
3848 }
3849
3850 return false;
3851}
3852
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003853static void page_flip_completed(struct intel_crtc *intel_crtc)
3854{
3855 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3856 struct intel_unpin_work *work = intel_crtc->unpin_work;
3857
3858 /* ensure that the unpin work is consistent wrt ->pending. */
3859 smp_rmb();
3860 intel_crtc->unpin_work = NULL;
3861
3862 if (work->event)
3863 drm_send_vblank_event(intel_crtc->base.dev,
3864 intel_crtc->pipe,
3865 work->event);
3866
3867 drm_crtc_vblank_put(&intel_crtc->base);
3868
3869 wake_up_all(&dev_priv->pending_flip_queue);
3870 queue_work(dev_priv->wq, &work->work);
3871
3872 trace_i915_flip_complete(intel_crtc->plane,
3873 work->pending_flip_obj);
3874}
3875
Ville Syrjälä46a55d32014-05-21 14:04:46 +03003876void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003877{
Chris Wilson0f911282012-04-17 10:05:38 +01003878 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003879 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003880
Daniel Vetter2c10d572012-12-20 21:24:07 +01003881 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Chris Wilson9c787942014-09-05 07:13:25 +01003882 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3883 !intel_crtc_has_pending_flip(crtc),
3884 60*HZ) == 0)) {
3885 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003886
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003887 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003888 if (intel_crtc->unpin_work) {
3889 WARN_ONCE(1, "Removing stuck page flip\n");
3890 page_flip_completed(intel_crtc);
3891 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003892 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003893 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003894
Chris Wilson975d5682014-08-20 13:13:34 +01003895 if (crtc->primary->fb) {
3896 mutex_lock(&dev->struct_mutex);
3897 intel_finish_fb(crtc->primary->fb);
3898 mutex_unlock(&dev->struct_mutex);
3899 }
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003900}
3901
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003902/* Program iCLKIP clock to the desired frequency */
3903static void lpt_program_iclkip(struct drm_crtc *crtc)
3904{
3905 struct drm_device *dev = crtc->dev;
3906 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003907 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003908 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3909 u32 temp;
3910
Ville Syrjäläa5805162015-05-26 20:42:30 +03003911 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01003912
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003913 /* It is necessary to ungate the pixclk gate prior to programming
3914 * the divisors, and gate it back when it is done.
3915 */
3916 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3917
3918 /* Disable SSCCTL */
3919 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003920 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3921 SBI_SSCCTL_DISABLE,
3922 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003923
3924 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003925 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003926 auxdiv = 1;
3927 divsel = 0x41;
3928 phaseinc = 0x20;
3929 } else {
3930 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003931 * but the adjusted_mode->crtc_clock in in KHz. To get the
3932 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003933 * convert the virtual clock precision to KHz here for higher
3934 * precision.
3935 */
3936 u32 iclk_virtual_root_freq = 172800 * 1000;
3937 u32 iclk_pi_range = 64;
3938 u32 desired_divisor, msb_divisor_value, pi_value;
3939
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003940 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003941 msb_divisor_value = desired_divisor / iclk_pi_range;
3942 pi_value = desired_divisor % iclk_pi_range;
3943
3944 auxdiv = 0;
3945 divsel = msb_divisor_value - 2;
3946 phaseinc = pi_value;
3947 }
3948
3949 /* This should not happen with any sane values */
3950 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3951 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3952 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3953 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3954
3955 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 +03003956 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003957 auxdiv,
3958 divsel,
3959 phasedir,
3960 phaseinc);
3961
3962 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003963 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003964 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3965 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3966 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3967 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3968 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3969 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003970 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003971
3972 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003973 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003974 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3975 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003976 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003977
3978 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003979 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003980 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003981 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003982
3983 /* Wait for initialization time */
3984 udelay(24);
3985
3986 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01003987
Ville Syrjäläa5805162015-05-26 20:42:30 +03003988 mutex_unlock(&dev_priv->sb_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003989}
3990
Daniel Vetter275f01b22013-05-03 11:49:47 +02003991static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3992 enum pipe pch_transcoder)
3993{
3994 struct drm_device *dev = crtc->base.dev;
3995 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003996 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02003997
3998 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3999 I915_READ(HTOTAL(cpu_transcoder)));
4000 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4001 I915_READ(HBLANK(cpu_transcoder)));
4002 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4003 I915_READ(HSYNC(cpu_transcoder)));
4004
4005 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4006 I915_READ(VTOTAL(cpu_transcoder)));
4007 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4008 I915_READ(VBLANK(cpu_transcoder)));
4009 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4010 I915_READ(VSYNC(cpu_transcoder)));
4011 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4012 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4013}
4014
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004015static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004016{
4017 struct drm_i915_private *dev_priv = dev->dev_private;
4018 uint32_t temp;
4019
4020 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004021 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004022 return;
4023
4024 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4025 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4026
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004027 temp &= ~FDI_BC_BIFURCATION_SELECT;
4028 if (enable)
4029 temp |= FDI_BC_BIFURCATION_SELECT;
4030
4031 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004032 I915_WRITE(SOUTH_CHICKEN1, temp);
4033 POSTING_READ(SOUTH_CHICKEN1);
4034}
4035
4036static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4037{
4038 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004039
4040 switch (intel_crtc->pipe) {
4041 case PIPE_A:
4042 break;
4043 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004044 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004045 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004046 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004047 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004048
4049 break;
4050 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004051 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004052
4053 break;
4054 default:
4055 BUG();
4056 }
4057}
4058
Jesse Barnesf67a5592011-01-05 10:31:48 -08004059/*
4060 * Enable PCH resources required for PCH ports:
4061 * - PCH PLLs
4062 * - FDI training & RX/TX
4063 * - update transcoder timings
4064 * - DP transcoding bits
4065 * - transcoder
4066 */
4067static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004068{
4069 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004070 struct drm_i915_private *dev_priv = dev->dev_private;
4071 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4072 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004073 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004074
Daniel Vetterab9412b2013-05-03 11:49:46 +02004075 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004076
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004077 if (IS_IVYBRIDGE(dev))
4078 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4079
Daniel Vettercd986ab2012-10-26 10:58:12 +02004080 /* Write the TU size bits before fdi link training, so that error
4081 * detection works. */
4082 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4083 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4084
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004085 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004086 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004087
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004088 /* We need to program the right clock selection before writing the pixel
4089 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004090 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004091 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004092
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004093 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004094 temp |= TRANS_DPLL_ENABLE(pipe);
4095 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004096 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004097 temp |= sel;
4098 else
4099 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004100 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004101 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004102
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004103 /* XXX: pch pll's can be enabled any time before we enable the PCH
4104 * transcoder, and we actually should do this to not upset any PCH
4105 * transcoder that already use the clock when we share it.
4106 *
4107 * Note that enable_shared_dpll tries to do the right thing, but
4108 * get_shared_dpll unconditionally resets the pll - we need that to have
4109 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004110 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004111
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004112 /* set transcoder timing, panel must allow it */
4113 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004114 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004115
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004116 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004117
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004118 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004119 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004120 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01004121 reg = TRANS_DP_CTL(pipe);
4122 temp = I915_READ(reg);
4123 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004124 TRANS_DP_SYNC_MASK |
4125 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004126 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004127 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004128
4129 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004130 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004131 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004132 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004133
4134 switch (intel_trans_dp_port_sel(crtc)) {
4135 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004136 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004137 break;
4138 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004139 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004140 break;
4141 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004142 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004143 break;
4144 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004145 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004146 }
4147
Chris Wilson5eddb702010-09-11 13:48:45 +01004148 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004149 }
4150
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004151 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004152}
4153
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004154static void lpt_pch_enable(struct drm_crtc *crtc)
4155{
4156 struct drm_device *dev = crtc->dev;
4157 struct drm_i915_private *dev_priv = dev->dev_private;
4158 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004159 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004160
Daniel Vetterab9412b2013-05-03 11:49:46 +02004161 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004162
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004163 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004164
Paulo Zanoni0540e482012-10-31 18:12:40 -02004165 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004166 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004167
Paulo Zanoni937bb612012-10-31 18:12:47 -02004168 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004169}
4170
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004171struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4172 struct intel_crtc_state *crtc_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004173{
Daniel Vettere2b78262013-06-07 23:10:03 +02004174 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004175 struct intel_shared_dpll *pll;
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004176 struct intel_shared_dpll_config *shared_dpll;
Daniel Vettere2b78262013-06-07 23:10:03 +02004177 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004178
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004179 shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
4180
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004181 if (HAS_PCH_IBX(dev_priv->dev)) {
4182 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02004183 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004184 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004185
Daniel Vetter46edb022013-06-05 13:34:12 +02004186 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4187 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004188
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004189 WARN_ON(shared_dpll[i].crtc_mask);
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004190
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004191 goto found;
4192 }
4193
Satheeshakrishna Mbcddf6102014-08-22 09:49:10 +05304194 if (IS_BROXTON(dev_priv->dev)) {
4195 /* PLL is attached to port in bxt */
4196 struct intel_encoder *encoder;
4197 struct intel_digital_port *intel_dig_port;
4198
4199 encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
4200 if (WARN_ON(!encoder))
4201 return NULL;
4202
4203 intel_dig_port = enc_to_dig_port(&encoder->base);
4204 /* 1:1 mapping between ports and PLLs */
4205 i = (enum intel_dpll_id)intel_dig_port->port;
4206 pll = &dev_priv->shared_dplls[i];
4207 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4208 crtc->base.base.id, pll->name);
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004209 WARN_ON(shared_dpll[i].crtc_mask);
Satheeshakrishna Mbcddf6102014-08-22 09:49:10 +05304210
4211 goto found;
4212 }
4213
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004214 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4215 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004216
4217 /* Only want to check enabled timings first */
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004218 if (shared_dpll[i].crtc_mask == 0)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004219 continue;
4220
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004221 if (memcmp(&crtc_state->dpll_hw_state,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004222 &shared_dpll[i].hw_state,
4223 sizeof(crtc_state->dpll_hw_state)) == 0) {
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004224 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004225 crtc->base.base.id, pll->name,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004226 shared_dpll[i].crtc_mask,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004227 pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004228 goto found;
4229 }
4230 }
4231
4232 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004233 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4234 pll = &dev_priv->shared_dplls[i];
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004235 if (shared_dpll[i].crtc_mask == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02004236 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4237 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004238 goto found;
4239 }
4240 }
4241
4242 return NULL;
4243
4244found:
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004245 if (shared_dpll[i].crtc_mask == 0)
4246 shared_dpll[i].hw_state =
4247 crtc_state->dpll_hw_state;
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004248
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004249 crtc_state->shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02004250 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4251 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02004252
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004253 shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004254
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004255 return pll;
4256}
4257
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004258static void intel_shared_dpll_commit(struct drm_atomic_state *state)
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004259{
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004260 struct drm_i915_private *dev_priv = to_i915(state->dev);
4261 struct intel_shared_dpll_config *shared_dpll;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004262 struct intel_shared_dpll *pll;
4263 enum intel_dpll_id i;
4264
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004265 if (!to_intel_atomic_state(state)->dpll_set)
4266 return;
4267
4268 shared_dpll = to_intel_atomic_state(state)->shared_dpll;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004269 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4270 pll = &dev_priv->shared_dplls[i];
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004271 pll->config = shared_dpll[i];
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004272 }
4273}
4274
Daniel Vettera1520312013-05-03 11:49:50 +02004275static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004276{
4277 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01004278 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004279 u32 temp;
4280
4281 temp = I915_READ(dslreg);
4282 udelay(500);
4283 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004284 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004285 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004286 }
4287}
4288
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004289static int
4290skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4291 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4292 int src_w, int src_h, int dst_w, int dst_h)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004293{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004294 struct intel_crtc_scaler_state *scaler_state =
4295 &crtc_state->scaler_state;
4296 struct intel_crtc *intel_crtc =
4297 to_intel_crtc(crtc_state->base.crtc);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004298 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004299
4300 need_scaling = intel_rotation_90_or_270(rotation) ?
4301 (src_h != dst_w || src_w != dst_h):
4302 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004303
4304 /*
4305 * if plane is being disabled or scaler is no more required or force detach
4306 * - free scaler binded to this plane/crtc
4307 * - in order to do this, update crtc->scaler_usage
4308 *
4309 * Here scaler state in crtc_state is set free so that
4310 * scaler can be assigned to other user. Actual register
4311 * update to free the scaler is done in plane/panel-fit programming.
4312 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4313 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004314 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004315 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004316 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004317 scaler_state->scalers[*scaler_id].in_use = 0;
4318
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004319 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4320 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4321 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004322 scaler_state->scaler_users);
4323 *scaler_id = -1;
4324 }
4325 return 0;
4326 }
4327
4328 /* range checks */
4329 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4330 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4331
4332 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4333 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004334 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004335 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004336 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004337 return -EINVAL;
4338 }
4339
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004340 /* mark this plane as a scaler user in crtc_state */
4341 scaler_state->scaler_users |= (1 << scaler_user);
4342 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4343 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4344 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4345 scaler_state->scaler_users);
4346
4347 return 0;
4348}
4349
4350/**
4351 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4352 *
4353 * @state: crtc's scaler state
4354 * @force_detach: whether to forcibly disable scaler
4355 *
4356 * Return
4357 * 0 - scaler_usage updated successfully
4358 * error - requested scaling cannot be supported or other error condition
4359 */
4360int skl_update_scaler_crtc(struct intel_crtc_state *state, int force_detach)
4361{
4362 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
4363 struct drm_display_mode *adjusted_mode =
4364 &state->base.adjusted_mode;
4365
4366 DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
4367 intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
4368
4369 return skl_update_scaler(state, force_detach, SKL_CRTC_INDEX,
4370 &state->scaler_state.scaler_id, DRM_ROTATE_0,
4371 state->pipe_src_w, state->pipe_src_h,
4372 adjusted_mode->hdisplay, adjusted_mode->hdisplay);
4373}
4374
4375/**
4376 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4377 *
4378 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004379 * @plane_state: atomic plane state to update
4380 *
4381 * Return
4382 * 0 - scaler_usage updated successfully
4383 * error - requested scaling cannot be supported or other error condition
4384 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004385static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4386 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004387{
4388
4389 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004390 struct intel_plane *intel_plane =
4391 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004392 struct drm_framebuffer *fb = plane_state->base.fb;
4393 int ret;
4394
4395 bool force_detach = !fb || !plane_state->visible;
4396
4397 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
4398 intel_plane->base.base.id, intel_crtc->pipe,
4399 drm_plane_index(&intel_plane->base));
4400
4401 ret = skl_update_scaler(crtc_state, force_detach,
4402 drm_plane_index(&intel_plane->base),
4403 &plane_state->scaler_id,
4404 plane_state->base.rotation,
4405 drm_rect_width(&plane_state->src) >> 16,
4406 drm_rect_height(&plane_state->src) >> 16,
4407 drm_rect_width(&plane_state->dst),
4408 drm_rect_height(&plane_state->dst));
4409
4410 if (ret || plane_state->scaler_id < 0)
4411 return ret;
4412
Chandra Kondurua1b22782015-04-07 15:28:45 -07004413 /* check colorkey */
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004414 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004415 DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004416 intel_plane->base.base.id);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004417 return -EINVAL;
4418 }
4419
4420 /* Check src format */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004421 switch (fb->pixel_format) {
4422 case DRM_FORMAT_RGB565:
4423 case DRM_FORMAT_XBGR8888:
4424 case DRM_FORMAT_XRGB8888:
4425 case DRM_FORMAT_ABGR8888:
4426 case DRM_FORMAT_ARGB8888:
4427 case DRM_FORMAT_XRGB2101010:
4428 case DRM_FORMAT_XBGR2101010:
4429 case DRM_FORMAT_YUYV:
4430 case DRM_FORMAT_YVYU:
4431 case DRM_FORMAT_UYVY:
4432 case DRM_FORMAT_VYUY:
4433 break;
4434 default:
4435 DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
4436 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4437 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004438 }
4439
Chandra Kondurua1b22782015-04-07 15:28:45 -07004440 return 0;
4441}
4442
4443static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004444{
4445 struct drm_device *dev = crtc->base.dev;
4446 struct drm_i915_private *dev_priv = dev->dev_private;
4447 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004448 struct intel_crtc_scaler_state *scaler_state =
4449 &crtc->config->scaler_state;
4450
4451 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4452
4453 /* To update pfit, first update scaler state */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004454 skl_update_scaler_crtc(crtc->config, !enable);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004455 intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
4456 skl_detach_scalers(crtc);
4457 if (!enable)
4458 return;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004459
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004460 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004461 int id;
4462
4463 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4464 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4465 return;
4466 }
4467
4468 id = scaler_state->scaler_id;
4469 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4470 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4471 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4472 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4473
4474 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004475 }
4476}
4477
Jesse Barnesb074cec2013-04-25 12:55:02 -07004478static void ironlake_pfit_enable(struct intel_crtc *crtc)
4479{
4480 struct drm_device *dev = crtc->base.dev;
4481 struct drm_i915_private *dev_priv = dev->dev_private;
4482 int pipe = crtc->pipe;
4483
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004484 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004485 /* Force use of hard-coded filter coefficients
4486 * as some pre-programmed values are broken,
4487 * e.g. x201.
4488 */
4489 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4490 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4491 PF_PIPE_SEL_IVB(pipe));
4492 else
4493 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004494 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4495 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004496 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004497}
4498
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004499void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004500{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004501 struct drm_device *dev = crtc->base.dev;
4502 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004503
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004504 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004505 return;
4506
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004507 /* We can only enable IPS after we enable a plane and wait for a vblank */
4508 intel_wait_for_vblank(dev, crtc->pipe);
4509
Paulo Zanonid77e4532013-09-24 13:52:55 -03004510 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004511 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004512 mutex_lock(&dev_priv->rps.hw_lock);
4513 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4514 mutex_unlock(&dev_priv->rps.hw_lock);
4515 /* Quoting Art Runyan: "its not safe to expect any particular
4516 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004517 * mailbox." Moreover, the mailbox may return a bogus state,
4518 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004519 */
4520 } else {
4521 I915_WRITE(IPS_CTL, IPS_ENABLE);
4522 /* The bit only becomes 1 in the next vblank, so this wait here
4523 * is essentially intel_wait_for_vblank. If we don't have this
4524 * and don't wait for vblanks until the end of crtc_enable, then
4525 * the HW state readout code will complain that the expected
4526 * IPS_CTL value is not the one we read. */
4527 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4528 DRM_ERROR("Timed out waiting for IPS enable\n");
4529 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004530}
4531
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004532void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004533{
4534 struct drm_device *dev = crtc->base.dev;
4535 struct drm_i915_private *dev_priv = dev->dev_private;
4536
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004537 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004538 return;
4539
4540 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004541 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004542 mutex_lock(&dev_priv->rps.hw_lock);
4543 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4544 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004545 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4546 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4547 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004548 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004549 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004550 POSTING_READ(IPS_CTL);
4551 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004552
4553 /* We need to wait for a vblank before we can disable the plane. */
4554 intel_wait_for_vblank(dev, crtc->pipe);
4555}
4556
4557/** Loads the palette/gamma unit for the CRTC with the prepared values */
4558static void intel_crtc_load_lut(struct drm_crtc *crtc)
4559{
4560 struct drm_device *dev = crtc->dev;
4561 struct drm_i915_private *dev_priv = dev->dev_private;
4562 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4563 enum pipe pipe = intel_crtc->pipe;
4564 int palreg = PALETTE(pipe);
4565 int i;
4566 bool reenable_ips = false;
4567
4568 /* The clocks have to be on to load the palette. */
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004569 if (!crtc->state->active)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004570 return;
4571
Imre Deak50360402015-01-16 00:55:16 -08004572 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03004573 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004574 assert_dsi_pll_enabled(dev_priv);
4575 else
4576 assert_pll_enabled(dev_priv, pipe);
4577 }
4578
4579 /* use legacy palette for Ironlake */
Sonika Jindal7a1db492014-07-22 11:18:27 +05304580 if (!HAS_GMCH_DISPLAY(dev))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004581 palreg = LGC_PALETTE(pipe);
4582
4583 /* Workaround : Do not read or write the pipe palette/gamma data while
4584 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4585 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004586 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03004587 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4588 GAMMA_MODE_MODE_SPLIT)) {
4589 hsw_disable_ips(intel_crtc);
4590 reenable_ips = true;
4591 }
4592
4593 for (i = 0; i < 256; i++) {
4594 I915_WRITE(palreg + 4 * i,
4595 (intel_crtc->lut_r[i] << 16) |
4596 (intel_crtc->lut_g[i] << 8) |
4597 intel_crtc->lut_b[i]);
4598 }
4599
4600 if (reenable_ips)
4601 hsw_enable_ips(intel_crtc);
4602}
4603
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004604static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004605{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004606 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004607 struct drm_device *dev = intel_crtc->base.dev;
4608 struct drm_i915_private *dev_priv = dev->dev_private;
4609
4610 mutex_lock(&dev->struct_mutex);
4611 dev_priv->mm.interruptible = false;
4612 (void) intel_overlay_switch_off(intel_crtc->overlay);
4613 dev_priv->mm.interruptible = true;
4614 mutex_unlock(&dev->struct_mutex);
4615 }
4616
4617 /* Let userspace switch the overlay on again. In most cases userspace
4618 * has to recompute where to put it anyway.
4619 */
4620}
4621
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004622/**
4623 * intel_post_enable_primary - Perform operations after enabling primary plane
4624 * @crtc: the CRTC whose primary plane was just enabled
4625 *
4626 * Performs potentially sleeping operations that must be done after the primary
4627 * plane is enabled, such as updating FBC and IPS. Note that this may be
4628 * called due to an explicit primary plane update, or due to an implicit
4629 * re-enable that is caused when a sprite plane is updated to no longer
4630 * completely hide the primary plane.
4631 */
4632static void
4633intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004634{
4635 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004636 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004637 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4638 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004639
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004640 /*
4641 * BDW signals flip done immediately if the plane
4642 * is disabled, even if the plane enable is already
4643 * armed to occur at the next vblank :(
4644 */
4645 if (IS_BROADWELL(dev))
4646 intel_wait_for_vblank(dev, pipe);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004647
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004648 /*
4649 * FIXME IPS should be fine as long as one plane is
4650 * enabled, but in practice it seems to have problems
4651 * when going from primary only to sprite only and vice
4652 * versa.
4653 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004654 hsw_enable_ips(intel_crtc);
4655
Daniel Vetterf99d7062014-06-19 16:01:59 +02004656 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004657 * Gen2 reports pipe underruns whenever all planes are disabled.
4658 * So don't enable underrun reporting before at least some planes
4659 * are enabled.
4660 * FIXME: Need to fix the logic to work when we turn off all planes
4661 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004662 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004663 if (IS_GEN2(dev))
4664 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4665
4666 /* Underruns don't raise interrupts, so check manually. */
4667 if (HAS_GMCH_DISPLAY(dev))
4668 i9xx_check_fifo_underruns(dev_priv);
4669}
4670
4671/**
4672 * intel_pre_disable_primary - Perform operations before disabling primary plane
4673 * @crtc: the CRTC whose primary plane is to be disabled
4674 *
4675 * Performs potentially sleeping operations that must be done before the
4676 * primary plane is disabled, such as updating FBC and IPS. Note that this may
4677 * be called due to an explicit primary plane update, or due to an implicit
4678 * disable that is caused when a sprite plane completely hides the primary
4679 * plane.
4680 */
4681static void
4682intel_pre_disable_primary(struct drm_crtc *crtc)
4683{
4684 struct drm_device *dev = crtc->dev;
4685 struct drm_i915_private *dev_priv = dev->dev_private;
4686 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4687 int pipe = intel_crtc->pipe;
4688
4689 /*
4690 * Gen2 reports pipe underruns whenever all planes are disabled.
4691 * So diasble underrun reporting before all the planes get disabled.
4692 * FIXME: Need to fix the logic to work when we turn off all planes
4693 * but leave the pipe running.
4694 */
4695 if (IS_GEN2(dev))
4696 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4697
4698 /*
4699 * Vblank time updates from the shadow to live plane control register
4700 * are blocked if the memory self-refresh mode is active at that
4701 * moment. So to make sure the plane gets truly disabled, disable
4702 * first the self-refresh mode. The self-refresh enable bit in turn
4703 * will be checked/applied by the HW only at the next frame start
4704 * event which is after the vblank start event, so we need to have a
4705 * wait-for-vblank between disabling the plane and the pipe.
4706 */
4707 if (HAS_GMCH_DISPLAY(dev))
4708 intel_set_memory_cxsr(dev_priv, false);
4709
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004710 /*
4711 * FIXME IPS should be fine as long as one plane is
4712 * enabled, but in practice it seems to have problems
4713 * when going from primary only to sprite only and vice
4714 * versa.
4715 */
4716 hsw_disable_ips(intel_crtc);
4717}
4718
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004719static void intel_post_plane_update(struct intel_crtc *crtc)
4720{
4721 struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
4722 struct drm_device *dev = crtc->base.dev;
4723 struct drm_plane *plane;
4724
4725 if (atomic->wait_vblank)
4726 intel_wait_for_vblank(dev, crtc->pipe);
4727
4728 intel_frontbuffer_flip(dev, atomic->fb_bits);
4729
4730 if (atomic->update_fbc) {
4731 mutex_lock(&dev->struct_mutex);
4732 intel_fbc_update(dev);
4733 mutex_unlock(&dev->struct_mutex);
4734 }
4735
4736 if (atomic->post_enable_primary)
4737 intel_post_enable_primary(&crtc->base);
4738
4739 drm_for_each_plane_mask(plane, dev, atomic->update_sprite_watermarks)
4740 intel_update_sprite_watermarks(plane, &crtc->base,
4741 0, 0, 0, false, false);
4742
4743 memset(atomic, 0, sizeof(*atomic));
4744}
4745
4746static void intel_pre_plane_update(struct intel_crtc *crtc)
4747{
4748 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02004749 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004750 struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
4751 struct drm_plane *p;
4752
4753 /* Track fb's for any planes being disabled */
4754
4755 drm_for_each_plane_mask(p, dev, atomic->disabled_planes) {
4756 struct intel_plane *plane = to_intel_plane(p);
4757 unsigned fb_bits = 0;
4758
4759 switch (p->type) {
4760 case DRM_PLANE_TYPE_PRIMARY:
4761 fb_bits = INTEL_FRONTBUFFER_PRIMARY(plane->pipe);
4762 break;
4763 case DRM_PLANE_TYPE_CURSOR:
4764 fb_bits = INTEL_FRONTBUFFER_CURSOR(plane->pipe);
4765 break;
4766 case DRM_PLANE_TYPE_OVERLAY:
4767 fb_bits = INTEL_FRONTBUFFER_SPRITE(plane->pipe);
4768 break;
4769 }
4770
4771 mutex_lock(&dev->struct_mutex);
4772 i915_gem_track_fb(intel_fb_obj(plane->base.fb), NULL, fb_bits);
4773 mutex_unlock(&dev->struct_mutex);
4774 }
4775
4776 if (atomic->wait_for_flips)
4777 intel_crtc_wait_for_pending_flips(&crtc->base);
4778
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02004779 if (atomic->disable_fbc &&
4780 dev_priv->fbc.crtc == crtc) {
4781 mutex_lock(&dev->struct_mutex);
4782 if (dev_priv->fbc.crtc == crtc)
4783 intel_fbc_disable(dev);
4784 mutex_unlock(&dev->struct_mutex);
4785 }
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004786
4787 if (atomic->pre_disable_primary)
4788 intel_pre_disable_primary(&crtc->base);
4789}
4790
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004791static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004792{
4793 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004794 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004795 struct drm_plane *p;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004796 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004797
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004798 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004799
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004800 drm_for_each_plane_mask(p, dev, plane_mask)
4801 to_intel_plane(p)->disable_plane(p, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004802
Daniel Vetterf99d7062014-06-19 16:01:59 +02004803 /*
4804 * FIXME: Once we grow proper nuclear flip support out of this we need
4805 * to compute the mask of flip planes precisely. For the time being
4806 * consider this a flip to a NULL plane.
4807 */
4808 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004809}
4810
Jesse Barnesf67a5592011-01-05 10:31:48 -08004811static void ironlake_crtc_enable(struct drm_crtc *crtc)
4812{
4813 struct drm_device *dev = crtc->dev;
4814 struct drm_i915_private *dev_priv = dev->dev_private;
4815 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004816 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004817 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004818
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004819 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08004820 return;
4821
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004822 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004823 intel_prepare_shared_dpll(intel_crtc);
4824
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004825 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304826 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004827
4828 intel_set_pipe_timings(intel_crtc);
4829
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004830 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004831 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004832 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004833 }
4834
4835 ironlake_set_pipeconf(crtc);
4836
Jesse Barnesf67a5592011-01-05 10:31:48 -08004837 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004838
Daniel Vettera72e4c92014-09-30 10:56:47 +02004839 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4840 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004841
Daniel Vetterf6736a12013-06-05 13:34:30 +02004842 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004843 if (encoder->pre_enable)
4844 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004845
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004846 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004847 /* Note: FDI PLL enabling _must_ be done before we enable the
4848 * cpu pipes, hence this is separate from all the other fdi/pch
4849 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004850 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004851 } else {
4852 assert_fdi_tx_disabled(dev_priv, pipe);
4853 assert_fdi_rx_disabled(dev_priv, pipe);
4854 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004855
Jesse Barnesb074cec2013-04-25 12:55:02 -07004856 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004857
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004858 /*
4859 * On ILK+ LUT must be loaded before the pipe is running but with
4860 * clocks enabled
4861 */
4862 intel_crtc_load_lut(crtc);
4863
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004864 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004865 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004866
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004867 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004868 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004869
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004870 assert_vblank_disabled(crtc);
4871 drm_crtc_vblank_on(crtc);
4872
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004873 for_each_encoder_on_crtc(dev, crtc, encoder)
4874 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004875
4876 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004877 cpt_verify_modeset(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004878}
4879
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004880/* IPS only exists on ULT machines and is tied to pipe A. */
4881static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4882{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004883 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004884}
4885
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004886static void haswell_crtc_enable(struct drm_crtc *crtc)
4887{
4888 struct drm_device *dev = crtc->dev;
4889 struct drm_i915_private *dev_priv = dev->dev_private;
4890 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4891 struct intel_encoder *encoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004892 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4893 struct intel_crtc_state *pipe_config =
4894 to_intel_crtc_state(crtc->state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004895
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004896 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004897 return;
4898
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004899 if (intel_crtc_to_shared_dpll(intel_crtc))
4900 intel_enable_shared_dpll(intel_crtc);
4901
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004902 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304903 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004904
4905 intel_set_pipe_timings(intel_crtc);
4906
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004907 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
4908 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
4909 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004910 }
4911
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004912 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004913 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004914 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004915 }
4916
4917 haswell_set_pipeconf(crtc);
4918
4919 intel_set_pipe_csc(crtc);
4920
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004921 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004922
Daniel Vettera72e4c92014-09-30 10:56:47 +02004923 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004924 for_each_encoder_on_crtc(dev, crtc, encoder)
4925 if (encoder->pre_enable)
4926 encoder->pre_enable(encoder);
4927
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004928 if (intel_crtc->config->has_pch_encoder) {
Daniel Vettera72e4c92014-09-30 10:56:47 +02004929 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4930 true);
Imre Deak4fe94672014-06-25 22:01:49 +03004931 dev_priv->display.fdi_link_train(crtc);
4932 }
4933
Paulo Zanoni1f544382012-10-24 11:32:00 -02004934 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004935
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004936 if (INTEL_INFO(dev)->gen == 9)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004937 skylake_pfit_update(intel_crtc, 1);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004938 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004939 ironlake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004940 else
4941 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004942
4943 /*
4944 * On ILK+ LUT must be loaded before the pipe is running but with
4945 * clocks enabled
4946 */
4947 intel_crtc_load_lut(crtc);
4948
Paulo Zanoni1f544382012-10-24 11:32:00 -02004949 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00004950 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004951
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004952 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004953 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004954
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004955 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004956 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004957
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004958 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10004959 intel_ddi_set_vc_payload_alloc(crtc, true);
4960
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004961 assert_vblank_disabled(crtc);
4962 drm_crtc_vblank_on(crtc);
4963
Jani Nikula8807e552013-08-30 19:40:32 +03004964 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004965 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004966 intel_opregion_notify_encoder(encoder, true);
4967 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004968
Paulo Zanonie4916942013-09-20 16:21:19 -03004969 /* If we change the relative order between pipe/planes enabling, we need
4970 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004971 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4972 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4973 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4974 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4975 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004976}
4977
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004978static void ironlake_pfit_disable(struct intel_crtc *crtc)
4979{
4980 struct drm_device *dev = crtc->base.dev;
4981 struct drm_i915_private *dev_priv = dev->dev_private;
4982 int pipe = crtc->pipe;
4983
4984 /* To avoid upsetting the power well on haswell only disable the pfit if
4985 * it's in use. The hw state code will make sure we get this right. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004986 if (crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004987 I915_WRITE(PF_CTL(pipe), 0);
4988 I915_WRITE(PF_WIN_POS(pipe), 0);
4989 I915_WRITE(PF_WIN_SZ(pipe), 0);
4990 }
4991}
4992
Jesse Barnes6be4a602010-09-10 10:26:01 -07004993static void ironlake_crtc_disable(struct drm_crtc *crtc)
4994{
4995 struct drm_device *dev = crtc->dev;
4996 struct drm_i915_private *dev_priv = dev->dev_private;
4997 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004998 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004999 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01005000 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005001
Daniel Vetterea9d7582012-07-10 10:42:52 +02005002 for_each_encoder_on_crtc(dev, crtc, encoder)
5003 encoder->disable(encoder);
5004
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005005 drm_crtc_vblank_off(crtc);
5006 assert_vblank_disabled(crtc);
5007
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005008 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005009 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Daniel Vetterd925c592013-06-05 13:34:04 +02005010
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005011 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005012
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005013 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005014
Ville Syrjälä5a74f702015-05-05 17:17:38 +03005015 if (intel_crtc->config->has_pch_encoder)
5016 ironlake_fdi_disable(crtc);
5017
Daniel Vetterbf49ec82012-09-06 22:15:40 +02005018 for_each_encoder_on_crtc(dev, crtc, encoder)
5019 if (encoder->post_disable)
5020 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005021
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005022 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005023 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005024
Daniel Vetterd925c592013-06-05 13:34:04 +02005025 if (HAS_PCH_CPT(dev)) {
5026 /* disable TRANS_DP_CTL */
5027 reg = TRANS_DP_CTL(pipe);
5028 temp = I915_READ(reg);
5029 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5030 TRANS_DP_PORT_SEL_MASK);
5031 temp |= TRANS_DP_PORT_SEL_NONE;
5032 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005033
Daniel Vetterd925c592013-06-05 13:34:04 +02005034 /* disable DPLL_SEL */
5035 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005036 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005037 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005038 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005039
Daniel Vetterd925c592013-06-05 13:34:04 +02005040 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005041 }
Jesse Barnes6be4a602010-09-10 10:26:01 -07005042}
5043
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005044static void haswell_crtc_disable(struct drm_crtc *crtc)
5045{
5046 struct drm_device *dev = crtc->dev;
5047 struct drm_i915_private *dev_priv = dev->dev_private;
5048 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5049 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005050 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005051
Jani Nikula8807e552013-08-30 19:40:32 +03005052 for_each_encoder_on_crtc(dev, crtc, encoder) {
5053 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005054 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005055 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005056
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005057 drm_crtc_vblank_off(crtc);
5058 assert_vblank_disabled(crtc);
5059
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005060 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005061 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5062 false);
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005063 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005064
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005065 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005066 intel_ddi_set_vc_payload_alloc(crtc, false);
5067
Paulo Zanoniad80a812012-10-24 16:06:19 -02005068 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005069
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005070 if (INTEL_INFO(dev)->gen == 9)
Chandra Kondurua1b22782015-04-07 15:28:45 -07005071 skylake_pfit_update(intel_crtc, 0);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005072 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00005073 ironlake_pfit_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005074 else
5075 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005076
Paulo Zanoni1f544382012-10-24 11:32:00 -02005077 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005078
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005079 if (intel_crtc->config->has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02005080 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02005081 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02005082 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005083
Imre Deak97b040a2014-06-25 22:01:50 +03005084 for_each_encoder_on_crtc(dev, crtc, encoder)
5085 if (encoder->post_disable)
5086 encoder->post_disable(encoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005087}
5088
Jesse Barnes2dd24552013-04-25 12:55:01 -07005089static void i9xx_pfit_enable(struct intel_crtc *crtc)
5090{
5091 struct drm_device *dev = crtc->base.dev;
5092 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005093 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005094
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005095 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005096 return;
5097
Daniel Vetterc0b03412013-05-28 12:05:54 +02005098 /*
5099 * The panel fitter should only be adjusted whilst the pipe is disabled,
5100 * according to register description and PRM.
5101 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005102 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5103 assert_pipe_disabled(dev_priv, crtc->pipe);
5104
Jesse Barnesb074cec2013-04-25 12:55:02 -07005105 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5106 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005107
5108 /* Border color in case we don't scale up to the full screen. Black by
5109 * default, change to something else for debugging. */
5110 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005111}
5112
Dave Airlied05410f2014-06-05 13:22:59 +10005113static enum intel_display_power_domain port_to_power_domain(enum port port)
5114{
5115 switch (port) {
5116 case PORT_A:
5117 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
5118 case PORT_B:
5119 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
5120 case PORT_C:
5121 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
5122 case PORT_D:
5123 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
5124 default:
5125 WARN_ON_ONCE(1);
5126 return POWER_DOMAIN_PORT_OTHER;
5127 }
5128}
5129
Imre Deak77d22dc2014-03-05 16:20:52 +02005130#define for_each_power_domain(domain, mask) \
5131 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
5132 if ((1 << (domain)) & (mask))
5133
Imre Deak319be8a2014-03-04 19:22:57 +02005134enum intel_display_power_domain
5135intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005136{
Imre Deak319be8a2014-03-04 19:22:57 +02005137 struct drm_device *dev = intel_encoder->base.dev;
5138 struct intel_digital_port *intel_dig_port;
5139
5140 switch (intel_encoder->type) {
5141 case INTEL_OUTPUT_UNKNOWN:
5142 /* Only DDI platforms should ever use this output type */
5143 WARN_ON_ONCE(!HAS_DDI(dev));
5144 case INTEL_OUTPUT_DISPLAYPORT:
5145 case INTEL_OUTPUT_HDMI:
5146 case INTEL_OUTPUT_EDP:
5147 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005148 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005149 case INTEL_OUTPUT_DP_MST:
5150 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5151 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005152 case INTEL_OUTPUT_ANALOG:
5153 return POWER_DOMAIN_PORT_CRT;
5154 case INTEL_OUTPUT_DSI:
5155 return POWER_DOMAIN_PORT_DSI;
5156 default:
5157 return POWER_DOMAIN_PORT_OTHER;
5158 }
5159}
5160
5161static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
5162{
5163 struct drm_device *dev = crtc->dev;
5164 struct intel_encoder *intel_encoder;
5165 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5166 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005167 unsigned long mask;
5168 enum transcoder transcoder;
5169
5170 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
5171
5172 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5173 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005174 if (intel_crtc->config->pch_pfit.enabled ||
5175 intel_crtc->config->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005176 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5177
Imre Deak319be8a2014-03-04 19:22:57 +02005178 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5179 mask |= BIT(intel_display_port_power_domain(intel_encoder));
5180
Imre Deak77d22dc2014-03-05 16:20:52 +02005181 return mask;
5182}
5183
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005184static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
Imre Deak77d22dc2014-03-05 16:20:52 +02005185{
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005186 struct drm_device *dev = state->dev;
Imre Deak77d22dc2014-03-05 16:20:52 +02005187 struct drm_i915_private *dev_priv = dev->dev_private;
5188 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
5189 struct intel_crtc *crtc;
5190
5191 /*
5192 * First get all needed power domains, then put all unneeded, to avoid
5193 * any unnecessary toggling of the power wells.
5194 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005195 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005196 enum intel_display_power_domain domain;
5197
Matt Roper83d65732015-02-25 13:12:16 -08005198 if (!crtc->base.state->enable)
Imre Deak77d22dc2014-03-05 16:20:52 +02005199 continue;
5200
Imre Deak319be8a2014-03-04 19:22:57 +02005201 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
Imre Deak77d22dc2014-03-05 16:20:52 +02005202
5203 for_each_power_domain(domain, pipe_domains[crtc->pipe])
5204 intel_display_power_get(dev_priv, domain);
5205 }
5206
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005207 if (dev_priv->display.modeset_global_resources)
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005208 dev_priv->display.modeset_global_resources(state);
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005209
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005210 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005211 enum intel_display_power_domain domain;
5212
5213 for_each_power_domain(domain, crtc->enabled_power_domains)
5214 intel_display_power_put(dev_priv, domain);
5215
5216 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
5217 }
5218
5219 intel_display_set_init_power(dev_priv, false);
5220}
5221
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005222static void intel_update_max_cdclk(struct drm_device *dev)
5223{
5224 struct drm_i915_private *dev_priv = dev->dev_private;
5225
5226 if (IS_SKYLAKE(dev)) {
5227 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5228
5229 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5230 dev_priv->max_cdclk_freq = 675000;
5231 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5232 dev_priv->max_cdclk_freq = 540000;
5233 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5234 dev_priv->max_cdclk_freq = 450000;
5235 else
5236 dev_priv->max_cdclk_freq = 337500;
5237 } else if (IS_BROADWELL(dev)) {
5238 /*
5239 * FIXME with extra cooling we can allow
5240 * 540 MHz for ULX and 675 Mhz for ULT.
5241 * How can we know if extra cooling is
5242 * available? PCI ID, VTB, something else?
5243 */
5244 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5245 dev_priv->max_cdclk_freq = 450000;
5246 else if (IS_BDW_ULX(dev))
5247 dev_priv->max_cdclk_freq = 450000;
5248 else if (IS_BDW_ULT(dev))
5249 dev_priv->max_cdclk_freq = 540000;
5250 else
5251 dev_priv->max_cdclk_freq = 675000;
Mika Kahola0904dea2015-06-12 10:11:32 +03005252 } else if (IS_CHERRYVIEW(dev)) {
5253 dev_priv->max_cdclk_freq = 320000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005254 } else if (IS_VALLEYVIEW(dev)) {
5255 dev_priv->max_cdclk_freq = 400000;
5256 } else {
5257 /* otherwise assume cdclk is fixed */
5258 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5259 }
5260
5261 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5262 dev_priv->max_cdclk_freq);
5263}
5264
5265static void intel_update_cdclk(struct drm_device *dev)
5266{
5267 struct drm_i915_private *dev_priv = dev->dev_private;
5268
5269 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5270 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5271 dev_priv->cdclk_freq);
5272
5273 /*
5274 * Program the gmbus_freq based on the cdclk frequency.
5275 * BSpec erroneously claims we should aim for 4MHz, but
5276 * in fact 1MHz is the correct frequency.
5277 */
5278 if (IS_VALLEYVIEW(dev)) {
5279 /*
5280 * Program the gmbus_freq based on the cdclk frequency.
5281 * BSpec erroneously claims we should aim for 4MHz, but
5282 * in fact 1MHz is the correct frequency.
5283 */
5284 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5285 }
5286
5287 if (dev_priv->max_cdclk_freq == 0)
5288 intel_update_max_cdclk(dev);
5289}
5290
Damien Lespiau70d0c572015-06-04 18:21:29 +01005291static void broxton_set_cdclk(struct drm_device *dev, int frequency)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305292{
5293 struct drm_i915_private *dev_priv = dev->dev_private;
5294 uint32_t divider;
5295 uint32_t ratio;
5296 uint32_t current_freq;
5297 int ret;
5298
5299 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5300 switch (frequency) {
5301 case 144000:
5302 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5303 ratio = BXT_DE_PLL_RATIO(60);
5304 break;
5305 case 288000:
5306 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5307 ratio = BXT_DE_PLL_RATIO(60);
5308 break;
5309 case 384000:
5310 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5311 ratio = BXT_DE_PLL_RATIO(60);
5312 break;
5313 case 576000:
5314 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5315 ratio = BXT_DE_PLL_RATIO(60);
5316 break;
5317 case 624000:
5318 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5319 ratio = BXT_DE_PLL_RATIO(65);
5320 break;
5321 case 19200:
5322 /*
5323 * Bypass frequency with DE PLL disabled. Init ratio, divider
5324 * to suppress GCC warning.
5325 */
5326 ratio = 0;
5327 divider = 0;
5328 break;
5329 default:
5330 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5331
5332 return;
5333 }
5334
5335 mutex_lock(&dev_priv->rps.hw_lock);
5336 /* Inform power controller of upcoming frequency change */
5337 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5338 0x80000000);
5339 mutex_unlock(&dev_priv->rps.hw_lock);
5340
5341 if (ret) {
5342 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5343 ret, frequency);
5344 return;
5345 }
5346
5347 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5348 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5349 current_freq = current_freq * 500 + 1000;
5350
5351 /*
5352 * DE PLL has to be disabled when
5353 * - setting to 19.2MHz (bypass, PLL isn't used)
5354 * - before setting to 624MHz (PLL needs toggling)
5355 * - before setting to any frequency from 624MHz (PLL needs toggling)
5356 */
5357 if (frequency == 19200 || frequency == 624000 ||
5358 current_freq == 624000) {
5359 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5360 /* Timeout 200us */
5361 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5362 1))
5363 DRM_ERROR("timout waiting for DE PLL unlock\n");
5364 }
5365
5366 if (frequency != 19200) {
5367 uint32_t val;
5368
5369 val = I915_READ(BXT_DE_PLL_CTL);
5370 val &= ~BXT_DE_PLL_RATIO_MASK;
5371 val |= ratio;
5372 I915_WRITE(BXT_DE_PLL_CTL, val);
5373
5374 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5375 /* Timeout 200us */
5376 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5377 DRM_ERROR("timeout waiting for DE PLL lock\n");
5378
5379 val = I915_READ(CDCLK_CTL);
5380 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5381 val |= divider;
5382 /*
5383 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5384 * enable otherwise.
5385 */
5386 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5387 if (frequency >= 500000)
5388 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5389
5390 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5391 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5392 val |= (frequency - 1000) / 500;
5393 I915_WRITE(CDCLK_CTL, val);
5394 }
5395
5396 mutex_lock(&dev_priv->rps.hw_lock);
5397 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5398 DIV_ROUND_UP(frequency, 25000));
5399 mutex_unlock(&dev_priv->rps.hw_lock);
5400
5401 if (ret) {
5402 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5403 ret, frequency);
5404 return;
5405 }
5406
Damien Lespiaua47871b2015-06-04 18:21:34 +01005407 intel_update_cdclk(dev);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305408}
5409
5410void broxton_init_cdclk(struct drm_device *dev)
5411{
5412 struct drm_i915_private *dev_priv = dev->dev_private;
5413 uint32_t val;
5414
5415 /*
5416 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5417 * or else the reset will hang because there is no PCH to respond.
5418 * Move the handshake programming to initialization sequence.
5419 * Previously was left up to BIOS.
5420 */
5421 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5422 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5423 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5424
5425 /* Enable PG1 for cdclk */
5426 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5427
5428 /* check if cd clock is enabled */
5429 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5430 DRM_DEBUG_KMS("Display already initialized\n");
5431 return;
5432 }
5433
5434 /*
5435 * FIXME:
5436 * - The initial CDCLK needs to be read from VBT.
5437 * Need to make this change after VBT has changes for BXT.
5438 * - check if setting the max (or any) cdclk freq is really necessary
5439 * here, it belongs to modeset time
5440 */
5441 broxton_set_cdclk(dev, 624000);
5442
5443 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005444 POSTING_READ(DBUF_CTL);
5445
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305446 udelay(10);
5447
5448 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5449 DRM_ERROR("DBuf power enable timeout!\n");
5450}
5451
5452void broxton_uninit_cdclk(struct drm_device *dev)
5453{
5454 struct drm_i915_private *dev_priv = dev->dev_private;
5455
5456 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005457 POSTING_READ(DBUF_CTL);
5458
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305459 udelay(10);
5460
5461 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5462 DRM_ERROR("DBuf power disable timeout!\n");
5463
5464 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5465 broxton_set_cdclk(dev, 19200);
5466
5467 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5468}
5469
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005470static const struct skl_cdclk_entry {
5471 unsigned int freq;
5472 unsigned int vco;
5473} skl_cdclk_frequencies[] = {
5474 { .freq = 308570, .vco = 8640 },
5475 { .freq = 337500, .vco = 8100 },
5476 { .freq = 432000, .vco = 8640 },
5477 { .freq = 450000, .vco = 8100 },
5478 { .freq = 540000, .vco = 8100 },
5479 { .freq = 617140, .vco = 8640 },
5480 { .freq = 675000, .vco = 8100 },
5481};
5482
5483static unsigned int skl_cdclk_decimal(unsigned int freq)
5484{
5485 return (freq - 1000) / 500;
5486}
5487
5488static unsigned int skl_cdclk_get_vco(unsigned int freq)
5489{
5490 unsigned int i;
5491
5492 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5493 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5494
5495 if (e->freq == freq)
5496 return e->vco;
5497 }
5498
5499 return 8100;
5500}
5501
5502static void
5503skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5504{
5505 unsigned int min_freq;
5506 u32 val;
5507
5508 /* select the minimum CDCLK before enabling DPLL 0 */
5509 val = I915_READ(CDCLK_CTL);
5510 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5511 val |= CDCLK_FREQ_337_308;
5512
5513 if (required_vco == 8640)
5514 min_freq = 308570;
5515 else
5516 min_freq = 337500;
5517
5518 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5519
5520 I915_WRITE(CDCLK_CTL, val);
5521 POSTING_READ(CDCLK_CTL);
5522
5523 /*
5524 * We always enable DPLL0 with the lowest link rate possible, but still
5525 * taking into account the VCO required to operate the eDP panel at the
5526 * desired frequency. The usual DP link rates operate with a VCO of
5527 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5528 * The modeset code is responsible for the selection of the exact link
5529 * rate later on, with the constraint of choosing a frequency that
5530 * works with required_vco.
5531 */
5532 val = I915_READ(DPLL_CTRL1);
5533
5534 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5535 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5536 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5537 if (required_vco == 8640)
5538 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5539 SKL_DPLL0);
5540 else
5541 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5542 SKL_DPLL0);
5543
5544 I915_WRITE(DPLL_CTRL1, val);
5545 POSTING_READ(DPLL_CTRL1);
5546
5547 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5548
5549 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5550 DRM_ERROR("DPLL0 not locked\n");
5551}
5552
5553static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5554{
5555 int ret;
5556 u32 val;
5557
5558 /* inform PCU we want to change CDCLK */
5559 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5560 mutex_lock(&dev_priv->rps.hw_lock);
5561 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5562 mutex_unlock(&dev_priv->rps.hw_lock);
5563
5564 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5565}
5566
5567static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5568{
5569 unsigned int i;
5570
5571 for (i = 0; i < 15; i++) {
5572 if (skl_cdclk_pcu_ready(dev_priv))
5573 return true;
5574 udelay(10);
5575 }
5576
5577 return false;
5578}
5579
5580static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5581{
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005582 struct drm_device *dev = dev_priv->dev;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005583 u32 freq_select, pcu_ack;
5584
5585 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5586
5587 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5588 DRM_ERROR("failed to inform PCU about cdclk change\n");
5589 return;
5590 }
5591
5592 /* set CDCLK_CTL */
5593 switch(freq) {
5594 case 450000:
5595 case 432000:
5596 freq_select = CDCLK_FREQ_450_432;
5597 pcu_ack = 1;
5598 break;
5599 case 540000:
5600 freq_select = CDCLK_FREQ_540;
5601 pcu_ack = 2;
5602 break;
5603 case 308570:
5604 case 337500:
5605 default:
5606 freq_select = CDCLK_FREQ_337_308;
5607 pcu_ack = 0;
5608 break;
5609 case 617140:
5610 case 675000:
5611 freq_select = CDCLK_FREQ_675_617;
5612 pcu_ack = 3;
5613 break;
5614 }
5615
5616 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5617 POSTING_READ(CDCLK_CTL);
5618
5619 /* inform PCU of the change */
5620 mutex_lock(&dev_priv->rps.hw_lock);
5621 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5622 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005623
5624 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005625}
5626
5627void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5628{
5629 /* disable DBUF power */
5630 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5631 POSTING_READ(DBUF_CTL);
5632
5633 udelay(10);
5634
5635 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5636 DRM_ERROR("DBuf power disable timeout\n");
5637
5638 /* disable DPLL0 */
5639 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5640 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5641 DRM_ERROR("Couldn't disable DPLL0\n");
5642
5643 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5644}
5645
5646void skl_init_cdclk(struct drm_i915_private *dev_priv)
5647{
5648 u32 val;
5649 unsigned int required_vco;
5650
5651 /* enable PCH reset handshake */
5652 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5653 I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
5654
5655 /* enable PG1 and Misc I/O */
5656 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5657
5658 /* DPLL0 already enabed !? */
5659 if (I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE) {
5660 DRM_DEBUG_DRIVER("DPLL0 already running\n");
5661 return;
5662 }
5663
5664 /* enable DPLL0 */
5665 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5666 skl_dpll0_enable(dev_priv, required_vco);
5667
5668 /* set CDCLK to the frequency the BIOS chose */
5669 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5670
5671 /* enable DBUF power */
5672 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5673 POSTING_READ(DBUF_CTL);
5674
5675 udelay(10);
5676
5677 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5678 DRM_ERROR("DBuf power enable timeout\n");
5679}
5680
Ville Syrjälädfcab172014-06-13 13:37:47 +03005681/* returns HPLL frequency in kHz */
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005682static int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005683{
Jesse Barnes586f49d2013-11-04 16:06:59 -08005684 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08005685
Jesse Barnes586f49d2013-11-04 16:06:59 -08005686 /* Obtain SKU information */
Ville Syrjäläa5805162015-05-26 20:42:30 +03005687 mutex_lock(&dev_priv->sb_lock);
Jesse Barnes586f49d2013-11-04 16:06:59 -08005688 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
5689 CCK_FUSE_HPLL_FREQ_MASK;
Ville Syrjäläa5805162015-05-26 20:42:30 +03005690 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005691
Ville Syrjälädfcab172014-06-13 13:37:47 +03005692 return vco_freq[hpll_freq] * 1000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005693}
5694
5695/* Adjust CDclk dividers to allow high res or save power if possible */
5696static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5697{
5698 struct drm_i915_private *dev_priv = dev->dev_private;
5699 u32 val, cmd;
5700
Vandana Kannan164dfd22014-11-24 13:37:41 +05305701 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5702 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005703
Ville Syrjälädfcab172014-06-13 13:37:47 +03005704 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005705 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005706 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005707 cmd = 1;
5708 else
5709 cmd = 0;
5710
5711 mutex_lock(&dev_priv->rps.hw_lock);
5712 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5713 val &= ~DSPFREQGUAR_MASK;
5714 val |= (cmd << DSPFREQGUAR_SHIFT);
5715 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5716 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5717 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5718 50)) {
5719 DRM_ERROR("timed out waiting for CDclk change\n");
5720 }
5721 mutex_unlock(&dev_priv->rps.hw_lock);
5722
Ville Syrjälä54433e92015-05-26 20:42:31 +03005723 mutex_lock(&dev_priv->sb_lock);
5724
Ville Syrjälädfcab172014-06-13 13:37:47 +03005725 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005726 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005727
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005728 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005729
Jesse Barnes30a970c2013-11-04 13:48:12 -08005730 /* adjust cdclk divider */
5731 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjälä9cf33db2014-06-13 13:37:48 +03005732 val &= ~DISPLAY_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005733 val |= divider;
5734 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005735
5736 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5737 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5738 50))
5739 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005740 }
5741
Jesse Barnes30a970c2013-11-04 13:48:12 -08005742 /* adjust self-refresh exit latency value */
5743 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5744 val &= ~0x7f;
5745
5746 /*
5747 * For high bandwidth configs, we set a higher latency in the bunit
5748 * so that the core display fetch happens in time to avoid underruns.
5749 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005750 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005751 val |= 4500 / 250; /* 4.5 usec */
5752 else
5753 val |= 3000 / 250; /* 3.0 usec */
5754 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03005755
Ville Syrjäläa5805162015-05-26 20:42:30 +03005756 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005757
Ville Syrjäläb6283052015-06-03 15:45:07 +03005758 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005759}
5760
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005761static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5762{
5763 struct drm_i915_private *dev_priv = dev->dev_private;
5764 u32 val, cmd;
5765
Vandana Kannan164dfd22014-11-24 13:37:41 +05305766 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5767 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005768
5769 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005770 case 333333:
5771 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005772 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005773 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005774 break;
5775 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005776 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005777 return;
5778 }
5779
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005780 /*
5781 * Specs are full of misinformation, but testing on actual
5782 * hardware has shown that we just need to write the desired
5783 * CCK divider into the Punit register.
5784 */
5785 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5786
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005787 mutex_lock(&dev_priv->rps.hw_lock);
5788 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5789 val &= ~DSPFREQGUAR_MASK_CHV;
5790 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5791 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5792 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5793 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5794 50)) {
5795 DRM_ERROR("timed out waiting for CDclk change\n");
5796 }
5797 mutex_unlock(&dev_priv->rps.hw_lock);
5798
Ville Syrjäläb6283052015-06-03 15:45:07 +03005799 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005800}
5801
Jesse Barnes30a970c2013-11-04 13:48:12 -08005802static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5803 int max_pixclk)
5804{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005805 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005806 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005807
Jesse Barnes30a970c2013-11-04 13:48:12 -08005808 /*
5809 * Really only a few cases to deal with, as only 4 CDclks are supported:
5810 * 200MHz
5811 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005812 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005813 * 400MHz (VLV only)
5814 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5815 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005816 *
5817 * We seem to get an unstable or solid color picture at 200MHz.
5818 * Not sure what's wrong. For now use 200MHz only when all pipes
5819 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005820 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005821 if (!IS_CHERRYVIEW(dev_priv) &&
5822 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005823 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005824 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005825 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005826 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005827 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005828 else
5829 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005830}
5831
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305832static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5833 int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005834{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305835 /*
5836 * FIXME:
5837 * - remove the guardband, it's not needed on BXT
5838 * - set 19.2MHz bypass frequency if there are no active pipes
5839 */
5840 if (max_pixclk > 576000*9/10)
5841 return 624000;
5842 else if (max_pixclk > 384000*9/10)
5843 return 576000;
5844 else if (max_pixclk > 288000*9/10)
5845 return 384000;
5846 else if (max_pixclk > 144000*9/10)
5847 return 288000;
5848 else
5849 return 144000;
5850}
5851
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005852/* Compute the max pixel clock for new configuration. Uses atomic state if
5853 * that's non-NULL, look at current state otherwise. */
5854static int intel_mode_max_pixclk(struct drm_device *dev,
5855 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005856{
Jesse Barnes30a970c2013-11-04 13:48:12 -08005857 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005858 struct intel_crtc_state *crtc_state;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005859 int max_pixclk = 0;
5860
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005861 for_each_intel_crtc(dev, intel_crtc) {
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005862 if (state)
5863 crtc_state =
5864 intel_atomic_get_crtc_state(state, intel_crtc);
5865 else
5866 crtc_state = intel_crtc->config;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005867 if (IS_ERR(crtc_state))
5868 return PTR_ERR(crtc_state);
5869
5870 if (!crtc_state->base.enable)
5871 continue;
5872
5873 max_pixclk = max(max_pixclk,
5874 crtc_state->base.adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005875 }
5876
5877 return max_pixclk;
5878}
5879
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005880static int valleyview_modeset_global_pipes(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005881{
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005882 struct drm_i915_private *dev_priv = to_i915(state->dev);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005883 struct drm_crtc *crtc;
5884 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005885 int max_pixclk = intel_mode_max_pixclk(state->dev, state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005886 int cdclk, ret = 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005887
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005888 if (max_pixclk < 0)
5889 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005890
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305891 if (IS_VALLEYVIEW(dev_priv))
5892 cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
5893 else
5894 cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
5895
5896 if (cdclk == dev_priv->cdclk_freq)
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005897 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005898
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005899 /* add all active pipes to the state */
5900 for_each_crtc(state->dev, crtc) {
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005901 crtc_state = drm_atomic_get_crtc_state(state, crtc);
5902 if (IS_ERR(crtc_state))
5903 return PTR_ERR(crtc_state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005904
5905 if (!crtc_state->active || needs_modeset(crtc_state))
5906 continue;
5907
5908 crtc_state->mode_changed = true;
5909
5910 ret = drm_atomic_add_affected_connectors(state, crtc);
5911 if (ret)
5912 break;
5913
5914 ret = drm_atomic_add_affected_planes(state, crtc);
5915 if (ret)
5916 break;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005917 }
5918
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005919 return ret;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005920}
5921
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005922static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5923{
5924 unsigned int credits, default_credits;
5925
5926 if (IS_CHERRYVIEW(dev_priv))
5927 default_credits = PFI_CREDIT(12);
5928 else
5929 default_credits = PFI_CREDIT(8);
5930
Vandana Kannan164dfd22014-11-24 13:37:41 +05305931 if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005932 /* CHV suggested value is 31 or 63 */
5933 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjäläfcc00082015-05-26 20:22:40 +03005934 credits = PFI_CREDIT_63;
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005935 else
5936 credits = PFI_CREDIT(15);
5937 } else {
5938 credits = default_credits;
5939 }
5940
5941 /*
5942 * WA - write default credits before re-programming
5943 * FIXME: should we also set the resend bit here?
5944 */
5945 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5946 default_credits);
5947
5948 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5949 credits | PFI_CREDIT_RESEND);
5950
5951 /*
5952 * FIXME is this guaranteed to clear
5953 * immediately or should we poll for it?
5954 */
5955 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
5956}
5957
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005958static void valleyview_modeset_global_resources(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005959{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005960 struct drm_device *dev = old_state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005961 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005962 int max_pixclk = intel_mode_max_pixclk(dev, NULL);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005963 int req_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005964
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005965 /* The path in intel_mode_max_pixclk() with a NULL atomic state should
5966 * never fail. */
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005967 if (WARN_ON(max_pixclk < 0))
5968 return;
5969
5970 req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005971
Vandana Kannan164dfd22014-11-24 13:37:41 +05305972 if (req_cdclk != dev_priv->cdclk_freq) {
Imre Deak738c05c2014-11-19 16:25:37 +02005973 /*
5974 * FIXME: We can end up here with all power domains off, yet
5975 * with a CDCLK frequency other than the minimum. To account
5976 * for this take the PIPE-A power domain, which covers the HW
5977 * blocks needed for the following programming. This can be
5978 * removed once it's guaranteed that we get here either with
5979 * the minimum CDCLK set, or the required power domains
5980 * enabled.
5981 */
5982 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
5983
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005984 if (IS_CHERRYVIEW(dev))
5985 cherryview_set_cdclk(dev, req_cdclk);
5986 else
5987 valleyview_set_cdclk(dev, req_cdclk);
Imre Deak738c05c2014-11-19 16:25:37 +02005988
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005989 vlv_program_pfi_credits(dev_priv);
5990
Imre Deak738c05c2014-11-19 16:25:37 +02005991 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005992 }
Jesse Barnes30a970c2013-11-04 13:48:12 -08005993}
5994
Jesse Barnes89b667f2013-04-18 14:51:36 -07005995static void valleyview_crtc_enable(struct drm_crtc *crtc)
5996{
5997 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02005998 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005999 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6000 struct intel_encoder *encoder;
6001 int pipe = intel_crtc->pipe;
Jani Nikula23538ef2013-08-27 15:12:22 +03006002 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006003
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006004 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006005 return;
6006
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006007 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
Shobhit Kumar8525a232014-06-25 12:20:39 +05306008
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006009 if (!is_dsi) {
6010 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006011 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006012 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006013 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006014 }
Daniel Vetter5b18e572014-04-24 23:55:06 +02006015
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006016 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306017 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006018
6019 intel_set_pipe_timings(intel_crtc);
6020
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006021 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6022 struct drm_i915_private *dev_priv = dev->dev_private;
6023
6024 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6025 I915_WRITE(CHV_CANVAS(pipe), 0);
6026 }
6027
Daniel Vetter5b18e572014-04-24 23:55:06 +02006028 i9xx_set_pipeconf(intel_crtc);
6029
Jesse Barnes89b667f2013-04-18 14:51:36 -07006030 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006031
Daniel Vettera72e4c92014-09-30 10:56:47 +02006032 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006033
Jesse Barnes89b667f2013-04-18 14:51:36 -07006034 for_each_encoder_on_crtc(dev, crtc, encoder)
6035 if (encoder->pre_pll_enable)
6036 encoder->pre_pll_enable(encoder);
6037
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006038 if (!is_dsi) {
6039 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006040 chv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006041 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006042 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006043 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006044
6045 for_each_encoder_on_crtc(dev, crtc, encoder)
6046 if (encoder->pre_enable)
6047 encoder->pre_enable(encoder);
6048
Jesse Barnes2dd24552013-04-25 12:55:01 -07006049 i9xx_pfit_enable(intel_crtc);
6050
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006051 intel_crtc_load_lut(crtc);
6052
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006053 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006054 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006055
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006056 assert_vblank_disabled(crtc);
6057 drm_crtc_vblank_on(crtc);
6058
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006059 for_each_encoder_on_crtc(dev, crtc, encoder)
6060 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006061}
6062
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006063static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6064{
6065 struct drm_device *dev = crtc->base.dev;
6066 struct drm_i915_private *dev_priv = dev->dev_private;
6067
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006068 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6069 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006070}
6071
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006072static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006073{
6074 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006075 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006076 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006077 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006078 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006079
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006080 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006081 return;
6082
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006083 i9xx_set_pll_dividers(intel_crtc);
6084
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006085 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306086 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006087
6088 intel_set_pipe_timings(intel_crtc);
6089
Daniel Vetter5b18e572014-04-24 23:55:06 +02006090 i9xx_set_pipeconf(intel_crtc);
6091
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006092 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006093
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006094 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006095 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006096
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006097 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006098 if (encoder->pre_enable)
6099 encoder->pre_enable(encoder);
6100
Daniel Vetterf6736a12013-06-05 13:34:30 +02006101 i9xx_enable_pll(intel_crtc);
6102
Jesse Barnes2dd24552013-04-25 12:55:01 -07006103 i9xx_pfit_enable(intel_crtc);
6104
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006105 intel_crtc_load_lut(crtc);
6106
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006107 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006108 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006109
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006110 assert_vblank_disabled(crtc);
6111 drm_crtc_vblank_on(crtc);
6112
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006113 for_each_encoder_on_crtc(dev, crtc, encoder)
6114 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006115}
6116
Daniel Vetter87476d62013-04-11 16:29:06 +02006117static void i9xx_pfit_disable(struct intel_crtc *crtc)
6118{
6119 struct drm_device *dev = crtc->base.dev;
6120 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02006121
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006122 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006123 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006124
6125 assert_pipe_disabled(dev_priv, crtc->pipe);
6126
Daniel Vetter328d8e82013-05-08 10:36:31 +02006127 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6128 I915_READ(PFIT_CONTROL));
6129 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006130}
6131
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006132static void i9xx_crtc_disable(struct drm_crtc *crtc)
6133{
6134 struct drm_device *dev = crtc->dev;
6135 struct drm_i915_private *dev_priv = dev->dev_private;
6136 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006137 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006138 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006139
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006140 /*
6141 * On gen2 planes are double buffered but the pipe isn't, so we must
6142 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03006143 * We also need to wait on all gmch platforms because of the
6144 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006145 */
Imre Deak564ed192014-06-13 14:54:21 +03006146 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006147
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006148 for_each_encoder_on_crtc(dev, crtc, encoder)
6149 encoder->disable(encoder);
6150
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006151 drm_crtc_vblank_off(crtc);
6152 assert_vblank_disabled(crtc);
6153
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006154 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006155
Daniel Vetter87476d62013-04-11 16:29:06 +02006156 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006157
Jesse Barnes89b667f2013-04-18 14:51:36 -07006158 for_each_encoder_on_crtc(dev, crtc, encoder)
6159 if (encoder->post_disable)
6160 encoder->post_disable(encoder);
6161
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006162 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006163 if (IS_CHERRYVIEW(dev))
6164 chv_disable_pll(dev_priv, pipe);
6165 else if (IS_VALLEYVIEW(dev))
6166 vlv_disable_pll(dev_priv, pipe);
6167 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006168 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006169 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006170
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006171 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006172 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006173}
6174
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006175static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006176{
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006177 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006178 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006179 enum intel_display_power_domain domain;
6180 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006181
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006182 if (!intel_crtc->active)
6183 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006184
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006185 if (to_intel_plane_state(crtc->primary->state)->visible) {
6186 intel_crtc_wait_for_pending_flips(crtc);
6187 intel_pre_disable_primary(crtc);
6188 }
6189
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02006190 intel_crtc_disable_planes(crtc, crtc->state->plane_mask);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006191 dev_priv->display.crtc_disable(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006192
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006193 domains = intel_crtc->enabled_power_domains;
6194 for_each_power_domain(domain, domains)
6195 intel_display_power_put(dev_priv, domain);
6196 intel_crtc->enabled_power_domains = 0;
6197}
6198
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006199/*
6200 * turn all crtc's off, but do not adjust state
6201 * This has to be paired with a call to intel_modeset_setup_hw_state.
6202 */
Maarten Lankhorst9716c692015-06-10 10:24:19 +02006203void intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006204{
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006205 struct drm_crtc *crtc;
6206
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006207 for_each_crtc(dev, crtc)
6208 intel_crtc_disable_noatomic(crtc);
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006209}
6210
Chris Wilsoncdd59982010-09-08 16:30:16 +01006211/* Master function to enable/disable CRTC and corresponding power wells */
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006212int intel_crtc_control(struct drm_crtc *crtc, bool enable)
Daniel Vetter976f8a22012-07-08 22:34:21 +02006213{
6214 struct drm_device *dev = crtc->dev;
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006215 struct drm_mode_config *config = &dev->mode_config;
6216 struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006217 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006218 struct intel_crtc_state *pipe_config;
6219 struct drm_atomic_state *state;
6220 int ret;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006221
Maarten Lankhorst1b509252015-06-01 12:49:48 +02006222 if (enable == intel_crtc->active)
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006223 return 0;
Maarten Lankhorst1b509252015-06-01 12:49:48 +02006224
6225 if (enable && !crtc->state->enable)
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006226 return 0;
Maarten Lankhorst1b509252015-06-01 12:49:48 +02006227
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006228 /* this function should be called with drm_modeset_lock_all for now */
6229 if (WARN_ON(!ctx))
6230 return -EIO;
6231 lockdep_assert_held(&ctx->ww_ctx);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006232
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006233 state = drm_atomic_state_alloc(dev);
6234 if (WARN_ON(!state))
6235 return -ENOMEM;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006236
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006237 state->acquire_ctx = ctx;
6238 state->allow_modeset = true;
6239
6240 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
6241 if (IS_ERR(pipe_config)) {
6242 ret = PTR_ERR(pipe_config);
6243 goto err;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006244 }
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006245 pipe_config->base.active = enable;
6246
6247 ret = intel_set_mode(state);
6248 if (!ret)
6249 return ret;
6250
6251err:
6252 DRM_ERROR("Updating crtc active failed with %i\n", ret);
6253 drm_atomic_state_free(state);
6254 return ret;
Borun Fub04c5bd2014-07-12 10:02:27 +05306255}
6256
6257/**
6258 * Sets the power management mode of the pipe and plane.
6259 */
6260void intel_crtc_update_dpms(struct drm_crtc *crtc)
6261{
6262 struct drm_device *dev = crtc->dev;
6263 struct intel_encoder *intel_encoder;
6264 bool enable = false;
6265
6266 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
6267 enable |= intel_encoder->connectors_active;
6268
6269 intel_crtc_control(crtc, enable);
Chris Wilsoncdd59982010-09-08 16:30:16 +01006270}
6271
Chris Wilsonea5b2132010-08-04 13:50:23 +01006272void intel_encoder_destroy(struct drm_encoder *encoder)
6273{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006274 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006275
Chris Wilsonea5b2132010-08-04 13:50:23 +01006276 drm_encoder_cleanup(encoder);
6277 kfree(intel_encoder);
6278}
6279
Damien Lespiau92373292013-08-08 22:28:57 +01006280/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006281 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
6282 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01006283static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006284{
6285 if (mode == DRM_MODE_DPMS_ON) {
6286 encoder->connectors_active = true;
6287
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006288 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006289 } else {
6290 encoder->connectors_active = false;
6291
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006292 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006293 }
6294}
6295
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006296/* Cross check the actual hw state with our own modeset state tracking (and it's
6297 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02006298static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006299{
6300 if (connector->get_hw_state(connector)) {
6301 struct intel_encoder *encoder = connector->encoder;
6302 struct drm_crtc *crtc;
6303 bool encoder_enabled;
6304 enum pipe pipe;
6305
6306 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6307 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03006308 connector->base.name);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006309
Dave Airlie0e32b392014-05-02 14:02:48 +10006310 /* there is no real hw state for MST connectors */
6311 if (connector->mst_port)
6312 return;
6313
Rob Clarke2c719b2014-12-15 13:56:32 -05006314 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006315 "wrong connector dpms state\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006316 I915_STATE_WARN(connector->base.encoder != &encoder->base,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006317 "active connector not linked to encoder\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006318
Dave Airlie36cd7442014-05-02 13:44:18 +10006319 if (encoder) {
Rob Clarke2c719b2014-12-15 13:56:32 -05006320 I915_STATE_WARN(!encoder->connectors_active,
Dave Airlie36cd7442014-05-02 13:44:18 +10006321 "encoder->connectors_active not set\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006322
Dave Airlie36cd7442014-05-02 13:44:18 +10006323 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -05006324 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
6325 if (I915_STATE_WARN_ON(!encoder->base.crtc))
Dave Airlie36cd7442014-05-02 13:44:18 +10006326 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006327
Dave Airlie36cd7442014-05-02 13:44:18 +10006328 crtc = encoder->base.crtc;
6329
Matt Roper83d65732015-02-25 13:12:16 -08006330 I915_STATE_WARN(!crtc->state->enable,
6331 "crtc not enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006332 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
6333 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
Dave Airlie36cd7442014-05-02 13:44:18 +10006334 "encoder active on the wrong pipe\n");
6335 }
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006336 }
6337}
6338
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006339int intel_connector_init(struct intel_connector *connector)
6340{
6341 struct drm_connector_state *connector_state;
6342
6343 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6344 if (!connector_state)
6345 return -ENOMEM;
6346
6347 connector->base.state = connector_state;
6348 return 0;
6349}
6350
6351struct intel_connector *intel_connector_alloc(void)
6352{
6353 struct intel_connector *connector;
6354
6355 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6356 if (!connector)
6357 return NULL;
6358
6359 if (intel_connector_init(connector) < 0) {
6360 kfree(connector);
6361 return NULL;
6362 }
6363
6364 return connector;
6365}
6366
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006367/* Even simpler default implementation, if there's really no special case to
6368 * consider. */
6369void intel_connector_dpms(struct drm_connector *connector, int mode)
6370{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006371 /* All the simple cases only support two dpms states. */
6372 if (mode != DRM_MODE_DPMS_ON)
6373 mode = DRM_MODE_DPMS_OFF;
6374
6375 if (mode == connector->dpms)
6376 return;
6377
6378 connector->dpms = mode;
6379
6380 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dc2013-09-29 19:15:07 +01006381 if (connector->encoder)
6382 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006383
Daniel Vetterb9805142012-08-31 17:37:33 +02006384 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006385}
6386
Daniel Vetterf0947c32012-07-02 13:10:34 +02006387/* Simple connector->get_hw_state implementation for encoders that support only
6388 * one connector and no cloning and hence the encoder state determines the state
6389 * of the connector. */
6390bool intel_connector_get_hw_state(struct intel_connector *connector)
6391{
Daniel Vetter24929352012-07-02 20:28:59 +02006392 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006393 struct intel_encoder *encoder = connector->encoder;
6394
6395 return encoder->get_hw_state(encoder, &pipe);
6396}
6397
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006398static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006399{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006400 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6401 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006402
6403 return 0;
6404}
6405
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006406static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006407 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006408{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006409 struct drm_atomic_state *state = pipe_config->base.state;
6410 struct intel_crtc *other_crtc;
6411 struct intel_crtc_state *other_crtc_state;
6412
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006413 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6414 pipe_name(pipe), pipe_config->fdi_lanes);
6415 if (pipe_config->fdi_lanes > 4) {
6416 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6417 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006418 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006419 }
6420
Paulo Zanonibafb6552013-11-02 21:07:44 -07006421 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006422 if (pipe_config->fdi_lanes > 2) {
6423 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6424 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006425 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006426 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006427 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006428 }
6429 }
6430
6431 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006432 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006433
6434 /* Ivybridge 3 pipe is really complicated */
6435 switch (pipe) {
6436 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006437 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006438 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006439 if (pipe_config->fdi_lanes <= 2)
6440 return 0;
6441
6442 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6443 other_crtc_state =
6444 intel_atomic_get_crtc_state(state, other_crtc);
6445 if (IS_ERR(other_crtc_state))
6446 return PTR_ERR(other_crtc_state);
6447
6448 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006449 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6450 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006451 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006452 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006453 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006454 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006455 if (pipe_config->fdi_lanes > 2) {
6456 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6457 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006458 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006459 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006460
6461 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6462 other_crtc_state =
6463 intel_atomic_get_crtc_state(state, other_crtc);
6464 if (IS_ERR(other_crtc_state))
6465 return PTR_ERR(other_crtc_state);
6466
6467 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006468 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006469 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006470 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006471 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006472 default:
6473 BUG();
6474 }
6475}
6476
Daniel Vettere29c22c2013-02-21 00:00:16 +01006477#define RETRY 1
6478static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006479 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006480{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006481 struct drm_device *dev = intel_crtc->base.dev;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006482 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006483 int lane, link_bw, fdi_dotclock, ret;
6484 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006485
Daniel Vettere29c22c2013-02-21 00:00:16 +01006486retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006487 /* FDI is a binary signal running at ~2.7GHz, encoding
6488 * each output octet as 10 bits. The actual frequency
6489 * is stored as a divider into a 100MHz clock, and the
6490 * mode pixel clock is stored in units of 1KHz.
6491 * Hence the bw of each lane in terms of the mode signal
6492 * is:
6493 */
6494 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6495
Damien Lespiau241bfc32013-09-25 16:45:37 +01006496 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006497
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006498 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006499 pipe_config->pipe_bpp);
6500
6501 pipe_config->fdi_lanes = lane;
6502
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006503 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006504 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006505
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006506 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6507 intel_crtc->pipe, pipe_config);
6508 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006509 pipe_config->pipe_bpp -= 2*3;
6510 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6511 pipe_config->pipe_bpp);
6512 needs_recompute = true;
6513 pipe_config->bw_constrained = true;
6514
6515 goto retry;
6516 }
6517
6518 if (needs_recompute)
6519 return RETRY;
6520
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006521 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006522}
6523
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006524static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6525 struct intel_crtc_state *pipe_config)
6526{
6527 if (pipe_config->pipe_bpp > 24)
6528 return false;
6529
6530 /* HSW can handle pixel rate up to cdclk? */
6531 if (IS_HASWELL(dev_priv->dev))
6532 return true;
6533
6534 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006535 * We compare against max which means we must take
6536 * the increased cdclk requirement into account when
6537 * calculating the new cdclk.
6538 *
6539 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006540 */
6541 return ilk_pipe_pixel_rate(pipe_config) <=
6542 dev_priv->max_cdclk_freq * 95 / 100;
6543}
6544
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006545static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006546 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006547{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006548 struct drm_device *dev = crtc->base.dev;
6549 struct drm_i915_private *dev_priv = dev->dev_private;
6550
Jani Nikulad330a952014-01-21 11:24:25 +02006551 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006552 hsw_crtc_supports_ips(crtc) &&
6553 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006554}
6555
Daniel Vettera43f6e02013-06-07 23:10:32 +02006556static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006557 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006558{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006559 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006560 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006561 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01006562
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006563 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006564 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä44913152015-06-03 15:45:10 +03006565 int clock_limit = dev_priv->max_cdclk_freq;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006566
6567 /*
6568 * Enable pixel doubling when the dot clock
6569 * is > 90% of the (display) core speed.
6570 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03006571 * GDG double wide on either pipe,
6572 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006573 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03006574 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01006575 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006576 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006577 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006578 }
6579
Damien Lespiau241bfc32013-09-25 16:45:37 +01006580 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006581 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006582 }
Chris Wilson89749352010-09-12 18:25:19 +01006583
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006584 /*
6585 * Pipe horizontal size must be even in:
6586 * - DVO ganged mode
6587 * - LVDS dual channel mode
6588 * - Double wide pipe
6589 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006590 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006591 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6592 pipe_config->pipe_src_w &= ~1;
6593
Damien Lespiau8693a822013-05-03 18:48:11 +01006594 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6595 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006596 */
6597 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
6598 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006599 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006600
Damien Lespiauf5adf942013-06-24 18:29:34 +01006601 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006602 hsw_compute_ips_config(crtc, pipe_config);
6603
Daniel Vetter877d48d2013-04-19 11:24:43 +02006604 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006605 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006606
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006607 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006608}
6609
Ville Syrjälä1652d192015-03-31 14:12:01 +03006610static int skylake_get_display_clock_speed(struct drm_device *dev)
6611{
6612 struct drm_i915_private *dev_priv = to_i915(dev);
6613 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6614 uint32_t cdctl = I915_READ(CDCLK_CTL);
6615 uint32_t linkrate;
6616
Damien Lespiau414355a2015-06-04 18:21:31 +01006617 if (!(lcpll1 & LCPLL_PLL_ENABLE))
Ville Syrjälä1652d192015-03-31 14:12:01 +03006618 return 24000; /* 24MHz is the cd freq with NSSC ref */
Ville Syrjälä1652d192015-03-31 14:12:01 +03006619
6620 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6621 return 540000;
6622
6623 linkrate = (I915_READ(DPLL_CTRL1) &
Damien Lespiau71cd8422015-04-30 16:39:17 +01006624 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006625
Damien Lespiau71cd8422015-04-30 16:39:17 +01006626 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6627 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006628 /* vco 8640 */
6629 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6630 case CDCLK_FREQ_450_432:
6631 return 432000;
6632 case CDCLK_FREQ_337_308:
6633 return 308570;
6634 case CDCLK_FREQ_675_617:
6635 return 617140;
6636 default:
6637 WARN(1, "Unknown cd freq selection\n");
6638 }
6639 } else {
6640 /* vco 8100 */
6641 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6642 case CDCLK_FREQ_450_432:
6643 return 450000;
6644 case CDCLK_FREQ_337_308:
6645 return 337500;
6646 case CDCLK_FREQ_675_617:
6647 return 675000;
6648 default:
6649 WARN(1, "Unknown cd freq selection\n");
6650 }
6651 }
6652
6653 /* error case, do as if DPLL0 isn't enabled */
6654 return 24000;
6655}
6656
6657static int broadwell_get_display_clock_speed(struct drm_device *dev)
6658{
6659 struct drm_i915_private *dev_priv = dev->dev_private;
6660 uint32_t lcpll = I915_READ(LCPLL_CTL);
6661 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6662
6663 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6664 return 800000;
6665 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6666 return 450000;
6667 else if (freq == LCPLL_CLK_FREQ_450)
6668 return 450000;
6669 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6670 return 540000;
6671 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6672 return 337500;
6673 else
6674 return 675000;
6675}
6676
6677static int haswell_get_display_clock_speed(struct drm_device *dev)
6678{
6679 struct drm_i915_private *dev_priv = dev->dev_private;
6680 uint32_t lcpll = I915_READ(LCPLL_CTL);
6681 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6682
6683 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6684 return 800000;
6685 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6686 return 450000;
6687 else if (freq == LCPLL_CLK_FREQ_450)
6688 return 450000;
6689 else if (IS_HSW_ULT(dev))
6690 return 337500;
6691 else
6692 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006693}
6694
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006695static int valleyview_get_display_clock_speed(struct drm_device *dev)
6696{
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006697 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006698 u32 val;
6699 int divider;
6700
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006701 if (dev_priv->hpll_freq == 0)
6702 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
6703
Ville Syrjäläa5805162015-05-26 20:42:30 +03006704 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006705 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03006706 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006707
6708 divider = val & DISPLAY_FREQUENCY_VALUES;
6709
Ville Syrjälä7d007f42014-06-13 13:37:53 +03006710 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
6711 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
6712 "cdclk change in progress\n");
6713
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006714 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006715}
6716
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006717static int ilk_get_display_clock_speed(struct drm_device *dev)
6718{
6719 return 450000;
6720}
6721
Jesse Barnese70236a2009-09-21 10:42:27 -07006722static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006723{
Jesse Barnese70236a2009-09-21 10:42:27 -07006724 return 400000;
6725}
Jesse Barnes79e53942008-11-07 14:24:08 -08006726
Jesse Barnese70236a2009-09-21 10:42:27 -07006727static int i915_get_display_clock_speed(struct drm_device *dev)
6728{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006729 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006730}
Jesse Barnes79e53942008-11-07 14:24:08 -08006731
Jesse Barnese70236a2009-09-21 10:42:27 -07006732static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6733{
6734 return 200000;
6735}
Jesse Barnes79e53942008-11-07 14:24:08 -08006736
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006737static int pnv_get_display_clock_speed(struct drm_device *dev)
6738{
6739 u16 gcfgc = 0;
6740
6741 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6742
6743 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6744 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006745 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006746 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006747 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006748 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006749 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006750 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6751 return 200000;
6752 default:
6753 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6754 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006755 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006756 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006757 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006758 }
6759}
6760
Jesse Barnese70236a2009-09-21 10:42:27 -07006761static int i915gm_get_display_clock_speed(struct drm_device *dev)
6762{
6763 u16 gcfgc = 0;
6764
6765 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6766
6767 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006768 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006769 else {
6770 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6771 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006772 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006773 default:
6774 case GC_DISPLAY_CLOCK_190_200_MHZ:
6775 return 190000;
6776 }
6777 }
6778}
Jesse Barnes79e53942008-11-07 14:24:08 -08006779
Jesse Barnese70236a2009-09-21 10:42:27 -07006780static int i865_get_display_clock_speed(struct drm_device *dev)
6781{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006782 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006783}
6784
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006785static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07006786{
6787 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006788
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03006789 /*
6790 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6791 * encoding is different :(
6792 * FIXME is this the right way to detect 852GM/852GMV?
6793 */
6794 if (dev->pdev->revision == 0x1)
6795 return 133333;
6796
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006797 pci_bus_read_config_word(dev->pdev->bus,
6798 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6799
Jesse Barnese70236a2009-09-21 10:42:27 -07006800 /* Assume that the hardware is in the high speed state. This
6801 * should be the default.
6802 */
6803 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6804 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006805 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07006806 case GC_CLOCK_100_200:
6807 return 200000;
6808 case GC_CLOCK_166_250:
6809 return 250000;
6810 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006811 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006812 case GC_CLOCK_133_266:
6813 case GC_CLOCK_133_266_2:
6814 case GC_CLOCK_166_266:
6815 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006816 }
6817
6818 /* Shouldn't happen */
6819 return 0;
6820}
6821
6822static int i830_get_display_clock_speed(struct drm_device *dev)
6823{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006824 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006825}
6826
Ville Syrjälä34edce22015-05-22 11:22:33 +03006827static unsigned int intel_hpll_vco(struct drm_device *dev)
6828{
6829 struct drm_i915_private *dev_priv = dev->dev_private;
6830 static const unsigned int blb_vco[8] = {
6831 [0] = 3200000,
6832 [1] = 4000000,
6833 [2] = 5333333,
6834 [3] = 4800000,
6835 [4] = 6400000,
6836 };
6837 static const unsigned int pnv_vco[8] = {
6838 [0] = 3200000,
6839 [1] = 4000000,
6840 [2] = 5333333,
6841 [3] = 4800000,
6842 [4] = 2666667,
6843 };
6844 static const unsigned int cl_vco[8] = {
6845 [0] = 3200000,
6846 [1] = 4000000,
6847 [2] = 5333333,
6848 [3] = 6400000,
6849 [4] = 3333333,
6850 [5] = 3566667,
6851 [6] = 4266667,
6852 };
6853 static const unsigned int elk_vco[8] = {
6854 [0] = 3200000,
6855 [1] = 4000000,
6856 [2] = 5333333,
6857 [3] = 4800000,
6858 };
6859 static const unsigned int ctg_vco[8] = {
6860 [0] = 3200000,
6861 [1] = 4000000,
6862 [2] = 5333333,
6863 [3] = 6400000,
6864 [4] = 2666667,
6865 [5] = 4266667,
6866 };
6867 const unsigned int *vco_table;
6868 unsigned int vco;
6869 uint8_t tmp = 0;
6870
6871 /* FIXME other chipsets? */
6872 if (IS_GM45(dev))
6873 vco_table = ctg_vco;
6874 else if (IS_G4X(dev))
6875 vco_table = elk_vco;
6876 else if (IS_CRESTLINE(dev))
6877 vco_table = cl_vco;
6878 else if (IS_PINEVIEW(dev))
6879 vco_table = pnv_vco;
6880 else if (IS_G33(dev))
6881 vco_table = blb_vco;
6882 else
6883 return 0;
6884
6885 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6886
6887 vco = vco_table[tmp & 0x7];
6888 if (vco == 0)
6889 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6890 else
6891 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6892
6893 return vco;
6894}
6895
6896static int gm45_get_display_clock_speed(struct drm_device *dev)
6897{
6898 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6899 uint16_t tmp = 0;
6900
6901 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6902
6903 cdclk_sel = (tmp >> 12) & 0x1;
6904
6905 switch (vco) {
6906 case 2666667:
6907 case 4000000:
6908 case 5333333:
6909 return cdclk_sel ? 333333 : 222222;
6910 case 3200000:
6911 return cdclk_sel ? 320000 : 228571;
6912 default:
6913 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6914 return 222222;
6915 }
6916}
6917
6918static int i965gm_get_display_clock_speed(struct drm_device *dev)
6919{
6920 static const uint8_t div_3200[] = { 16, 10, 8 };
6921 static const uint8_t div_4000[] = { 20, 12, 10 };
6922 static const uint8_t div_5333[] = { 24, 16, 14 };
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 >> 8) & 0x1f) - 1;
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 5333333:
6942 div_table = div_5333;
6943 break;
6944 default:
6945 goto fail;
6946 }
6947
6948 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6949
Damien Lespiaucaf4e252015-06-04 16:56:18 +01006950fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03006951 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
6952 return 200000;
6953}
6954
6955static int g33_get_display_clock_speed(struct drm_device *dev)
6956{
6957 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
6958 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
6959 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
6960 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
6961 const uint8_t *div_table;
6962 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6963 uint16_t tmp = 0;
6964
6965 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6966
6967 cdclk_sel = (tmp >> 4) & 0x7;
6968
6969 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6970 goto fail;
6971
6972 switch (vco) {
6973 case 3200000:
6974 div_table = div_3200;
6975 break;
6976 case 4000000:
6977 div_table = div_4000;
6978 break;
6979 case 4800000:
6980 div_table = div_4800;
6981 break;
6982 case 5333333:
6983 div_table = div_5333;
6984 break;
6985 default:
6986 goto fail;
6987 }
6988
6989 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6990
Damien Lespiaucaf4e252015-06-04 16:56:18 +01006991fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03006992 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
6993 return 190476;
6994}
6995
Zhenyu Wang2c072452009-06-05 15:38:42 +08006996static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006997intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006998{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006999 while (*num > DATA_LINK_M_N_MASK ||
7000 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08007001 *num >>= 1;
7002 *den >>= 1;
7003 }
7004}
7005
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007006static void compute_m_n(unsigned int m, unsigned int n,
7007 uint32_t *ret_m, uint32_t *ret_n)
7008{
7009 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7010 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7011 intel_reduce_m_n_ratio(ret_m, ret_n);
7012}
7013
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007014void
7015intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7016 int pixel_clock, int link_clock,
7017 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007018{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007019 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007020
7021 compute_m_n(bits_per_pixel * pixel_clock,
7022 link_clock * nlanes * 8,
7023 &m_n->gmch_m, &m_n->gmch_n);
7024
7025 compute_m_n(pixel_clock, link_clock,
7026 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007027}
7028
Chris Wilsona7615032011-01-12 17:04:08 +00007029static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7030{
Jani Nikulad330a952014-01-21 11:24:25 +02007031 if (i915.panel_use_ssc >= 0)
7032 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007033 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007034 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007035}
7036
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007037static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
7038 int num_connectors)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007039{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007040 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007041 struct drm_i915_private *dev_priv = dev->dev_private;
7042 int refclk;
7043
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007044 WARN_ON(!crtc_state->base.state);
7045
Imre Deak5ab7b0b2015-03-06 03:29:25 +02007046 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02007047 refclk = 100000;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007048 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007049 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007050 refclk = dev_priv->vbt.lvds_ssc_freq;
7051 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007052 } else if (!IS_GEN2(dev)) {
7053 refclk = 96000;
7054 } else {
7055 refclk = 48000;
7056 }
7057
7058 return refclk;
7059}
7060
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007061static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007062{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007063 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007064}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007065
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007066static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7067{
7068 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007069}
7070
Daniel Vetterf47709a2013-03-28 10:42:02 +01007071static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007072 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08007073 intel_clock_t *reduced_clock)
7074{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007075 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007076 u32 fp, fp2 = 0;
7077
7078 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007079 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007080 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007081 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007082 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007083 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007084 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007085 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007086 }
7087
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007088 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007089
Daniel Vetterf47709a2013-03-28 10:42:02 +01007090 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007091 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007092 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007093 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007094 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007095 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007096 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007097 }
7098}
7099
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007100static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7101 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007102{
7103 u32 reg_val;
7104
7105 /*
7106 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7107 * and set it to a reasonable value instead.
7108 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007109 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007110 reg_val &= 0xffffff00;
7111 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007112 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007113
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007114 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007115 reg_val &= 0x8cffffff;
7116 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007117 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007118
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007119 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007120 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007121 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007122
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007123 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007124 reg_val &= 0x00ffffff;
7125 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007126 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007127}
7128
Daniel Vetterb5518422013-05-03 11:49:48 +02007129static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7130 struct intel_link_m_n *m_n)
7131{
7132 struct drm_device *dev = crtc->base.dev;
7133 struct drm_i915_private *dev_priv = dev->dev_private;
7134 int pipe = crtc->pipe;
7135
Daniel Vettere3b95f12013-05-03 11:49:49 +02007136 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7137 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7138 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7139 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007140}
7141
7142static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007143 struct intel_link_m_n *m_n,
7144 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007145{
7146 struct drm_device *dev = crtc->base.dev;
7147 struct drm_i915_private *dev_priv = dev->dev_private;
7148 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007149 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007150
7151 if (INTEL_INFO(dev)->gen >= 5) {
7152 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7153 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7154 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7155 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007156 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7157 * for gen < 8) and if DRRS is supported (to make sure the
7158 * registers are not unnecessarily accessed).
7159 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307160 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007161 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007162 I915_WRITE(PIPE_DATA_M2(transcoder),
7163 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7164 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7165 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7166 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7167 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007168 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007169 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7170 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7171 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7172 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007173 }
7174}
7175
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307176void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007177{
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307178 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7179
7180 if (m_n == M1_N1) {
7181 dp_m_n = &crtc->config->dp_m_n;
7182 dp_m2_n2 = &crtc->config->dp_m2_n2;
7183 } else if (m_n == M2_N2) {
7184
7185 /*
7186 * M2_N2 registers are not supported. Hence m2_n2 divider value
7187 * needs to be programmed into M1_N1.
7188 */
7189 dp_m_n = &crtc->config->dp_m2_n2;
7190 } else {
7191 DRM_ERROR("Unsupported divider value\n");
7192 return;
7193 }
7194
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007195 if (crtc->config->has_pch_encoder)
7196 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007197 else
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307198 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007199}
7200
Ville Syrjäläd288f652014-10-28 13:20:22 +02007201static void vlv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007202 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007203{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007204 u32 dpll, dpll_md;
7205
7206 /*
7207 * Enable DPIO clock input. We should never disable the reference
7208 * clock for pipe B, since VGA hotplug / manual detection depends
7209 * on it.
7210 */
7211 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
7212 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
7213 /* We should never disable this, set it here for state tracking */
7214 if (crtc->pipe == PIPE_B)
7215 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7216 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007217 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007218
Ville Syrjäläd288f652014-10-28 13:20:22 +02007219 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007220 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007221 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007222}
7223
Ville Syrjäläd288f652014-10-28 13:20:22 +02007224static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007225 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007226{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007227 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007228 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007229 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007230 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007231 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007232 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007233
Ville Syrjäläa5805162015-05-26 20:42:30 +03007234 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007235
Ville Syrjäläd288f652014-10-28 13:20:22 +02007236 bestn = pipe_config->dpll.n;
7237 bestm1 = pipe_config->dpll.m1;
7238 bestm2 = pipe_config->dpll.m2;
7239 bestp1 = pipe_config->dpll.p1;
7240 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007241
Jesse Barnes89b667f2013-04-18 14:51:36 -07007242 /* See eDP HDMI DPIO driver vbios notes doc */
7243
7244 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007245 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007246 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007247
7248 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007249 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007250
7251 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007252 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007253 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007254 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007255
7256 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007257 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007258
7259 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007260 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7261 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7262 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007263 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007264
7265 /*
7266 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7267 * but we don't support that).
7268 * Note: don't use the DAC post divider as it seems unstable.
7269 */
7270 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007271 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007272
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007273 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007274 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007275
Jesse Barnes89b667f2013-04-18 14:51:36 -07007276 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007277 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007278 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7279 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007280 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03007281 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007282 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007283 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007284 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007285
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007286 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007287 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007288 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007289 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007290 0x0df40000);
7291 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007292 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007293 0x0df70000);
7294 } else { /* HDMI or VGA */
7295 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007296 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007297 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007298 0x0df70000);
7299 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007300 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007301 0x0df40000);
7302 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007303
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007304 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007305 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007306 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7307 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007308 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007309 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007310
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007311 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007312 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007313}
7314
Ville Syrjäläd288f652014-10-28 13:20:22 +02007315static void chv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007316 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007317{
Ville Syrjäläd288f652014-10-28 13:20:22 +02007318 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007319 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
7320 DPLL_VCO_ENABLE;
7321 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007322 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007323
Ville Syrjäläd288f652014-10-28 13:20:22 +02007324 pipe_config->dpll_hw_state.dpll_md =
7325 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007326}
7327
Ville Syrjäläd288f652014-10-28 13:20:22 +02007328static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007329 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007330{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007331 struct drm_device *dev = crtc->base.dev;
7332 struct drm_i915_private *dev_priv = dev->dev_private;
7333 int pipe = crtc->pipe;
7334 int dpll_reg = DPLL(crtc->pipe);
7335 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307336 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007337 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307338 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307339 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007340
Ville Syrjäläd288f652014-10-28 13:20:22 +02007341 bestn = pipe_config->dpll.n;
7342 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7343 bestm1 = pipe_config->dpll.m1;
7344 bestm2 = pipe_config->dpll.m2 >> 22;
7345 bestp1 = pipe_config->dpll.p1;
7346 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307347 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307348 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307349 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007350
7351 /*
7352 * Enable Refclk and SSC
7353 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007354 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007355 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007356
Ville Syrjäläa5805162015-05-26 20:42:30 +03007357 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007358
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007359 /* p1 and p2 divider */
7360 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7361 5 << DPIO_CHV_S1_DIV_SHIFT |
7362 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7363 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7364 1 << DPIO_CHV_K_DIV_SHIFT);
7365
7366 /* Feedback post-divider - m2 */
7367 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7368
7369 /* Feedback refclk divider - n and m1 */
7370 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7371 DPIO_CHV_M1_DIV_BY_2 |
7372 1 << DPIO_CHV_N_DIV_SHIFT);
7373
7374 /* M2 fraction division */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307375 if (bestm2_frac)
7376 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007377
7378 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307379 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7380 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7381 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7382 if (bestm2_frac)
7383 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7384 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007385
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307386 /* Program digital lock detect threshold */
7387 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7388 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7389 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7390 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7391 if (!bestm2_frac)
7392 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7393 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7394
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007395 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307396 if (vco == 5400000) {
7397 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7398 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7399 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7400 tribuf_calcntr = 0x9;
7401 } else if (vco <= 6200000) {
7402 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7403 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7404 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7405 tribuf_calcntr = 0x9;
7406 } else if (vco <= 6480000) {
7407 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7408 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7409 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7410 tribuf_calcntr = 0x8;
7411 } else {
7412 /* Not supported. Apply the same limits as in the max case */
7413 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7414 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7415 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7416 tribuf_calcntr = 0;
7417 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007418 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7419
Ville Syrjälä968040b2015-03-11 22:52:08 +02007420 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307421 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7422 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7423 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7424
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007425 /* AFC Recal */
7426 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7427 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7428 DPIO_AFC_RECAL);
7429
Ville Syrjäläa5805162015-05-26 20:42:30 +03007430 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007431}
7432
Ville Syrjäläd288f652014-10-28 13:20:22 +02007433/**
7434 * vlv_force_pll_on - forcibly enable just the PLL
7435 * @dev_priv: i915 private structure
7436 * @pipe: pipe PLL to enable
7437 * @dpll: PLL configuration
7438 *
7439 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7440 * in cases where we need the PLL enabled even when @pipe is not going to
7441 * be enabled.
7442 */
7443void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7444 const struct dpll *dpll)
7445{
7446 struct intel_crtc *crtc =
7447 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007448 struct intel_crtc_state pipe_config = {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007449 .base.crtc = &crtc->base,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007450 .pixel_multiplier = 1,
7451 .dpll = *dpll,
7452 };
7453
7454 if (IS_CHERRYVIEW(dev)) {
7455 chv_update_pll(crtc, &pipe_config);
7456 chv_prepare_pll(crtc, &pipe_config);
7457 chv_enable_pll(crtc, &pipe_config);
7458 } else {
7459 vlv_update_pll(crtc, &pipe_config);
7460 vlv_prepare_pll(crtc, &pipe_config);
7461 vlv_enable_pll(crtc, &pipe_config);
7462 }
7463}
7464
7465/**
7466 * vlv_force_pll_off - forcibly disable just the PLL
7467 * @dev_priv: i915 private structure
7468 * @pipe: pipe PLL to disable
7469 *
7470 * Disable the PLL for @pipe. To be used in cases where we need
7471 * the PLL enabled even when @pipe is not going to be enabled.
7472 */
7473void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7474{
7475 if (IS_CHERRYVIEW(dev))
7476 chv_disable_pll(to_i915(dev), pipe);
7477 else
7478 vlv_disable_pll(to_i915(dev), pipe);
7479}
7480
Daniel Vetterf47709a2013-03-28 10:42:02 +01007481static void i9xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007482 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01007483 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007484 int num_connectors)
7485{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007486 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007487 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007488 u32 dpll;
7489 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007490 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007491
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007492 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307493
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007494 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7495 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007496
7497 dpll = DPLL_VGA_MODE_DIS;
7498
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007499 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007500 dpll |= DPLLB_MODE_LVDS;
7501 else
7502 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007503
Daniel Vetteref1b4602013-06-01 17:17:04 +02007504 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007505 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007506 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007507 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007508
7509 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007510 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007511
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007512 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007513 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007514
7515 /* compute bitmask from p1 value */
7516 if (IS_PINEVIEW(dev))
7517 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7518 else {
7519 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7520 if (IS_G4X(dev) && reduced_clock)
7521 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7522 }
7523 switch (clock->p2) {
7524 case 5:
7525 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7526 break;
7527 case 7:
7528 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7529 break;
7530 case 10:
7531 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7532 break;
7533 case 14:
7534 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7535 break;
7536 }
7537 if (INTEL_INFO(dev)->gen >= 4)
7538 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7539
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007540 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007541 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007542 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007543 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7544 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7545 else
7546 dpll |= PLL_REF_INPUT_DREFCLK;
7547
7548 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007549 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007550
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007551 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007552 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007553 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007554 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007555 }
7556}
7557
Daniel Vetterf47709a2013-03-28 10:42:02 +01007558static void i8xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007559 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01007560 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007561 int num_connectors)
7562{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007563 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007564 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007565 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007566 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007567
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007568 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307569
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007570 dpll = DPLL_VGA_MODE_DIS;
7571
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007572 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007573 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7574 } else {
7575 if (clock->p1 == 2)
7576 dpll |= PLL_P1_DIVIDE_BY_TWO;
7577 else
7578 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7579 if (clock->p2 == 4)
7580 dpll |= PLL_P2_DIVIDE_BY_4;
7581 }
7582
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007583 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007584 dpll |= DPLL_DVO_2X_MODE;
7585
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007586 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007587 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7588 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7589 else
7590 dpll |= PLL_REF_INPUT_DREFCLK;
7591
7592 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007593 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007594}
7595
Daniel Vetter8a654f32013-06-01 17:16:22 +02007596static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007597{
7598 struct drm_device *dev = intel_crtc->base.dev;
7599 struct drm_i915_private *dev_priv = dev->dev_private;
7600 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007601 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02007602 struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007603 &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007604 uint32_t crtc_vtotal, crtc_vblank_end;
7605 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007606
7607 /* We need to be careful not to changed the adjusted mode, for otherwise
7608 * the hw state checker will get angry at the mismatch. */
7609 crtc_vtotal = adjusted_mode->crtc_vtotal;
7610 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007611
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007612 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007613 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007614 crtc_vtotal -= 1;
7615 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007616
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007617 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007618 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7619 else
7620 vsyncshift = adjusted_mode->crtc_hsync_start -
7621 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007622 if (vsyncshift < 0)
7623 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007624 }
7625
7626 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007627 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007628
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007629 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007630 (adjusted_mode->crtc_hdisplay - 1) |
7631 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007632 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007633 (adjusted_mode->crtc_hblank_start - 1) |
7634 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007635 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007636 (adjusted_mode->crtc_hsync_start - 1) |
7637 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7638
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007639 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007640 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007641 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007642 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007643 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007644 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007645 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007646 (adjusted_mode->crtc_vsync_start - 1) |
7647 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7648
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007649 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7650 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7651 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7652 * bits. */
7653 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7654 (pipe == PIPE_B || pipe == PIPE_C))
7655 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7656
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007657 /* pipesrc controls the size that is scaled from, which should
7658 * always be the user's requested size.
7659 */
7660 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007661 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7662 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007663}
7664
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007665static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007666 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007667{
7668 struct drm_device *dev = crtc->base.dev;
7669 struct drm_i915_private *dev_priv = dev->dev_private;
7670 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7671 uint32_t tmp;
7672
7673 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007674 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7675 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007676 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007677 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7678 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007679 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007680 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7681 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007682
7683 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007684 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7685 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007686 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007687 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7688 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007689 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007690 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7691 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007692
7693 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007694 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7695 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7696 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007697 }
7698
7699 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007700 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7701 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7702
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007703 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7704 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007705}
7706
Daniel Vetterf6a83282014-02-11 15:28:57 -08007707void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007708 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007709{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007710 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7711 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7712 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7713 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007714
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007715 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7716 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7717 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7718 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007719
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007720 mode->flags = pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007721
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007722 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7723 mode->flags |= pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007724}
7725
Daniel Vetter84b046f2013-02-19 18:48:54 +01007726static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7727{
7728 struct drm_device *dev = intel_crtc->base.dev;
7729 struct drm_i915_private *dev_priv = dev->dev_private;
7730 uint32_t pipeconf;
7731
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007732 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007733
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007734 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7735 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7736 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007737
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007738 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007739 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007740
Daniel Vetterff9ce462013-04-24 14:57:17 +02007741 /* only g4x and later have fancy bpc/dither controls */
7742 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007743 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007744 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007745 pipeconf |= PIPECONF_DITHER_EN |
7746 PIPECONF_DITHER_TYPE_SP;
7747
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007748 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007749 case 18:
7750 pipeconf |= PIPECONF_6BPC;
7751 break;
7752 case 24:
7753 pipeconf |= PIPECONF_8BPC;
7754 break;
7755 case 30:
7756 pipeconf |= PIPECONF_10BPC;
7757 break;
7758 default:
7759 /* Case prevented by intel_choose_pipe_bpp_dither. */
7760 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007761 }
7762 }
7763
7764 if (HAS_PIPE_CXSR(dev)) {
7765 if (intel_crtc->lowfreq_avail) {
7766 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7767 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7768 } else {
7769 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007770 }
7771 }
7772
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007773 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007774 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007775 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007776 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7777 else
7778 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7779 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007780 pipeconf |= PIPECONF_PROGRESSIVE;
7781
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007782 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007783 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007784
Daniel Vetter84b046f2013-02-19 18:48:54 +01007785 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7786 POSTING_READ(PIPECONF(intel_crtc->pipe));
7787}
7788
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007789static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7790 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007791{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007792 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007793 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc751ce42010-03-25 11:48:48 -07007794 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07007795 intel_clock_t clock, reduced_clock;
Daniel Vettera16af722013-04-30 14:01:44 +02007796 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007797 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01007798 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08007799 const intel_limit_t *limit;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007800 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007801 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007802 struct drm_connector_state *connector_state;
7803 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -08007804
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007805 memset(&crtc_state->dpll_hw_state, 0,
7806 sizeof(crtc_state->dpll_hw_state));
7807
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007808 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007809 if (connector_state->crtc != &crtc->base)
7810 continue;
7811
7812 encoder = to_intel_encoder(connector_state->best_encoder);
7813
Chris Wilson5eddb702010-09-11 13:48:45 +01007814 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007815 case INTEL_OUTPUT_LVDS:
7816 is_lvds = true;
7817 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007818 case INTEL_OUTPUT_DSI:
7819 is_dsi = true;
7820 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007821 default:
7822 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007823 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05007824
Eric Anholtc751ce42010-03-25 11:48:48 -07007825 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08007826 }
7827
Jani Nikulaf2335332013-09-13 11:03:09 +03007828 if (is_dsi)
Daniel Vetter5b18e572014-04-24 23:55:06 +02007829 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007830
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007831 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007832 refclk = i9xx_get_refclk(crtc_state, num_connectors);
Jani Nikulaf2335332013-09-13 11:03:09 +03007833
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007834 /*
7835 * Returns a set of divisors for the desired target clock with
7836 * the given refclk, or FALSE. The returned values represent
7837 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7838 * 2) / p1 / p2.
7839 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007840 limit = intel_limit(crtc_state, refclk);
7841 ok = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007842 crtc_state->port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007843 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03007844 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007845 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7846 return -EINVAL;
7847 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007848
Jani Nikulaf2335332013-09-13 11:03:09 +03007849 if (is_lvds && dev_priv->lvds_downclock_avail) {
7850 /*
7851 * Ensure we match the reduced clock's P to the target
7852 * clock. If the clocks don't match, we can't switch
7853 * the display clock by using the FP0/FP1. In such case
7854 * we will disable the LVDS downclock feature.
7855 */
7856 has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007857 dev_priv->display.find_dpll(limit, crtc_state,
Jani Nikulaf2335332013-09-13 11:03:09 +03007858 dev_priv->lvds_downclock,
7859 refclk, &clock,
7860 &reduced_clock);
7861 }
7862 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007863 crtc_state->dpll.n = clock.n;
7864 crtc_state->dpll.m1 = clock.m1;
7865 crtc_state->dpll.m2 = clock.m2;
7866 crtc_state->dpll.p1 = clock.p1;
7867 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007868 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007869
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007870 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007871 i8xx_update_pll(crtc, crtc_state,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307872 has_reduced_clock ? &reduced_clock : NULL,
7873 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007874 } else if (IS_CHERRYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007875 chv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007876 } else if (IS_VALLEYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007877 vlv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007878 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007879 i9xx_update_pll(crtc, crtc_state,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007880 has_reduced_clock ? &reduced_clock : NULL,
Robin Schroereba905b2014-05-18 02:24:50 +02007881 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007882 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007883
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007884 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007885}
7886
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007887static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007888 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007889{
7890 struct drm_device *dev = crtc->base.dev;
7891 struct drm_i915_private *dev_priv = dev->dev_private;
7892 uint32_t tmp;
7893
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007894 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7895 return;
7896
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007897 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007898 if (!(tmp & PFIT_ENABLE))
7899 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007900
Daniel Vetter06922822013-07-11 13:35:40 +02007901 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007902 if (INTEL_INFO(dev)->gen < 4) {
7903 if (crtc->pipe != PIPE_B)
7904 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007905 } else {
7906 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7907 return;
7908 }
7909
Daniel Vetter06922822013-07-11 13:35:40 +02007910 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007911 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7912 if (INTEL_INFO(dev)->gen < 5)
7913 pipe_config->gmch_pfit.lvds_border_bits =
7914 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7915}
7916
Jesse Barnesacbec812013-09-20 11:29:32 -07007917static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007918 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07007919{
7920 struct drm_device *dev = crtc->base.dev;
7921 struct drm_i915_private *dev_priv = dev->dev_private;
7922 int pipe = pipe_config->cpu_transcoder;
7923 intel_clock_t clock;
7924 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07007925 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07007926
Shobhit Kumarf573de52014-07-30 20:32:37 +05307927 /* In case of MIPI DPLL will not even be used */
7928 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7929 return;
7930
Ville Syrjäläa5805162015-05-26 20:42:30 +03007931 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007932 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03007933 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007934
7935 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7936 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7937 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7938 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7939 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7940
Ville Syrjäläf6466282013-10-14 14:50:31 +03007941 vlv_clock(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007942
Ville Syrjäläf6466282013-10-14 14:50:31 +03007943 /* clock.dot is the fast clock */
7944 pipe_config->port_clock = clock.dot / 5;
Jesse Barnesacbec812013-09-20 11:29:32 -07007945}
7946
Damien Lespiau5724dbd2015-01-20 12:51:52 +00007947static void
7948i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7949 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007950{
7951 struct drm_device *dev = crtc->base.dev;
7952 struct drm_i915_private *dev_priv = dev->dev_private;
7953 u32 val, base, offset;
7954 int pipe = crtc->pipe, plane = crtc->plane;
7955 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00007956 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007957 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00007958 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007959
Damien Lespiau42a7b082015-02-05 19:35:13 +00007960 val = I915_READ(DSPCNTR(plane));
7961 if (!(val & DISPLAY_PLANE_ENABLE))
7962 return;
7963
Damien Lespiaud9806c92015-01-21 14:07:19 +00007964 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00007965 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007966 DRM_DEBUG_KMS("failed to alloc fb\n");
7967 return;
7968 }
7969
Damien Lespiau1b842c82015-01-21 13:50:54 +00007970 fb = &intel_fb->base;
7971
Daniel Vetter18c52472015-02-10 17:16:09 +00007972 if (INTEL_INFO(dev)->gen >= 4) {
7973 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007974 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00007975 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
7976 }
7977 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007978
7979 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00007980 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007981 fb->pixel_format = fourcc;
7982 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007983
7984 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007985 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007986 offset = I915_READ(DSPTILEOFF(plane));
7987 else
7988 offset = I915_READ(DSPLINOFF(plane));
7989 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7990 } else {
7991 base = I915_READ(DSPADDR(plane));
7992 }
7993 plane_config->base = base;
7994
7995 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007996 fb->width = ((val >> 16) & 0xfff) + 1;
7997 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007998
7999 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008000 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008001
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008002 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008003 fb->pixel_format,
8004 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008005
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008006 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008007
Damien Lespiau2844a922015-01-20 12:51:48 +00008008 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8009 pipe_name(pipe), plane, fb->width, fb->height,
8010 fb->bits_per_pixel, base, fb->pitches[0],
8011 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008012
Damien Lespiau2d140302015-02-05 17:22:18 +00008013 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008014}
8015
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008016static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008017 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008018{
8019 struct drm_device *dev = crtc->base.dev;
8020 struct drm_i915_private *dev_priv = dev->dev_private;
8021 int pipe = pipe_config->cpu_transcoder;
8022 enum dpio_channel port = vlv_pipe_to_channel(pipe);
8023 intel_clock_t clock;
8024 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
8025 int refclk = 100000;
8026
Ville Syrjäläa5805162015-05-26 20:42:30 +03008027 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008028 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8029 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8030 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8031 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008032 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008033
8034 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
8035 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
8036 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8037 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8038 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8039
8040 chv_clock(refclk, &clock);
8041
8042 /* clock.dot is the fast clock */
8043 pipe_config->port_clock = clock.dot / 5;
8044}
8045
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008046static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008047 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008048{
8049 struct drm_device *dev = crtc->base.dev;
8050 struct drm_i915_private *dev_priv = dev->dev_private;
8051 uint32_t tmp;
8052
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008053 if (!intel_display_power_is_enabled(dev_priv,
8054 POWER_DOMAIN_PIPE(crtc->pipe)))
Imre Deakb5482bd2014-03-05 16:20:55 +02008055 return false;
8056
Daniel Vettere143a212013-07-04 12:01:15 +02008057 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008058 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02008059
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008060 tmp = I915_READ(PIPECONF(crtc->pipe));
8061 if (!(tmp & PIPECONF_ENABLE))
8062 return false;
8063
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008064 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
8065 switch (tmp & PIPECONF_BPC_MASK) {
8066 case PIPECONF_6BPC:
8067 pipe_config->pipe_bpp = 18;
8068 break;
8069 case PIPECONF_8BPC:
8070 pipe_config->pipe_bpp = 24;
8071 break;
8072 case PIPECONF_10BPC:
8073 pipe_config->pipe_bpp = 30;
8074 break;
8075 default:
8076 break;
8077 }
8078 }
8079
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008080 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
8081 pipe_config->limited_color_range = true;
8082
Ville Syrjälä282740f2013-09-04 18:30:03 +03008083 if (INTEL_INFO(dev)->gen < 4)
8084 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8085
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008086 intel_get_pipe_timings(crtc, pipe_config);
8087
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008088 i9xx_get_pfit_config(crtc, pipe_config);
8089
Daniel Vetter6c49f242013-06-06 12:45:25 +02008090 if (INTEL_INFO(dev)->gen >= 4) {
8091 tmp = I915_READ(DPLL_MD(crtc->pipe));
8092 pipe_config->pixel_multiplier =
8093 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8094 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008095 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008096 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8097 tmp = I915_READ(DPLL(crtc->pipe));
8098 pipe_config->pixel_multiplier =
8099 ((tmp & SDVO_MULTIPLIER_MASK)
8100 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8101 } else {
8102 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8103 * port and will be fixed up in the encoder->get_config
8104 * function. */
8105 pipe_config->pixel_multiplier = 1;
8106 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008107 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
8108 if (!IS_VALLEYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008109 /*
8110 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8111 * on 830. Filter it out here so that we don't
8112 * report errors due to that.
8113 */
8114 if (IS_I830(dev))
8115 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8116
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008117 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8118 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008119 } else {
8120 /* Mask out read-only status bits. */
8121 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8122 DPLL_PORTC_READY_MASK |
8123 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008124 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008125
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008126 if (IS_CHERRYVIEW(dev))
8127 chv_crtc_clock_get(crtc, pipe_config);
8128 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008129 vlv_crtc_clock_get(crtc, pipe_config);
8130 else
8131 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008132
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008133 return true;
8134}
8135
Paulo Zanonidde86e22012-12-01 12:04:25 -02008136static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008137{
8138 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008139 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008140 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008141 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008142 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008143 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008144 bool has_ck505 = false;
8145 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008146
8147 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008148 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008149 switch (encoder->type) {
8150 case INTEL_OUTPUT_LVDS:
8151 has_panel = true;
8152 has_lvds = true;
8153 break;
8154 case INTEL_OUTPUT_EDP:
8155 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008156 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008157 has_cpu_edp = true;
8158 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008159 default:
8160 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008161 }
8162 }
8163
Keith Packard99eb6a02011-09-26 14:29:12 -07008164 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008165 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008166 can_ssc = has_ck505;
8167 } else {
8168 has_ck505 = false;
8169 can_ssc = true;
8170 }
8171
Imre Deak2de69052013-05-08 13:14:04 +03008172 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8173 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008174
8175 /* Ironlake: try to setup display ref clock before DPLL
8176 * enabling. This is only under driver's control after
8177 * PCH B stepping, previous chipset stepping should be
8178 * ignoring this setting.
8179 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008180 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008181
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008182 /* As we must carefully and slowly disable/enable each source in turn,
8183 * compute the final state we want first and check if we need to
8184 * make any changes at all.
8185 */
8186 final = val;
8187 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008188 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008189 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008190 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008191 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8192
8193 final &= ~DREF_SSC_SOURCE_MASK;
8194 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8195 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008196
Keith Packard199e5d72011-09-22 12:01:57 -07008197 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008198 final |= DREF_SSC_SOURCE_ENABLE;
8199
8200 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8201 final |= DREF_SSC1_ENABLE;
8202
8203 if (has_cpu_edp) {
8204 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8205 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8206 else
8207 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8208 } else
8209 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8210 } else {
8211 final |= DREF_SSC_SOURCE_DISABLE;
8212 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8213 }
8214
8215 if (final == val)
8216 return;
8217
8218 /* Always enable nonspread source */
8219 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8220
8221 if (has_ck505)
8222 val |= DREF_NONSPREAD_CK505_ENABLE;
8223 else
8224 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8225
8226 if (has_panel) {
8227 val &= ~DREF_SSC_SOURCE_MASK;
8228 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008229
Keith Packard199e5d72011-09-22 12:01:57 -07008230 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008231 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008232 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008233 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008234 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008235 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008236
8237 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008238 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008239 POSTING_READ(PCH_DREF_CONTROL);
8240 udelay(200);
8241
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008242 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008243
8244 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008245 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008246 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008247 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008248 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008249 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008250 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008251 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008252 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008253
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008254 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008255 POSTING_READ(PCH_DREF_CONTROL);
8256 udelay(200);
8257 } else {
8258 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8259
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008260 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008261
8262 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008263 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008264
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008265 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008266 POSTING_READ(PCH_DREF_CONTROL);
8267 udelay(200);
8268
8269 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008270 val &= ~DREF_SSC_SOURCE_MASK;
8271 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008272
8273 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008274 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008275
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008276 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008277 POSTING_READ(PCH_DREF_CONTROL);
8278 udelay(200);
8279 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008280
8281 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008282}
8283
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008284static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008285{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008286 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008287
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008288 tmp = I915_READ(SOUTH_CHICKEN2);
8289 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8290 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008291
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008292 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8293 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8294 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008295
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008296 tmp = I915_READ(SOUTH_CHICKEN2);
8297 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8298 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008299
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008300 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8301 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8302 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008303}
8304
8305/* WaMPhyProgramming:hsw */
8306static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8307{
8308 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008309
8310 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8311 tmp &= ~(0xFF << 24);
8312 tmp |= (0x12 << 24);
8313 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8314
Paulo Zanonidde86e22012-12-01 12:04:25 -02008315 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8316 tmp |= (1 << 11);
8317 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8318
8319 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8320 tmp |= (1 << 11);
8321 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8322
Paulo Zanonidde86e22012-12-01 12:04:25 -02008323 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8324 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8325 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8326
8327 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8328 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8329 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8330
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008331 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8332 tmp &= ~(7 << 13);
8333 tmp |= (5 << 13);
8334 intel_sbi_write(dev_priv, 0x2080, 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, 0x2180, SBI_MPHY);
8337 tmp &= ~(7 << 13);
8338 tmp |= (5 << 13);
8339 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008340
8341 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8342 tmp &= ~0xFF;
8343 tmp |= 0x1C;
8344 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8345
8346 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8347 tmp &= ~0xFF;
8348 tmp |= 0x1C;
8349 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8350
8351 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8352 tmp &= ~(0xFF << 16);
8353 tmp |= (0x1C << 16);
8354 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8355
8356 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8357 tmp &= ~(0xFF << 16);
8358 tmp |= (0x1C << 16);
8359 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8360
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008361 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8362 tmp |= (1 << 27);
8363 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008364
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008365 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8366 tmp |= (1 << 27);
8367 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008368
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008369 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8370 tmp &= ~(0xF << 28);
8371 tmp |= (4 << 28);
8372 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008373
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008374 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8375 tmp &= ~(0xF << 28);
8376 tmp |= (4 << 28);
8377 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008378}
8379
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008380/* Implements 3 different sequences from BSpec chapter "Display iCLK
8381 * Programming" based on the parameters passed:
8382 * - Sequence to enable CLKOUT_DP
8383 * - Sequence to enable CLKOUT_DP without spread
8384 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8385 */
8386static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8387 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008388{
8389 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008390 uint32_t reg, tmp;
8391
8392 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8393 with_spread = true;
8394 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
8395 with_fdi, "LP PCH doesn't have FDI\n"))
8396 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008397
Ville Syrjäläa5805162015-05-26 20:42:30 +03008398 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008399
8400 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8401 tmp &= ~SBI_SSCCTL_DISABLE;
8402 tmp |= SBI_SSCCTL_PATHALT;
8403 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8404
8405 udelay(24);
8406
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008407 if (with_spread) {
8408 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8409 tmp &= ~SBI_SSCCTL_PATHALT;
8410 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008411
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008412 if (with_fdi) {
8413 lpt_reset_fdi_mphy(dev_priv);
8414 lpt_program_fdi_mphy(dev_priv);
8415 }
8416 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008417
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008418 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8419 SBI_GEN0 : SBI_DBUFF0;
8420 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8421 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8422 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008423
Ville Syrjäläa5805162015-05-26 20:42:30 +03008424 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008425}
8426
Paulo Zanoni47701c32013-07-23 11:19:25 -03008427/* Sequence to disable CLKOUT_DP */
8428static void lpt_disable_clkout_dp(struct drm_device *dev)
8429{
8430 struct drm_i915_private *dev_priv = dev->dev_private;
8431 uint32_t reg, tmp;
8432
Ville Syrjäläa5805162015-05-26 20:42:30 +03008433 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008434
8435 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8436 SBI_GEN0 : SBI_DBUFF0;
8437 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8438 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8439 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8440
8441 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8442 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8443 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8444 tmp |= SBI_SSCCTL_PATHALT;
8445 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8446 udelay(32);
8447 }
8448 tmp |= SBI_SSCCTL_DISABLE;
8449 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8450 }
8451
Ville Syrjäläa5805162015-05-26 20:42:30 +03008452 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008453}
8454
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008455static void lpt_init_pch_refclk(struct drm_device *dev)
8456{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008457 struct intel_encoder *encoder;
8458 bool has_vga = false;
8459
Damien Lespiaub2784e12014-08-05 11:29:37 +01008460 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008461 switch (encoder->type) {
8462 case INTEL_OUTPUT_ANALOG:
8463 has_vga = true;
8464 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008465 default:
8466 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008467 }
8468 }
8469
Paulo Zanoni47701c32013-07-23 11:19:25 -03008470 if (has_vga)
8471 lpt_enable_clkout_dp(dev, true, true);
8472 else
8473 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008474}
8475
Paulo Zanonidde86e22012-12-01 12:04:25 -02008476/*
8477 * Initialize reference clocks when the driver loads
8478 */
8479void intel_init_pch_refclk(struct drm_device *dev)
8480{
8481 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8482 ironlake_init_pch_refclk(dev);
8483 else if (HAS_PCH_LPT(dev))
8484 lpt_init_pch_refclk(dev);
8485}
8486
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008487static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008488{
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008489 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008490 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008491 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008492 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008493 struct drm_connector_state *connector_state;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008494 struct intel_encoder *encoder;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008495 int num_connectors = 0, i;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008496 bool is_lvds = false;
8497
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008498 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008499 if (connector_state->crtc != crtc_state->base.crtc)
8500 continue;
8501
8502 encoder = to_intel_encoder(connector_state->best_encoder);
8503
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008504 switch (encoder->type) {
8505 case INTEL_OUTPUT_LVDS:
8506 is_lvds = true;
8507 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008508 default:
8509 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008510 }
8511 num_connectors++;
8512 }
8513
8514 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008515 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008516 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008517 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008518 }
8519
8520 return 120000;
8521}
8522
Daniel Vetter6ff93602013-04-19 11:24:36 +02008523static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008524{
8525 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8526 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8527 int pipe = intel_crtc->pipe;
8528 uint32_t val;
8529
Daniel Vetter78114072013-06-13 00:54:57 +02008530 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008531
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008532 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008533 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008534 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008535 break;
8536 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008537 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008538 break;
8539 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008540 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008541 break;
8542 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008543 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008544 break;
8545 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008546 /* Case prevented by intel_choose_pipe_bpp_dither. */
8547 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008548 }
8549
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008550 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008551 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8552
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008553 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008554 val |= PIPECONF_INTERLACED_ILK;
8555 else
8556 val |= PIPECONF_PROGRESSIVE;
8557
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008558 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008559 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008560
Paulo Zanonic8203562012-09-12 10:06:29 -03008561 I915_WRITE(PIPECONF(pipe), val);
8562 POSTING_READ(PIPECONF(pipe));
8563}
8564
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008565/*
8566 * Set up the pipe CSC unit.
8567 *
8568 * Currently only full range RGB to limited range RGB conversion
8569 * is supported, but eventually this should handle various
8570 * RGB<->YCbCr scenarios as well.
8571 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01008572static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008573{
8574 struct drm_device *dev = crtc->dev;
8575 struct drm_i915_private *dev_priv = dev->dev_private;
8576 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8577 int pipe = intel_crtc->pipe;
8578 uint16_t coeff = 0x7800; /* 1.0 */
8579
8580 /*
8581 * TODO: Check what kind of values actually come out of the pipe
8582 * with these coeff/postoff values and adjust to get the best
8583 * accuracy. Perhaps we even need to take the bpc value into
8584 * consideration.
8585 */
8586
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008587 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008588 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8589
8590 /*
8591 * GY/GU and RY/RU should be the other way around according
8592 * to BSpec, but reality doesn't agree. Just set them up in
8593 * a way that results in the correct picture.
8594 */
8595 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8596 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8597
8598 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8599 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8600
8601 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8602 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8603
8604 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8605 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8606 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8607
8608 if (INTEL_INFO(dev)->gen > 6) {
8609 uint16_t postoff = 0;
8610
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008611 if (intel_crtc->config->limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02008612 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008613
8614 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8615 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8616 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8617
8618 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8619 } else {
8620 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8621
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008622 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008623 mode |= CSC_BLACK_SCREEN_OFFSET;
8624
8625 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8626 }
8627}
8628
Daniel Vetter6ff93602013-04-19 11:24:36 +02008629static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008630{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008631 struct drm_device *dev = crtc->dev;
8632 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008633 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008634 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008635 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008636 uint32_t val;
8637
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008638 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008639
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008640 if (IS_HASWELL(dev) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008641 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8642
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008643 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008644 val |= PIPECONF_INTERLACED_ILK;
8645 else
8646 val |= PIPECONF_PROGRESSIVE;
8647
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008648 I915_WRITE(PIPECONF(cpu_transcoder), val);
8649 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008650
8651 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8652 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008653
Satheeshakrishna M3cdf122c2014-04-08 15:46:53 +05308654 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008655 val = 0;
8656
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008657 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008658 case 18:
8659 val |= PIPEMISC_DITHER_6_BPC;
8660 break;
8661 case 24:
8662 val |= PIPEMISC_DITHER_8_BPC;
8663 break;
8664 case 30:
8665 val |= PIPEMISC_DITHER_10_BPC;
8666 break;
8667 case 36:
8668 val |= PIPEMISC_DITHER_12_BPC;
8669 break;
8670 default:
8671 /* Case prevented by pipe_config_set_bpp. */
8672 BUG();
8673 }
8674
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008675 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008676 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8677
8678 I915_WRITE(PIPEMISC(pipe), val);
8679 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008680}
8681
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008682static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008683 struct intel_crtc_state *crtc_state,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008684 intel_clock_t *clock,
8685 bool *has_reduced_clock,
8686 intel_clock_t *reduced_clock)
8687{
8688 struct drm_device *dev = crtc->dev;
8689 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008690 int refclk;
8691 const intel_limit_t *limit;
Daniel Vettera16af722013-04-30 14:01:44 +02008692 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008693
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008694 is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008695
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008696 refclk = ironlake_get_refclk(crtc_state);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008697
8698 /*
8699 * Returns a set of divisors for the desired target clock with the given
8700 * refclk, or FALSE. The returned values represent the clock equation:
8701 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8702 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008703 limit = intel_limit(crtc_state, refclk);
8704 ret = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008705 crtc_state->port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008706 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008707 if (!ret)
8708 return false;
8709
8710 if (is_lvds && dev_priv->lvds_downclock_avail) {
8711 /*
8712 * Ensure we match the reduced clock's P to the target clock.
8713 * If the clocks don't match, we can't switch the display clock
8714 * by using the FP0/FP1. In such case we will disable the LVDS
8715 * downclock feature.
8716 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02008717 *has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008718 dev_priv->display.find_dpll(limit, crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008719 dev_priv->lvds_downclock,
8720 refclk, clock,
8721 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008722 }
8723
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008724 return true;
8725}
8726
Paulo Zanonid4b19312012-11-29 11:29:32 -02008727int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8728{
8729 /*
8730 * Account for spread spectrum to avoid
8731 * oversubscribing the link. Max center spread
8732 * is 2.5%; use 5% for safety's sake.
8733 */
8734 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008735 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008736}
8737
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008738static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008739{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008740 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008741}
8742
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008743static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008744 struct intel_crtc_state *crtc_state,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008745 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008746 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008747{
8748 struct drm_crtc *crtc = &intel_crtc->base;
8749 struct drm_device *dev = crtc->dev;
8750 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008751 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008752 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008753 struct drm_connector_state *connector_state;
8754 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008755 uint32_t dpll;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008756 int factor, num_connectors = 0, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008757 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008758
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008759 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008760 if (connector_state->crtc != crtc_state->base.crtc)
8761 continue;
8762
8763 encoder = to_intel_encoder(connector_state->best_encoder);
8764
8765 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008766 case INTEL_OUTPUT_LVDS:
8767 is_lvds = true;
8768 break;
8769 case INTEL_OUTPUT_SDVO:
8770 case INTEL_OUTPUT_HDMI:
8771 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008772 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008773 default:
8774 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008775 }
8776
8777 num_connectors++;
8778 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008779
Chris Wilsonc1858122010-12-03 21:35:48 +00008780 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008781 factor = 21;
8782 if (is_lvds) {
8783 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008784 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008785 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008786 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008787 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008788 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008789
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008790 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02008791 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00008792
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008793 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8794 *fp2 |= FP_CB_TUNE;
8795
Chris Wilson5eddb702010-09-11 13:48:45 +01008796 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008797
Eric Anholta07d6782011-03-30 13:01:08 -07008798 if (is_lvds)
8799 dpll |= DPLLB_MODE_LVDS;
8800 else
8801 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008802
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008803 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008804 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008805
8806 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008807 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008808 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008809 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008810
Eric Anholta07d6782011-03-30 13:01:08 -07008811 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008812 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008813 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008814 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008815
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008816 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008817 case 5:
8818 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8819 break;
8820 case 7:
8821 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8822 break;
8823 case 10:
8824 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8825 break;
8826 case 14:
8827 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8828 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008829 }
8830
Daniel Vetterb4c09f32013-04-30 14:01:42 +02008831 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008832 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008833 else
8834 dpll |= PLL_REF_INPUT_DREFCLK;
8835
Daniel Vetter959e16d2013-06-05 13:34:21 +02008836 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008837}
8838
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008839static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8840 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008841{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008842 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008843 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008844 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03008845 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01008846 bool is_lvds = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008847 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008848
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008849 memset(&crtc_state->dpll_hw_state, 0,
8850 sizeof(crtc_state->dpll_hw_state));
8851
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03008852 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
Jesse Barnes79e53942008-11-07 14:24:08 -08008853
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008854 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8855 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
8856
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008857 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008858 &has_reduced_clock, &reduced_clock);
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008859 if (!ok && !crtc_state->clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008860 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8861 return -EINVAL;
8862 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01008863 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008864 if (!crtc_state->clock_set) {
8865 crtc_state->dpll.n = clock.n;
8866 crtc_state->dpll.m1 = clock.m1;
8867 crtc_state->dpll.m2 = clock.m2;
8868 crtc_state->dpll.p1 = clock.p1;
8869 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008870 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008871
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008872 /* 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 +02008873 if (crtc_state->has_pch_encoder) {
8874 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008875 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008876 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008877
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008878 dpll = ironlake_compute_dpll(crtc, crtc_state,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008879 &fp, &reduced_clock,
8880 has_reduced_clock ? &fp2 : NULL);
8881
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008882 crtc_state->dpll_hw_state.dpll = dpll;
8883 crtc_state->dpll_hw_state.fp0 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008884 if (has_reduced_clock)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008885 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008886 else
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008887 crtc_state->dpll_hw_state.fp1 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008888
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008889 pll = intel_get_shared_dpll(crtc, crtc_state);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008890 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03008891 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008892 pipe_name(crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07008893 return -EINVAL;
8894 }
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008895 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008896
Rodrigo Viviab585de2015-03-24 12:40:09 -07008897 if (is_lvds && has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008898 crtc->lowfreq_avail = true;
Daniel Vetterbcd644e2013-06-05 13:34:22 +02008899 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008900 crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008901
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008902 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008903}
8904
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008905static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8906 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008907{
8908 struct drm_device *dev = crtc->base.dev;
8909 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008910 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008911
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008912 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8913 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8914 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8915 & ~TU_SIZE_MASK;
8916 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8917 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8918 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8919}
8920
8921static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8922 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008923 struct intel_link_m_n *m_n,
8924 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008925{
8926 struct drm_device *dev = crtc->base.dev;
8927 struct drm_i915_private *dev_priv = dev->dev_private;
8928 enum pipe pipe = crtc->pipe;
8929
8930 if (INTEL_INFO(dev)->gen >= 5) {
8931 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8932 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8933 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8934 & ~TU_SIZE_MASK;
8935 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8936 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8937 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008938 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8939 * gen < 8) and if DRRS is supported (to make sure the
8940 * registers are not unnecessarily read).
8941 */
8942 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008943 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008944 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8945 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8946 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8947 & ~TU_SIZE_MASK;
8948 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8949 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8950 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8951 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008952 } else {
8953 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8954 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8955 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8956 & ~TU_SIZE_MASK;
8957 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8958 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8959 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8960 }
8961}
8962
8963void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008964 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008965{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02008966 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008967 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8968 else
8969 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008970 &pipe_config->dp_m_n,
8971 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008972}
8973
Daniel Vetter72419202013-04-04 13:28:53 +02008974static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008975 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02008976{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008977 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008978 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02008979}
8980
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008981static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008982 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008983{
8984 struct drm_device *dev = crtc->base.dev;
8985 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07008986 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8987 uint32_t ps_ctrl = 0;
8988 int id = -1;
8989 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008990
Chandra Kondurua1b22782015-04-07 15:28:45 -07008991 /* find scaler attached to this pipe */
8992 for (i = 0; i < crtc->num_scalers; i++) {
8993 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8994 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8995 id = i;
8996 pipe_config->pch_pfit.enabled = true;
8997 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8998 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8999 break;
9000 }
9001 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009002
Chandra Kondurua1b22782015-04-07 15:28:45 -07009003 scaler_state->scaler_id = id;
9004 if (id >= 0) {
9005 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9006 } else {
9007 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009008 }
9009}
9010
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009011static void
9012skylake_get_initial_plane_config(struct intel_crtc *crtc,
9013 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009014{
9015 struct drm_device *dev = crtc->base.dev;
9016 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00009017 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009018 int pipe = crtc->pipe;
9019 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009020 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009021 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009022 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009023
Damien Lespiaud9806c92015-01-21 14:07:19 +00009024 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009025 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009026 DRM_DEBUG_KMS("failed to alloc fb\n");
9027 return;
9028 }
9029
Damien Lespiau1b842c82015-01-21 13:50:54 +00009030 fb = &intel_fb->base;
9031
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009032 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00009033 if (!(val & PLANE_CTL_ENABLE))
9034 goto error;
9035
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009036 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9037 fourcc = skl_format_to_fourcc(pixel_format,
9038 val & PLANE_CTL_ORDER_RGBX,
9039 val & PLANE_CTL_ALPHA_MASK);
9040 fb->pixel_format = fourcc;
9041 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9042
Damien Lespiau40f46282015-02-27 11:15:21 +00009043 tiling = val & PLANE_CTL_TILED_MASK;
9044 switch (tiling) {
9045 case PLANE_CTL_TILED_LINEAR:
9046 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9047 break;
9048 case PLANE_CTL_TILED_X:
9049 plane_config->tiling = I915_TILING_X;
9050 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9051 break;
9052 case PLANE_CTL_TILED_Y:
9053 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9054 break;
9055 case PLANE_CTL_TILED_YF:
9056 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9057 break;
9058 default:
9059 MISSING_CASE(tiling);
9060 goto error;
9061 }
9062
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009063 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9064 plane_config->base = base;
9065
9066 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9067
9068 val = I915_READ(PLANE_SIZE(pipe, 0));
9069 fb->height = ((val >> 16) & 0xfff) + 1;
9070 fb->width = ((val >> 0) & 0x1fff) + 1;
9071
9072 val = I915_READ(PLANE_STRIDE(pipe, 0));
Damien Lespiau40f46282015-02-27 11:15:21 +00009073 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
9074 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009075 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9076
9077 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009078 fb->pixel_format,
9079 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009080
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009081 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009082
9083 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9084 pipe_name(pipe), fb->width, fb->height,
9085 fb->bits_per_pixel, base, fb->pitches[0],
9086 plane_config->size);
9087
Damien Lespiau2d140302015-02-05 17:22:18 +00009088 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009089 return;
9090
9091error:
9092 kfree(fb);
9093}
9094
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009095static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009096 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009097{
9098 struct drm_device *dev = crtc->base.dev;
9099 struct drm_i915_private *dev_priv = dev->dev_private;
9100 uint32_t tmp;
9101
9102 tmp = I915_READ(PF_CTL(crtc->pipe));
9103
9104 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009105 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009106 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9107 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009108
9109 /* We currently do not free assignements of panel fitters on
9110 * ivb/hsw (since we don't use the higher upscaling modes which
9111 * differentiates them) so just WARN about this case for now. */
9112 if (IS_GEN7(dev)) {
9113 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9114 PF_PIPE_SEL_IVB(crtc->pipe));
9115 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009116 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009117}
9118
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009119static void
9120ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9121 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009122{
9123 struct drm_device *dev = crtc->base.dev;
9124 struct drm_i915_private *dev_priv = dev->dev_private;
9125 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009126 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009127 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009128 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009129 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009130 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009131
Damien Lespiau42a7b082015-02-05 19:35:13 +00009132 val = I915_READ(DSPCNTR(pipe));
9133 if (!(val & DISPLAY_PLANE_ENABLE))
9134 return;
9135
Damien Lespiaud9806c92015-01-21 14:07:19 +00009136 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009137 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009138 DRM_DEBUG_KMS("failed to alloc fb\n");
9139 return;
9140 }
9141
Damien Lespiau1b842c82015-01-21 13:50:54 +00009142 fb = &intel_fb->base;
9143
Daniel Vetter18c52472015-02-10 17:16:09 +00009144 if (INTEL_INFO(dev)->gen >= 4) {
9145 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009146 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009147 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9148 }
9149 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009150
9151 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009152 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009153 fb->pixel_format = fourcc;
9154 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009155
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009156 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009157 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009158 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009159 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009160 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009161 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009162 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009163 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009164 }
9165 plane_config->base = base;
9166
9167 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009168 fb->width = ((val >> 16) & 0xfff) + 1;
9169 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009170
9171 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009172 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009173
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009174 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009175 fb->pixel_format,
9176 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009177
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009178 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009179
Damien Lespiau2844a922015-01-20 12:51:48 +00009180 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9181 pipe_name(pipe), fb->width, fb->height,
9182 fb->bits_per_pixel, base, fb->pitches[0],
9183 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009184
Damien Lespiau2d140302015-02-05 17:22:18 +00009185 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009186}
9187
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009188static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009189 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009190{
9191 struct drm_device *dev = crtc->base.dev;
9192 struct drm_i915_private *dev_priv = dev->dev_private;
9193 uint32_t tmp;
9194
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009195 if (!intel_display_power_is_enabled(dev_priv,
9196 POWER_DOMAIN_PIPE(crtc->pipe)))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009197 return false;
9198
Daniel Vettere143a212013-07-04 12:01:15 +02009199 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009200 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02009201
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009202 tmp = I915_READ(PIPECONF(crtc->pipe));
9203 if (!(tmp & PIPECONF_ENABLE))
9204 return false;
9205
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009206 switch (tmp & PIPECONF_BPC_MASK) {
9207 case PIPECONF_6BPC:
9208 pipe_config->pipe_bpp = 18;
9209 break;
9210 case PIPECONF_8BPC:
9211 pipe_config->pipe_bpp = 24;
9212 break;
9213 case PIPECONF_10BPC:
9214 pipe_config->pipe_bpp = 30;
9215 break;
9216 case PIPECONF_12BPC:
9217 pipe_config->pipe_bpp = 36;
9218 break;
9219 default:
9220 break;
9221 }
9222
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009223 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9224 pipe_config->limited_color_range = true;
9225
Daniel Vetterab9412b2013-05-03 11:49:46 +02009226 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009227 struct intel_shared_dpll *pll;
9228
Daniel Vetter88adfff2013-03-28 10:42:01 +01009229 pipe_config->has_pch_encoder = true;
9230
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009231 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9232 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9233 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009234
9235 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009236
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009237 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02009238 pipe_config->shared_dpll =
9239 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009240 } else {
9241 tmp = I915_READ(PCH_DPLL_SEL);
9242 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
9243 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
9244 else
9245 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
9246 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009247
9248 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9249
9250 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9251 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009252
9253 tmp = pipe_config->dpll_hw_state.dpll;
9254 pipe_config->pixel_multiplier =
9255 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9256 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009257
9258 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009259 } else {
9260 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009261 }
9262
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009263 intel_get_pipe_timings(crtc, pipe_config);
9264
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009265 ironlake_get_pfit_config(crtc, pipe_config);
9266
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009267 return true;
9268}
9269
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009270static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9271{
9272 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009273 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009274
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009275 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009276 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009277 pipe_name(crtc->pipe));
9278
Rob Clarke2c719b2014-12-15 13:56:32 -05009279 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9280 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
9281 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9282 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
9283 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9284 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009285 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009286 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009287 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009288 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009289 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009290 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009291 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009292 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009293 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009294
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009295 /*
9296 * In theory we can still leave IRQs enabled, as long as only the HPD
9297 * interrupts remain enabled. We used to check for that, but since it's
9298 * gen-specific and since we only disable LCPLL after we fully disable
9299 * the interrupts, the check below should be enough.
9300 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009301 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009302}
9303
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009304static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9305{
9306 struct drm_device *dev = dev_priv->dev;
9307
9308 if (IS_HASWELL(dev))
9309 return I915_READ(D_COMP_HSW);
9310 else
9311 return I915_READ(D_COMP_BDW);
9312}
9313
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009314static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9315{
9316 struct drm_device *dev = dev_priv->dev;
9317
9318 if (IS_HASWELL(dev)) {
9319 mutex_lock(&dev_priv->rps.hw_lock);
9320 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9321 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009322 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009323 mutex_unlock(&dev_priv->rps.hw_lock);
9324 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009325 I915_WRITE(D_COMP_BDW, val);
9326 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009327 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009328}
9329
9330/*
9331 * This function implements pieces of two sequences from BSpec:
9332 * - Sequence for display software to disable LCPLL
9333 * - Sequence for display software to allow package C8+
9334 * The steps implemented here are just the steps that actually touch the LCPLL
9335 * register. Callers should take care of disabling all the display engine
9336 * functions, doing the mode unset, fixing interrupts, etc.
9337 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009338static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9339 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009340{
9341 uint32_t val;
9342
9343 assert_can_disable_lcpll(dev_priv);
9344
9345 val = I915_READ(LCPLL_CTL);
9346
9347 if (switch_to_fclk) {
9348 val |= LCPLL_CD_SOURCE_FCLK;
9349 I915_WRITE(LCPLL_CTL, val);
9350
9351 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9352 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9353 DRM_ERROR("Switching to FCLK failed\n");
9354
9355 val = I915_READ(LCPLL_CTL);
9356 }
9357
9358 val |= LCPLL_PLL_DISABLE;
9359 I915_WRITE(LCPLL_CTL, val);
9360 POSTING_READ(LCPLL_CTL);
9361
9362 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9363 DRM_ERROR("LCPLL still locked\n");
9364
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009365 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009366 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009367 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009368 ndelay(100);
9369
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009370 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9371 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009372 DRM_ERROR("D_COMP RCOMP still in progress\n");
9373
9374 if (allow_power_down) {
9375 val = I915_READ(LCPLL_CTL);
9376 val |= LCPLL_POWER_DOWN_ALLOW;
9377 I915_WRITE(LCPLL_CTL, val);
9378 POSTING_READ(LCPLL_CTL);
9379 }
9380}
9381
9382/*
9383 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9384 * source.
9385 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009386static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009387{
9388 uint32_t val;
9389
9390 val = I915_READ(LCPLL_CTL);
9391
9392 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9393 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9394 return;
9395
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009396 /*
9397 * Make sure we're not on PC8 state before disabling PC8, otherwise
9398 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009399 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009400 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009401
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009402 if (val & LCPLL_POWER_DOWN_ALLOW) {
9403 val &= ~LCPLL_POWER_DOWN_ALLOW;
9404 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009405 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009406 }
9407
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009408 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009409 val |= D_COMP_COMP_FORCE;
9410 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009411 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009412
9413 val = I915_READ(LCPLL_CTL);
9414 val &= ~LCPLL_PLL_DISABLE;
9415 I915_WRITE(LCPLL_CTL, val);
9416
9417 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9418 DRM_ERROR("LCPLL not locked yet\n");
9419
9420 if (val & LCPLL_CD_SOURCE_FCLK) {
9421 val = I915_READ(LCPLL_CTL);
9422 val &= ~LCPLL_CD_SOURCE_FCLK;
9423 I915_WRITE(LCPLL_CTL, val);
9424
9425 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9426 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9427 DRM_ERROR("Switching back to LCPLL failed\n");
9428 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009429
Mika Kuoppala59bad942015-01-16 11:34:40 +02009430 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjäläb6283052015-06-03 15:45:07 +03009431 intel_update_cdclk(dev_priv->dev);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009432}
9433
Paulo Zanoni765dab672014-03-07 20:08:18 -03009434/*
9435 * Package states C8 and deeper are really deep PC states that can only be
9436 * reached when all the devices on the system allow it, so even if the graphics
9437 * device allows PC8+, it doesn't mean the system will actually get to these
9438 * states. Our driver only allows PC8+ when going into runtime PM.
9439 *
9440 * The requirements for PC8+ are that all the outputs are disabled, the power
9441 * well is disabled and most interrupts are disabled, and these are also
9442 * requirements for runtime PM. When these conditions are met, we manually do
9443 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9444 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9445 * hang the machine.
9446 *
9447 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9448 * the state of some registers, so when we come back from PC8+ we need to
9449 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9450 * need to take care of the registers kept by RC6. Notice that this happens even
9451 * if we don't put the device in PCI D3 state (which is what currently happens
9452 * because of the runtime PM support).
9453 *
9454 * For more, read "Display Sequences for Package C8" on the hardware
9455 * documentation.
9456 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009457void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009458{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009459 struct drm_device *dev = dev_priv->dev;
9460 uint32_t val;
9461
Paulo Zanonic67a4702013-08-19 13:18:09 -03009462 DRM_DEBUG_KMS("Enabling package C8+\n");
9463
Paulo Zanonic67a4702013-08-19 13:18:09 -03009464 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9465 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9466 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9467 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9468 }
9469
9470 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009471 hsw_disable_lcpll(dev_priv, true, true);
9472}
9473
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009474void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009475{
9476 struct drm_device *dev = dev_priv->dev;
9477 uint32_t val;
9478
Paulo Zanonic67a4702013-08-19 13:18:09 -03009479 DRM_DEBUG_KMS("Disabling package C8+\n");
9480
9481 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009482 lpt_init_pch_refclk(dev);
9483
9484 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9485 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9486 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9487 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9488 }
9489
9490 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009491}
9492
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009493static void broxton_modeset_global_resources(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309494{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009495 struct drm_device *dev = old_state->dev;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309496 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009497 int max_pixclk = intel_mode_max_pixclk(dev, NULL);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309498 int req_cdclk;
9499
9500 /* see the comment in valleyview_modeset_global_resources */
9501 if (WARN_ON(max_pixclk < 0))
9502 return;
9503
9504 req_cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
9505
9506 if (req_cdclk != dev_priv->cdclk_freq)
9507 broxton_set_cdclk(dev, req_cdclk);
9508}
9509
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009510/* compute the max rate for new configuration */
9511static int ilk_max_pixel_rate(struct drm_i915_private *dev_priv)
9512{
9513 struct drm_device *dev = dev_priv->dev;
9514 struct intel_crtc *intel_crtc;
9515 struct drm_crtc *crtc;
9516 int max_pixel_rate = 0;
9517 int pixel_rate;
9518
9519 for_each_crtc(dev, crtc) {
9520 if (!crtc->state->enable)
9521 continue;
9522
9523 intel_crtc = to_intel_crtc(crtc);
9524 pixel_rate = ilk_pipe_pixel_rate(intel_crtc->config);
9525
9526 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
9527 if (IS_BROADWELL(dev) && intel_crtc->config->ips_enabled)
9528 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9529
9530 max_pixel_rate = max(max_pixel_rate, pixel_rate);
9531 }
9532
9533 return max_pixel_rate;
9534}
9535
9536static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9537{
9538 struct drm_i915_private *dev_priv = dev->dev_private;
9539 uint32_t val, data;
9540 int ret;
9541
9542 if (WARN((I915_READ(LCPLL_CTL) &
9543 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9544 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9545 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9546 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9547 "trying to change cdclk frequency with cdclk not enabled\n"))
9548 return;
9549
9550 mutex_lock(&dev_priv->rps.hw_lock);
9551 ret = sandybridge_pcode_write(dev_priv,
9552 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9553 mutex_unlock(&dev_priv->rps.hw_lock);
9554 if (ret) {
9555 DRM_ERROR("failed to inform pcode about cdclk change\n");
9556 return;
9557 }
9558
9559 val = I915_READ(LCPLL_CTL);
9560 val |= LCPLL_CD_SOURCE_FCLK;
9561 I915_WRITE(LCPLL_CTL, val);
9562
9563 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9564 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9565 DRM_ERROR("Switching to FCLK failed\n");
9566
9567 val = I915_READ(LCPLL_CTL);
9568 val &= ~LCPLL_CLK_FREQ_MASK;
9569
9570 switch (cdclk) {
9571 case 450000:
9572 val |= LCPLL_CLK_FREQ_450;
9573 data = 0;
9574 break;
9575 case 540000:
9576 val |= LCPLL_CLK_FREQ_54O_BDW;
9577 data = 1;
9578 break;
9579 case 337500:
9580 val |= LCPLL_CLK_FREQ_337_5_BDW;
9581 data = 2;
9582 break;
9583 case 675000:
9584 val |= LCPLL_CLK_FREQ_675_BDW;
9585 data = 3;
9586 break;
9587 default:
9588 WARN(1, "invalid cdclk frequency\n");
9589 return;
9590 }
9591
9592 I915_WRITE(LCPLL_CTL, val);
9593
9594 val = I915_READ(LCPLL_CTL);
9595 val &= ~LCPLL_CD_SOURCE_FCLK;
9596 I915_WRITE(LCPLL_CTL, val);
9597
9598 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9599 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9600 DRM_ERROR("Switching back to LCPLL failed\n");
9601
9602 mutex_lock(&dev_priv->rps.hw_lock);
9603 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9604 mutex_unlock(&dev_priv->rps.hw_lock);
9605
9606 intel_update_cdclk(dev);
9607
9608 WARN(cdclk != dev_priv->cdclk_freq,
9609 "cdclk requested %d kHz but got %d kHz\n",
9610 cdclk, dev_priv->cdclk_freq);
9611}
9612
9613static int broadwell_calc_cdclk(struct drm_i915_private *dev_priv,
9614 int max_pixel_rate)
9615{
9616 int cdclk;
9617
9618 /*
9619 * FIXME should also account for plane ratio
9620 * once 64bpp pixel formats are supported.
9621 */
9622 if (max_pixel_rate > 540000)
9623 cdclk = 675000;
9624 else if (max_pixel_rate > 450000)
9625 cdclk = 540000;
9626 else if (max_pixel_rate > 337500)
9627 cdclk = 450000;
9628 else
9629 cdclk = 337500;
9630
9631 /*
9632 * FIXME move the cdclk caclulation to
9633 * compute_config() so we can fail gracegully.
9634 */
9635 if (cdclk > dev_priv->max_cdclk_freq) {
9636 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9637 cdclk, dev_priv->max_cdclk_freq);
9638 cdclk = dev_priv->max_cdclk_freq;
9639 }
9640
9641 return cdclk;
9642}
9643
9644static int broadwell_modeset_global_pipes(struct drm_atomic_state *state)
9645{
9646 struct drm_i915_private *dev_priv = to_i915(state->dev);
9647 struct drm_crtc *crtc;
9648 struct drm_crtc_state *crtc_state;
9649 int max_pixclk = ilk_max_pixel_rate(dev_priv);
9650 int cdclk, i;
9651
9652 cdclk = broadwell_calc_cdclk(dev_priv, max_pixclk);
9653
9654 if (cdclk == dev_priv->cdclk_freq)
9655 return 0;
9656
9657 /* add all active pipes to the state */
9658 for_each_crtc(state->dev, crtc) {
9659 if (!crtc->state->enable)
9660 continue;
9661
9662 crtc_state = drm_atomic_get_crtc_state(state, crtc);
9663 if (IS_ERR(crtc_state))
9664 return PTR_ERR(crtc_state);
9665 }
9666
9667 /* disable/enable all currently active pipes while we change cdclk */
9668 for_each_crtc_in_state(state, crtc, crtc_state, i)
9669 if (crtc_state->enable)
9670 crtc_state->mode_changed = true;
9671
9672 return 0;
9673}
9674
9675static void broadwell_modeset_global_resources(struct drm_atomic_state *state)
9676{
9677 struct drm_device *dev = state->dev;
9678 struct drm_i915_private *dev_priv = dev->dev_private;
9679 int max_pixel_rate = ilk_max_pixel_rate(dev_priv);
9680 int req_cdclk = broadwell_calc_cdclk(dev_priv, max_pixel_rate);
9681
9682 if (req_cdclk != dev_priv->cdclk_freq)
9683 broadwell_set_cdclk(dev, req_cdclk);
9684}
9685
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009686static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9687 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009688{
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009689 if (!intel_ddi_pll_select(crtc, crtc_state))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009690 return -EINVAL;
Daniel Vetter716c2e52014-06-25 22:02:02 +03009691
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009692 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009693
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009694 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009695}
9696
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309697static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9698 enum port port,
9699 struct intel_crtc_state *pipe_config)
9700{
9701 switch (port) {
9702 case PORT_A:
9703 pipe_config->ddi_pll_sel = SKL_DPLL0;
9704 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9705 break;
9706 case PORT_B:
9707 pipe_config->ddi_pll_sel = SKL_DPLL1;
9708 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9709 break;
9710 case PORT_C:
9711 pipe_config->ddi_pll_sel = SKL_DPLL2;
9712 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9713 break;
9714 default:
9715 DRM_ERROR("Incorrect port type\n");
9716 }
9717}
9718
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009719static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9720 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009721 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009722{
Damien Lespiau3148ade2014-11-21 16:14:56 +00009723 u32 temp, dpll_ctl1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009724
9725 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9726 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9727
9728 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009729 case SKL_DPLL0:
9730 /*
9731 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9732 * of the shared DPLL framework and thus needs to be read out
9733 * separately
9734 */
9735 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9736 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9737 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009738 case SKL_DPLL1:
9739 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9740 break;
9741 case SKL_DPLL2:
9742 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9743 break;
9744 case SKL_DPLL3:
9745 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9746 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009747 }
9748}
9749
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009750static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9751 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009752 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009753{
9754 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9755
9756 switch (pipe_config->ddi_pll_sel) {
9757 case PORT_CLK_SEL_WRPLL1:
9758 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9759 break;
9760 case PORT_CLK_SEL_WRPLL2:
9761 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9762 break;
9763 }
9764}
9765
Daniel Vetter26804af2014-06-25 22:01:55 +03009766static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009767 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009768{
9769 struct drm_device *dev = crtc->base.dev;
9770 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009771 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009772 enum port port;
9773 uint32_t tmp;
9774
9775 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9776
9777 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9778
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009779 if (IS_SKYLAKE(dev))
9780 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309781 else if (IS_BROXTON(dev))
9782 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009783 else
9784 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009785
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009786 if (pipe_config->shared_dpll >= 0) {
9787 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9788
9789 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9790 &pipe_config->dpll_hw_state));
9791 }
9792
Daniel Vetter26804af2014-06-25 22:01:55 +03009793 /*
9794 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9795 * DDI E. So just check whether this pipe is wired to DDI E and whether
9796 * the PCH transcoder is on.
9797 */
Damien Lespiauca370452013-12-03 13:56:24 +00009798 if (INTEL_INFO(dev)->gen < 9 &&
9799 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009800 pipe_config->has_pch_encoder = true;
9801
9802 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9803 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9804 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9805
9806 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9807 }
9808}
9809
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009810static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009811 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009812{
9813 struct drm_device *dev = crtc->base.dev;
9814 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009815 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009816 uint32_t tmp;
9817
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009818 if (!intel_display_power_is_enabled(dev_priv,
Imre Deakb5482bd2014-03-05 16:20:55 +02009819 POWER_DOMAIN_PIPE(crtc->pipe)))
9820 return false;
9821
Daniel Vettere143a212013-07-04 12:01:15 +02009822 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009823 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9824
Daniel Vettereccb1402013-05-22 00:50:22 +02009825 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9826 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9827 enum pipe trans_edp_pipe;
9828 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9829 default:
9830 WARN(1, "unknown pipe linked to edp transcoder\n");
9831 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9832 case TRANS_DDI_EDP_INPUT_A_ON:
9833 trans_edp_pipe = PIPE_A;
9834 break;
9835 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9836 trans_edp_pipe = PIPE_B;
9837 break;
9838 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9839 trans_edp_pipe = PIPE_C;
9840 break;
9841 }
9842
9843 if (trans_edp_pipe == crtc->pipe)
9844 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9845 }
9846
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009847 if (!intel_display_power_is_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02009848 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03009849 return false;
9850
Daniel Vettereccb1402013-05-22 00:50:22 +02009851 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009852 if (!(tmp & PIPECONF_ENABLE))
9853 return false;
9854
Daniel Vetter26804af2014-06-25 22:01:55 +03009855 haswell_get_ddi_port_state(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009856
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009857 intel_get_pipe_timings(crtc, pipe_config);
9858
Chandra Kondurua1b22782015-04-07 15:28:45 -07009859 if (INTEL_INFO(dev)->gen >= 9) {
9860 skl_init_scalers(dev, crtc, pipe_config);
9861 }
9862
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009863 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Chandra Konduruaf99ceda2015-05-11 14:35:47 -07009864
9865 if (INTEL_INFO(dev)->gen >= 9) {
9866 pipe_config->scaler_state.scaler_id = -1;
9867 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9868 }
9869
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009870 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009871 if (INTEL_INFO(dev)->gen == 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009872 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009873 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009874 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009875 else
9876 MISSING_CASE(INTEL_INFO(dev)->gen);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009877 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01009878
Jesse Barnese59150d2014-01-07 13:30:45 -08009879 if (IS_HASWELL(dev))
9880 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9881 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009882
Clint Taylorebb69c92014-09-30 10:30:22 -07009883 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
9884 pipe_config->pixel_multiplier =
9885 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9886 } else {
9887 pipe_config->pixel_multiplier = 1;
9888 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009889
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009890 return true;
9891}
9892
Chris Wilson560b85b2010-08-07 11:01:38 +01009893static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
9894{
9895 struct drm_device *dev = crtc->dev;
9896 struct drm_i915_private *dev_priv = dev->dev_private;
9897 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +03009898 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01009899
Ville Syrjälädc41c152014-08-13 11:57:05 +03009900 if (base) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009901 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
9902 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009903 unsigned int stride = roundup_pow_of_two(width) * 4;
9904
9905 switch (stride) {
9906 default:
9907 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9908 width, stride);
9909 stride = 256;
9910 /* fallthrough */
9911 case 256:
9912 case 512:
9913 case 1024:
9914 case 2048:
9915 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009916 }
9917
Ville Syrjälädc41c152014-08-13 11:57:05 +03009918 cntl |= CURSOR_ENABLE |
9919 CURSOR_GAMMA_ENABLE |
9920 CURSOR_FORMAT_ARGB |
9921 CURSOR_STRIDE(stride);
9922
9923 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009924 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009925
Ville Syrjälädc41c152014-08-13 11:57:05 +03009926 if (intel_crtc->cursor_cntl != 0 &&
9927 (intel_crtc->cursor_base != base ||
9928 intel_crtc->cursor_size != size ||
9929 intel_crtc->cursor_cntl != cntl)) {
9930 /* On these chipsets we can only modify the base/size/stride
9931 * whilst the cursor is disabled.
9932 */
9933 I915_WRITE(_CURACNTR, 0);
9934 POSTING_READ(_CURACNTR);
9935 intel_crtc->cursor_cntl = 0;
9936 }
9937
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009938 if (intel_crtc->cursor_base != base) {
Ville Syrjälädc41c152014-08-13 11:57:05 +03009939 I915_WRITE(_CURABASE, base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009940 intel_crtc->cursor_base = base;
9941 }
Ville Syrjälädc41c152014-08-13 11:57:05 +03009942
9943 if (intel_crtc->cursor_size != size) {
9944 I915_WRITE(CURSIZE, size);
9945 intel_crtc->cursor_size = size;
9946 }
9947
Chris Wilson4b0e3332014-05-30 16:35:26 +03009948 if (intel_crtc->cursor_cntl != cntl) {
9949 I915_WRITE(_CURACNTR, cntl);
9950 POSTING_READ(_CURACNTR);
9951 intel_crtc->cursor_cntl = cntl;
9952 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009953}
9954
9955static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
9956{
9957 struct drm_device *dev = crtc->dev;
9958 struct drm_i915_private *dev_priv = dev->dev_private;
9959 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9960 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009961 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +01009962
Chris Wilson4b0e3332014-05-30 16:35:26 +03009963 cntl = 0;
9964 if (base) {
9965 cntl = MCURSOR_GAMMA_ENABLE;
Matt Roper3dd512f2015-02-27 10:12:00 -08009966 switch (intel_crtc->base.cursor->state->crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309967 case 64:
9968 cntl |= CURSOR_MODE_64_ARGB_AX;
9969 break;
9970 case 128:
9971 cntl |= CURSOR_MODE_128_ARGB_AX;
9972 break;
9973 case 256:
9974 cntl |= CURSOR_MODE_256_ARGB_AX;
9975 break;
9976 default:
Matt Roper3dd512f2015-02-27 10:12:00 -08009977 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309978 return;
Chris Wilson560b85b2010-08-07 11:01:38 +01009979 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009980 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +03009981
9982 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
9983 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson560b85b2010-08-07 11:01:38 +01009984 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009985
Matt Roper8e7d6882015-01-21 16:35:41 -08009986 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009987 cntl |= CURSOR_ROTATE_180;
9988
Chris Wilson4b0e3332014-05-30 16:35:26 +03009989 if (intel_crtc->cursor_cntl != cntl) {
9990 I915_WRITE(CURCNTR(pipe), cntl);
9991 POSTING_READ(CURCNTR(pipe));
9992 intel_crtc->cursor_cntl = cntl;
9993 }
9994
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009995 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009996 I915_WRITE(CURBASE(pipe), base);
9997 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009998
9999 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010000}
10001
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010002/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +010010003static void intel_crtc_update_cursor(struct drm_crtc *crtc,
10004 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010005{
10006 struct drm_device *dev = crtc->dev;
10007 struct drm_i915_private *dev_priv = dev->dev_private;
10008 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10009 int pipe = intel_crtc->pipe;
Matt Roper3d7d6512014-06-10 08:28:13 -070010010 int x = crtc->cursor_x;
10011 int y = crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +030010012 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010013
Ville Syrjäläd6e4db12013-09-04 18:25:31 +030010014 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010015 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010016
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010017 if (x >= intel_crtc->config->pipe_src_w)
Ville Syrjäläd6e4db12013-09-04 18:25:31 +030010018 base = 0;
10019
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010020 if (y >= intel_crtc->config->pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010021 base = 0;
10022
10023 if (x < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -080010024 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010025 base = 0;
10026
10027 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10028 x = -x;
10029 }
10030 pos |= x << CURSOR_X_SHIFT;
10031
10032 if (y < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -080010033 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010034 base = 0;
10035
10036 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10037 y = -y;
10038 }
10039 pos |= y << CURSOR_Y_SHIFT;
10040
Chris Wilson4b0e3332014-05-30 16:35:26 +030010041 if (base == 0 && intel_crtc->cursor_base == 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010042 return;
10043
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010044 I915_WRITE(CURPOS(pipe), pos);
10045
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010046 /* ILK+ do this automagically */
10047 if (HAS_GMCH_DISPLAY(dev) &&
Matt Roper8e7d6882015-01-21 16:35:41 -080010048 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
Matt Roper3dd512f2015-02-27 10:12:00 -080010049 base += (intel_crtc->base.cursor->state->crtc_h *
10050 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010051 }
10052
Ville Syrjälä8ac54662014-08-12 19:39:54 +030010053 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010054 i845_update_cursor(crtc, base);
10055 else
10056 i9xx_update_cursor(crtc, base);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010057}
10058
Ville Syrjälädc41c152014-08-13 11:57:05 +030010059static bool cursor_size_ok(struct drm_device *dev,
10060 uint32_t width, uint32_t height)
10061{
10062 if (width == 0 || height == 0)
10063 return false;
10064
10065 /*
10066 * 845g/865g are special in that they are only limited by
10067 * the width of their cursors, the height is arbitrary up to
10068 * the precision of the register. Everything else requires
10069 * square cursors, limited to a few power-of-two sizes.
10070 */
10071 if (IS_845G(dev) || IS_I865G(dev)) {
10072 if ((width & 63) != 0)
10073 return false;
10074
10075 if (width > (IS_845G(dev) ? 64 : 512))
10076 return false;
10077
10078 if (height > 1023)
10079 return false;
10080 } else {
10081 switch (width | height) {
10082 case 256:
10083 case 128:
10084 if (IS_GEN2(dev))
10085 return false;
10086 case 64:
10087 break;
10088 default:
10089 return false;
10090 }
10091 }
10092
10093 return true;
10094}
10095
Jesse Barnes79e53942008-11-07 14:24:08 -080010096static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +010010097 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -080010098{
James Simmons72034252010-08-03 01:33:19 +010010099 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -080010100 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080010101
James Simmons72034252010-08-03 01:33:19 +010010102 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010103 intel_crtc->lut_r[i] = red[i] >> 8;
10104 intel_crtc->lut_g[i] = green[i] >> 8;
10105 intel_crtc->lut_b[i] = blue[i] >> 8;
10106 }
10107
10108 intel_crtc_load_lut(crtc);
10109}
10110
Jesse Barnes79e53942008-11-07 14:24:08 -080010111/* VESA 640x480x72Hz mode to set on the pipe */
10112static struct drm_display_mode load_detect_mode = {
10113 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10114 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10115};
10116
Daniel Vettera8bb6812014-02-10 18:00:39 +010010117struct drm_framebuffer *
10118__intel_framebuffer_create(struct drm_device *dev,
10119 struct drm_mode_fb_cmd2 *mode_cmd,
10120 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010121{
10122 struct intel_framebuffer *intel_fb;
10123 int ret;
10124
10125 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
10126 if (!intel_fb) {
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +030010127 drm_gem_object_unreference(&obj->base);
Chris Wilsond2dff872011-04-19 08:36:26 +010010128 return ERR_PTR(-ENOMEM);
10129 }
10130
10131 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010132 if (ret)
10133 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010134
10135 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010136err:
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +030010137 drm_gem_object_unreference(&obj->base);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010138 kfree(intel_fb);
10139
10140 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010141}
10142
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010143static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010144intel_framebuffer_create(struct drm_device *dev,
10145 struct drm_mode_fb_cmd2 *mode_cmd,
10146 struct drm_i915_gem_object *obj)
10147{
10148 struct drm_framebuffer *fb;
10149 int ret;
10150
10151 ret = i915_mutex_lock_interruptible(dev);
10152 if (ret)
10153 return ERR_PTR(ret);
10154 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10155 mutex_unlock(&dev->struct_mutex);
10156
10157 return fb;
10158}
10159
Chris Wilsond2dff872011-04-19 08:36:26 +010010160static u32
10161intel_framebuffer_pitch_for_width(int width, int bpp)
10162{
10163 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10164 return ALIGN(pitch, 64);
10165}
10166
10167static u32
10168intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10169{
10170 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010171 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010172}
10173
10174static struct drm_framebuffer *
10175intel_framebuffer_create_for_mode(struct drm_device *dev,
10176 struct drm_display_mode *mode,
10177 int depth, int bpp)
10178{
10179 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010180 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010181
10182 obj = i915_gem_alloc_object(dev,
10183 intel_framebuffer_size_for_mode(mode, bpp));
10184 if (obj == NULL)
10185 return ERR_PTR(-ENOMEM);
10186
10187 mode_cmd.width = mode->hdisplay;
10188 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010189 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10190 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010191 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010192
10193 return intel_framebuffer_create(dev, &mode_cmd, obj);
10194}
10195
10196static struct drm_framebuffer *
10197mode_fits_in_fbdev(struct drm_device *dev,
10198 struct drm_display_mode *mode)
10199{
Daniel Vetter4520f532013-10-09 09:18:51 +020010200#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +010010201 struct drm_i915_private *dev_priv = dev->dev_private;
10202 struct drm_i915_gem_object *obj;
10203 struct drm_framebuffer *fb;
10204
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010205 if (!dev_priv->fbdev)
10206 return NULL;
10207
10208 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010209 return NULL;
10210
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010211 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010212 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010213
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010214 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010215 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10216 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010217 return NULL;
10218
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010219 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010220 return NULL;
10221
10222 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010223#else
10224 return NULL;
10225#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010226}
10227
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010228static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10229 struct drm_crtc *crtc,
10230 struct drm_display_mode *mode,
10231 struct drm_framebuffer *fb,
10232 int x, int y)
10233{
10234 struct drm_plane_state *plane_state;
10235 int hdisplay, vdisplay;
10236 int ret;
10237
10238 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10239 if (IS_ERR(plane_state))
10240 return PTR_ERR(plane_state);
10241
10242 if (mode)
10243 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10244 else
10245 hdisplay = vdisplay = 0;
10246
10247 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10248 if (ret)
10249 return ret;
10250 drm_atomic_set_fb_for_plane(plane_state, fb);
10251 plane_state->crtc_x = 0;
10252 plane_state->crtc_y = 0;
10253 plane_state->crtc_w = hdisplay;
10254 plane_state->crtc_h = vdisplay;
10255 plane_state->src_x = x << 16;
10256 plane_state->src_y = y << 16;
10257 plane_state->src_w = hdisplay << 16;
10258 plane_state->src_h = vdisplay << 16;
10259
10260 return 0;
10261}
10262
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010263bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010264 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010265 struct intel_load_detect_pipe *old,
10266 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010267{
10268 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010269 struct intel_encoder *intel_encoder =
10270 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010271 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010272 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010273 struct drm_crtc *crtc = NULL;
10274 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010275 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010276 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010277 struct drm_atomic_state *state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010278 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010279 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010280 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010281
Chris Wilsond2dff872011-04-19 08:36:26 +010010282 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010283 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010284 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010285
Rob Clark51fd3712013-11-19 12:10:12 -050010286retry:
10287 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10288 if (ret)
10289 goto fail_unlock;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010290
Jesse Barnes79e53942008-11-07 14:24:08 -080010291 /*
10292 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010293 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010294 * - if the connector already has an assigned crtc, use it (but make
10295 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010296 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010297 * - try to find the first unused crtc that can drive this connector,
10298 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010299 */
10300
10301 /* See if we already have a CRTC for this connector */
10302 if (encoder->crtc) {
10303 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010304
Rob Clark51fd3712013-11-19 12:10:12 -050010305 ret = drm_modeset_lock(&crtc->mutex, ctx);
10306 if (ret)
10307 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010308 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10309 if (ret)
10310 goto fail_unlock;
Daniel Vetter7b240562012-12-12 00:35:33 +010010311
Daniel Vetter24218aa2012-08-12 19:27:11 +020010312 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +010010313 old->load_detect_temp = false;
10314
10315 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +020010316 if (connector->dpms != DRM_MODE_DPMS_ON)
10317 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +010010318
Chris Wilson71731882011-04-19 23:10:58 +010010319 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -080010320 }
10321
10322 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010323 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010324 i++;
10325 if (!(encoder->possible_crtcs & (1 << i)))
10326 continue;
Matt Roper83d65732015-02-25 13:12:16 -080010327 if (possible_crtc->state->enable)
Ville Syrjäläa4592492014-08-11 13:15:36 +030010328 continue;
10329 /* This can occur when applying the pipe A quirk on resume. */
10330 if (to_intel_crtc(possible_crtc)->new_enabled)
10331 continue;
10332
10333 crtc = possible_crtc;
10334 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010335 }
10336
10337 /*
10338 * If we didn't find an unused CRTC, don't use any.
10339 */
10340 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010341 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Rob Clark51fd3712013-11-19 12:10:12 -050010342 goto fail_unlock;
Jesse Barnes79e53942008-11-07 14:24:08 -080010343 }
10344
Rob Clark51fd3712013-11-19 12:10:12 -050010345 ret = drm_modeset_lock(&crtc->mutex, ctx);
10346 if (ret)
10347 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010348 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10349 if (ret)
10350 goto fail_unlock;
Daniel Vetterfc303102012-07-09 10:40:58 +020010351 intel_encoder->new_crtc = to_intel_crtc(crtc);
10352 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010353
10354 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010355 intel_crtc->new_enabled = true;
Daniel Vetter24218aa2012-08-12 19:27:11 +020010356 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +010010357 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +010010358 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -080010359
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010360 state = drm_atomic_state_alloc(dev);
10361 if (!state)
10362 return false;
10363
10364 state->acquire_ctx = ctx;
10365
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010366 connector_state = drm_atomic_get_connector_state(state, connector);
10367 if (IS_ERR(connector_state)) {
10368 ret = PTR_ERR(connector_state);
10369 goto fail;
10370 }
10371
10372 connector_state->crtc = crtc;
10373 connector_state->best_encoder = &intel_encoder->base;
10374
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010375 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10376 if (IS_ERR(crtc_state)) {
10377 ret = PTR_ERR(crtc_state);
10378 goto fail;
10379 }
10380
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010381 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010382
Chris Wilson64927112011-04-20 07:25:26 +010010383 if (!mode)
10384 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010385
Chris Wilsond2dff872011-04-19 08:36:26 +010010386 /* We need a framebuffer large enough to accommodate all accesses
10387 * that the plane may generate whilst we perform load detection.
10388 * We can not rely on the fbcon either being present (we get called
10389 * during its initialisation to detect all boot displays, or it may
10390 * not even exist) or that it is large enough to satisfy the
10391 * requested mode.
10392 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010393 fb = mode_fits_in_fbdev(dev, mode);
10394 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010395 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010396 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
10397 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010398 } else
10399 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010400 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010401 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010402 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010403 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010404
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010405 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10406 if (ret)
10407 goto fail;
10408
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010409 drm_mode_copy(&crtc_state->base.mode, mode);
10410
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020010411 if (intel_set_mode(state)) {
Chris Wilson64927112011-04-20 07:25:26 +010010412 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +010010413 if (old->release_fb)
10414 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010415 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010416 }
Daniel Vetter9128b042015-03-03 17:31:21 +010010417 crtc->primary->crtc = crtc;
Chris Wilson71731882011-04-19 23:10:58 +010010418
Jesse Barnes79e53942008-11-07 14:24:08 -080010419 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010420 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010421 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010422
10423 fail:
Matt Roper83d65732015-02-25 13:12:16 -080010424 intel_crtc->new_enabled = crtc->state->enable;
Rob Clark51fd3712013-11-19 12:10:12 -050010425fail_unlock:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010426 drm_atomic_state_free(state);
10427 state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010428
Rob Clark51fd3712013-11-19 12:10:12 -050010429 if (ret == -EDEADLK) {
10430 drm_modeset_backoff(ctx);
10431 goto retry;
10432 }
10433
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010434 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010435}
10436
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010437void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010438 struct intel_load_detect_pipe *old,
10439 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010440{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010441 struct drm_device *dev = connector->dev;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010442 struct intel_encoder *intel_encoder =
10443 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010444 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +010010445 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010446 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010447 struct drm_atomic_state *state;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010448 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010449 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010450 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010451
Chris Wilsond2dff872011-04-19 08:36:26 +010010452 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010453 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010454 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010455
Chris Wilson8261b192011-04-19 23:18:09 +010010456 if (old->load_detect_temp) {
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010457 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010458 if (!state)
10459 goto fail;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010460
10461 state->acquire_ctx = ctx;
10462
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010463 connector_state = drm_atomic_get_connector_state(state, connector);
10464 if (IS_ERR(connector_state))
10465 goto fail;
10466
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010467 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10468 if (IS_ERR(crtc_state))
10469 goto fail;
10470
Daniel Vetterfc303102012-07-09 10:40:58 +020010471 to_intel_connector(connector)->new_encoder = NULL;
10472 intel_encoder->new_crtc = NULL;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010473 intel_crtc->new_enabled = false;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010474
10475 connector_state->best_encoder = NULL;
10476 connector_state->crtc = NULL;
10477
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010478 crtc_state->base.enable = crtc_state->base.active = false;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010479
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010480 ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
10481 0, 0);
10482 if (ret)
10483 goto fail;
10484
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020010485 ret = intel_set_mode(state);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030010486 if (ret)
10487 goto fail;
Chris Wilsond2dff872011-04-19 08:36:26 +010010488
Daniel Vetter36206362012-12-10 20:42:17 +010010489 if (old->release_fb) {
10490 drm_framebuffer_unregister_private(old->release_fb);
10491 drm_framebuffer_unreference(old->release_fb);
10492 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010493
Chris Wilson0622a532011-04-21 09:32:11 +010010494 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010495 }
10496
Eric Anholtc751ce42010-03-25 11:48:48 -070010497 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +020010498 if (old->dpms_mode != DRM_MODE_DPMS_ON)
10499 connector->funcs->dpms(connector, old->dpms_mode);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010500
10501 return;
10502fail:
10503 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
10504 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010505}
10506
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010507static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010508 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010509{
10510 struct drm_i915_private *dev_priv = dev->dev_private;
10511 u32 dpll = pipe_config->dpll_hw_state.dpll;
10512
10513 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010514 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010515 else if (HAS_PCH_SPLIT(dev))
10516 return 120000;
10517 else if (!IS_GEN2(dev))
10518 return 96000;
10519 else
10520 return 48000;
10521}
10522
Jesse Barnes79e53942008-11-07 14:24:08 -080010523/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010524static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010525 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010526{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010527 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010528 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010529 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010530 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010531 u32 fp;
10532 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010533 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010534
10535 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010536 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010537 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010538 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010539
10540 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010541 if (IS_PINEVIEW(dev)) {
10542 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10543 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010544 } else {
10545 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10546 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10547 }
10548
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010549 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010550 if (IS_PINEVIEW(dev))
10551 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10552 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010553 else
10554 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010555 DPLL_FPA01_P1_POST_DIV_SHIFT);
10556
10557 switch (dpll & DPLL_MODE_MASK) {
10558 case DPLLB_MODE_DAC_SERIAL:
10559 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10560 5 : 10;
10561 break;
10562 case DPLLB_MODE_LVDS:
10563 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10564 7 : 14;
10565 break;
10566 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010567 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010568 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010569 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010570 }
10571
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010572 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010573 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010574 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010575 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010576 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010577 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010578 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010579
10580 if (is_lvds) {
10581 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10582 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010583
10584 if (lvds & LVDS_CLKB_POWER_UP)
10585 clock.p2 = 7;
10586 else
10587 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010588 } else {
10589 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10590 clock.p1 = 2;
10591 else {
10592 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10593 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10594 }
10595 if (dpll & PLL_P2_DIVIDE_BY_4)
10596 clock.p2 = 4;
10597 else
10598 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010599 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010600
10601 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010602 }
10603
Ville Syrjälä18442d02013-09-13 16:00:08 +030010604 /*
10605 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010606 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010607 * encoder's get_config() function.
10608 */
10609 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010610}
10611
Ville Syrjälä6878da02013-09-13 15:59:11 +030010612int intel_dotclock_calculate(int link_freq,
10613 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010614{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010615 /*
10616 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010617 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010618 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010619 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010620 *
10621 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010622 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010623 */
10624
Ville Syrjälä6878da02013-09-13 15:59:11 +030010625 if (!m_n->link_n)
10626 return 0;
10627
10628 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10629}
10630
Ville Syrjälä18442d02013-09-13 16:00:08 +030010631static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010632 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010633{
10634 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +030010635
10636 /* read out port_clock from the DPLL */
10637 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010638
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010639 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +030010640 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +010010641 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +030010642 * agree once we know their relationship in the encoder's
10643 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010644 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010645 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +030010646 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10647 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010648}
10649
10650/** Returns the currently programmed mode of the given pipe. */
10651struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10652 struct drm_crtc *crtc)
10653{
Jesse Barnes548f2452011-02-17 10:40:53 -080010654 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010655 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010656 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010657 struct drm_display_mode *mode;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010658 struct intel_crtc_state pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010659 int htot = I915_READ(HTOTAL(cpu_transcoder));
10660 int hsync = I915_READ(HSYNC(cpu_transcoder));
10661 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10662 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010663 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010664
10665 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10666 if (!mode)
10667 return NULL;
10668
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010669 /*
10670 * Construct a pipe_config sufficient for getting the clock info
10671 * back out of crtc_clock_get.
10672 *
10673 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10674 * to use a real value here instead.
10675 */
Ville Syrjälä293623f2013-09-13 16:18:46 +030010676 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010677 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010678 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10679 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10680 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010681 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10682
Ville Syrjälä773ae032013-09-23 17:48:20 +030010683 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010684 mode->hdisplay = (htot & 0xffff) + 1;
10685 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10686 mode->hsync_start = (hsync & 0xffff) + 1;
10687 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10688 mode->vdisplay = (vtot & 0xffff) + 1;
10689 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10690 mode->vsync_start = (vsync & 0xffff) + 1;
10691 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10692
10693 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010694
10695 return mode;
10696}
10697
Jesse Barnes652c3932009-08-17 13:31:43 -070010698static void intel_decrease_pllclock(struct drm_crtc *crtc)
10699{
10700 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030010701 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -070010702 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -070010703
Sonika Jindalbaff2962014-07-22 11:16:35 +053010704 if (!HAS_GMCH_DISPLAY(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -070010705 return;
10706
10707 if (!dev_priv->lvds_downclock_avail)
10708 return;
10709
10710 /*
10711 * Since this is called by a timer, we should never get here in
10712 * the manual case.
10713 */
10714 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +010010715 int pipe = intel_crtc->pipe;
10716 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +020010717 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +010010718
Zhao Yakui44d98a62009-10-09 11:39:40 +080010719 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -070010720
Sean Paul8ac5a6d2012-02-13 13:14:51 -050010721 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -070010722
Chris Wilson074b5e12012-05-02 12:07:06 +010010723 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -070010724 dpll |= DISPLAY_RATE_SELECT_FPA1;
10725 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010726 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -070010727 dpll = I915_READ(dpll_reg);
10728 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +080010729 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -070010730 }
10731
10732}
10733
Chris Wilsonf047e392012-07-21 12:31:41 +010010734void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010735{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010736 struct drm_i915_private *dev_priv = dev->dev_private;
10737
Chris Wilsonf62a0072014-02-21 17:55:39 +000010738 if (dev_priv->mm.busy)
10739 return;
10740
Paulo Zanoni43694d62014-03-07 20:08:08 -030010741 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010742 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010743 if (INTEL_INFO(dev)->gen >= 6)
10744 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010745 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010746}
10747
10748void intel_mark_idle(struct drm_device *dev)
10749{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010750 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010751 struct drm_crtc *crtc;
10752
Chris Wilsonf62a0072014-02-21 17:55:39 +000010753 if (!dev_priv->mm.busy)
10754 return;
10755
10756 dev_priv->mm.busy = false;
10757
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010758 for_each_crtc(dev, crtc) {
Matt Roperf4510a22014-04-01 15:22:40 -070010759 if (!crtc->primary->fb)
Chris Wilson725a5b52013-01-08 11:02:57 +000010760 continue;
10761
10762 intel_decrease_pllclock(crtc);
10763 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010764
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010765 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010766 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010767
Paulo Zanoni43694d62014-03-07 20:08:08 -030010768 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010769}
10770
Jesse Barnes79e53942008-11-07 14:24:08 -080010771static void intel_crtc_destroy(struct drm_crtc *crtc)
10772{
10773 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010774 struct drm_device *dev = crtc->dev;
10775 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010776
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010777 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010778 work = intel_crtc->unpin_work;
10779 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010780 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010781
10782 if (work) {
10783 cancel_work_sync(&work->work);
10784 kfree(work);
10785 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010786
10787 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010788
Jesse Barnes79e53942008-11-07 14:24:08 -080010789 kfree(intel_crtc);
10790}
10791
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010792static void intel_unpin_work_fn(struct work_struct *__work)
10793{
10794 struct intel_unpin_work *work =
10795 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010796 struct drm_device *dev = work->crtc->dev;
Daniel Vetterf99d7062014-06-19 16:01:59 +020010797 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010798
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010799 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000010800 intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
Chris Wilson05394f32010-11-08 19:18:58 +000010801 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010802
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020010803 intel_fbc_update(dev);
John Harrisonf06cc1b2014-11-24 18:49:37 +000010804
10805 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010806 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010807 mutex_unlock(&dev->struct_mutex);
10808
Daniel Vetterf99d7062014-06-19 16:01:59 +020010809 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Chris Wilson89ed88b2015-02-16 14:31:49 +000010810 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010811
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010812 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
10813 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
10814
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010815 kfree(work);
10816}
10817
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010818static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010010819 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010820{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010821 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10822 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010823 unsigned long flags;
10824
10825 /* Ignore early vblank irqs */
10826 if (intel_crtc == NULL)
10827 return;
10828
Daniel Vetterf3260382014-09-15 14:55:23 +020010829 /*
10830 * This is called both by irq handlers and the reset code (to complete
10831 * lost pageflips) so needs the full irqsave spinlocks.
10832 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010833 spin_lock_irqsave(&dev->event_lock, flags);
10834 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000010835
10836 /* Ensure we don't miss a work->pending update ... */
10837 smp_rmb();
10838
10839 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010840 spin_unlock_irqrestore(&dev->event_lock, flags);
10841 return;
10842 }
10843
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010844 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010010845
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010846 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010847}
10848
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010849void intel_finish_page_flip(struct drm_device *dev, int pipe)
10850{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010851 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010852 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10853
Mario Kleiner49b14a52010-12-09 07:00:07 +010010854 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010855}
10856
10857void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10858{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010859 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010860 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10861
Mario Kleiner49b14a52010-12-09 07:00:07 +010010862 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010863}
10864
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010865/* Is 'a' after or equal to 'b'? */
10866static bool g4x_flip_count_after_eq(u32 a, u32 b)
10867{
10868 return !((a - b) & 0x80000000);
10869}
10870
10871static bool page_flip_finished(struct intel_crtc *crtc)
10872{
10873 struct drm_device *dev = crtc->base.dev;
10874 struct drm_i915_private *dev_priv = dev->dev_private;
10875
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030010876 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10877 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10878 return true;
10879
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010880 /*
10881 * The relevant registers doen't exist on pre-ctg.
10882 * As the flip done interrupt doesn't trigger for mmio
10883 * flips on gmch platforms, a flip count check isn't
10884 * really needed there. But since ctg has the registers,
10885 * include it in the check anyway.
10886 */
10887 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10888 return true;
10889
10890 /*
10891 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10892 * used the same base address. In that case the mmio flip might
10893 * have completed, but the CS hasn't even executed the flip yet.
10894 *
10895 * A flip count check isn't enough as the CS might have updated
10896 * the base address just after start of vblank, but before we
10897 * managed to process the interrupt. This means we'd complete the
10898 * CS flip too soon.
10899 *
10900 * Combining both checks should get us a good enough result. It may
10901 * still happen that the CS flip has been executed, but has not
10902 * yet actually completed. But in case the base address is the same
10903 * anyway, we don't really care.
10904 */
10905 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10906 crtc->unpin_work->gtt_offset &&
10907 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
10908 crtc->unpin_work->flip_count);
10909}
10910
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010911void intel_prepare_page_flip(struct drm_device *dev, int plane)
10912{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010913 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010914 struct intel_crtc *intel_crtc =
10915 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10916 unsigned long flags;
10917
Daniel Vetterf3260382014-09-15 14:55:23 +020010918
10919 /*
10920 * This is called both by irq handlers and the reset code (to complete
10921 * lost pageflips) so needs the full irqsave spinlocks.
10922 *
10923 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000010924 * generate a page-flip completion irq, i.e. every modeset
10925 * is also accompanied by a spurious intel_prepare_page_flip().
10926 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010927 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010928 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000010929 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010930 spin_unlock_irqrestore(&dev->event_lock, flags);
10931}
10932
Robin Schroereba905b2014-05-18 02:24:50 +020010933static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
Chris Wilsone7d841c2012-12-03 11:36:30 +000010934{
10935 /* Ensure that the work item is consistent when activating it ... */
10936 smp_wmb();
10937 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
10938 /* and that it is marked active as soon as the irq could fire. */
10939 smp_wmb();
10940}
10941
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010942static int intel_gen2_queue_flip(struct drm_device *dev,
10943 struct drm_crtc *crtc,
10944 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010945 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010946 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010947 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010948{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010949 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010950 u32 flip_mask;
10951 int ret;
10952
Daniel Vetter6d90c952012-04-26 23:28:05 +020010953 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010954 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010955 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010956
10957 /* Can't queue multiple flips, so wait for the previous
10958 * one to finish before executing the next.
10959 */
10960 if (intel_crtc->plane)
10961 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10962 else
10963 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010964 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10965 intel_ring_emit(ring, MI_NOOP);
10966 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10967 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10968 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010969 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010970 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000010971
10972 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010973 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010974 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010975}
10976
10977static int intel_gen3_queue_flip(struct drm_device *dev,
10978 struct drm_crtc *crtc,
10979 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010980 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010981 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010982 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010983{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010984 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010985 u32 flip_mask;
10986 int ret;
10987
Daniel Vetter6d90c952012-04-26 23:28:05 +020010988 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010989 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010990 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010991
10992 if (intel_crtc->plane)
10993 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10994 else
10995 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010996 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10997 intel_ring_emit(ring, MI_NOOP);
10998 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
10999 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11000 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011001 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020011002 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011003
Chris Wilsone7d841c2012-12-03 11:36:30 +000011004 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
11009static int intel_gen4_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 Barnes8c9f3aa2011-06-16 09:19:13 -070011015{
11016 struct drm_i915_private *dev_priv = dev->dev_private;
11017 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11018 uint32_t pf, pipesrc;
11019 int ret;
11020
Daniel Vetter6d90c952012-04-26 23:28:05 +020011021 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011022 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011023 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011024
11025 /* i965+ uses the linear or tiled offsets from the
11026 * Display Registers (which do not change across a page-flip)
11027 * so we need only reprogram the base address.
11028 */
Daniel Vetter6d90c952012-04-26 23:28:05 +020011029 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11030 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11031 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011032 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020011033 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011034
11035 /* XXX Enabling the panel-fitter across page-flip is so far
11036 * untested on non-native modes, so ignore it for now.
11037 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11038 */
11039 pf = 0;
11040 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020011041 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011042
11043 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010011044 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010011045 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011046}
11047
11048static int intel_gen6_queue_flip(struct drm_device *dev,
11049 struct drm_crtc *crtc,
11050 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011051 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011052 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070011053 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011054{
11055 struct drm_i915_private *dev_priv = dev->dev_private;
11056 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11057 uint32_t pf, pipesrc;
11058 int ret;
11059
Daniel Vetter6d90c952012-04-26 23:28:05 +020011060 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011061 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011062 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011063
Daniel Vetter6d90c952012-04-26 23:28:05 +020011064 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11065 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11066 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011067 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011068
Chris Wilson99d9acd2012-04-17 20:37:00 +010011069 /* Contrary to the suggestions in the documentation,
11070 * "Enable Panel Fitter" does not seem to be required when page
11071 * flipping with a non-native mode, and worse causes a normal
11072 * modeset to fail.
11073 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11074 */
11075 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011076 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020011077 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011078
11079 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010011080 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010011081 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011082}
11083
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011084static int intel_gen7_queue_flip(struct drm_device *dev,
11085 struct drm_crtc *crtc,
11086 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011087 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011088 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070011089 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011090{
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011091 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011092 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010011093 int len, ret;
11094
Robin Schroereba905b2014-05-18 02:24:50 +020011095 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011096 case PLANE_A:
11097 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11098 break;
11099 case PLANE_B:
11100 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11101 break;
11102 case PLANE_C:
11103 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11104 break;
11105 default:
11106 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011107 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011108 }
11109
Chris Wilsonffe74d72013-08-26 20:58:12 +010011110 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +010011111 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010011112 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010011113 /*
11114 * On Gen 8, SRM is now taking an extra dword to accommodate
11115 * 48bits addresses, and we need a NOOP for the batch size to
11116 * stay even.
11117 */
11118 if (IS_GEN8(dev))
11119 len += 2;
11120 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011121
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011122 /*
11123 * BSpec MI_DISPLAY_FLIP for IVB:
11124 * "The full packet must be contained within the same cache line."
11125 *
11126 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11127 * cacheline, if we ever start emitting more commands before
11128 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11129 * then do the cacheline alignment, and finally emit the
11130 * MI_DISPLAY_FLIP.
11131 */
11132 ret = intel_ring_cacheline_align(ring);
11133 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011134 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011135
Chris Wilsonffe74d72013-08-26 20:58:12 +010011136 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011137 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011138 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011139
Chris Wilsonffe74d72013-08-26 20:58:12 +010011140 /* Unmask the flip-done completion message. Note that the bspec says that
11141 * we should do this for both the BCS and RCS, and that we must not unmask
11142 * more than one flip event at any time (or ensure that one flip message
11143 * can be sent by waiting for flip-done prior to queueing new flips).
11144 * Experimentation says that BCS works despite DERRMR masking all
11145 * flip-done completion events and that unmasking all planes at once
11146 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11147 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11148 */
11149 if (ring->id == RCS) {
11150 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
11151 intel_ring_emit(ring, DERRMR);
11152 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11153 DERRMR_PIPEB_PRI_FLIP_DONE |
11154 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010011155 if (IS_GEN8(dev))
11156 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
11157 MI_SRM_LRM_GLOBAL_GTT);
11158 else
11159 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
11160 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +010011161 intel_ring_emit(ring, DERRMR);
11162 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010011163 if (IS_GEN8(dev)) {
11164 intel_ring_emit(ring, 0);
11165 intel_ring_emit(ring, MI_NOOP);
11166 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011167 }
11168
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011169 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +020011170 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011171 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011172 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000011173
11174 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010011175 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010011176 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011177}
11178
Sourab Gupta84c33a62014-06-02 16:47:17 +053011179static bool use_mmio_flip(struct intel_engine_cs *ring,
11180 struct drm_i915_gem_object *obj)
11181{
11182 /*
11183 * This is not being used for older platforms, because
11184 * non-availability of flip done interrupt forces us to use
11185 * CS flips. Older platforms derive flip done using some clever
11186 * tricks involving the flip_pending status bits and vblank irqs.
11187 * So using MMIO flips there would disrupt this mechanism.
11188 */
11189
Chris Wilson8e09bf82014-07-08 10:40:30 +010011190 if (ring == NULL)
11191 return true;
11192
Sourab Gupta84c33a62014-06-02 16:47:17 +053011193 if (INTEL_INFO(ring->dev)->gen < 5)
11194 return false;
11195
11196 if (i915.use_mmio_flip < 0)
11197 return false;
11198 else if (i915.use_mmio_flip > 0)
11199 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010011200 else if (i915.enable_execlists)
11201 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011202 else
Chris Wilsonb4716182015-04-27 13:41:17 +010011203 return ring != i915_gem_request_get_ring(obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011204}
11205
Damien Lespiauff944562014-11-20 14:58:16 +000011206static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
11207{
11208 struct drm_device *dev = intel_crtc->base.dev;
11209 struct drm_i915_private *dev_priv = dev->dev_private;
11210 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000011211 const enum pipe pipe = intel_crtc->pipe;
11212 u32 ctl, stride;
11213
11214 ctl = I915_READ(PLANE_CTL(pipe, 0));
11215 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011216 switch (fb->modifier[0]) {
11217 case DRM_FORMAT_MOD_NONE:
11218 break;
11219 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000011220 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011221 break;
11222 case I915_FORMAT_MOD_Y_TILED:
11223 ctl |= PLANE_CTL_TILED_Y;
11224 break;
11225 case I915_FORMAT_MOD_Yf_TILED:
11226 ctl |= PLANE_CTL_TILED_YF;
11227 break;
11228 default:
11229 MISSING_CASE(fb->modifier[0]);
11230 }
Damien Lespiauff944562014-11-20 14:58:16 +000011231
11232 /*
11233 * The stride is either expressed as a multiple of 64 bytes chunks for
11234 * linear buffers or in number of tiles for tiled buffers.
11235 */
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011236 stride = fb->pitches[0] /
11237 intel_fb_stride_alignment(dev, fb->modifier[0],
11238 fb->pixel_format);
Damien Lespiauff944562014-11-20 14:58:16 +000011239
11240 /*
11241 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11242 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11243 */
11244 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11245 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11246
11247 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
11248 POSTING_READ(PLANE_SURF(pipe, 0));
11249}
11250
11251static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011252{
11253 struct drm_device *dev = intel_crtc->base.dev;
11254 struct drm_i915_private *dev_priv = dev->dev_private;
11255 struct intel_framebuffer *intel_fb =
11256 to_intel_framebuffer(intel_crtc->base.primary->fb);
11257 struct drm_i915_gem_object *obj = intel_fb->obj;
11258 u32 dspcntr;
11259 u32 reg;
11260
Sourab Gupta84c33a62014-06-02 16:47:17 +053011261 reg = DSPCNTR(intel_crtc->plane);
11262 dspcntr = I915_READ(reg);
11263
Damien Lespiauc5d97472014-10-25 00:11:11 +010011264 if (obj->tiling_mode != I915_TILING_NONE)
11265 dspcntr |= DISPPLANE_TILED;
11266 else
11267 dspcntr &= ~DISPPLANE_TILED;
11268
Sourab Gupta84c33a62014-06-02 16:47:17 +053011269 I915_WRITE(reg, dspcntr);
11270
11271 I915_WRITE(DSPSURF(intel_crtc->plane),
11272 intel_crtc->unpin_work->gtt_offset);
11273 POSTING_READ(DSPSURF(intel_crtc->plane));
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011274
Damien Lespiauff944562014-11-20 14:58:16 +000011275}
11276
11277/*
11278 * XXX: This is the temporary way to update the plane registers until we get
11279 * around to using the usual plane update functions for MMIO flips
11280 */
11281static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
11282{
11283 struct drm_device *dev = intel_crtc->base.dev;
11284 bool atomic_update;
11285 u32 start_vbl_count;
11286
11287 intel_mark_page_flip_active(intel_crtc);
11288
11289 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
11290
11291 if (INTEL_INFO(dev)->gen >= 9)
11292 skl_do_mmio_flip(intel_crtc);
11293 else
11294 /* use_mmio_flip() retricts MMIO flips to ilk+ */
11295 ilk_do_mmio_flip(intel_crtc);
11296
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011297 if (atomic_update)
11298 intel_pipe_update_end(intel_crtc, start_vbl_count);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011299}
11300
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011301static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011302{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011303 struct intel_mmio_flip *mmio_flip =
11304 container_of(work, struct intel_mmio_flip, work);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011305
Daniel Vettereed29a52015-05-21 14:21:25 +020011306 if (mmio_flip->req)
11307 WARN_ON(__i915_wait_request(mmio_flip->req,
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011308 mmio_flip->crtc->reset_counter,
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011309 false, NULL,
11310 &mmio_flip->i915->rps.mmioflips));
Sourab Gupta84c33a62014-06-02 16:47:17 +053011311
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011312 intel_do_mmio_flip(mmio_flip->crtc);
11313
Daniel Vettereed29a52015-05-21 14:21:25 +020011314 i915_gem_request_unreference__unlocked(mmio_flip->req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011315 kfree(mmio_flip);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011316}
11317
11318static int intel_queue_mmio_flip(struct drm_device *dev,
11319 struct drm_crtc *crtc,
11320 struct drm_framebuffer *fb,
11321 struct drm_i915_gem_object *obj,
11322 struct intel_engine_cs *ring,
11323 uint32_t flags)
11324{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011325 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011326
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011327 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11328 if (mmio_flip == NULL)
11329 return -ENOMEM;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011330
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011331 mmio_flip->i915 = to_i915(dev);
Daniel Vettereed29a52015-05-21 14:21:25 +020011332 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011333 mmio_flip->crtc = to_intel_crtc(crtc);
11334
11335 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11336 schedule_work(&mmio_flip->work);
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020011337
Sourab Gupta84c33a62014-06-02 16:47:17 +053011338 return 0;
11339}
11340
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011341static int intel_default_queue_flip(struct drm_device *dev,
11342 struct drm_crtc *crtc,
11343 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011344 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011345 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070011346 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011347{
11348 return -ENODEV;
11349}
11350
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011351static bool __intel_pageflip_stall_check(struct drm_device *dev,
11352 struct drm_crtc *crtc)
11353{
11354 struct drm_i915_private *dev_priv = dev->dev_private;
11355 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11356 struct intel_unpin_work *work = intel_crtc->unpin_work;
11357 u32 addr;
11358
11359 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11360 return true;
11361
11362 if (!work->enable_stall_check)
11363 return false;
11364
11365 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010011366 if (work->flip_queued_req &&
11367 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011368 return false;
11369
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011370 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011371 }
11372
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011373 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011374 return false;
11375
11376 /* Potential stall - if we see that the flip has happened,
11377 * assume a missed interrupt. */
11378 if (INTEL_INFO(dev)->gen >= 4)
11379 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11380 else
11381 addr = I915_READ(DSPADDR(intel_crtc->plane));
11382
11383 /* There is a potential issue here with a false positive after a flip
11384 * to the same address. We could address this by checking for a
11385 * non-incrementing frame counter.
11386 */
11387 return addr == work->gtt_offset;
11388}
11389
11390void intel_check_page_flip(struct drm_device *dev, int pipe)
11391{
11392 struct drm_i915_private *dev_priv = dev->dev_private;
11393 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11394 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011395 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020011396
Dave Gordon6c51d462015-03-06 15:34:26 +000011397 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011398
11399 if (crtc == NULL)
11400 return;
11401
Daniel Vetterf3260382014-09-15 14:55:23 +020011402 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011403 work = intel_crtc->unpin_work;
11404 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011405 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010011406 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011407 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011408 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011409 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010011410 if (work != NULL &&
11411 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11412 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020011413 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011414}
11415
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011416static int intel_crtc_page_flip(struct drm_crtc *crtc,
11417 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011418 struct drm_pending_vblank_event *event,
11419 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011420{
11421 struct drm_device *dev = crtc->dev;
11422 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070011423 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070011424 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011425 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080011426 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020011427 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011428 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011429 struct intel_engine_cs *ring;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011430 bool mmio_flip;
Chris Wilson52e68632010-08-08 10:15:59 +010011431 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011432
Matt Roper2ff8fde2014-07-08 07:50:07 -070011433 /*
11434 * drm_mode_page_flip_ioctl() should already catch this, but double
11435 * check to be safe. In the future we may enable pageflipping from
11436 * a disabled primary plane.
11437 */
11438 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11439 return -EBUSY;
11440
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011441 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070011442 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011443 return -EINVAL;
11444
11445 /*
11446 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11447 * Note that pitch changes could also affect these register.
11448 */
11449 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070011450 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11451 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011452 return -EINVAL;
11453
Chris Wilsonf900db42014-02-20 09:26:13 +000011454 if (i915_terminally_wedged(&dev_priv->gpu_error))
11455 goto out_hang;
11456
Daniel Vetterb14c5672013-09-19 12:18:32 +020011457 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011458 if (work == NULL)
11459 return -ENOMEM;
11460
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011461 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011462 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011463 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011464 INIT_WORK(&work->work, intel_unpin_work_fn);
11465
Daniel Vetter87b6b102014-05-15 15:33:46 +020011466 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011467 if (ret)
11468 goto free_work;
11469
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011470 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011471 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011472 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011473 /* Before declaring the flip queue wedged, check if
11474 * the hardware completed the operation behind our backs.
11475 */
11476 if (__intel_pageflip_stall_check(dev, crtc)) {
11477 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11478 page_flip_completed(intel_crtc);
11479 } else {
11480 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011481 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010011482
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011483 drm_crtc_vblank_put(crtc);
11484 kfree(work);
11485 return -EBUSY;
11486 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011487 }
11488 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011489 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011490
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011491 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11492 flush_workqueue(dev_priv->wq);
11493
Jesse Barnes75dfca82010-02-10 15:09:44 -080011494 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011495 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000011496 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011497
Matt Roperf4510a22014-04-01 15:22:40 -070011498 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011499 update_state_fb(crtc->primary);
Matt Roper1ed1f962015-01-30 16:22:36 -080011500
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011501 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011502
Chris Wilson89ed88b2015-02-16 14:31:49 +000011503 ret = i915_mutex_lock_interruptible(dev);
11504 if (ret)
11505 goto cleanup;
11506
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011507 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020011508 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011509
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011510 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Daniel Vettera071fa02014-06-18 23:28:09 +020011511 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011512
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011513 if (IS_VALLEYVIEW(dev)) {
11514 ring = &dev_priv->ring[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011515 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011516 /* vlv: DISPLAY_FLIP fails to change tiling */
11517 ring = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000011518 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Chris Wilson2a92d5b2014-07-08 10:40:29 +010011519 ring = &dev_priv->ring[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011520 } else if (INTEL_INFO(dev)->gen >= 7) {
Chris Wilsonb4716182015-04-27 13:41:17 +010011521 ring = i915_gem_request_get_ring(obj->last_write_req);
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011522 if (ring == NULL || ring->id != RCS)
11523 ring = &dev_priv->ring[BCS];
11524 } else {
11525 ring = &dev_priv->ring[RCS];
11526 }
11527
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011528 mmio_flip = use_mmio_flip(ring, obj);
11529
11530 /* When using CS flips, we want to emit semaphores between rings.
11531 * However, when using mmio flips we will create a task to do the
11532 * synchronisation, so all we want here is to pin the framebuffer
11533 * into the display plane and skip any waits.
11534 */
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011535 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011536 crtc->primary->state,
Chris Wilsonb4716182015-04-27 13:41:17 +010011537 mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011538 if (ret)
11539 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011540
Tvrtko Ursulin121920f2015-03-23 11:10:37 +000011541 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
11542 + intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011543
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011544 if (mmio_flip) {
Sourab Gupta84c33a62014-06-02 16:47:17 +053011545 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
11546 page_flip_flags);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011547 if (ret)
11548 goto cleanup_unpin;
11549
John Harrisonf06cc1b2014-11-24 18:49:37 +000011550 i915_gem_request_assign(&work->flip_queued_req,
11551 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011552 } else {
Chris Wilsond94b5032015-04-27 13:41:15 +010011553 if (obj->last_write_req) {
11554 ret = i915_gem_check_olr(obj->last_write_req);
11555 if (ret)
11556 goto cleanup_unpin;
11557 }
11558
Sourab Gupta84c33a62014-06-02 16:47:17 +053011559 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011560 page_flip_flags);
11561 if (ret)
11562 goto cleanup_unpin;
11563
John Harrisonf06cc1b2014-11-24 18:49:37 +000011564 i915_gem_request_assign(&work->flip_queued_req,
11565 intel_ring_get_request(ring));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011566 }
11567
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011568 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011569 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011570
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011571 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Daniel Vettera071fa02014-06-18 23:28:09 +020011572 INTEL_FRONTBUFFER_PRIMARY(pipe));
11573
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020011574 intel_fbc_disable(dev);
Daniel Vetterf99d7062014-06-19 16:01:59 +020011575 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011576 mutex_unlock(&dev->struct_mutex);
11577
Jesse Barnese5510fa2010-07-01 16:48:37 -070011578 trace_i915_flip_request(intel_crtc->plane, obj);
11579
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011580 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010011581
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011582cleanup_unpin:
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011583 intel_unpin_fb_obj(fb, crtc->primary->state);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011584cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011585 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000011586 mutex_unlock(&dev->struct_mutex);
11587cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070011588 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011589 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010011590
Chris Wilson89ed88b2015-02-16 14:31:49 +000011591 drm_gem_object_unreference_unlocked(&obj->base);
11592 drm_framebuffer_unreference(work->old_fb);
11593
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011594 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011595 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011596 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011597
Daniel Vetter87b6b102014-05-15 15:33:46 +020011598 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011599free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010011600 kfree(work);
11601
Chris Wilsonf900db42014-02-20 09:26:13 +000011602 if (ret == -EIO) {
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011603 struct drm_atomic_state *state;
11604 struct drm_plane_state *plane_state;
11605
Chris Wilsonf900db42014-02-20 09:26:13 +000011606out_hang:
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011607 state = drm_atomic_state_alloc(dev);
11608 if (!state)
11609 return -ENOMEM;
11610 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11611
11612retry:
11613 plane_state = drm_atomic_get_plane_state(state, primary);
11614 ret = PTR_ERR_OR_ZERO(plane_state);
11615 if (!ret) {
11616 drm_atomic_set_fb_for_plane(plane_state, fb);
11617
11618 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11619 if (!ret)
11620 ret = drm_atomic_commit(state);
11621 }
11622
11623 if (ret == -EDEADLK) {
11624 drm_modeset_backoff(state->acquire_ctx);
11625 drm_atomic_state_clear(state);
11626 goto retry;
11627 }
11628
11629 if (ret)
11630 drm_atomic_state_free(state);
11631
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011632 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011633 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020011634 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011635 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011636 }
Chris Wilsonf900db42014-02-20 09:26:13 +000011637 }
Chris Wilson96b099f2010-06-07 14:03:04 +010011638 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011639}
11640
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011641
11642/**
11643 * intel_wm_need_update - Check whether watermarks need updating
11644 * @plane: drm plane
11645 * @state: new plane state
11646 *
11647 * Check current plane state versus the new one to determine whether
11648 * watermarks need to be recalculated.
11649 *
11650 * Returns true or false.
11651 */
11652static bool intel_wm_need_update(struct drm_plane *plane,
11653 struct drm_plane_state *state)
11654{
11655 /* Update watermarks on tiling changes. */
11656 if (!plane->state->fb || !state->fb ||
11657 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
11658 plane->state->rotation != state->rotation)
11659 return true;
11660
11661 if (plane->state->crtc_w != state->crtc_w)
11662 return true;
11663
11664 return false;
11665}
11666
11667int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11668 struct drm_plane_state *plane_state)
11669{
11670 struct drm_crtc *crtc = crtc_state->crtc;
11671 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11672 struct drm_plane *plane = plane_state->plane;
11673 struct drm_device *dev = crtc->dev;
11674 struct drm_i915_private *dev_priv = dev->dev_private;
11675 struct intel_plane_state *old_plane_state =
11676 to_intel_plane_state(plane->state);
11677 int idx = intel_crtc->base.base.id, ret;
11678 int i = drm_plane_index(plane);
11679 bool mode_changed = needs_modeset(crtc_state);
11680 bool was_crtc_enabled = crtc->state->active;
11681 bool is_crtc_enabled = crtc_state->active;
11682
11683 bool turn_off, turn_on, visible, was_visible;
11684 struct drm_framebuffer *fb = plane_state->fb;
11685
11686 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11687 plane->type != DRM_PLANE_TYPE_CURSOR) {
11688 ret = skl_update_scaler_plane(
11689 to_intel_crtc_state(crtc_state),
11690 to_intel_plane_state(plane_state));
11691 if (ret)
11692 return ret;
11693 }
11694
11695 /*
11696 * Disabling a plane is always okay; we just need to update
11697 * fb tracking in a special way since cleanup_fb() won't
11698 * get called by the plane helpers.
11699 */
11700 if (old_plane_state->base.fb && !fb)
11701 intel_crtc->atomic.disabled_planes |= 1 << i;
11702
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011703 was_visible = old_plane_state->visible;
11704 visible = to_intel_plane_state(plane_state)->visible;
11705
11706 if (!was_crtc_enabled && WARN_ON(was_visible))
11707 was_visible = false;
11708
11709 if (!is_crtc_enabled && WARN_ON(visible))
11710 visible = false;
11711
11712 if (!was_visible && !visible)
11713 return 0;
11714
11715 turn_off = was_visible && (!visible || mode_changed);
11716 turn_on = visible && (!was_visible || mode_changed);
11717
11718 DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
11719 plane->base.id, fb ? fb->base.id : -1);
11720
11721 DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11722 plane->base.id, was_visible, visible,
11723 turn_off, turn_on, mode_changed);
11724
11725 if (intel_wm_need_update(plane, plane_state))
11726 intel_crtc->atomic.update_wm = true;
11727
11728 switch (plane->type) {
11729 case DRM_PLANE_TYPE_PRIMARY:
11730 if (visible)
11731 intel_crtc->atomic.fb_bits |=
11732 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
11733
11734 intel_crtc->atomic.wait_for_flips = true;
11735 intel_crtc->atomic.pre_disable_primary = turn_off;
11736 intel_crtc->atomic.post_enable_primary = turn_on;
11737
11738 if (turn_off)
11739 intel_crtc->atomic.disable_fbc = true;
11740
11741 /*
11742 * FBC does not work on some platforms for rotated
11743 * planes, so disable it when rotation is not 0 and
11744 * update it when rotation is set back to 0.
11745 *
11746 * FIXME: This is redundant with the fbc update done in
11747 * the primary plane enable function except that that
11748 * one is done too late. We eventually need to unify
11749 * this.
11750 */
11751
11752 if (visible &&
11753 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
11754 dev_priv->fbc.crtc == intel_crtc &&
11755 plane_state->rotation != BIT(DRM_ROTATE_0))
11756 intel_crtc->atomic.disable_fbc = true;
11757
11758 /*
11759 * BDW signals flip done immediately if the plane
11760 * is disabled, even if the plane enable is already
11761 * armed to occur at the next vblank :(
11762 */
11763 if (turn_on && IS_BROADWELL(dev))
11764 intel_crtc->atomic.wait_vblank = true;
11765
11766 intel_crtc->atomic.update_fbc |= visible || mode_changed;
11767 break;
11768 case DRM_PLANE_TYPE_CURSOR:
11769 if (visible)
11770 intel_crtc->atomic.fb_bits |=
11771 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
11772 break;
11773 case DRM_PLANE_TYPE_OVERLAY:
11774 /*
11775 * 'prepare' is never called when plane is being disabled, so
11776 * we need to handle frontbuffer tracking as a special case
11777 */
11778 if (visible)
11779 intel_crtc->atomic.fb_bits |=
11780 INTEL_FRONTBUFFER_SPRITE(intel_crtc->pipe);
11781
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020011782 if (turn_off && !mode_changed) {
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011783 intel_crtc->atomic.wait_vblank = true;
11784 intel_crtc->atomic.update_sprite_watermarks |=
11785 1 << i;
11786 }
11787 break;
11788 }
11789 return 0;
11790}
11791
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011792static bool encoders_cloneable(const struct intel_encoder *a,
11793 const struct intel_encoder *b)
11794{
11795 /* masks could be asymmetric, so check both ways */
11796 return a == b || (a->cloneable & (1 << b->type) &&
11797 b->cloneable & (1 << a->type));
11798}
11799
11800static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11801 struct intel_crtc *crtc,
11802 struct intel_encoder *encoder)
11803{
11804 struct intel_encoder *source_encoder;
11805 struct drm_connector *connector;
11806 struct drm_connector_state *connector_state;
11807 int i;
11808
11809 for_each_connector_in_state(state, connector, connector_state, i) {
11810 if (connector_state->crtc != &crtc->base)
11811 continue;
11812
11813 source_encoder =
11814 to_intel_encoder(connector_state->best_encoder);
11815 if (!encoders_cloneable(encoder, source_encoder))
11816 return false;
11817 }
11818
11819 return true;
11820}
11821
11822static bool check_encoder_cloning(struct drm_atomic_state *state,
11823 struct intel_crtc *crtc)
11824{
11825 struct intel_encoder *encoder;
11826 struct drm_connector *connector;
11827 struct drm_connector_state *connector_state;
11828 int i;
11829
11830 for_each_connector_in_state(state, connector, connector_state, i) {
11831 if (connector_state->crtc != &crtc->base)
11832 continue;
11833
11834 encoder = to_intel_encoder(connector_state->best_encoder);
11835 if (!check_single_encoder_cloning(state, crtc, encoder))
11836 return false;
11837 }
11838
11839 return true;
11840}
11841
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020011842static void intel_crtc_check_initial_planes(struct drm_crtc *crtc,
11843 struct drm_crtc_state *crtc_state)
11844{
11845 struct intel_crtc_state *pipe_config =
11846 to_intel_crtc_state(crtc_state);
11847 struct drm_plane *p;
11848 unsigned visible_mask = 0;
11849
11850 drm_for_each_plane_mask(p, crtc->dev, crtc_state->plane_mask) {
11851 struct drm_plane_state *plane_state =
11852 drm_atomic_get_existing_plane_state(crtc_state->state, p);
11853
11854 if (WARN_ON(!plane_state))
11855 continue;
11856
11857 if (!plane_state->fb)
11858 crtc_state->plane_mask &=
11859 ~(1 << drm_plane_index(p));
11860 else if (to_intel_plane_state(plane_state)->visible)
11861 visible_mask |= 1 << drm_plane_index(p);
11862 }
11863
11864 if (!visible_mask)
11865 return;
11866
11867 pipe_config->quirks &= ~PIPE_CONFIG_QUIRK_INITIAL_PLANES;
11868}
11869
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011870static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11871 struct drm_crtc_state *crtc_state)
11872{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011873 struct drm_device *dev = crtc->dev;
Maarten Lankhorstad421372015-06-15 12:33:42 +020011874 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011875 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011876 struct intel_crtc_state *pipe_config =
11877 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011878 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorstad421372015-06-15 12:33:42 +020011879 int ret, idx = crtc->base.id;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011880 bool mode_changed = needs_modeset(crtc_state);
11881
11882 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
11883 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11884 return -EINVAL;
11885 }
11886
11887 I915_STATE_WARN(crtc->state->active != intel_crtc->active,
11888 "[CRTC:%i] mismatch between state->active(%i) and crtc->active(%i)\n",
11889 idx, crtc->state->active, intel_crtc->active);
11890
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020011891 /* plane mask is fixed up after all initial planes are calculated */
11892 if (pipe_config->quirks & PIPE_CONFIG_QUIRK_INITIAL_PLANES)
11893 intel_crtc_check_initial_planes(crtc, crtc_state);
11894
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020011895 if (mode_changed)
11896 intel_crtc->atomic.update_wm = !crtc_state->active;
11897
Maarten Lankhorstad421372015-06-15 12:33:42 +020011898 if (mode_changed && crtc_state->enable &&
11899 dev_priv->display.crtc_compute_clock &&
11900 !WARN_ON(pipe_config->shared_dpll != DPLL_ID_PRIVATE)) {
11901 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11902 pipe_config);
11903 if (ret)
11904 return ret;
11905 }
11906
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011907 return intel_atomic_setup_scalers(dev, intel_crtc, pipe_config);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011908}
11909
Jani Nikula65b38e02015-04-13 11:26:56 +030011910static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011911 .mode_set_base_atomic = intel_pipe_set_base_atomic,
11912 .load_lut = intel_crtc_load_lut,
Matt Roperea2c67b2014-12-23 10:41:52 -080011913 .atomic_begin = intel_begin_crtc_commit,
11914 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011915 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011916};
11917
Daniel Vetter9a935852012-07-05 22:34:27 +020011918/**
11919 * intel_modeset_update_staged_output_state
11920 *
11921 * Updates the staged output configuration state, e.g. after we've read out the
11922 * current hw state.
11923 */
11924static void intel_modeset_update_staged_output_state(struct drm_device *dev)
11925{
Ville Syrjälä76688512014-01-10 11:28:06 +020011926 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011927 struct intel_encoder *encoder;
11928 struct intel_connector *connector;
11929
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011930 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011931 connector->new_encoder =
11932 to_intel_encoder(connector->base.encoder);
11933 }
11934
Damien Lespiaub2784e12014-08-05 11:29:37 +010011935 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011936 encoder->new_crtc =
11937 to_intel_crtc(encoder->base.crtc);
11938 }
Ville Syrjälä76688512014-01-10 11:28:06 +020011939
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011940 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011941 crtc->new_enabled = crtc->base.state->enable;
Ville Syrjälä76688512014-01-10 11:28:06 +020011942 }
Daniel Vetter9a935852012-07-05 22:34:27 +020011943}
11944
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011945/* Transitional helper to copy current connector/encoder state to
11946 * connector->state. This is needed so that code that is partially
11947 * converted to atomic does the right thing.
11948 */
11949static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11950{
11951 struct intel_connector *connector;
11952
11953 for_each_intel_connector(dev, connector) {
11954 if (connector->base.encoder) {
11955 connector->base.state->best_encoder =
11956 connector->base.encoder;
11957 connector->base.state->crtc =
11958 connector->base.encoder->crtc;
11959 } else {
11960 connector->base.state->best_encoder = NULL;
11961 connector->base.state->crtc = NULL;
11962 }
11963 }
11964}
11965
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011966static void
Robin Schroereba905b2014-05-18 02:24:50 +020011967connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011968 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011969{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011970 int bpp = pipe_config->pipe_bpp;
11971
11972 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11973 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011974 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011975
11976 /* Don't use an invalid EDID bpc value */
11977 if (connector->base.display_info.bpc &&
11978 connector->base.display_info.bpc * 3 < bpp) {
11979 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11980 bpp, connector->base.display_info.bpc*3);
11981 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11982 }
11983
11984 /* Clamp bpp to 8 on screens without EDID 1.4 */
11985 if (connector->base.display_info.bpc == 0 && bpp > 24) {
11986 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11987 bpp);
11988 pipe_config->pipe_bpp = 24;
11989 }
11990}
11991
11992static int
11993compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011994 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011995{
11996 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011997 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011998 struct drm_connector *connector;
11999 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012000 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012001
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012002 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012003 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012004 else if (INTEL_INFO(dev)->gen >= 5)
12005 bpp = 12*3;
12006 else
12007 bpp = 8*3;
12008
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012009
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012010 pipe_config->pipe_bpp = bpp;
12011
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012012 state = pipe_config->base.state;
12013
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012014 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012015 for_each_connector_in_state(state, connector, connector_state, i) {
12016 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012017 continue;
12018
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012019 connected_sink_compute_bpp(to_intel_connector(connector),
12020 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012021 }
12022
12023 return bpp;
12024}
12025
Daniel Vetter644db712013-09-19 14:53:58 +020012026static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12027{
12028 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12029 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010012030 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020012031 mode->crtc_hdisplay, mode->crtc_hsync_start,
12032 mode->crtc_hsync_end, mode->crtc_htotal,
12033 mode->crtc_vdisplay, mode->crtc_vsync_start,
12034 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12035}
12036
Daniel Vetterc0b03412013-05-28 12:05:54 +020012037static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012038 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012039 const char *context)
12040{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012041 struct drm_device *dev = crtc->base.dev;
12042 struct drm_plane *plane;
12043 struct intel_plane *intel_plane;
12044 struct intel_plane_state *state;
12045 struct drm_framebuffer *fb;
12046
12047 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
12048 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012049
12050 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
12051 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12052 pipe_config->pipe_bpp, pipe_config->dither);
12053 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12054 pipe_config->has_pch_encoder,
12055 pipe_config->fdi_lanes,
12056 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12057 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12058 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012059 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12060 pipe_config->has_dp_encoder,
12061 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12062 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12063 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012064
12065 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
12066 pipe_config->has_dp_encoder,
12067 pipe_config->dp_m2_n2.gmch_m,
12068 pipe_config->dp_m2_n2.gmch_n,
12069 pipe_config->dp_m2_n2.link_m,
12070 pipe_config->dp_m2_n2.link_n,
12071 pipe_config->dp_m2_n2.tu);
12072
Daniel Vetter55072d12014-11-20 16:10:28 +010012073 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12074 pipe_config->has_audio,
12075 pipe_config->has_infoframe);
12076
Daniel Vetterc0b03412013-05-28 12:05:54 +020012077 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012078 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012079 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012080 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12081 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030012082 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012083 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12084 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010012085 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12086 crtc->num_scalers,
12087 pipe_config->scaler_state.scaler_users,
12088 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012089 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12090 pipe_config->gmch_pfit.control,
12091 pipe_config->gmch_pfit.pgm_ratios,
12092 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012093 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020012094 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012095 pipe_config->pch_pfit.size,
12096 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012097 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030012098 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012099
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012100 if (IS_BROXTON(dev)) {
12101 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, "
12102 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
12103 "pll6: 0x%x, pll8: 0x%x, pcsdw12: 0x%x\n",
12104 pipe_config->ddi_pll_sel,
12105 pipe_config->dpll_hw_state.ebb0,
12106 pipe_config->dpll_hw_state.pll0,
12107 pipe_config->dpll_hw_state.pll1,
12108 pipe_config->dpll_hw_state.pll2,
12109 pipe_config->dpll_hw_state.pll3,
12110 pipe_config->dpll_hw_state.pll6,
12111 pipe_config->dpll_hw_state.pll8,
12112 pipe_config->dpll_hw_state.pcsdw12);
12113 } else if (IS_SKYLAKE(dev)) {
12114 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12115 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12116 pipe_config->ddi_pll_sel,
12117 pipe_config->dpll_hw_state.ctrl1,
12118 pipe_config->dpll_hw_state.cfgcr1,
12119 pipe_config->dpll_hw_state.cfgcr2);
12120 } else if (HAS_DDI(dev)) {
12121 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
12122 pipe_config->ddi_pll_sel,
12123 pipe_config->dpll_hw_state.wrpll);
12124 } else {
12125 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12126 "fp0: 0x%x, fp1: 0x%x\n",
12127 pipe_config->dpll_hw_state.dpll,
12128 pipe_config->dpll_hw_state.dpll_md,
12129 pipe_config->dpll_hw_state.fp0,
12130 pipe_config->dpll_hw_state.fp1);
12131 }
12132
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012133 DRM_DEBUG_KMS("planes on this crtc\n");
12134 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12135 intel_plane = to_intel_plane(plane);
12136 if (intel_plane->pipe != crtc->pipe)
12137 continue;
12138
12139 state = to_intel_plane_state(plane->state);
12140 fb = state->base.fb;
12141 if (!fb) {
12142 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
12143 "disabled, scaler_id = %d\n",
12144 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12145 plane->base.id, intel_plane->pipe,
12146 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
12147 drm_plane_index(plane), state->scaler_id);
12148 continue;
12149 }
12150
12151 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
12152 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12153 plane->base.id, intel_plane->pipe,
12154 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
12155 drm_plane_index(plane));
12156 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
12157 fb->base.id, fb->width, fb->height, fb->pixel_format);
12158 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
12159 state->scaler_id,
12160 state->src.x1 >> 16, state->src.y1 >> 16,
12161 drm_rect_width(&state->src) >> 16,
12162 drm_rect_height(&state->src) >> 16,
12163 state->dst.x1, state->dst.y1,
12164 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
12165 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020012166}
12167
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012168static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012169{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012170 struct drm_device *dev = state->dev;
12171 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012172 struct drm_connector *connector;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012173 struct drm_connector_state *connector_state;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012174 unsigned int used_ports = 0;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012175 int i;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012176
12177 /*
12178 * Walk the connector list instead of the encoder
12179 * list to detect the problem on ddi platforms
12180 * where there's just one encoder per digital port.
12181 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012182 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012183 if (!connector_state->best_encoder)
12184 continue;
12185
12186 encoder = to_intel_encoder(connector_state->best_encoder);
12187
12188 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012189
12190 switch (encoder->type) {
12191 unsigned int port_mask;
12192 case INTEL_OUTPUT_UNKNOWN:
12193 if (WARN_ON(!HAS_DDI(dev)))
12194 break;
12195 case INTEL_OUTPUT_DISPLAYPORT:
12196 case INTEL_OUTPUT_HDMI:
12197 case INTEL_OUTPUT_EDP:
12198 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12199
12200 /* the same port mustn't appear more than once */
12201 if (used_ports & port_mask)
12202 return false;
12203
12204 used_ports |= port_mask;
12205 default:
12206 break;
12207 }
12208 }
12209
12210 return true;
12211}
12212
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012213static void
12214clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12215{
12216 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012217 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012218 struct intel_dpll_hw_state dpll_hw_state;
12219 enum intel_dpll_id shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012220 uint32_t ddi_pll_sel;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012221
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012222 /* FIXME: before the switch to atomic started, a new pipe_config was
12223 * kzalloc'd. Code that depends on any field being zero should be
12224 * fixed, so that the crtc_state can be safely duplicated. For now,
12225 * only fields that are know to not cause problems are preserved. */
12226
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012227 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012228 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012229 shared_dpll = crtc_state->shared_dpll;
12230 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012231 ddi_pll_sel = crtc_state->ddi_pll_sel;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012232
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012233 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012234
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012235 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012236 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012237 crtc_state->shared_dpll = shared_dpll;
12238 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012239 crtc_state->ddi_pll_sel = ddi_pll_sel;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012240}
12241
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012242static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012243intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012244 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012245{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012246 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020012247 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012248 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012249 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012250 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012251 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012252 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012253
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012254 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012255
Daniel Vettere143a212013-07-04 12:01:15 +020012256 pipe_config->cpu_transcoder =
12257 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012258
Imre Deak2960bc92013-07-30 13:36:32 +030012259 /*
12260 * Sanitize sync polarity flags based on requested ones. If neither
12261 * positive or negative polarity is requested, treat this as meaning
12262 * negative polarity.
12263 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012264 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012265 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012266 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012267
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012268 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012269 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012270 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012271
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012272 /* Compute a starting value for pipe_config->pipe_bpp taking the source
12273 * plane pixel format and any sink constraints into account. Returns the
12274 * source plane bpp so that dithering can be selected on mismatches
12275 * after encoders and crtc also have had their say. */
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012276 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12277 pipe_config);
12278 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012279 goto fail;
12280
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012281 /*
12282 * Determine the real pipe dimensions. Note that stereo modes can
12283 * increase the actual pipe size due to the frame doubling and
12284 * insertion of additional space for blanks between the frame. This
12285 * is stored in the crtc timings. We use the requested mode to do this
12286 * computation to clearly distinguish it from the adjusted mode, which
12287 * can be changed by the connectors in the below retry loop.
12288 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012289 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080012290 &pipe_config->pipe_src_w,
12291 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012292
Daniel Vettere29c22c2013-02-21 00:00:16 +010012293encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020012294 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020012295 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020012296 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012297
Daniel Vetter135c81b2013-07-21 21:37:09 +020012298 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012299 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12300 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020012301
Daniel Vetter7758a112012-07-08 19:40:39 +020012302 /* Pass our mode to the connectors and the CRTC to give them a chance to
12303 * adjust it according to limitations or connector properties, and also
12304 * a chance to reject the mode entirely.
12305 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012306 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012307 if (connector_state->crtc != crtc)
12308 continue;
12309
12310 encoder = to_intel_encoder(connector_state->best_encoder);
12311
Daniel Vetterefea6e82013-07-21 21:36:59 +020012312 if (!(encoder->compute_config(encoder, pipe_config))) {
12313 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020012314 goto fail;
12315 }
12316 }
12317
Daniel Vetterff9a6752013-06-01 17:16:21 +020012318 /* Set default port clock if not overwritten by the encoder. Needs to be
12319 * done afterwards in case the encoder adjusts the mode. */
12320 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012321 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010012322 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012323
Daniel Vettera43f6e02013-06-07 23:10:32 +020012324 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010012325 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020012326 DRM_DEBUG_KMS("CRTC fixup failed\n");
12327 goto fail;
12328 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010012329
12330 if (ret == RETRY) {
12331 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12332 ret = -EINVAL;
12333 goto fail;
12334 }
12335
12336 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12337 retry = false;
12338 goto encoder_retry;
12339 }
12340
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012341 pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012342 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012343 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012344
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +020012345 /* Check if we need to force a modeset */
12346 if (pipe_config->has_audio !=
Maarten Lankhorst85a96e72015-06-01 12:49:53 +020012347 to_intel_crtc_state(crtc->state)->has_audio) {
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +020012348 pipe_config->base.mode_changed = true;
Maarten Lankhorst85a96e72015-06-01 12:49:53 +020012349 ret = drm_atomic_add_affected_planes(state, crtc);
12350 }
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +020012351
12352 /*
12353 * Note we have an issue here with infoframes: current code
12354 * only updates them on the full mode set path per hw
12355 * requirements. So here we should be checking for any
12356 * required changes and forcing a mode set.
12357 */
Daniel Vetter7758a112012-07-08 19:40:39 +020012358fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012359 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020012360}
12361
Daniel Vetterea9d7582012-07-10 10:42:52 +020012362static bool intel_crtc_in_use(struct drm_crtc *crtc)
12363{
12364 struct drm_encoder *encoder;
12365 struct drm_device *dev = crtc->dev;
12366
12367 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
12368 if (encoder->crtc == crtc)
12369 return true;
12370
12371 return false;
12372}
12373
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012374static void
12375intel_modeset_update_state(struct drm_atomic_state *state)
12376{
12377 struct drm_device *dev = state->dev;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012378 struct intel_encoder *intel_encoder;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012379 struct drm_crtc *crtc;
12380 struct drm_crtc_state *crtc_state;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012381 struct drm_connector *connector;
12382
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020012383 intel_shared_dpll_commit(state);
Daniel Vetterba41c0de2014-11-03 15:04:55 +010012384
Damien Lespiaub2784e12014-08-05 11:29:37 +010012385 for_each_intel_encoder(dev, intel_encoder) {
Daniel Vetterea9d7582012-07-10 10:42:52 +020012386 if (!intel_encoder->base.crtc)
12387 continue;
12388
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012389 crtc = intel_encoder->base.crtc;
12390 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
12391 if (!crtc_state || !needs_modeset(crtc->state))
12392 continue;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012393
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012394 intel_encoder->connectors_active = false;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012395 }
12396
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012397 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorstf7217902015-06-10 10:24:20 +020012398 intel_modeset_update_staged_output_state(state->dev);
Daniel Vetterea9d7582012-07-10 10:42:52 +020012399
Ville Syrjälä76688512014-01-10 11:28:06 +020012400 /* Double check state. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012401 for_each_crtc(dev, crtc) {
12402 WARN_ON(crtc->state->enable != intel_crtc_in_use(crtc));
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012403
12404 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020012405
12406 /* Update hwmode for vblank functions */
12407 if (crtc->state->active)
12408 crtc->hwmode = crtc->state->adjusted_mode;
12409 else
12410 crtc->hwmode.crtc_clock = 0;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012411 }
12412
12413 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
12414 if (!connector->encoder || !connector->encoder->crtc)
12415 continue;
12416
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012417 crtc = connector->encoder->crtc;
12418 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
12419 if (!crtc_state || !needs_modeset(crtc->state))
12420 continue;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012421
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020012422 if (crtc->state->active) {
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012423 struct drm_property *dpms_property =
12424 dev->mode_config.dpms_property;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012425
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012426 connector->dpms = DRM_MODE_DPMS_ON;
12427 drm_object_property_set_value(&connector->base, dpms_property, DRM_MODE_DPMS_ON);
Daniel Vetter68d34722012-09-06 22:08:35 +020012428
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012429 intel_encoder = to_intel_encoder(connector->encoder);
12430 intel_encoder->connectors_active = true;
12431 } else
12432 connector->dpms = DRM_MODE_DPMS_OFF;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012433 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012434}
12435
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012436static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012437{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012438 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012439
12440 if (clock1 == clock2)
12441 return true;
12442
12443 if (!clock1 || !clock2)
12444 return false;
12445
12446 diff = abs(clock1 - clock2);
12447
12448 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12449 return true;
12450
12451 return false;
12452}
12453
Daniel Vetter25c5b262012-07-08 22:08:04 +020012454#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12455 list_for_each_entry((intel_crtc), \
12456 &(dev)->mode_config.crtc_list, \
12457 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +020012458 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020012459
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012460static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012461intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012462 struct intel_crtc_state *current_config,
12463 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012464{
Daniel Vetter66e985c2013-06-05 13:34:20 +020012465#define PIPE_CONF_CHECK_X(name) \
12466 if (current_config->name != pipe_config->name) { \
12467 DRM_ERROR("mismatch in " #name " " \
12468 "(expected 0x%08x, found 0x%08x)\n", \
12469 current_config->name, \
12470 pipe_config->name); \
12471 return false; \
12472 }
12473
Daniel Vetter08a24032013-04-19 11:25:34 +020012474#define PIPE_CONF_CHECK_I(name) \
12475 if (current_config->name != pipe_config->name) { \
12476 DRM_ERROR("mismatch in " #name " " \
12477 "(expected %i, found %i)\n", \
12478 current_config->name, \
12479 pipe_config->name); \
12480 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010012481 }
12482
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012483/* This is required for BDW+ where there is only one set of registers for
12484 * switching between high and low RR.
12485 * This macro can be used whenever a comparison has to be made between one
12486 * hw state and multiple sw state variables.
12487 */
12488#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
12489 if ((current_config->name != pipe_config->name) && \
12490 (current_config->alt_name != pipe_config->name)) { \
12491 DRM_ERROR("mismatch in " #name " " \
12492 "(expected %i or %i, found %i)\n", \
12493 current_config->name, \
12494 current_config->alt_name, \
12495 pipe_config->name); \
12496 return false; \
12497 }
12498
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012499#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12500 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -070012501 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012502 "(expected %i, found %i)\n", \
12503 current_config->name & (mask), \
12504 pipe_config->name & (mask)); \
12505 return false; \
12506 }
12507
Ville Syrjälä5e550652013-09-06 23:29:07 +030012508#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12509 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
12510 DRM_ERROR("mismatch in " #name " " \
12511 "(expected %i, found %i)\n", \
12512 current_config->name, \
12513 pipe_config->name); \
12514 return false; \
12515 }
12516
Daniel Vetterbb760062013-06-06 14:55:52 +020012517#define PIPE_CONF_QUIRK(quirk) \
12518 ((current_config->quirks | pipe_config->quirks) & (quirk))
12519
Daniel Vettereccb1402013-05-22 00:50:22 +020012520 PIPE_CONF_CHECK_I(cpu_transcoder);
12521
Daniel Vetter08a24032013-04-19 11:25:34 +020012522 PIPE_CONF_CHECK_I(has_pch_encoder);
12523 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +020012524 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
12525 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
12526 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
12527 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
12528 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +020012529
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012530 PIPE_CONF_CHECK_I(has_dp_encoder);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012531
12532 if (INTEL_INFO(dev)->gen < 8) {
12533 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
12534 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
12535 PIPE_CONF_CHECK_I(dp_m_n.link_m);
12536 PIPE_CONF_CHECK_I(dp_m_n.link_n);
12537 PIPE_CONF_CHECK_I(dp_m_n.tu);
12538
12539 if (current_config->has_drrs) {
12540 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
12541 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
12542 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
12543 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
12544 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
12545 }
12546 } else {
12547 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
12548 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
12549 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
12550 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
12551 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
12552 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012553
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012554 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12555 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12556 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12557 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12558 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12559 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012560
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012561 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12562 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12563 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12564 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12565 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12566 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012567
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012568 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b52014-04-24 23:54:47 +020012569 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012570 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
12571 IS_VALLEYVIEW(dev))
12572 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012573 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012574
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012575 PIPE_CONF_CHECK_I(has_audio);
12576
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012577 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012578 DRM_MODE_FLAG_INTERLACE);
12579
Daniel Vetterbb760062013-06-06 14:55:52 +020012580 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012581 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012582 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012583 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012584 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012585 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012586 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012587 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012588 DRM_MODE_FLAG_NVSYNC);
12589 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012590
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012591 PIPE_CONF_CHECK_I(pipe_src_w);
12592 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012593
Daniel Vetter99535992014-04-13 12:00:33 +020012594 /*
12595 * FIXME: BIOS likes to set up a cloned config with lvds+external
12596 * screen. Since we don't yet re-compute the pipe config when moving
12597 * just the lvds port away to another pipe the sw tracking won't match.
12598 *
12599 * Proper atomic modesets with recomputed global state will fix this.
12600 * Until then just don't check gmch state for inherited modes.
12601 */
12602 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
12603 PIPE_CONF_CHECK_I(gmch_pfit.control);
12604 /* pfit ratios are autocomputed by the hw on gen4+ */
12605 if (INTEL_INFO(dev)->gen < 4)
12606 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
12607 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
12608 }
12609
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012610 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12611 if (current_config->pch_pfit.enabled) {
12612 PIPE_CONF_CHECK_I(pch_pfit.pos);
12613 PIPE_CONF_CHECK_I(pch_pfit.size);
12614 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012615
Chandra Kondurua1b22782015-04-07 15:28:45 -070012616 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12617
Jesse Barnese59150d2014-01-07 13:30:45 -080012618 /* BDW+ don't expose a synchronous way to read the state */
12619 if (IS_HASWELL(dev))
12620 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012621
Ville Syrjälä282740f2013-09-04 18:30:03 +030012622 PIPE_CONF_CHECK_I(double_wide);
12623
Daniel Vetter26804af2014-06-25 22:01:55 +030012624 PIPE_CONF_CHECK_X(ddi_pll_sel);
12625
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012626 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012627 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012628 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012629 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12630 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012631 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012632 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12633 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12634 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012635
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012636 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12637 PIPE_CONF_CHECK_I(pipe_bpp);
12638
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012639 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012640 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012641
Daniel Vetter66e985c2013-06-05 13:34:20 +020012642#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012643#undef PIPE_CONF_CHECK_I
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012644#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012645#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012646#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012647#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012648
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012649 return true;
12650}
12651
Damien Lespiau08db6652014-11-04 17:06:52 +000012652static void check_wm_state(struct drm_device *dev)
12653{
12654 struct drm_i915_private *dev_priv = dev->dev_private;
12655 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12656 struct intel_crtc *intel_crtc;
12657 int plane;
12658
12659 if (INTEL_INFO(dev)->gen < 9)
12660 return;
12661
12662 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12663 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12664
12665 for_each_intel_crtc(dev, intel_crtc) {
12666 struct skl_ddb_entry *hw_entry, *sw_entry;
12667 const enum pipe pipe = intel_crtc->pipe;
12668
12669 if (!intel_crtc->active)
12670 continue;
12671
12672 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000012673 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012674 hw_entry = &hw_ddb.plane[pipe][plane];
12675 sw_entry = &sw_ddb->plane[pipe][plane];
12676
12677 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12678 continue;
12679
12680 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12681 "(expected (%u,%u), found (%u,%u))\n",
12682 pipe_name(pipe), plane + 1,
12683 sw_entry->start, sw_entry->end,
12684 hw_entry->start, hw_entry->end);
12685 }
12686
12687 /* cursor */
12688 hw_entry = &hw_ddb.cursor[pipe];
12689 sw_entry = &sw_ddb->cursor[pipe];
12690
12691 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12692 continue;
12693
12694 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12695 "(expected (%u,%u), found (%u,%u))\n",
12696 pipe_name(pipe),
12697 sw_entry->start, sw_entry->end,
12698 hw_entry->start, hw_entry->end);
12699 }
12700}
12701
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012702static void
12703check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012704{
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012705 struct intel_connector *connector;
12706
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012707 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012708 /* This also checks the encoder/connector hw state with the
12709 * ->get_hw_state callbacks. */
12710 intel_connector_check_state(connector);
12711
Rob Clarke2c719b2014-12-15 13:56:32 -050012712 I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012713 "connector's staged encoder doesn't match current encoder\n");
12714 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012715}
12716
12717static void
12718check_encoder_state(struct drm_device *dev)
12719{
12720 struct intel_encoder *encoder;
12721 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012722
Damien Lespiaub2784e12014-08-05 11:29:37 +010012723 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012724 bool enabled = false;
12725 bool active = false;
12726 enum pipe pipe, tracked_pipe;
12727
12728 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12729 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012730 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012731
Rob Clarke2c719b2014-12-15 13:56:32 -050012732 I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012733 "encoder's stage crtc doesn't match current crtc\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012734 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012735 "encoder's active_connectors set, but no crtc\n");
12736
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012737 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012738 if (connector->base.encoder != &encoder->base)
12739 continue;
12740 enabled = true;
12741 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
12742 active = true;
12743 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012744 /*
12745 * for MST connectors if we unplug the connector is gone
12746 * away but the encoder is still connected to a crtc
12747 * until a modeset happens in response to the hotplug.
12748 */
12749 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
12750 continue;
12751
Rob Clarke2c719b2014-12-15 13:56:32 -050012752 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012753 "encoder's enabled state mismatch "
12754 "(expected %i, found %i)\n",
12755 !!encoder->base.crtc, enabled);
Rob Clarke2c719b2014-12-15 13:56:32 -050012756 I915_STATE_WARN(active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012757 "active encoder with no crtc\n");
12758
Rob Clarke2c719b2014-12-15 13:56:32 -050012759 I915_STATE_WARN(encoder->connectors_active != active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012760 "encoder's computed active state doesn't match tracked active state "
12761 "(expected %i, found %i)\n", active, encoder->connectors_active);
12762
12763 active = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -050012764 I915_STATE_WARN(active != encoder->connectors_active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012765 "encoder's hw state doesn't match sw tracking "
12766 "(expected %i, found %i)\n",
12767 encoder->connectors_active, active);
12768
12769 if (!encoder->base.crtc)
12770 continue;
12771
12772 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
Rob Clarke2c719b2014-12-15 13:56:32 -050012773 I915_STATE_WARN(active && pipe != tracked_pipe,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012774 "active encoder's pipe doesn't match"
12775 "(expected %i, found %i)\n",
12776 tracked_pipe, pipe);
12777
12778 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012779}
12780
12781static void
12782check_crtc_state(struct drm_device *dev)
12783{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012784 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012785 struct intel_crtc *crtc;
12786 struct intel_encoder *encoder;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012787 struct intel_crtc_state pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012788
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012789 for_each_intel_crtc(dev, crtc) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012790 bool enabled = false;
12791 bool active = false;
12792
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012793 memset(&pipe_config, 0, sizeof(pipe_config));
12794
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012795 DRM_DEBUG_KMS("[CRTC:%d]\n",
12796 crtc->base.base.id);
12797
Matt Roper83d65732015-02-25 13:12:16 -080012798 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012799 "active crtc, but not enabled in sw tracking\n");
12800
Damien Lespiaub2784e12014-08-05 11:29:37 +010012801 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012802 if (encoder->base.crtc != &crtc->base)
12803 continue;
12804 enabled = true;
12805 if (encoder->connectors_active)
12806 active = true;
12807 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020012808
Rob Clarke2c719b2014-12-15 13:56:32 -050012809 I915_STATE_WARN(active != crtc->active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012810 "crtc's computed active state doesn't match tracked active state "
12811 "(expected %i, found %i)\n", active, crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080012812 I915_STATE_WARN(enabled != crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012813 "crtc's computed enabled state doesn't match tracked enabled state "
Matt Roper83d65732015-02-25 13:12:16 -080012814 "(expected %i, found %i)\n", enabled,
12815 crtc->base.state->enable);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012816
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012817 active = dev_priv->display.get_pipe_config(crtc,
12818 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +020012819
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030012820 /* hw state is inconsistent with the pipe quirk */
12821 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12822 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetterd62cf622013-05-29 10:41:29 +020012823 active = crtc->active;
12824
Damien Lespiaub2784e12014-08-05 11:29:37 +010012825 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +030012826 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +020012827 if (encoder->base.crtc != &crtc->base)
12828 continue;
Daniel Vetter1d37b682013-11-18 09:00:59 +010012829 if (encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +020012830 encoder->get_config(encoder, &pipe_config);
12831 }
12832
Rob Clarke2c719b2014-12-15 13:56:32 -050012833 I915_STATE_WARN(crtc->active != active,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012834 "crtc active state doesn't match with hw state "
12835 "(expected %i, found %i)\n", crtc->active, active);
12836
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020012837 I915_STATE_WARN(crtc->active != crtc->base.state->active,
12838 "transitional active state does not match atomic hw state "
12839 "(expected %i, found %i)\n", crtc->base.state->active, crtc->active);
12840
Daniel Vetterc0b03412013-05-28 12:05:54 +020012841 if (active &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012842 !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050012843 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Daniel Vetterc0b03412013-05-28 12:05:54 +020012844 intel_dump_pipe_config(crtc, &pipe_config,
12845 "[hw state]");
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012846 intel_dump_pipe_config(crtc, crtc->config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012847 "[sw state]");
12848 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012849 }
12850}
12851
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012852static void
12853check_shared_dpll_state(struct drm_device *dev)
12854{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012855 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012856 struct intel_crtc *crtc;
12857 struct intel_dpll_hw_state dpll_hw_state;
12858 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020012859
12860 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12861 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12862 int enabled_crtcs = 0, active_crtcs = 0;
12863 bool active;
12864
12865 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12866
12867 DRM_DEBUG_KMS("%s\n", pll->name);
12868
12869 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
12870
Rob Clarke2c719b2014-12-15 13:56:32 -050012871 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
Daniel Vetter53589012013-06-05 13:34:16 +020012872 "more active pll users than references: %i vs %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012873 pll->active, hweight32(pll->config.crtc_mask));
Rob Clarke2c719b2014-12-15 13:56:32 -050012874 I915_STATE_WARN(pll->active && !pll->on,
Daniel Vetter53589012013-06-05 13:34:16 +020012875 "pll in active use but not on in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012876 I915_STATE_WARN(pll->on && !pll->active,
Daniel Vetter35c95372013-07-17 06:55:04 +020012877 "pll in on but not on in use in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012878 I915_STATE_WARN(pll->on != active,
Daniel Vetter53589012013-06-05 13:34:16 +020012879 "pll on state mismatch (expected %i, found %i)\n",
12880 pll->on, active);
12881
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012882 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080012883 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
Daniel Vetter53589012013-06-05 13:34:16 +020012884 enabled_crtcs++;
12885 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12886 active_crtcs++;
12887 }
Rob Clarke2c719b2014-12-15 13:56:32 -050012888 I915_STATE_WARN(pll->active != active_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012889 "pll active crtcs mismatch (expected %i, found %i)\n",
12890 pll->active, active_crtcs);
Rob Clarke2c719b2014-12-15 13:56:32 -050012891 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012892 "pll enabled crtcs mismatch (expected %i, found %i)\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012893 hweight32(pll->config.crtc_mask), enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012894
Rob Clarke2c719b2014-12-15 13:56:32 -050012895 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020012896 sizeof(dpll_hw_state)),
12897 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020012898 }
Daniel Vettere2e1ed42012-07-08 21:14:38 +020012899}
12900
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012901void
12902intel_modeset_check_state(struct drm_device *dev)
12903{
Damien Lespiau08db6652014-11-04 17:06:52 +000012904 check_wm_state(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012905 check_connector_state(dev);
12906 check_encoder_state(dev);
12907 check_crtc_state(dev);
12908 check_shared_dpll_state(dev);
12909}
12910
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012911void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
Ville Syrjälä18442d02013-09-13 16:00:08 +030012912 int dotclock)
12913{
12914 /*
12915 * FDI already provided one idea for the dotclock.
12916 * Yell if the encoder disagrees.
12917 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012918 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030012919 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012920 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030012921}
12922
Ville Syrjälä80715b22014-05-15 20:23:23 +030012923static void update_scanline_offset(struct intel_crtc *crtc)
12924{
12925 struct drm_device *dev = crtc->base.dev;
12926
12927 /*
12928 * The scanline counter increments at the leading edge of hsync.
12929 *
12930 * On most platforms it starts counting from vtotal-1 on the
12931 * first active line. That means the scanline counter value is
12932 * always one less than what we would expect. Ie. just after
12933 * start of vblank, which also occurs at start of hsync (on the
12934 * last active line), the scanline counter will read vblank_start-1.
12935 *
12936 * On gen2 the scanline counter starts counting from 1 instead
12937 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12938 * to keep the value positive), instead of adding one.
12939 *
12940 * On HSW+ the behaviour of the scanline counter depends on the output
12941 * type. For DP ports it behaves like most other platforms, but on HDMI
12942 * there's an extra 1 line difference. So we need to add two instead of
12943 * one to the value.
12944 */
12945 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012946 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012947 int vtotal;
12948
12949 vtotal = mode->crtc_vtotal;
12950 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
12951 vtotal /= 2;
12952
12953 crtc->scanline_offset = vtotal - 1;
12954 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030012955 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030012956 crtc->scanline_offset = 2;
12957 } else
12958 crtc->scanline_offset = 1;
12959}
12960
Maarten Lankhorstad421372015-06-15 12:33:42 +020012961static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012962{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012963 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012964 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstad421372015-06-15 12:33:42 +020012965 struct intel_shared_dpll_config *shared_dpll = NULL;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012966 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012967 struct intel_crtc_state *intel_crtc_state;
12968 struct drm_crtc *crtc;
12969 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012970 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012971
12972 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020012973 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012974
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012975 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020012976 int dpll;
12977
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012978 intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012979 intel_crtc_state = to_intel_crtc_state(crtc_state);
Maarten Lankhorstad421372015-06-15 12:33:42 +020012980 dpll = intel_crtc_state->shared_dpll;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012981
Maarten Lankhorstad421372015-06-15 12:33:42 +020012982 if (!needs_modeset(crtc_state) || dpll == DPLL_ID_PRIVATE)
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012983 continue;
12984
Maarten Lankhorstad421372015-06-15 12:33:42 +020012985 intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012986
Maarten Lankhorstad421372015-06-15 12:33:42 +020012987 if (!shared_dpll)
12988 shared_dpll = intel_atomic_get_shared_dpll_state(state);
12989
12990 shared_dpll[dpll].crtc_mask &= ~(1 << intel_crtc->pipe);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012991 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012992}
12993
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020012994/*
12995 * This implements the workaround described in the "notes" section of the mode
12996 * set sequence documentation. When going from no pipes or single pipe to
12997 * multiple pipes, and planes are enabled after the pipe, we need to wait at
12998 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
12999 */
13000static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13001{
13002 struct drm_crtc_state *crtc_state;
13003 struct intel_crtc *intel_crtc;
13004 struct drm_crtc *crtc;
13005 struct intel_crtc_state *first_crtc_state = NULL;
13006 struct intel_crtc_state *other_crtc_state = NULL;
13007 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13008 int i;
13009
13010 /* look at all crtc's that are going to be enabled in during modeset */
13011 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13012 intel_crtc = to_intel_crtc(crtc);
13013
13014 if (!crtc_state->active || !needs_modeset(crtc_state))
13015 continue;
13016
13017 if (first_crtc_state) {
13018 other_crtc_state = to_intel_crtc_state(crtc_state);
13019 break;
13020 } else {
13021 first_crtc_state = to_intel_crtc_state(crtc_state);
13022 first_pipe = intel_crtc->pipe;
13023 }
13024 }
13025
13026 /* No workaround needed? */
13027 if (!first_crtc_state)
13028 return 0;
13029
13030 /* w/a possibly needed, check how many crtc's are already enabled. */
13031 for_each_intel_crtc(state->dev, intel_crtc) {
13032 struct intel_crtc_state *pipe_config;
13033
13034 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13035 if (IS_ERR(pipe_config))
13036 return PTR_ERR(pipe_config);
13037
13038 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13039
13040 if (!pipe_config->base.active ||
13041 needs_modeset(&pipe_config->base))
13042 continue;
13043
13044 /* 2 or more enabled crtcs means no need for w/a */
13045 if (enabled_pipe != INVALID_PIPE)
13046 return 0;
13047
13048 enabled_pipe = intel_crtc->pipe;
13049 }
13050
13051 if (enabled_pipe != INVALID_PIPE)
13052 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13053 else if (other_crtc_state)
13054 other_crtc_state->hsw_workaround_pipe = first_pipe;
13055
13056 return 0;
13057}
13058
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013059/* Code that should eventually be part of atomic_check() */
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013060static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013061{
13062 struct drm_device *dev = state->dev;
13063 int ret;
13064
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013065 if (!check_digital_port_conflicts(state)) {
13066 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13067 return -EINVAL;
13068 }
13069
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013070 /*
13071 * See if the config requires any additional preparation, e.g.
13072 * to adjust global state with pipes off. We need to do this
13073 * here so we can get the modeset_pipe updated config for the new
13074 * mode set on this crtc. For other crtcs we need to use the
13075 * adjusted_mode bits in the crtc directly.
13076 */
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030013077 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev) || IS_BROADWELL(dev)) {
13078 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev))
13079 ret = valleyview_modeset_global_pipes(state);
13080 else
13081 ret = broadwell_modeset_global_pipes(state);
13082
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013083 if (ret)
13084 return ret;
13085 }
13086
Maarten Lankhorstad421372015-06-15 12:33:42 +020013087 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013088
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013089 if (IS_HASWELL(dev))
Maarten Lankhorstad421372015-06-15 12:33:42 +020013090 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013091
Maarten Lankhorstad421372015-06-15 12:33:42 +020013092 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013093}
13094
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013095static int
13096intel_modeset_compute_config(struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013097{
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013098 struct drm_crtc *crtc;
13099 struct drm_crtc_state *crtc_state;
13100 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013101 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013102
13103 ret = drm_atomic_helper_check_modeset(state->dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013104 if (ret)
13105 return ret;
13106
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013107 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013108 if (!crtc_state->enable) {
13109 if (needs_modeset(crtc_state))
13110 any_ms = true;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013111 continue;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013112 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013113
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020013114 if (to_intel_crtc_state(crtc_state)->quirks &
13115 PIPE_CONFIG_QUIRK_INITIAL_PLANES) {
13116 ret = drm_atomic_add_affected_planes(state, crtc);
13117 if (ret)
13118 return ret;
13119
13120 /*
13121 * We ought to handle i915.fastboot here.
13122 * If no modeset is required and the primary plane has
13123 * a fb, update the members of crtc_state as needed,
13124 * and run the necessary updates during vblank evasion.
13125 */
13126 }
13127
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013128 if (!needs_modeset(crtc_state)) {
13129 ret = drm_atomic_add_affected_connectors(state, crtc);
13130 if (ret)
13131 return ret;
13132 }
13133
13134 ret = intel_modeset_pipe_config(crtc,
13135 to_intel_crtc_state(crtc_state));
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013136 if (ret)
13137 return ret;
13138
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013139 if (needs_modeset(crtc_state))
13140 any_ms = true;
13141
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013142 intel_dump_pipe_config(to_intel_crtc(crtc),
13143 to_intel_crtc_state(crtc_state),
13144 "[modeset]");
13145 }
13146
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013147 if (any_ms) {
13148 ret = intel_modeset_checks(state);
13149
13150 if (ret)
13151 return ret;
13152 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013153
13154 return drm_atomic_helper_check_planes(state->dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013155}
13156
Ander Conselvan de Oliveirac72d9692015-06-01 12:49:50 +020013157static int __intel_set_mode(struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013158{
Ander Conselvan de Oliveirac72d9692015-06-01 12:49:50 +020013159 struct drm_device *dev = state->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030013160 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013161 struct drm_crtc *crtc;
13162 struct drm_crtc_state *crtc_state;
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013163 int ret = 0;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013164 int i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013165 bool any_ms = false;
Daniel Vettera6778b32012-07-02 09:56:42 +020013166
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013167 ret = drm_atomic_helper_prepare_planes(dev, state);
13168 if (ret)
13169 return ret;
13170
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013171 drm_atomic_helper_swap_state(dev, state);
13172
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013173 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013174 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13175
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013176 if (!needs_modeset(crtc->state))
13177 continue;
13178
13179 any_ms = true;
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013180 intel_pre_plane_update(intel_crtc);
Daniel Vetter460da9162013-03-27 00:44:51 +010013181
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013182 if (crtc_state->active) {
13183 intel_crtc_disable_planes(crtc, crtc_state->plane_mask);
13184 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013185 intel_crtc->active = false;
13186 intel_disable_shared_dpll(intel_crtc);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013187 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010013188 }
Daniel Vetter7758a112012-07-08 19:40:39 +020013189
Daniel Vetterea9d7582012-07-10 10:42:52 +020013190 /* Only after disabling all output pipelines that will be changed can we
13191 * update the the output configuration. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013192 intel_modeset_update_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020013193
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030013194 /* The state has been swaped above, so state actually contains the
13195 * old state now. */
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013196 if (any_ms)
13197 modeset_update_crtc_power_domains(state);
Daniel Vetter47fab732012-10-26 10:58:18 +020013198
Daniel Vettera6778b32012-07-02 09:56:42 +020013199 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013200 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013201 if (needs_modeset(crtc->state) && crtc->state->active) {
13202 update_scanline_offset(to_intel_crtc(crtc));
13203 dev_priv->display.crtc_enable(crtc);
13204 }
13205
Maarten Lankhorst5ac1c4b2015-06-01 12:50:01 +020013206 drm_atomic_helper_commit_planes_on_crtc(crtc_state);
Ville Syrjälä80715b22014-05-15 20:23:23 +030013207 }
Daniel Vettera6778b32012-07-02 09:56:42 +020013208
Daniel Vettera6778b32012-07-02 09:56:42 +020013209 /* FIXME: add subpixel order */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013210
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013211 drm_atomic_helper_cleanup_planes(dev, state);
13212
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013213 drm_atomic_state_free(state);
13214
Ander Conselvan de Oliveira9eb45f22015-04-21 17:13:07 +030013215 return 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020013216}
13217
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013218static int intel_set_mode_checked(struct drm_atomic_state *state)
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013219{
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013220 struct drm_device *dev = state->dev;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013221 int ret;
13222
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013223 ret = __intel_set_mode(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013224 if (ret == 0)
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013225 intel_modeset_check_state(dev);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013226
13227 return ret;
13228}
13229
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013230static int intel_set_mode(struct drm_atomic_state *state)
Daniel Vetterf30da182013-04-11 20:22:50 +020013231{
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013232 int ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020013233
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013234 ret = intel_modeset_compute_config(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013235 if (ret)
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013236 return ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013237
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013238 return intel_set_mode_checked(state);
Daniel Vetterf30da182013-04-11 20:22:50 +020013239}
13240
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013241void intel_crtc_restore_mode(struct drm_crtc *crtc)
13242{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013243 struct drm_device *dev = crtc->dev;
13244 struct drm_atomic_state *state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013245 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013246 struct intel_encoder *encoder;
13247 struct intel_connector *connector;
13248 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013249 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013250 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013251
13252 state = drm_atomic_state_alloc(dev);
13253 if (!state) {
13254 DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
13255 crtc->base.id);
13256 return;
13257 }
13258
13259 state->acquire_ctx = dev->mode_config.acquire_ctx;
13260
13261 /* The force restore path in the HW readout code relies on the staged
13262 * config still keeping the user requested config while the actual
13263 * state has been overwritten by the configuration read from HW. We
13264 * need to copy the staged config to the atomic state, otherwise the
13265 * mode set will just reapply the state the HW is already in. */
13266 for_each_intel_encoder(dev, encoder) {
13267 if (&encoder->new_crtc->base != crtc)
13268 continue;
13269
13270 for_each_intel_connector(dev, connector) {
13271 if (connector->new_encoder != encoder)
13272 continue;
13273
13274 connector_state = drm_atomic_get_connector_state(state, &connector->base);
13275 if (IS_ERR(connector_state)) {
13276 DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
13277 connector->base.base.id,
13278 connector->base.name,
13279 PTR_ERR(connector_state));
13280 continue;
13281 }
13282
13283 connector_state->crtc = crtc;
13284 connector_state->best_encoder = &encoder->base;
13285 }
13286 }
13287
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013288 for_each_intel_crtc(dev, intel_crtc) {
13289 if (intel_crtc->new_enabled == intel_crtc->base.enabled)
13290 continue;
13291
13292 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
13293 if (IS_ERR(crtc_state)) {
13294 DRM_DEBUG_KMS("Failed to add [CRTC:%d] to state: %ld\n",
13295 intel_crtc->base.base.id,
13296 PTR_ERR(crtc_state));
13297 continue;
13298 }
13299
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020013300 crtc_state->base.active = crtc_state->base.enable =
13301 intel_crtc->new_enabled;
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030013302
13303 if (&intel_crtc->base == crtc)
13304 drm_mode_copy(&crtc_state->base.mode, &crtc->mode);
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013305 }
13306
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030013307 intel_modeset_setup_plane_state(state, crtc, &crtc->mode,
13308 crtc->primary->fb, crtc->x, crtc->y);
13309
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013310 ret = intel_set_mode(state);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013311 if (ret)
13312 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013313}
13314
Daniel Vetter25c5b262012-07-08 22:08:04 +020013315#undef for_each_intel_crtc_masked
13316
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013317static bool intel_connector_in_mode_set(struct intel_connector *connector,
13318 struct drm_mode_set *set)
13319{
13320 int ro;
13321
13322 for (ro = 0; ro < set->num_connectors; ro++)
13323 if (set->connectors[ro] == &connector->base)
13324 return true;
13325
13326 return false;
13327}
13328
Daniel Vetter2e431052012-07-04 22:42:15 +020013329static int
Daniel Vetter9a935852012-07-05 22:34:27 +020013330intel_modeset_stage_output_state(struct drm_device *dev,
13331 struct drm_mode_set *set,
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020013332 struct drm_atomic_state *state)
Daniel Vetter50f56112012-07-02 09:35:43 +020013333{
Daniel Vetter9a935852012-07-05 22:34:27 +020013334 struct intel_connector *connector;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013335 struct drm_connector *drm_connector;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020013336 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013337 struct drm_crtc *crtc;
13338 struct drm_crtc_state *crtc_state;
13339 int i, ret;
Daniel Vetter50f56112012-07-02 09:35:43 +020013340
Damien Lespiau9abdda72013-02-13 13:29:23 +000013341 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +020013342 * of connectors. For paranoia, double-check this. */
13343 WARN_ON(!set->fb && (set->num_connectors != 0));
13344 WARN_ON(set->fb && (set->num_connectors == 0));
13345
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020013346 for_each_intel_connector(dev, connector) {
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013347 bool in_mode_set = intel_connector_in_mode_set(connector, set);
13348
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013349 if (!in_mode_set && connector->base.state->crtc != set->crtc)
13350 continue;
13351
13352 connector_state =
13353 drm_atomic_get_connector_state(state, &connector->base);
13354 if (IS_ERR(connector_state))
13355 return PTR_ERR(connector_state);
13356
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013357 if (in_mode_set) {
13358 int pipe = to_intel_crtc(set->crtc)->pipe;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013359 connector_state->best_encoder =
13360 &intel_find_encoder(connector, pipe)->base;
Daniel Vetter50f56112012-07-02 09:35:43 +020013361 }
13362
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013363 if (connector->base.state->crtc != set->crtc)
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013364 continue;
13365
Daniel Vetter9a935852012-07-05 22:34:27 +020013366 /* If we disable the crtc, disable all its connectors. Also, if
13367 * the connector is on the changing crtc but not on the new
13368 * connector list, disable it. */
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013369 if (!set->fb || !in_mode_set) {
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013370 connector_state->best_encoder = NULL;
Daniel Vetter9a935852012-07-05 22:34:27 +020013371
13372 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
13373 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030013374 connector->base.name);
Daniel Vetter9a935852012-07-05 22:34:27 +020013375 }
Daniel Vetter9a935852012-07-05 22:34:27 +020013376 }
13377 /* connector->new_encoder is now updated for all connectors. */
13378
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013379 for_each_connector_in_state(state, drm_connector, connector_state, i) {
13380 connector = to_intel_connector(drm_connector);
Ville Syrjälä76688512014-01-10 11:28:06 +020013381
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013382 if (!connector_state->best_encoder) {
13383 ret = drm_atomic_set_crtc_for_connector(connector_state,
13384 NULL);
13385 if (ret)
13386 return ret;
13387
Daniel Vetter50f56112012-07-02 09:35:43 +020013388 continue;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013389 }
Daniel Vetter50f56112012-07-02 09:35:43 +020013390
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013391 if (intel_connector_in_mode_set(connector, set)) {
13392 struct drm_crtc *crtc = connector->base.state->crtc;
13393
13394 /* If this connector was in a previous crtc, add it
13395 * to the state. We might need to disable it. */
13396 if (crtc) {
13397 crtc_state =
13398 drm_atomic_get_crtc_state(state, crtc);
13399 if (IS_ERR(crtc_state))
13400 return PTR_ERR(crtc_state);
13401 }
13402
13403 ret = drm_atomic_set_crtc_for_connector(connector_state,
13404 set->crtc);
13405 if (ret)
13406 return ret;
13407 }
Daniel Vetter50f56112012-07-02 09:35:43 +020013408
13409 /* Make sure the new CRTC will work with the encoder */
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013410 if (!drm_encoder_crtc_ok(connector_state->best_encoder,
13411 connector_state->crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020013412 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +020013413 }
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020013414
Daniel Vetter9a935852012-07-05 22:34:27 +020013415 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
13416 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030013417 connector->base.name,
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013418 connector_state->crtc->base.id);
13419
13420 if (connector_state->best_encoder != &connector->encoder->base)
13421 connector->encoder =
13422 to_intel_encoder(connector_state->best_encoder);
Daniel Vetter9a935852012-07-05 22:34:27 +020013423 }
13424
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013425 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020013426 bool has_connectors;
13427
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013428 ret = drm_atomic_add_affected_connectors(state, crtc);
13429 if (ret)
13430 return ret;
Paulo Zanoni5a65f352014-01-07 14:55:53 -020013431
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020013432 has_connectors = !!drm_atomic_connectors_for_crtc(state, crtc);
13433 if (has_connectors != crtc_state->enable)
13434 crtc_state->enable =
13435 crtc_state->active = has_connectors;
Ville Syrjälä76688512014-01-10 11:28:06 +020013436 }
13437
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030013438 ret = intel_modeset_setup_plane_state(state, set->crtc, set->mode,
13439 set->fb, set->x, set->y);
13440 if (ret)
13441 return ret;
13442
13443 crtc_state = drm_atomic_get_crtc_state(state, set->crtc);
13444 if (IS_ERR(crtc_state))
13445 return PTR_ERR(crtc_state);
13446
Matt Roperce522992015-06-05 15:08:24 -070013447 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
13448 if (ret)
13449 return ret;
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030013450
13451 if (set->num_connectors)
13452 crtc_state->active = true;
13453
Daniel Vetter2e431052012-07-04 22:42:15 +020013454 return 0;
13455}
13456
13457static int intel_crtc_set_config(struct drm_mode_set *set)
13458{
13459 struct drm_device *dev;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013460 struct drm_atomic_state *state = NULL;
Daniel Vetter2e431052012-07-04 22:42:15 +020013461 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +020013462
Daniel Vetter8d3e3752012-07-05 16:09:09 +020013463 BUG_ON(!set);
13464 BUG_ON(!set->crtc);
13465 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +020013466
Daniel Vetter7e53f3a2013-01-21 10:52:17 +010013467 /* Enforce sane interface api - has been abused by the fb helper. */
13468 BUG_ON(!set->mode && set->fb);
13469 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +020013470
Daniel Vetter2e431052012-07-04 22:42:15 +020013471 if (set->fb) {
13472 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
13473 set->crtc->base.id, set->fb->base.id,
13474 (int)set->num_connectors, set->x, set->y);
13475 } else {
13476 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +020013477 }
13478
13479 dev = set->crtc->dev;
13480
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013481 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013482 if (!state)
13483 return -ENOMEM;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013484
13485 state->acquire_ctx = dev->mode_config.acquire_ctx;
13486
Ander Conselvan de Oliveira462a4252015-04-21 17:13:00 +030013487 ret = intel_modeset_stage_output_state(dev, set, state);
Daniel Vetter2e431052012-07-04 22:42:15 +020013488 if (ret)
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013489 goto out;
Daniel Vetter2e431052012-07-04 22:42:15 +020013490
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013491 ret = intel_modeset_compute_config(state);
13492 if (ret)
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013493 goto out;
Jesse Barnes50f52752014-11-07 13:11:00 -080013494
Jesse Barnes1f9954d2014-11-05 14:26:10 -080013495 intel_update_pipe_size(to_intel_crtc(set->crtc));
13496
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013497 ret = intel_set_mode_checked(state);
Chris Wilson2d05eae2013-05-03 17:36:25 +010013498 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +020013499 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
13500 set->crtc->base.id, ret);
Chris Wilson2d05eae2013-05-03 17:36:25 +010013501 }
Daniel Vetter50f56112012-07-02 09:35:43 +020013502
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013503out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013504 if (ret)
13505 drm_atomic_state_free(state);
Daniel Vetter50f56112012-07-02 09:35:43 +020013506 return ret;
13507}
13508
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013509static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013510 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +020013511 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013512 .destroy = intel_crtc_destroy,
13513 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013514 .atomic_duplicate_state = intel_crtc_duplicate_state,
13515 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013516};
13517
Daniel Vetter53589012013-06-05 13:34:16 +020013518static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
13519 struct intel_shared_dpll *pll,
13520 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013521{
Daniel Vetter53589012013-06-05 13:34:16 +020013522 uint32_t val;
13523
Daniel Vetterf458ebb2014-09-30 10:56:39 +020013524 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030013525 return false;
13526
Daniel Vetter53589012013-06-05 13:34:16 +020013527 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020013528 hw_state->dpll = val;
13529 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
13530 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020013531
13532 return val & DPLL_VCO_ENABLE;
13533}
13534
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013535static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
13536 struct intel_shared_dpll *pll)
13537{
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013538 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
13539 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013540}
13541
Daniel Vettere7b903d2013-06-05 13:34:14 +020013542static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
13543 struct intel_shared_dpll *pll)
13544{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013545 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020013546 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020013547
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013548 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013549
13550 /* Wait for the clocks to stabilize. */
13551 POSTING_READ(PCH_DPLL(pll->id));
13552 udelay(150);
13553
13554 /* The pixel multiplier can only be updated once the
13555 * DPLL is enabled and the clocks are stable.
13556 *
13557 * So write it again.
13558 */
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013559 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013560 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013561 udelay(200);
13562}
13563
13564static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
13565 struct intel_shared_dpll *pll)
13566{
13567 struct drm_device *dev = dev_priv->dev;
13568 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013569
13570 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013571 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020013572 if (intel_crtc_to_shared_dpll(crtc) == pll)
13573 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
13574 }
13575
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013576 I915_WRITE(PCH_DPLL(pll->id), 0);
13577 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013578 udelay(200);
13579}
13580
Daniel Vetter46edb022013-06-05 13:34:12 +020013581static char *ibx_pch_dpll_names[] = {
13582 "PCH DPLL A",
13583 "PCH DPLL B",
13584};
13585
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013586static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013587{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013588 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013589 int i;
13590
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013591 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013592
Daniel Vettere72f9fb2013-06-05 13:34:06 +020013593 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020013594 dev_priv->shared_dplls[i].id = i;
13595 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013596 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013597 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
13598 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020013599 dev_priv->shared_dplls[i].get_hw_state =
13600 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013601 }
13602}
13603
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013604static void intel_shared_dpll_init(struct drm_device *dev)
13605{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013606 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013607
Ville Syrjäläb6283052015-06-03 15:45:07 +030013608 intel_update_cdclk(dev);
13609
Daniel Vetter9cd86932014-06-25 22:01:57 +030013610 if (HAS_DDI(dev))
13611 intel_ddi_pll_init(dev);
13612 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013613 ibx_pch_dpll_init(dev);
13614 else
13615 dev_priv->num_shared_dpll = 0;
13616
13617 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013618}
13619
Matt Roper6beb8c232014-12-01 15:40:14 -080013620/**
13621 * intel_prepare_plane_fb - Prepare fb for usage on plane
13622 * @plane: drm plane to prepare for
13623 * @fb: framebuffer to prepare for presentation
13624 *
13625 * Prepares a framebuffer for usage on a display plane. Generally this
13626 * involves pinning the underlying object and updating the frontbuffer tracking
13627 * bits. Some older platforms need special physical address handling for
13628 * cursor planes.
13629 *
13630 * Returns 0 on success, negative error code on failure.
13631 */
13632int
13633intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013634 struct drm_framebuffer *fb,
13635 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013636{
13637 struct drm_device *dev = plane->dev;
Matt Roper6beb8c232014-12-01 15:40:14 -080013638 struct intel_plane *intel_plane = to_intel_plane(plane);
13639 enum pipe pipe = intel_plane->pipe;
13640 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13641 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
13642 unsigned frontbuffer_bits = 0;
13643 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013644
Matt Roperea2c67b2014-12-23 10:41:52 -080013645 if (!obj)
Matt Roper465c1202014-05-29 08:06:54 -070013646 return 0;
13647
Matt Roper6beb8c232014-12-01 15:40:14 -080013648 switch (plane->type) {
13649 case DRM_PLANE_TYPE_PRIMARY:
13650 frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
13651 break;
13652 case DRM_PLANE_TYPE_CURSOR:
13653 frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
13654 break;
13655 case DRM_PLANE_TYPE_OVERLAY:
13656 frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
13657 break;
13658 }
Matt Roper465c1202014-05-29 08:06:54 -070013659
Matt Roper4c345742014-07-09 16:22:10 -070013660 mutex_lock(&dev->struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070013661
Matt Roper6beb8c232014-12-01 15:40:14 -080013662 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13663 INTEL_INFO(dev)->cursor_needs_physical) {
13664 int align = IS_I830(dev) ? 16 * 1024 : 256;
13665 ret = i915_gem_object_attach_phys(obj, align);
13666 if (ret)
13667 DRM_DEBUG_KMS("failed to attach phys object\n");
13668 } else {
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000013669 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
Matt Roper6beb8c232014-12-01 15:40:14 -080013670 }
13671
13672 if (ret == 0)
13673 i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
13674
13675 mutex_unlock(&dev->struct_mutex);
13676
13677 return ret;
13678}
13679
Matt Roper38f3ce32014-12-02 07:45:25 -080013680/**
13681 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13682 * @plane: drm plane to clean up for
13683 * @fb: old framebuffer that was on plane
13684 *
13685 * Cleans up a framebuffer that has just been removed from a plane.
13686 */
13687void
13688intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013689 struct drm_framebuffer *fb,
13690 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013691{
13692 struct drm_device *dev = plane->dev;
13693 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13694
13695 if (WARN_ON(!obj))
13696 return;
13697
13698 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
13699 !INTEL_INFO(dev)->cursor_needs_physical) {
13700 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000013701 intel_unpin_fb_obj(fb, old_state);
Matt Roper38f3ce32014-12-02 07:45:25 -080013702 mutex_unlock(&dev->struct_mutex);
13703 }
Matt Roper465c1202014-05-29 08:06:54 -070013704}
13705
Chandra Konduru6156a452015-04-27 13:48:39 -070013706int
13707skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13708{
13709 int max_scale;
13710 struct drm_device *dev;
13711 struct drm_i915_private *dev_priv;
13712 int crtc_clock, cdclk;
13713
13714 if (!intel_crtc || !crtc_state)
13715 return DRM_PLANE_HELPER_NO_SCALING;
13716
13717 dev = intel_crtc->base.dev;
13718 dev_priv = dev->dev_private;
13719 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
13720 cdclk = dev_priv->display.get_display_clock_speed(dev);
13721
13722 if (!crtc_clock || !cdclk)
13723 return DRM_PLANE_HELPER_NO_SCALING;
13724
13725 /*
13726 * skl max scale is lower of:
13727 * close to 3 but not 3, -1 is for that purpose
13728 * or
13729 * cdclk/crtc_clock
13730 */
13731 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13732
13733 return max_scale;
13734}
13735
Matt Roper465c1202014-05-29 08:06:54 -070013736static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013737intel_check_primary_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013738 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013739 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013740{
Matt Roper2b875c22014-12-01 15:40:13 -080013741 struct drm_crtc *crtc = state->base.crtc;
13742 struct drm_framebuffer *fb = state->base.fb;
Chandra Konduru6156a452015-04-27 13:48:39 -070013743 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013744 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13745 bool can_position = false;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013746
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013747 /* use scaler when colorkey is not required */
13748 if (INTEL_INFO(plane->dev)->gen >= 9 &&
Maarten Lankhorst818ed962015-06-15 12:33:54 +020013749 state->ckey.flags == I915_SET_COLORKEY_NONE) {
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013750 min_scale = 1;
13751 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
Sonika Jindald8106362015-04-10 14:37:28 +053013752 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070013753 }
Sonika Jindald8106362015-04-10 14:37:28 +053013754
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013755 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13756 &state->dst, &state->clip,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013757 min_scale, max_scale,
13758 can_position, true,
13759 &state->visible);
Matt Roper465c1202014-05-29 08:06:54 -070013760}
13761
Gustavo Padovan14af2932014-10-24 14:51:31 +010013762static void
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013763intel_commit_primary_plane(struct drm_plane *plane,
13764 struct intel_plane_state *state)
13765{
Matt Roper2b875c22014-12-01 15:40:13 -080013766 struct drm_crtc *crtc = state->base.crtc;
13767 struct drm_framebuffer *fb = state->base.fb;
13768 struct drm_device *dev = plane->dev;
Sonika Jindal48404c12014-08-22 14:06:04 +053013769 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperea2c67b2014-12-23 10:41:52 -080013770 struct intel_crtc *intel_crtc;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013771 struct drm_rect *src = &state->src;
Matt Ropercf4c7c12014-12-04 10:27:42 -080013772
Matt Roperea2c67b2014-12-23 10:41:52 -080013773 crtc = crtc ? crtc : plane->crtc;
13774 intel_crtc = to_intel_crtc(crtc);
13775
Matt Ropercf4c7c12014-12-04 10:27:42 -080013776 plane->fb = fb;
Matt Roper9dc806f2014-11-17 18:10:38 -080013777 crtc->x = src->x1 >> 16;
13778 crtc->y = src->y1 >> 16;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013779
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013780 if (!crtc->state->active)
Maarten Lankhorst302d19a2015-06-15 12:33:45 +020013781 return;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013782
Maarten Lankhorst302d19a2015-06-15 12:33:45 +020013783 if (state->visible)
13784 /* FIXME: kill this fastboot hack */
13785 intel_update_pipe_size(intel_crtc);
13786
13787 dev_priv->display.update_primary_plane(crtc, fb, crtc->x, crtc->y);
Matt Roper32b7eee2014-12-24 07:59:06 -080013788}
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013789
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013790static void
13791intel_disable_primary_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020013792 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013793{
13794 struct drm_device *dev = plane->dev;
13795 struct drm_i915_private *dev_priv = dev->dev_private;
13796
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013797 dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
13798}
13799
Matt Roper32b7eee2014-12-24 07:59:06 -080013800static void intel_begin_crtc_commit(struct drm_crtc *crtc)
13801{
13802 struct drm_device *dev = crtc->dev;
13803 struct drm_i915_private *dev_priv = dev->dev_private;
13804 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013805
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013806 if (!needs_modeset(crtc->state))
13807 intel_pre_plane_update(intel_crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013808
13809 if (intel_crtc->atomic.update_wm)
13810 intel_update_watermarks(crtc);
13811
13812 intel_runtime_pm_get(dev_priv);
Matt Roperc34c9ee2014-12-23 10:41:50 -080013813
13814 /* Perform vblank evasion around commit operation */
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013815 if (crtc->state->active)
Matt Roperc34c9ee2014-12-23 10:41:50 -080013816 intel_crtc->atomic.evade =
13817 intel_pipe_update_start(intel_crtc,
13818 &intel_crtc->atomic.start_vbl_count);
Maarten Lankhorst05832362015-06-15 12:33:48 +020013819
13820 if (!needs_modeset(crtc->state) && INTEL_INFO(dev)->gen >= 9)
13821 skl_detach_scalers(intel_crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013822}
13823
13824static void intel_finish_crtc_commit(struct drm_crtc *crtc)
13825{
13826 struct drm_device *dev = crtc->dev;
13827 struct drm_i915_private *dev_priv = dev->dev_private;
13828 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013829
Matt Roperc34c9ee2014-12-23 10:41:50 -080013830 if (intel_crtc->atomic.evade)
13831 intel_pipe_update_end(intel_crtc,
13832 intel_crtc->atomic.start_vbl_count);
13833
Matt Roper32b7eee2014-12-24 07:59:06 -080013834 intel_runtime_pm_put(dev_priv);
13835
Maarten Lankhorstac21b222015-06-15 12:33:49 +020013836 intel_post_plane_update(intel_crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013837}
13838
Matt Ropercf4c7c12014-12-04 10:27:42 -080013839/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013840 * intel_plane_destroy - destroy a plane
13841 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013842 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013843 * Common destruction function for all types of planes (primary, cursor,
13844 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013845 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013846void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013847{
13848 struct intel_plane *intel_plane = to_intel_plane(plane);
13849 drm_plane_cleanup(plane);
13850 kfree(intel_plane);
13851}
13852
Matt Roper65a3fea2015-01-21 16:35:42 -080013853const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013854 .update_plane = drm_atomic_helper_update_plane,
13855 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013856 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080013857 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080013858 .atomic_get_property = intel_plane_atomic_get_property,
13859 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013860 .atomic_duplicate_state = intel_plane_duplicate_state,
13861 .atomic_destroy_state = intel_plane_destroy_state,
13862
Matt Roper465c1202014-05-29 08:06:54 -070013863};
13864
13865static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13866 int pipe)
13867{
13868 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080013869 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070013870 const uint32_t *intel_primary_formats;
13871 int num_formats;
13872
13873 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13874 if (primary == NULL)
13875 return NULL;
13876
Matt Roper8e7d6882015-01-21 16:35:41 -080013877 state = intel_create_plane_state(&primary->base);
13878 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013879 kfree(primary);
13880 return NULL;
13881 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013882 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013883
Matt Roper465c1202014-05-29 08:06:54 -070013884 primary->can_scale = false;
13885 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013886 if (INTEL_INFO(dev)->gen >= 9) {
13887 primary->can_scale = true;
Chandra Konduruaf99ceda2015-05-11 14:35:47 -070013888 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013889 }
Matt Roper465c1202014-05-29 08:06:54 -070013890 primary->pipe = pipe;
13891 primary->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080013892 primary->check_plane = intel_check_primary_plane;
13893 primary->commit_plane = intel_commit_primary_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013894 primary->disable_plane = intel_disable_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070013895 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13896 primary->plane = !pipe;
13897
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013898 if (INTEL_INFO(dev)->gen >= 9) {
13899 intel_primary_formats = skl_primary_formats;
13900 num_formats = ARRAY_SIZE(skl_primary_formats);
13901 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010013902 intel_primary_formats = i965_primary_formats;
13903 num_formats = ARRAY_SIZE(i965_primary_formats);
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013904 } else {
13905 intel_primary_formats = i8xx_primary_formats;
13906 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Matt Roper465c1202014-05-29 08:06:54 -070013907 }
13908
13909 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013910 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070013911 intel_primary_formats, num_formats,
13912 DRM_PLANE_TYPE_PRIMARY);
Sonika Jindal48404c12014-08-22 14:06:04 +053013913
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013914 if (INTEL_INFO(dev)->gen >= 4)
13915 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053013916
Matt Roperea2c67b2014-12-23 10:41:52 -080013917 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13918
Matt Roper465c1202014-05-29 08:06:54 -070013919 return &primary->base;
13920}
13921
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013922void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13923{
13924 if (!dev->mode_config.rotation_property) {
13925 unsigned long flags = BIT(DRM_ROTATE_0) |
13926 BIT(DRM_ROTATE_180);
13927
13928 if (INTEL_INFO(dev)->gen >= 9)
13929 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13930
13931 dev->mode_config.rotation_property =
13932 drm_mode_create_rotation_property(dev, flags);
13933 }
13934 if (dev->mode_config.rotation_property)
13935 drm_object_attach_property(&plane->base.base,
13936 dev->mode_config.rotation_property,
13937 plane->base.state->rotation);
13938}
13939
Matt Roper3d7d6512014-06-10 08:28:13 -070013940static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030013941intel_check_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013942 struct intel_crtc_state *crtc_state,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013943 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070013944{
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013945 struct drm_crtc *crtc = crtc_state->base.crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080013946 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013947 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013948 unsigned stride;
13949 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013950
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013951 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13952 &state->dst, &state->clip,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013953 DRM_PLANE_HELPER_NO_SCALING,
13954 DRM_PLANE_HELPER_NO_SCALING,
13955 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013956 if (ret)
13957 return ret;
13958
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013959 /* if we want to turn off the cursor ignore width and height */
13960 if (!obj)
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013961 return 0;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013962
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013963 /* Check for which cursor types we support */
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013964 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013965 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13966 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013967 return -EINVAL;
13968 }
13969
Matt Roperea2c67b2014-12-23 10:41:52 -080013970 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13971 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013972 DRM_DEBUG_KMS("buffer is too small\n");
13973 return -ENOMEM;
13974 }
13975
Ville Syrjälä3a656b52015-03-09 21:08:37 +020013976 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013977 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013978 return -EINVAL;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013979 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013980
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013981 return 0;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013982}
13983
Matt Roperf4a2cf22014-12-01 15:40:12 -080013984static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013985intel_disable_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020013986 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013987{
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013988 intel_crtc_update_cursor(crtc, false);
13989}
13990
13991static void
Gustavo Padovan852e7872014-09-05 17:22:31 -030013992intel_commit_cursor_plane(struct drm_plane *plane,
13993 struct intel_plane_state *state)
13994{
Matt Roper2b875c22014-12-01 15:40:13 -080013995 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013996 struct drm_device *dev = plane->dev;
13997 struct intel_crtc *intel_crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080013998 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080013999 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070014000
Matt Roperea2c67b2014-12-23 10:41:52 -080014001 crtc = crtc ? crtc : plane->crtc;
14002 intel_crtc = to_intel_crtc(crtc);
Sonika Jindala919db92014-10-23 07:41:33 -070014003
Matt Roperea2c67b2014-12-23 10:41:52 -080014004 plane->fb = state->base.fb;
14005 crtc->cursor_x = state->base.crtc_x;
14006 crtc->cursor_y = state->base.crtc_y;
14007
Gustavo Padovana912f122014-12-01 15:40:10 -080014008 if (intel_crtc->cursor_bo == obj)
14009 goto update;
14010
Matt Roperf4a2cf22014-12-01 15:40:12 -080014011 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080014012 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080014013 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080014014 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080014015 else
Gustavo Padovana912f122014-12-01 15:40:10 -080014016 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080014017
Gustavo Padovana912f122014-12-01 15:40:10 -080014018 intel_crtc->cursor_addr = addr;
14019 intel_crtc->cursor_bo = obj;
Gustavo Padovana912f122014-12-01 15:40:10 -080014020
Maarten Lankhorst302d19a2015-06-15 12:33:45 +020014021update:
Maarten Lankhorsta5392052015-06-15 12:33:52 +020014022 if (crtc->state->active)
Gustavo Padovan852e7872014-09-05 17:22:31 -030014023 intel_crtc_update_cursor(crtc, state->visible);
Matt Roper3d7d6512014-06-10 08:28:13 -070014024}
Gustavo Padovan852e7872014-09-05 17:22:31 -030014025
Matt Roper3d7d6512014-06-10 08:28:13 -070014026static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14027 int pipe)
14028{
14029 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080014030 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070014031
14032 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
14033 if (cursor == NULL)
14034 return NULL;
14035
Matt Roper8e7d6882015-01-21 16:35:41 -080014036 state = intel_create_plane_state(&cursor->base);
14037 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014038 kfree(cursor);
14039 return NULL;
14040 }
Matt Roper8e7d6882015-01-21 16:35:41 -080014041 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014042
Matt Roper3d7d6512014-06-10 08:28:13 -070014043 cursor->can_scale = false;
14044 cursor->max_downscale = 1;
14045 cursor->pipe = pipe;
14046 cursor->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080014047 cursor->check_plane = intel_check_cursor_plane;
14048 cursor->commit_plane = intel_commit_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014049 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070014050
14051 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080014052 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070014053 intel_cursor_formats,
14054 ARRAY_SIZE(intel_cursor_formats),
14055 DRM_PLANE_TYPE_CURSOR);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014056
14057 if (INTEL_INFO(dev)->gen >= 4) {
14058 if (!dev->mode_config.rotation_property)
14059 dev->mode_config.rotation_property =
14060 drm_mode_create_rotation_property(dev,
14061 BIT(DRM_ROTATE_0) |
14062 BIT(DRM_ROTATE_180));
14063 if (dev->mode_config.rotation_property)
14064 drm_object_attach_property(&cursor->base.base,
14065 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080014066 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014067 }
14068
Chandra Konduruaf99ceda2015-05-11 14:35:47 -070014069 if (INTEL_INFO(dev)->gen >=9)
14070 state->scaler_id = -1;
14071
Matt Roperea2c67b2014-12-23 10:41:52 -080014072 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14073
Matt Roper3d7d6512014-06-10 08:28:13 -070014074 return &cursor->base;
14075}
14076
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014077static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14078 struct intel_crtc_state *crtc_state)
14079{
14080 int i;
14081 struct intel_scaler *intel_scaler;
14082 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14083
14084 for (i = 0; i < intel_crtc->num_scalers; i++) {
14085 intel_scaler = &scaler_state->scalers[i];
14086 intel_scaler->in_use = 0;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014087 intel_scaler->mode = PS_SCALER_MODE_DYN;
14088 }
14089
14090 scaler_state->scaler_id = -1;
14091}
14092
Hannes Ederb358d0a2008-12-18 21:18:47 +010014093static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080014094{
Jani Nikulafbee40d2014-03-31 14:27:18 +030014095 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080014096 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014097 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014098 struct drm_plane *primary = NULL;
14099 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014100 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014101
Daniel Vetter955382f2013-09-19 14:05:45 +020014102 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080014103 if (intel_crtc == NULL)
14104 return;
14105
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014106 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14107 if (!crtc_state)
14108 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030014109 intel_crtc->config = crtc_state;
14110 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080014111 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014112
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014113 /* initialize shared scalers */
14114 if (INTEL_INFO(dev)->gen >= 9) {
14115 if (pipe == PIPE_C)
14116 intel_crtc->num_scalers = 1;
14117 else
14118 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14119
14120 skl_init_scalers(dev, intel_crtc, crtc_state);
14121 }
14122
Matt Roper465c1202014-05-29 08:06:54 -070014123 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014124 if (!primary)
14125 goto fail;
14126
14127 cursor = intel_cursor_plane_create(dev, pipe);
14128 if (!cursor)
14129 goto fail;
14130
Matt Roper465c1202014-05-29 08:06:54 -070014131 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Matt Roper3d7d6512014-06-10 08:28:13 -070014132 cursor, &intel_crtc_funcs);
14133 if (ret)
14134 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080014135
14136 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080014137 for (i = 0; i < 256; i++) {
14138 intel_crtc->lut_r[i] = i;
14139 intel_crtc->lut_g[i] = i;
14140 intel_crtc->lut_b[i] = i;
14141 }
14142
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014143 /*
14144 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020014145 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014146 */
Jesse Barnes80824002009-09-10 15:28:06 -070014147 intel_crtc->pipe = pipe;
14148 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010014149 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080014150 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010014151 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070014152 }
14153
Chris Wilson4b0e3332014-05-30 16:35:26 +030014154 intel_crtc->cursor_base = ~0;
14155 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030014156 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030014157
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080014158 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14159 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14160 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14161 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14162
Jesse Barnes79e53942008-11-07 14:24:08 -080014163 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020014164
14165 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014166 return;
14167
14168fail:
14169 if (primary)
14170 drm_plane_cleanup(primary);
14171 if (cursor)
14172 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014173 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014174 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080014175}
14176
Jesse Barnes752aa882013-10-31 18:55:49 +020014177enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14178{
14179 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014180 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020014181
Rob Clark51fd3712013-11-19 12:10:12 -050014182 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020014183
Ville Syrjäläd3babd32014-11-07 11:16:01 +020014184 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020014185 return INVALID_PIPE;
14186
14187 return to_intel_crtc(encoder->crtc)->pipe;
14188}
14189
Carl Worth08d7b3d2009-04-29 14:43:54 -070014190int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000014191 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070014192{
Carl Worth08d7b3d2009-04-29 14:43:54 -070014193 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040014194 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020014195 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014196
Rob Clark7707e652014-07-17 23:30:04 -040014197 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070014198
Rob Clark7707e652014-07-17 23:30:04 -040014199 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070014200 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030014201 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014202 }
14203
Rob Clark7707e652014-07-17 23:30:04 -040014204 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020014205 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014206
Daniel Vetterc05422d2009-08-11 16:05:30 +020014207 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014208}
14209
Daniel Vetter66a92782012-07-12 20:08:18 +020014210static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080014211{
Daniel Vetter66a92782012-07-12 20:08:18 +020014212 struct drm_device *dev = encoder->base.dev;
14213 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080014214 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080014215 int entry = 0;
14216
Damien Lespiaub2784e12014-08-05 11:29:37 +010014217 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020014218 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020014219 index_mask |= (1 << entry);
14220
Jesse Barnes79e53942008-11-07 14:24:08 -080014221 entry++;
14222 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010014223
Jesse Barnes79e53942008-11-07 14:24:08 -080014224 return index_mask;
14225}
14226
Chris Wilson4d302442010-12-14 19:21:29 +000014227static bool has_edp_a(struct drm_device *dev)
14228{
14229 struct drm_i915_private *dev_priv = dev->dev_private;
14230
14231 if (!IS_MOBILE(dev))
14232 return false;
14233
14234 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14235 return false;
14236
Damien Lespiaue3589902014-02-07 19:12:50 +000014237 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000014238 return false;
14239
14240 return true;
14241}
14242
Jesse Barnes84b4e042014-06-25 08:24:29 -070014243static bool intel_crt_present(struct drm_device *dev)
14244{
14245 struct drm_i915_private *dev_priv = dev->dev_private;
14246
Damien Lespiau884497e2013-12-03 13:56:23 +000014247 if (INTEL_INFO(dev)->gen >= 9)
14248 return false;
14249
Damien Lespiaucf404ce2014-10-01 20:04:15 +010014250 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014251 return false;
14252
14253 if (IS_CHERRYVIEW(dev))
14254 return false;
14255
14256 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
14257 return false;
14258
14259 return true;
14260}
14261
Jesse Barnes79e53942008-11-07 14:24:08 -080014262static void intel_setup_outputs(struct drm_device *dev)
14263{
Eric Anholt725e30a2009-01-22 13:01:02 -080014264 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010014265 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014266 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080014267
Daniel Vetterc9093352013-06-06 22:22:47 +020014268 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014269
Jesse Barnes84b4e042014-06-25 08:24:29 -070014270 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020014271 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014272
Vandana Kannanc776eb22014-08-19 12:05:01 +053014273 if (IS_BROXTON(dev)) {
14274 /*
14275 * FIXME: Broxton doesn't support port detection via the
14276 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14277 * detect the ports.
14278 */
14279 intel_ddi_init(dev, PORT_A);
14280 intel_ddi_init(dev, PORT_B);
14281 intel_ddi_init(dev, PORT_C);
14282 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014283 int found;
14284
Jesse Barnesde31fac2015-03-06 15:53:32 -080014285 /*
14286 * Haswell uses DDI functions to detect digital outputs.
14287 * On SKL pre-D0 the strap isn't connected, so we assume
14288 * it's there.
14289 */
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014290 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014291 /* WaIgnoreDDIAStrap: skl */
14292 if (found ||
14293 (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014294 intel_ddi_init(dev, PORT_A);
14295
14296 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14297 * register */
14298 found = I915_READ(SFUSE_STRAP);
14299
14300 if (found & SFUSE_STRAP_DDIB_DETECTED)
14301 intel_ddi_init(dev, PORT_B);
14302 if (found & SFUSE_STRAP_DDIC_DETECTED)
14303 intel_ddi_init(dev, PORT_C);
14304 if (found & SFUSE_STRAP_DDID_DETECTED)
14305 intel_ddi_init(dev, PORT_D);
14306 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014307 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020014308 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014309
14310 if (has_edp_a(dev))
14311 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014312
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014313 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014314 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010014315 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014316 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014317 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014318 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014319 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014320 }
14321
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014322 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014323 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014324
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014325 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014326 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014327
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014328 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014329 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014330
Daniel Vetter270b3042012-10-27 15:52:05 +020014331 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014332 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070014333 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014334 /*
14335 * The DP_DETECTED bit is the latched state of the DDC
14336 * SDA pin at boot. However since eDP doesn't require DDC
14337 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14338 * eDP ports may have been muxed to an alternate function.
14339 * Thus we can't rely on the DP_DETECTED bit alone to detect
14340 * eDP ports. Consult the VBT as well as DP_DETECTED to
14341 * detect eDP ports.
14342 */
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014343 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
14344 !intel_dp_is_edp(dev, PORT_B))
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014345 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
14346 PORT_B);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014347 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
14348 intel_dp_is_edp(dev, PORT_B))
14349 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014350
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014351 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
14352 !intel_dp_is_edp(dev, PORT_C))
Jesse Barnes6f6005a2013-08-09 09:34:35 -070014353 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
14354 PORT_C);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014355 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
14356 intel_dp_is_edp(dev, PORT_C))
14357 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014358
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014359 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014360 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014361 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
14362 PORT_D);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014363 /* eDP not supported on port D, so don't check VBT */
14364 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
14365 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014366 }
14367
Jani Nikula3cfca972013-08-27 15:12:26 +030014368 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +080014369 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014370 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014371
Paulo Zanonie2debe92013-02-18 19:00:27 -030014372 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014373 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014374 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014375 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
14376 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014377 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014378 }
Ma Ling27185ae2009-08-24 13:50:23 +080014379
Imre Deake7281ea2013-05-08 13:14:08 +030014380 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014381 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014382 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014383
14384 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014385
Paulo Zanonie2debe92013-02-18 19:00:27 -030014386 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014387 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014388 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014389 }
Ma Ling27185ae2009-08-24 13:50:23 +080014390
Paulo Zanonie2debe92013-02-18 19:00:27 -030014391 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014392
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014393 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
14394 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014395 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014396 }
Imre Deake7281ea2013-05-08 13:14:08 +030014397 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014398 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014399 }
Ma Ling27185ae2009-08-24 13:50:23 +080014400
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014401 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014402 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014403 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014404 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014405 intel_dvo_init(dev);
14406
Zhenyu Wang103a1962009-11-27 11:44:36 +080014407 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014408 intel_tv_init(dev);
14409
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014410 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014411
Damien Lespiaub2784e12014-08-05 11:29:37 +010014412 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014413 encoder->base.possible_crtcs = encoder->crtc_mask;
14414 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014415 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014416 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014417
Paulo Zanonidde86e22012-12-01 12:04:25 -020014418 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014419
14420 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014421}
14422
14423static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14424{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014425 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014426 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014427
Daniel Vetteref2d6332014-02-10 18:00:38 +010014428 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014429 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014430 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014431 drm_gem_object_unreference(&intel_fb->obj->base);
14432 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014433 kfree(intel_fb);
14434}
14435
14436static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014437 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014438 unsigned int *handle)
14439{
14440 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014441 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014442
Chris Wilson05394f32010-11-08 19:18:58 +000014443 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014444}
14445
14446static const struct drm_framebuffer_funcs intel_fb_funcs = {
14447 .destroy = intel_user_framebuffer_destroy,
14448 .create_handle = intel_user_framebuffer_create_handle,
14449};
14450
Damien Lespiaub3218032015-02-27 11:15:18 +000014451static
14452u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14453 uint32_t pixel_format)
14454{
14455 u32 gen = INTEL_INFO(dev)->gen;
14456
14457 if (gen >= 9) {
14458 /* "The stride in bytes must not exceed the of the size of 8K
14459 * pixels and 32K bytes."
14460 */
14461 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
14462 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
14463 return 32*1024;
14464 } else if (gen >= 4) {
14465 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14466 return 16*1024;
14467 else
14468 return 32*1024;
14469 } else if (gen >= 3) {
14470 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14471 return 8*1024;
14472 else
14473 return 16*1024;
14474 } else {
14475 /* XXX DSPC is limited to 4k tiled */
14476 return 8*1024;
14477 }
14478}
14479
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014480static int intel_framebuffer_init(struct drm_device *dev,
14481 struct intel_framebuffer *intel_fb,
14482 struct drm_mode_fb_cmd2 *mode_cmd,
14483 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014484{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014485 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014486 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014487 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014488
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014489 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14490
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014491 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14492 /* Enforce that fb modifier and tiling mode match, but only for
14493 * X-tiled. This is needed for FBC. */
14494 if (!!(obj->tiling_mode == I915_TILING_X) !=
14495 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14496 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14497 return -EINVAL;
14498 }
14499 } else {
14500 if (obj->tiling_mode == I915_TILING_X)
14501 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14502 else if (obj->tiling_mode == I915_TILING_Y) {
14503 DRM_DEBUG("No Y tiling for legacy addfb\n");
14504 return -EINVAL;
14505 }
14506 }
14507
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014508 /* Passed in modifier sanity checking. */
14509 switch (mode_cmd->modifier[0]) {
14510 case I915_FORMAT_MOD_Y_TILED:
14511 case I915_FORMAT_MOD_Yf_TILED:
14512 if (INTEL_INFO(dev)->gen < 9) {
14513 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14514 mode_cmd->modifier[0]);
14515 return -EINVAL;
14516 }
14517 case DRM_FORMAT_MOD_NONE:
14518 case I915_FORMAT_MOD_X_TILED:
14519 break;
14520 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014521 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14522 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014523 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014524 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014525
Damien Lespiaub3218032015-02-27 11:15:18 +000014526 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
14527 mode_cmd->pixel_format);
14528 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14529 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14530 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014531 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014532 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014533
Damien Lespiaub3218032015-02-27 11:15:18 +000014534 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14535 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014536 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014537 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14538 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014539 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014540 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014541 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014542 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014543
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014544 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014545 mode_cmd->pitches[0] != obj->stride) {
14546 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14547 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014548 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014549 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014550
Ville Syrjälä57779d02012-10-31 17:50:14 +020014551 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014552 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014553 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014554 case DRM_FORMAT_RGB565:
14555 case DRM_FORMAT_XRGB8888:
14556 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014557 break;
14558 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014559 if (INTEL_INFO(dev)->gen > 3) {
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));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014562 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014563 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014564 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014565 case DRM_FORMAT_ABGR8888:
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014566 if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
14567 DRM_DEBUG("unsupported pixel format: %s\n",
14568 drm_get_format_name(mode_cmd->pixel_format));
14569 return -EINVAL;
14570 }
14571 break;
14572 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014573 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014574 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014575 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014576 DRM_DEBUG("unsupported pixel format: %s\n",
14577 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014578 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014579 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014580 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014581 case DRM_FORMAT_ABGR2101010:
14582 if (!IS_VALLEYVIEW(dev)) {
14583 DRM_DEBUG("unsupported pixel format: %s\n",
14584 drm_get_format_name(mode_cmd->pixel_format));
14585 return -EINVAL;
14586 }
14587 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014588 case DRM_FORMAT_YUYV:
14589 case DRM_FORMAT_UYVY:
14590 case DRM_FORMAT_YVYU:
14591 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014592 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014593 DRM_DEBUG("unsupported pixel format: %s\n",
14594 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014595 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014596 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014597 break;
14598 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014599 DRM_DEBUG("unsupported pixel format: %s\n",
14600 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014601 return -EINVAL;
14602 }
14603
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014604 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14605 if (mode_cmd->offsets[0] != 0)
14606 return -EINVAL;
14607
Damien Lespiauec2c9812015-01-20 12:51:45 +000014608 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014609 mode_cmd->pixel_format,
14610 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014611 /* FIXME drm helper for size checks (especially planar formats)? */
14612 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14613 return -EINVAL;
14614
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014615 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14616 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020014617 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014618
Jesse Barnes79e53942008-11-07 14:24:08 -080014619 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14620 if (ret) {
14621 DRM_ERROR("framebuffer init failed %d\n", ret);
14622 return ret;
14623 }
14624
Jesse Barnes79e53942008-11-07 14:24:08 -080014625 return 0;
14626}
14627
Jesse Barnes79e53942008-11-07 14:24:08 -080014628static struct drm_framebuffer *
14629intel_user_framebuffer_create(struct drm_device *dev,
14630 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014631 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014632{
Chris Wilson05394f32010-11-08 19:18:58 +000014633 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014634
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014635 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
14636 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014637 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014638 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014639
Chris Wilsond2dff872011-04-19 08:36:26 +010014640 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080014641}
14642
Daniel Vetter4520f532013-10-09 09:18:51 +020014643#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020014644static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014645{
14646}
14647#endif
14648
Jesse Barnes79e53942008-11-07 14:24:08 -080014649static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014650 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014651 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014652 .atomic_check = intel_atomic_check,
14653 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020014654 .atomic_state_alloc = intel_atomic_state_alloc,
14655 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080014656};
14657
Jesse Barnese70236a2009-09-21 10:42:27 -070014658/* Set up chip specific display functions */
14659static void intel_init_display(struct drm_device *dev)
14660{
14661 struct drm_i915_private *dev_priv = dev->dev_private;
14662
Daniel Vetteree9300b2013-06-03 22:40:22 +020014663 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14664 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030014665 else if (IS_CHERRYVIEW(dev))
14666 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020014667 else if (IS_VALLEYVIEW(dev))
14668 dev_priv->display.find_dpll = vlv_find_best_dpll;
14669 else if (IS_PINEVIEW(dev))
14670 dev_priv->display.find_dpll = pnv_find_best_dpll;
14671 else
14672 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14673
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014674 if (INTEL_INFO(dev)->gen >= 9) {
14675 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014676 dev_priv->display.get_initial_plane_config =
14677 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014678 dev_priv->display.crtc_compute_clock =
14679 haswell_crtc_compute_clock;
14680 dev_priv->display.crtc_enable = haswell_crtc_enable;
14681 dev_priv->display.crtc_disable = haswell_crtc_disable;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014682 dev_priv->display.update_primary_plane =
14683 skylake_update_primary_plane;
14684 } else if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014685 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014686 dev_priv->display.get_initial_plane_config =
14687 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014688 dev_priv->display.crtc_compute_clock =
14689 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014690 dev_priv->display.crtc_enable = haswell_crtc_enable;
14691 dev_priv->display.crtc_disable = haswell_crtc_disable;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014692 dev_priv->display.update_primary_plane =
14693 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030014694 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014695 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014696 dev_priv->display.get_initial_plane_config =
14697 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014698 dev_priv->display.crtc_compute_clock =
14699 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014700 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14701 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014702 dev_priv->display.update_primary_plane =
14703 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014704 } else if (IS_VALLEYVIEW(dev)) {
14705 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014706 dev_priv->display.get_initial_plane_config =
14707 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014708 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014709 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14710 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014711 dev_priv->display.update_primary_plane =
14712 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014713 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014714 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014715 dev_priv->display.get_initial_plane_config =
14716 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014717 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014718 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14719 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014720 dev_priv->display.update_primary_plane =
14721 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014722 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014723
Jesse Barnese70236a2009-09-21 10:42:27 -070014724 /* Returns the core display clock speed */
Ville Syrjälä1652d192015-03-31 14:12:01 +030014725 if (IS_SKYLAKE(dev))
14726 dev_priv->display.get_display_clock_speed =
14727 skylake_get_display_clock_speed;
14728 else if (IS_BROADWELL(dev))
14729 dev_priv->display.get_display_clock_speed =
14730 broadwell_get_display_clock_speed;
14731 else if (IS_HASWELL(dev))
14732 dev_priv->display.get_display_clock_speed =
14733 haswell_get_display_clock_speed;
14734 else if (IS_VALLEYVIEW(dev))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070014735 dev_priv->display.get_display_clock_speed =
14736 valleyview_get_display_clock_speed;
Ville Syrjäläb37a6432015-03-31 14:11:54 +030014737 else if (IS_GEN5(dev))
14738 dev_priv->display.get_display_clock_speed =
14739 ilk_get_display_clock_speed;
Ville Syrjäläa7c66cd2015-03-31 14:11:56 +030014740 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
Ville Syrjälä34edce22015-05-22 11:22:33 +030014741 IS_GEN6(dev) || IS_IVYBRIDGE(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014742 dev_priv->display.get_display_clock_speed =
14743 i945_get_display_clock_speed;
Ville Syrjälä34edce22015-05-22 11:22:33 +030014744 else if (IS_GM45(dev))
14745 dev_priv->display.get_display_clock_speed =
14746 gm45_get_display_clock_speed;
14747 else if (IS_CRESTLINE(dev))
14748 dev_priv->display.get_display_clock_speed =
14749 i965gm_get_display_clock_speed;
14750 else if (IS_PINEVIEW(dev))
14751 dev_priv->display.get_display_clock_speed =
14752 pnv_get_display_clock_speed;
14753 else if (IS_G33(dev) || IS_G4X(dev))
14754 dev_priv->display.get_display_clock_speed =
14755 g33_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014756 else if (IS_I915G(dev))
14757 dev_priv->display.get_display_clock_speed =
14758 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014759 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014760 dev_priv->display.get_display_clock_speed =
14761 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014762 else if (IS_PINEVIEW(dev))
14763 dev_priv->display.get_display_clock_speed =
14764 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014765 else if (IS_I915GM(dev))
14766 dev_priv->display.get_display_clock_speed =
14767 i915gm_get_display_clock_speed;
14768 else if (IS_I865G(dev))
14769 dev_priv->display.get_display_clock_speed =
14770 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020014771 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014772 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030014773 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014774 else { /* 830 */
14775 WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070014776 dev_priv->display.get_display_clock_speed =
14777 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014778 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014779
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014780 if (IS_GEN5(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014781 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014782 } else if (IS_GEN6(dev)) {
14783 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014784 } else if (IS_IVYBRIDGE(dev)) {
14785 /* FIXME: detect B0+ stepping and use auto training */
14786 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Paulo Zanoni059b2fe2014-09-02 16:53:57 -030014787 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014788 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030014789 if (IS_BROADWELL(dev))
14790 dev_priv->display.modeset_global_resources =
14791 broadwell_modeset_global_resources;
Jesse Barnes30a970c2013-11-04 13:48:12 -080014792 } else if (IS_VALLEYVIEW(dev)) {
14793 dev_priv->display.modeset_global_resources =
14794 valleyview_modeset_global_resources;
Vandana Kannanf8437dd12014-11-24 13:37:39 +053014795 } else if (IS_BROXTON(dev)) {
14796 dev_priv->display.modeset_global_resources =
14797 broxton_modeset_global_resources;
Jesse Barnese70236a2009-09-21 10:42:27 -070014798 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014799
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014800 switch (INTEL_INFO(dev)->gen) {
14801 case 2:
14802 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14803 break;
14804
14805 case 3:
14806 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14807 break;
14808
14809 case 4:
14810 case 5:
14811 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14812 break;
14813
14814 case 6:
14815 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14816 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014817 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070014818 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014819 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14820 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000014821 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000014822 /* Drop through - unsupported since execlist only. */
14823 default:
14824 /* Default just returns -ENODEV to indicate unsupported */
14825 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014826 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020014827
14828 intel_panel_init_backlight_funcs(dev);
Ville Syrjäläe39b9992014-09-04 14:53:14 +030014829
14830 mutex_init(&dev_priv->pps_mutex);
Jesse Barnese70236a2009-09-21 10:42:27 -070014831}
14832
Jesse Barnesb690e962010-07-19 13:53:12 -070014833/*
14834 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14835 * resume, or other times. This quirk makes sure that's the case for
14836 * affected systems.
14837 */
Akshay Joshi0206e352011-08-16 15:34:10 -040014838static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070014839{
14840 struct drm_i915_private *dev_priv = dev->dev_private;
14841
14842 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014843 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014844}
14845
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014846static void quirk_pipeb_force(struct drm_device *dev)
14847{
14848 struct drm_i915_private *dev_priv = dev->dev_private;
14849
14850 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14851 DRM_INFO("applying pipe b force quirk\n");
14852}
14853
Keith Packard435793d2011-07-12 14:56:22 -070014854/*
14855 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14856 */
14857static void quirk_ssc_force_disable(struct drm_device *dev)
14858{
14859 struct drm_i915_private *dev_priv = dev->dev_private;
14860 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014861 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070014862}
14863
Carsten Emde4dca20e2012-03-15 15:56:26 +010014864/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010014865 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14866 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010014867 */
14868static void quirk_invert_brightness(struct drm_device *dev)
14869{
14870 struct drm_i915_private *dev_priv = dev->dev_private;
14871 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014872 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014873}
14874
Scot Doyle9c72cc62014-07-03 23:27:50 +000014875/* Some VBT's incorrectly indicate no backlight is present */
14876static void quirk_backlight_present(struct drm_device *dev)
14877{
14878 struct drm_i915_private *dev_priv = dev->dev_private;
14879 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14880 DRM_INFO("applying backlight present quirk\n");
14881}
14882
Jesse Barnesb690e962010-07-19 13:53:12 -070014883struct intel_quirk {
14884 int device;
14885 int subsystem_vendor;
14886 int subsystem_device;
14887 void (*hook)(struct drm_device *dev);
14888};
14889
Egbert Eich5f85f172012-10-14 15:46:38 +020014890/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14891struct intel_dmi_quirk {
14892 void (*hook)(struct drm_device *dev);
14893 const struct dmi_system_id (*dmi_id_list)[];
14894};
14895
14896static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14897{
14898 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14899 return 1;
14900}
14901
14902static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14903 {
14904 .dmi_id_list = &(const struct dmi_system_id[]) {
14905 {
14906 .callback = intel_dmi_reverse_brightness,
14907 .ident = "NCR Corporation",
14908 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14909 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14910 },
14911 },
14912 { } /* terminating entry */
14913 },
14914 .hook = quirk_invert_brightness,
14915 },
14916};
14917
Ben Widawskyc43b5632012-04-16 14:07:40 -070014918static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070014919 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14920 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14921
Jesse Barnesb690e962010-07-19 13:53:12 -070014922 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14923 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14924
Ville Syrjälä5f080c02014-08-15 01:22:06 +030014925 /* 830 needs to leave pipe A & dpll A up */
14926 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14927
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014928 /* 830 needs to leave pipe B & dpll B up */
14929 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14930
Keith Packard435793d2011-07-12 14:56:22 -070014931 /* Lenovo U160 cannot use SSC on LVDS */
14932 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020014933
14934 /* Sony Vaio Y cannot use SSC on LVDS */
14935 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010014936
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010014937 /* Acer Aspire 5734Z must invert backlight brightness */
14938 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14939
14940 /* Acer/eMachines G725 */
14941 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14942
14943 /* Acer/eMachines e725 */
14944 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14945
14946 /* Acer/Packard Bell NCL20 */
14947 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14948
14949 /* Acer Aspire 4736Z */
14950 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020014951
14952 /* Acer Aspire 5336 */
14953 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000014954
14955 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14956 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000014957
Scot Doyledfb3d47b2014-08-21 16:08:02 +000014958 /* Acer C720 Chromebook (Core i3 4005U) */
14959 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14960
jens steinb2a96012014-10-28 20:25:53 +010014961 /* Apple Macbook 2,1 (Core 2 T7400) */
14962 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14963
Scot Doyled4967d82014-07-03 23:27:52 +000014964 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14965 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000014966
14967 /* HP Chromebook 14 (Celeron 2955U) */
14968 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020014969
14970 /* Dell Chromebook 11 */
14971 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070014972};
14973
14974static void intel_init_quirks(struct drm_device *dev)
14975{
14976 struct pci_dev *d = dev->pdev;
14977 int i;
14978
14979 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14980 struct intel_quirk *q = &intel_quirks[i];
14981
14982 if (d->device == q->device &&
14983 (d->subsystem_vendor == q->subsystem_vendor ||
14984 q->subsystem_vendor == PCI_ANY_ID) &&
14985 (d->subsystem_device == q->subsystem_device ||
14986 q->subsystem_device == PCI_ANY_ID))
14987 q->hook(dev);
14988 }
Egbert Eich5f85f172012-10-14 15:46:38 +020014989 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14990 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14991 intel_dmi_quirks[i].hook(dev);
14992 }
Jesse Barnesb690e962010-07-19 13:53:12 -070014993}
14994
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014995/* Disable the VGA plane that we never use */
14996static void i915_disable_vga(struct drm_device *dev)
14997{
14998 struct drm_i915_private *dev_priv = dev->dev_private;
14999 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020015000 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015001
Ville Syrjälä2b37c612014-01-22 21:32:38 +020015002 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015003 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070015004 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015005 sr1 = inb(VGA_SR_DATA);
15006 outb(sr1 | 1<<5, VGA_SR_DATA);
15007 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15008 udelay(300);
15009
Ville Syrjälä01f5a622014-12-16 18:38:37 +020015010 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015011 POSTING_READ(vga_reg);
15012}
15013
Daniel Vetterf8175862012-04-10 15:50:11 +020015014void intel_modeset_init_hw(struct drm_device *dev)
15015{
Ville Syrjäläb6283052015-06-03 15:45:07 +030015016 intel_update_cdclk(dev);
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030015017 intel_prepare_ddi(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020015018 intel_init_clock_gating(dev);
Daniel Vetter8090c6b2012-06-24 16:42:32 +020015019 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020015020}
15021
Jesse Barnes79e53942008-11-07 14:24:08 -080015022void intel_modeset_init(struct drm_device *dev)
15023{
Jesse Barnes652c3932009-08-17 13:31:43 -070015024 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000015025 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015026 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080015027 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080015028
15029 drm_mode_config_init(dev);
15030
15031 dev->mode_config.min_width = 0;
15032 dev->mode_config.min_height = 0;
15033
Dave Airlie019d96c2011-09-29 16:20:42 +010015034 dev->mode_config.preferred_depth = 24;
15035 dev->mode_config.prefer_shadow = 1;
15036
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000015037 dev->mode_config.allow_fb_modifiers = true;
15038
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020015039 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080015040
Jesse Barnesb690e962010-07-19 13:53:12 -070015041 intel_init_quirks(dev);
15042
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030015043 intel_init_pm(dev);
15044
Ben Widawskye3c74752013-04-05 13:12:39 -070015045 if (INTEL_INFO(dev)->num_pipes == 0)
15046 return;
15047
Jesse Barnese70236a2009-09-21 10:42:27 -070015048 intel_init_display(dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +020015049 intel_init_audio(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070015050
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015051 if (IS_GEN2(dev)) {
15052 dev->mode_config.max_width = 2048;
15053 dev->mode_config.max_height = 2048;
15054 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070015055 dev->mode_config.max_width = 4096;
15056 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080015057 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015058 dev->mode_config.max_width = 8192;
15059 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080015060 }
Damien Lespiau068be562014-03-28 14:17:49 +000015061
Ville Syrjälädc41c152014-08-13 11:57:05 +030015062 if (IS_845G(dev) || IS_I865G(dev)) {
15063 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15064 dev->mode_config.cursor_height = 1023;
15065 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000015066 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15067 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15068 } else {
15069 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15070 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15071 }
15072
Ben Widawsky5d4545a2013-01-17 12:45:15 -080015073 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080015074
Zhao Yakui28c97732009-10-09 11:39:41 +080015075 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015076 INTEL_INFO(dev)->num_pipes,
15077 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080015078
Damien Lespiau055e3932014-08-18 13:49:10 +010015079 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015080 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000015081 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000015082 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015083 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030015084 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000015085 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015086 }
Jesse Barnes79e53942008-11-07 14:24:08 -080015087 }
15088
Jesse Barnesf42bb702013-12-16 16:34:23 -080015089 intel_init_dpio(dev);
15090
Daniel Vettere72f9fb2013-06-05 13:34:06 +020015091 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010015092
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015093 /* Just disable it once at startup */
15094 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015095 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000015096
15097 /* Just in case the BIOS is doing something questionable. */
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020015098 intel_fbc_disable(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080015099
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015100 drm_modeset_lock_all(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080015101 intel_modeset_setup_hw_state(dev, false);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015102 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015103
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015104 for_each_intel_crtc(dev, crtc) {
Jesse Barnes46f297f2014-03-07 08:57:48 -080015105 if (!crtc->active)
15106 continue;
15107
Jesse Barnes46f297f2014-03-07 08:57:48 -080015108 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080015109 * Note that reserving the BIOS fb up front prevents us
15110 * from stuffing other stolen allocations like the ring
15111 * on top. This prevents some ugliness at boot time, and
15112 * can even allow for smooth boot transitions if the BIOS
15113 * fb is large enough for the active pipe configuration.
15114 */
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015115 if (dev_priv->display.get_initial_plane_config) {
15116 dev_priv->display.get_initial_plane_config(crtc,
Jesse Barnes46f297f2014-03-07 08:57:48 -080015117 &crtc->plane_config);
15118 /*
15119 * If the fb is shared between multiple heads, we'll
15120 * just get the first one.
15121 */
Daniel Vetterf6936e22015-03-26 12:17:05 +010015122 intel_find_initial_plane_obj(crtc, &crtc->plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015123 }
Jesse Barnes46f297f2014-03-07 08:57:48 -080015124 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010015125}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015126
Daniel Vetter7fad7982012-07-04 17:51:47 +020015127static void intel_enable_pipe_a(struct drm_device *dev)
15128{
15129 struct intel_connector *connector;
15130 struct drm_connector *crt = NULL;
15131 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015132 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020015133
15134 /* We can't just switch on the pipe A, we need to set things up with a
15135 * proper mode and output configuration. As a gross hack, enable pipe A
15136 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015137 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020015138 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15139 crt = &connector->base;
15140 break;
15141 }
15142 }
15143
15144 if (!crt)
15145 return;
15146
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015147 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020015148 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020015149}
15150
Daniel Vetterfa555832012-10-10 23:14:00 +020015151static bool
15152intel_check_plane_mapping(struct intel_crtc *crtc)
15153{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015154 struct drm_device *dev = crtc->base.dev;
15155 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020015156 u32 reg, val;
15157
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015158 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020015159 return true;
15160
15161 reg = DSPCNTR(!crtc->plane);
15162 val = I915_READ(reg);
15163
15164 if ((val & DISPLAY_PLANE_ENABLE) &&
15165 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15166 return false;
15167
15168 return true;
15169}
15170
Daniel Vetter24929352012-07-02 20:28:59 +020015171static void intel_sanitize_crtc(struct intel_crtc *crtc)
15172{
15173 struct drm_device *dev = crtc->base.dev;
15174 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015175 struct intel_encoder *encoder;
Daniel Vetterfa555832012-10-10 23:14:00 +020015176 u32 reg;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015177 bool enable;
Daniel Vetter24929352012-07-02 20:28:59 +020015178
Daniel Vetter24929352012-07-02 20:28:59 +020015179 /* Clear any frame start delays used for debugging left by the BIOS */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015180 reg = PIPECONF(crtc->config->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015181 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15182
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015183 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010015184 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015185 if (crtc->active) {
15186 update_scanline_offset(crtc);
Daniel Vetter96256042015-02-13 21:03:42 +010015187 drm_crtc_vblank_on(&crtc->base);
15188 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015189
Daniel Vetter24929352012-07-02 20:28:59 +020015190 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020015191 * disable the crtc (and hence change the state) if it is wrong. Note
15192 * that gen4+ has a fixed plane -> pipe mapping. */
15193 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020015194 bool plane;
15195
Daniel Vetter24929352012-07-02 20:28:59 +020015196 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15197 crtc->base.base.id);
15198
15199 /* Pipe has the wrong plane attached and the plane is active.
15200 * Temporarily change the plane mapping and disable everything
15201 * ... */
15202 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015203 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020015204 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015205 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015206 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020015207 }
Daniel Vetter24929352012-07-02 20:28:59 +020015208
Daniel Vetter7fad7982012-07-04 17:51:47 +020015209 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15210 crtc->pipe == PIPE_A && !crtc->active) {
15211 /* BIOS forgot to enable pipe A, this mostly happens after
15212 * resume. Force-enable the pipe to fix this, the update_dpms
15213 * call below we restore the pipe to the right state, but leave
15214 * the required bits on. */
15215 intel_enable_pipe_a(dev);
15216 }
15217
Daniel Vetter24929352012-07-02 20:28:59 +020015218 /* Adjust the state of the output pipe according to whether we
15219 * have active connectors/encoders. */
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015220 enable = false;
15221 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15222 enable |= encoder->connectors_active;
Daniel Vetter24929352012-07-02 20:28:59 +020015223
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015224 if (!enable)
15225 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015226
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020015227 if (crtc->active != crtc->base.state->active) {
Daniel Vetter24929352012-07-02 20:28:59 +020015228
15229 /* This can happen either due to bugs in the get_hw_state
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015230 * functions or because of calls to intel_crtc_disable_noatomic,
15231 * or because the pipe is force-enabled due to the
Daniel Vetter24929352012-07-02 20:28:59 +020015232 * pipe A quirk. */
15233 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
15234 crtc->base.base.id,
Matt Roper83d65732015-02-25 13:12:16 -080015235 crtc->base.state->enable ? "enabled" : "disabled",
Daniel Vetter24929352012-07-02 20:28:59 +020015236 crtc->active ? "enabled" : "disabled");
15237
Matt Roper83d65732015-02-25 13:12:16 -080015238 crtc->base.state->enable = crtc->active;
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020015239 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020015240 crtc->base.enabled = crtc->active;
15241
15242 /* Because we only establish the connector -> encoder ->
15243 * crtc links if something is active, this means the
15244 * crtc is now deactivated. Break the links. connector
15245 * -> encoder links are only establish when things are
15246 * actually up, hence no need to break them. */
15247 WARN_ON(crtc->active);
15248
15249 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
15250 WARN_ON(encoder->connectors_active);
15251 encoder->base.crtc = NULL;
15252 }
15253 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015254
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030015255 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015256 /*
15257 * We start out with underrun reporting disabled to avoid races.
15258 * For correct bookkeeping mark this on active crtcs.
15259 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015260 * Also on gmch platforms we dont have any hardware bits to
15261 * disable the underrun reporting. Which means we need to start
15262 * out with underrun reporting disabled also on inactive pipes,
15263 * since otherwise we'll complain about the garbage we read when
15264 * e.g. coming up after runtime pm.
15265 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015266 * No protection against concurrent access is required - at
15267 * worst a fifo underrun happens which also sets this to false.
15268 */
15269 crtc->cpu_fifo_underrun_disabled = true;
15270 crtc->pch_fifo_underrun_disabled = true;
15271 }
Daniel Vetter24929352012-07-02 20:28:59 +020015272}
15273
15274static void intel_sanitize_encoder(struct intel_encoder *encoder)
15275{
15276 struct intel_connector *connector;
15277 struct drm_device *dev = encoder->base.dev;
15278
15279 /* We need to check both for a crtc link (meaning that the
15280 * encoder is active and trying to read from a pipe) and the
15281 * pipe itself being active. */
15282 bool has_active_crtc = encoder->base.crtc &&
15283 to_intel_crtc(encoder->base.crtc)->active;
15284
15285 if (encoder->connectors_active && !has_active_crtc) {
15286 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15287 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015288 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015289
15290 /* Connector is active, but has no active pipe. This is
15291 * fallout from our resume register restoring. Disable
15292 * the encoder manually again. */
15293 if (encoder->base.crtc) {
15294 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15295 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015296 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015297 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015298 if (encoder->post_disable)
15299 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015300 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015301 encoder->base.crtc = NULL;
15302 encoder->connectors_active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020015303
15304 /* Inconsistent output/port/pipe state happens presumably due to
15305 * a bug in one of the get_hw_state functions. Or someplace else
15306 * in our code, like the register restore mess on resume. Clamp
15307 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015308 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015309 if (connector->encoder != encoder)
15310 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020015311 connector->base.dpms = DRM_MODE_DPMS_OFF;
15312 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015313 }
15314 }
15315 /* Enabled encoders without active connectors will be fixed in
15316 * the crtc fixup. */
15317}
15318
Imre Deak04098752014-02-18 00:02:16 +020015319void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015320{
15321 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020015322 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015323
Imre Deak04098752014-02-18 00:02:16 +020015324 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15325 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15326 i915_disable_vga(dev);
15327 }
15328}
15329
15330void i915_redisable_vga(struct drm_device *dev)
15331{
15332 struct drm_i915_private *dev_priv = dev->dev_private;
15333
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015334 /* This function can be called both from intel_modeset_setup_hw_state or
15335 * at a very early point in our resume sequence, where the power well
15336 * structures are not yet restored. Since this function is at a very
15337 * paranoid "someone might have enabled VGA while we were not looking"
15338 * level, just check if the power well is enabled instead of trying to
15339 * follow the "don't touch the power well if we don't need it" policy
15340 * the rest of the driver uses. */
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015341 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015342 return;
15343
Imre Deak04098752014-02-18 00:02:16 +020015344 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015345}
15346
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015347static bool primary_get_hw_state(struct intel_crtc *crtc)
15348{
15349 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
15350
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015351 return !!(I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE);
15352}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015353
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015354static void readout_plane_state(struct intel_crtc *crtc,
15355 struct intel_crtc_state *crtc_state)
15356{
15357 struct intel_plane *p;
15358 struct drm_plane_state *drm_plane_state;
15359 bool active = crtc_state->base.active;
15360
15361 if (active) {
15362 crtc_state->quirks |= PIPE_CONFIG_QUIRK_INITIAL_PLANES;
15363
15364 /* apply to previous sw state too */
15365 to_intel_crtc_state(crtc->base.state)->quirks |=
15366 PIPE_CONFIG_QUIRK_INITIAL_PLANES;
15367 }
15368
15369 for_each_intel_plane(crtc->base.dev, p) {
15370 bool visible = active;
15371
15372 if (crtc->pipe != p->pipe)
15373 continue;
15374
15375 drm_plane_state = p->base.state;
15376 if (active && p->base.type == DRM_PLANE_TYPE_PRIMARY) {
15377 visible = primary_get_hw_state(crtc);
15378 to_intel_plane_state(drm_plane_state)->visible = visible;
15379 } else {
15380 /*
15381 * unknown state, assume it's off to force a transition
15382 * to on when calculating state changes.
15383 */
15384 to_intel_plane_state(drm_plane_state)->visible = false;
15385 }
15386
15387 if (visible) {
15388 crtc_state->base.plane_mask |=
15389 1 << drm_plane_index(&p->base);
15390 } else if (crtc_state->base.state) {
15391 /* Make this unconditional for atomic hw readout. */
15392 crtc_state->base.plane_mask &=
15393 ~(1 << drm_plane_index(&p->base));
15394 }
15395 }
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015396}
15397
Daniel Vetter30e984d2013-06-05 13:34:17 +020015398static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015399{
15400 struct drm_i915_private *dev_priv = dev->dev_private;
15401 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015402 struct intel_crtc *crtc;
15403 struct intel_encoder *encoder;
15404 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020015405 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015406
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015407 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015408 memset(crtc->config, 0, sizeof(*crtc->config));
Maarten Lankhorstf7217902015-06-10 10:24:20 +020015409 crtc->config->base.crtc = &crtc->base;
Daniel Vetter3b117c82013-04-17 20:15:07 +020015410
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015411 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
Daniel Vetter99535992014-04-13 12:00:33 +020015412
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015413 crtc->active = dev_priv->display.get_pipe_config(crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015414 crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015415
Matt Roper83d65732015-02-25 13:12:16 -080015416 crtc->base.state->enable = crtc->active;
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020015417 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020015418 crtc->base.enabled = crtc->active;
Maarten Lankhorstb8b7fad2015-06-12 11:15:41 +020015419 crtc->base.hwmode = crtc->config->base.adjusted_mode;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015420
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015421 readout_plane_state(crtc, to_intel_crtc_state(crtc->base.state));
Daniel Vetter24929352012-07-02 20:28:59 +020015422
15423 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15424 crtc->base.base.id,
15425 crtc->active ? "enabled" : "disabled");
15426 }
15427
Daniel Vetter53589012013-06-05 13:34:16 +020015428 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15429 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15430
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015431 pll->on = pll->get_hw_state(dev_priv, pll,
15432 &pll->config.hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020015433 pll->active = 0;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015434 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015435 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015436 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
Daniel Vetter53589012013-06-05 13:34:16 +020015437 pll->active++;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015438 pll->config.crtc_mask |= 1 << crtc->pipe;
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015439 }
Daniel Vetter53589012013-06-05 13:34:16 +020015440 }
Daniel Vetter53589012013-06-05 13:34:16 +020015441
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015442 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015443 pll->name, pll->config.crtc_mask, pll->on);
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015444
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015445 if (pll->config.crtc_mask)
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015446 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
Daniel Vetter53589012013-06-05 13:34:16 +020015447 }
15448
Damien Lespiaub2784e12014-08-05 11:29:37 +010015449 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015450 pipe = 0;
15451
15452 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015453 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15454 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015455 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015456 } else {
15457 encoder->base.crtc = NULL;
15458 }
15459
15460 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015461 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020015462 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015463 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015464 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015465 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015466 }
15467
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015468 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015469 if (connector->get_hw_state(connector)) {
15470 connector->base.dpms = DRM_MODE_DPMS_ON;
15471 connector->encoder->connectors_active = true;
15472 connector->base.encoder = &connector->encoder->base;
15473 } else {
15474 connector->base.dpms = DRM_MODE_DPMS_OFF;
15475 connector->base.encoder = NULL;
15476 }
15477 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15478 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030015479 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015480 connector->base.encoder ? "enabled" : "disabled");
15481 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015482}
15483
15484/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
15485 * and i915 state tracking structures. */
15486void intel_modeset_setup_hw_state(struct drm_device *dev,
15487 bool force_restore)
15488{
15489 struct drm_i915_private *dev_priv = dev->dev_private;
15490 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015491 struct intel_crtc *crtc;
15492 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015493 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015494
15495 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015496
Jesse Barnesbabea612013-06-26 18:57:38 +030015497 /*
15498 * Now that we have the config, copy it to each CRTC struct
15499 * Note that this could go away if we move to using crtc_config
15500 * checking everywhere.
15501 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015502 for_each_intel_crtc(dev, crtc) {
Jani Nikulad330a952014-01-21 11:24:25 +020015503 if (crtc->active && i915.fastboot) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015504 intel_mode_from_pipe_config(&crtc->base.mode,
15505 crtc->config);
Jesse Barnesbabea612013-06-26 18:57:38 +030015506 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
15507 crtc->base.base.id);
15508 drm_mode_debug_printmodeline(&crtc->base.mode);
15509 }
15510 }
15511
Daniel Vetter24929352012-07-02 20:28:59 +020015512 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010015513 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015514 intel_sanitize_encoder(encoder);
15515 }
15516
Damien Lespiau055e3932014-08-18 13:49:10 +010015517 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020015518 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15519 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015520 intel_dump_pipe_config(crtc, crtc->config,
15521 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015522 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015523
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015524 intel_modeset_update_connector_atomic_state(dev);
15525
Daniel Vetter35c95372013-07-17 06:55:04 +020015526 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15527 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15528
15529 if (!pll->on || pll->active)
15530 continue;
15531
15532 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15533
15534 pll->disable(dev_priv, pll);
15535 pll->on = false;
15536 }
15537
Pradeep Bhat30789992014-11-04 17:06:45 +000015538 if (IS_GEN9(dev))
15539 skl_wm_get_hw_state(dev);
15540 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015541 ilk_wm_get_hw_state(dev);
15542
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015543 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015544 i915_redisable_vga(dev);
15545
Daniel Vetterf30da182013-04-11 20:22:50 +020015546 /*
15547 * We need to use raw interfaces for restoring state to avoid
15548 * checking (bogus) intermediate states.
15549 */
Damien Lespiau055e3932014-08-18 13:49:10 +010015550 for_each_pipe(dev_priv, pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070015551 struct drm_crtc *crtc =
15552 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020015553
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020015554 intel_crtc_restore_mode(crtc);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015555 }
15556 } else {
15557 intel_modeset_update_staged_output_state(dev);
15558 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020015559
15560 intel_modeset_check_state(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015561}
15562
15563void intel_modeset_gem_init(struct drm_device *dev)
15564{
Jesse Barnes92122782014-10-09 12:57:42 -070015565 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015566 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070015567 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015568 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015569
Imre Deakae484342014-03-31 15:10:44 +030015570 mutex_lock(&dev->struct_mutex);
15571 intel_init_gt_powersave(dev);
15572 mutex_unlock(&dev->struct_mutex);
15573
Jesse Barnes92122782014-10-09 12:57:42 -070015574 /*
15575 * There may be no VBT; and if the BIOS enabled SSC we can
15576 * just keep using it to avoid unnecessary flicker. Whereas if the
15577 * BIOS isn't using it, don't assume it will work even if the VBT
15578 * indicates as much.
15579 */
15580 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
15581 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15582 DREF_SSC1_ENABLE);
15583
Chris Wilson1833b132012-05-09 11:56:28 +010015584 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020015585
15586 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015587
15588 /*
15589 * Make sure any fbs we allocated at startup are properly
15590 * pinned & fenced. When we do the allocation it's too early
15591 * for this.
15592 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010015593 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070015594 obj = intel_fb_obj(c->primary->fb);
15595 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080015596 continue;
15597
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015598 mutex_lock(&dev->struct_mutex);
15599 ret = intel_pin_and_fence_fb_obj(c->primary,
15600 c->primary->fb,
15601 c->primary->state,
15602 NULL);
15603 mutex_unlock(&dev->struct_mutex);
15604 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080015605 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15606 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100015607 drm_framebuffer_unreference(c->primary->fb);
15608 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015609 c->primary->crtc = c->primary->state->crtc = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080015610 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015611 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080015612 }
15613 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015614
15615 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015616}
15617
Imre Deak4932e2c2014-02-11 17:12:48 +020015618void intel_connector_unregister(struct intel_connector *intel_connector)
15619{
15620 struct drm_connector *connector = &intel_connector->base;
15621
15622 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010015623 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015624}
15625
Jesse Barnes79e53942008-11-07 14:24:08 -080015626void intel_modeset_cleanup(struct drm_device *dev)
15627{
Jesse Barnes652c3932009-08-17 13:31:43 -070015628 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid9255d52013-09-26 20:05:59 -030015629 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070015630
Imre Deak2eb52522014-11-19 15:30:05 +020015631 intel_disable_gt_powersave(dev);
15632
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015633 intel_backlight_unregister(dev);
15634
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015635 /*
15636 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020015637 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015638 * experience fancy races otherwise.
15639 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020015640 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070015641
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015642 /*
15643 * Due to the hpd irq storm handling the hotplug work can re-arm the
15644 * poll handlers. Hence disable polling after hpd handling is shut down.
15645 */
Keith Packardf87ea762010-10-03 19:36:26 -070015646 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015647
Jesse Barnes652c3932009-08-17 13:31:43 -070015648 mutex_lock(&dev->struct_mutex);
15649
Jesse Barnes723bfd72010-10-07 16:01:13 -070015650 intel_unregister_dsm_handler();
15651
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020015652 intel_fbc_disable(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070015653
Kristian Høgsberg69341a52009-11-11 12:19:17 -050015654 mutex_unlock(&dev->struct_mutex);
15655
Chris Wilson1630fe72011-07-08 12:22:42 +010015656 /* flush any delayed tasks or pending work */
15657 flush_scheduled_work();
15658
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015659 /* destroy the backlight and sysfs files before encoders/connectors */
15660 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020015661 struct intel_connector *intel_connector;
15662
15663 intel_connector = to_intel_connector(connector);
15664 intel_connector->unregister(intel_connector);
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015665 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030015666
Jesse Barnes79e53942008-11-07 14:24:08 -080015667 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010015668
15669 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030015670
15671 mutex_lock(&dev->struct_mutex);
15672 intel_cleanup_gt_powersave(dev);
15673 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080015674}
15675
Dave Airlie28d52042009-09-21 14:33:58 +100015676/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080015677 * Return which encoder is currently attached for connector.
15678 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010015679struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080015680{
Chris Wilsondf0e9242010-09-09 16:20:55 +010015681 return &intel_attached_encoder(connector)->base;
15682}
Jesse Barnes79e53942008-11-07 14:24:08 -080015683
Chris Wilsondf0e9242010-09-09 16:20:55 +010015684void intel_connector_attach_encoder(struct intel_connector *connector,
15685 struct intel_encoder *encoder)
15686{
15687 connector->encoder = encoder;
15688 drm_mode_connector_attach_encoder(&connector->base,
15689 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080015690}
Dave Airlie28d52042009-09-21 14:33:58 +100015691
15692/*
15693 * set vga decode state - true == enable VGA decode
15694 */
15695int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15696{
15697 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000015698 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100015699 u16 gmch_ctrl;
15700
Chris Wilson75fa0412014-02-07 18:37:02 -020015701 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15702 DRM_ERROR("failed to read control word\n");
15703 return -EIO;
15704 }
15705
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020015706 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15707 return 0;
15708
Dave Airlie28d52042009-09-21 14:33:58 +100015709 if (state)
15710 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15711 else
15712 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020015713
15714 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15715 DRM_ERROR("failed to write control word\n");
15716 return -EIO;
15717 }
15718
Dave Airlie28d52042009-09-21 14:33:58 +100015719 return 0;
15720}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015721
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015722struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015723
15724 u32 power_well_driver;
15725
Chris Wilson63b66e52013-08-08 15:12:06 +020015726 int num_transcoders;
15727
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015728 struct intel_cursor_error_state {
15729 u32 control;
15730 u32 position;
15731 u32 base;
15732 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010015733 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015734
15735 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015736 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015737 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030015738 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010015739 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015740
15741 struct intel_plane_error_state {
15742 u32 control;
15743 u32 stride;
15744 u32 size;
15745 u32 pos;
15746 u32 addr;
15747 u32 surface;
15748 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010015749 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020015750
15751 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015752 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020015753 enum transcoder cpu_transcoder;
15754
15755 u32 conf;
15756
15757 u32 htotal;
15758 u32 hblank;
15759 u32 hsync;
15760 u32 vtotal;
15761 u32 vblank;
15762 u32 vsync;
15763 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015764};
15765
15766struct intel_display_error_state *
15767intel_display_capture_error_state(struct drm_device *dev)
15768{
Jani Nikulafbee40d2014-03-31 14:27:18 +030015769 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015770 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020015771 int transcoders[] = {
15772 TRANSCODER_A,
15773 TRANSCODER_B,
15774 TRANSCODER_C,
15775 TRANSCODER_EDP,
15776 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015777 int i;
15778
Chris Wilson63b66e52013-08-08 15:12:06 +020015779 if (INTEL_INFO(dev)->num_pipes == 0)
15780 return NULL;
15781
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015782 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015783 if (error == NULL)
15784 return NULL;
15785
Imre Deak190be112013-11-25 17:15:31 +020015786 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015787 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15788
Damien Lespiau055e3932014-08-18 13:49:10 +010015789 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020015790 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015791 __intel_display_power_is_enabled(dev_priv,
15792 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020015793 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015794 continue;
15795
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030015796 error->cursor[i].control = I915_READ(CURCNTR(i));
15797 error->cursor[i].position = I915_READ(CURPOS(i));
15798 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015799
15800 error->plane[i].control = I915_READ(DSPCNTR(i));
15801 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015802 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030015803 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015804 error->plane[i].pos = I915_READ(DSPPOS(i));
15805 }
Paulo Zanonica291362013-03-06 20:03:14 -030015806 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15807 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015808 if (INTEL_INFO(dev)->gen >= 4) {
15809 error->plane[i].surface = I915_READ(DSPSURF(i));
15810 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15811 }
15812
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015813 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030015814
Sonika Jindal3abfce72014-07-21 15:23:43 +053015815 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e2014-04-18 15:55:04 +030015816 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020015817 }
15818
15819 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
15820 if (HAS_DDI(dev_priv->dev))
15821 error->num_transcoders++; /* Account for eDP. */
15822
15823 for (i = 0; i < error->num_transcoders; i++) {
15824 enum transcoder cpu_transcoder = transcoders[i];
15825
Imre Deakddf9c532013-11-27 22:02:02 +020015826 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015827 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020015828 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015829 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015830 continue;
15831
Chris Wilson63b66e52013-08-08 15:12:06 +020015832 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15833
15834 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15835 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15836 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15837 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15838 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15839 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15840 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015841 }
15842
15843 return error;
15844}
15845
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015846#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15847
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015848void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015849intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015850 struct drm_device *dev,
15851 struct intel_display_error_state *error)
15852{
Damien Lespiau055e3932014-08-18 13:49:10 +010015853 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015854 int i;
15855
Chris Wilson63b66e52013-08-08 15:12:06 +020015856 if (!error)
15857 return;
15858
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015859 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020015860 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015861 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015862 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010015863 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015864 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020015865 err_printf(m, " Power: %s\n",
15866 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015867 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030015868 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015869
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015870 err_printf(m, "Plane [%d]:\n", i);
15871 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15872 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015873 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015874 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15875 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015876 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030015877 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015878 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015879 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015880 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15881 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015882 }
15883
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015884 err_printf(m, "Cursor [%d]:\n", i);
15885 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15886 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15887 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015888 }
Chris Wilson63b66e52013-08-08 15:12:06 +020015889
15890 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010015891 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020015892 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015893 err_printf(m, " Power: %s\n",
15894 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020015895 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15896 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15897 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15898 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15899 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15900 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15901 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15902 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015903}
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015904
15905void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
15906{
15907 struct intel_crtc *crtc;
15908
15909 for_each_intel_crtc(dev, crtc) {
15910 struct intel_unpin_work *work;
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015911
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015912 spin_lock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015913
15914 work = crtc->unpin_work;
15915
15916 if (work && work->event &&
15917 work->event->base.file_priv == file) {
15918 kfree(work->event);
15919 work->event = NULL;
15920 }
15921
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015922 spin_unlock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015923 }
15924}