blob: eb665d7ffda70c1b9e7326f152ad917d6072bebe [file] [log] [blame]
Jesse Barnes79e53942008-11-07 14:24:08 -08001/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
Daniel Vetter618563e2012-04-01 13:38:50 +020027#include <linux/dmi.h>
Jesse Barnesc1c7af62009-09-10 15:28:03 -070028#include <linux/module.h>
29#include <linux/input.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080030#include <linux/i2c.h>
Shaohua Li7662c8b2009-06-26 11:23:55 +080031#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Jesse Barnes9cce37f2010-08-13 15:11:26 -070033#include <linux/vgaarb.h>
Wu Fengguange0dac652011-09-05 14:25:34 +080034#include <drm/drm_edid.h>
David Howells760285e2012-10-02 18:01:07 +010035#include <drm/drmP.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include "intel_drv.h"
David Howells760285e2012-10-02 18:01:07 +010037#include <drm/i915_drm.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080038#include "i915_drv.h"
Jesse Barnese5510fa2010-07-01 16:48:37 -070039#include "i915_trace.h"
Xi Ruoyao319c1d42015-03-12 20:16:32 +080040#include <drm/drm_atomic.h>
Matt Roperc196e1d2015-01-21 16:35:48 -080041#include <drm/drm_atomic_helper.h>
David Howells760285e2012-10-02 18:01:07 +010042#include <drm/drm_dp_helper.h>
43#include <drm/drm_crtc_helper.h>
Matt Roper465c1202014-05-29 08:06:54 -070044#include <drm/drm_plane_helper.h>
45#include <drm/drm_rect.h>
Keith Packardc0f372b32011-11-16 22:24:52 -080046#include <linux/dma_remapping.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080047
Matt Roper465c1202014-05-29 08:06:54 -070048/* Primary plane formats for gen <= 3 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010049static const uint32_t i8xx_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010050 DRM_FORMAT_C8,
51 DRM_FORMAT_RGB565,
Matt Roper465c1202014-05-29 08:06:54 -070052 DRM_FORMAT_XRGB1555,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010053 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070054};
55
56/* Primary plane formats for gen >= 4 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010057static const uint32_t i965_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010058 DRM_FORMAT_C8,
59 DRM_FORMAT_RGB565,
60 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070061 DRM_FORMAT_XBGR8888,
Damien Lespiau6c0fd452015-05-19 12:29:16 +010062 DRM_FORMAT_XRGB2101010,
63 DRM_FORMAT_XBGR2101010,
64};
65
66static const uint32_t skl_primary_formats[] = {
67 DRM_FORMAT_C8,
68 DRM_FORMAT_RGB565,
69 DRM_FORMAT_XRGB8888,
70 DRM_FORMAT_XBGR8888,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010071 DRM_FORMAT_ARGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070072 DRM_FORMAT_ABGR8888,
73 DRM_FORMAT_XRGB2101010,
Matt Roper465c1202014-05-29 08:06:54 -070074 DRM_FORMAT_XBGR2101010,
Matt Roper465c1202014-05-29 08:06:54 -070075};
76
Matt Roper3d7d6512014-06-10 08:28:13 -070077/* Cursor formats */
78static const uint32_t intel_cursor_formats[] = {
79 DRM_FORMAT_ARGB8888,
80};
81
Chris Wilson6b383a72010-09-13 13:54:26 +010082static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
Jesse Barnes79e53942008-11-07 14:24:08 -080083
Jesse Barnesf1f644d2013-06-27 00:39:25 +030084static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020085 struct intel_crtc_state *pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +030086static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020087 struct intel_crtc_state *pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030088
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020089static int intel_set_mode(struct drm_atomic_state *state);
Jesse Barneseb1bfe82014-02-12 12:26:25 -080090static int intel_framebuffer_init(struct drm_device *dev,
91 struct intel_framebuffer *ifb,
92 struct drm_mode_fb_cmd2 *mode_cmd,
93 struct drm_i915_gem_object *obj);
Daniel Vetter5b18e572014-04-24 23:55:06 +020094static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
95static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +020096static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -070097 struct intel_link_m_n *m_n,
98 struct intel_link_m_n *m2_n2);
Daniel Vetter29407aa2014-04-24 23:55:08 +020099static void ironlake_set_pipeconf(struct drm_crtc *crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +0200100static void haswell_set_pipeconf(struct drm_crtc *crtc);
101static void intel_set_pipe_csc(struct drm_crtc *crtc);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200102static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200103 const struct intel_crtc_state *pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200104static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200105 const struct intel_crtc_state *pipe_config);
Matt Roperea2c67b2014-12-23 10:41:52 -0800106static void intel_begin_crtc_commit(struct drm_crtc *crtc);
107static void intel_finish_crtc_commit(struct drm_crtc *crtc);
Chandra Konduru549e2bf2015-04-07 15:28:38 -0700108static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
109 struct intel_crtc_state *crtc_state);
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200110static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
111 int num_connectors);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100112
Dave Airlie0e32b392014-05-02 14:02:48 +1000113static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
114{
115 if (!connector->mst_port)
116 return connector->encoder;
117 else
118 return &connector->mst_port->mst_encoders[pipe]->base;
119}
120
Jesse Barnes79e53942008-11-07 14:24:08 -0800121typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400122 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -0800123} intel_range_t;
124
125typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400126 int dot_limit;
127 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800128} intel_p2_t;
129
Ma Lingd4906092009-03-18 20:13:27 +0800130typedef struct intel_limit intel_limit_t;
131struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -0400132 intel_range_t dot, vco, n, m, m1, m2, p, p1;
133 intel_p2_t p2;
Ma Lingd4906092009-03-18 20:13:27 +0800134};
Jesse Barnes79e53942008-11-07 14:24:08 -0800135
Daniel Vetterd2acd212012-10-20 20:57:43 +0200136int
137intel_pch_rawclk(struct drm_device *dev)
138{
139 struct drm_i915_private *dev_priv = dev->dev_private;
140
141 WARN_ON(!HAS_PCH_SPLIT(dev));
142
143 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
144}
145
Chris Wilson021357a2010-09-07 20:54:59 +0100146static inline u32 /* units of 100MHz */
147intel_fdi_link_freq(struct drm_device *dev)
148{
Chris Wilson8b99e682010-10-13 09:59:17 +0100149 if (IS_GEN5(dev)) {
150 struct drm_i915_private *dev_priv = dev->dev_private;
151 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
152 } else
153 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +0100154}
155
Daniel Vetter5d536e22013-07-06 12:52:06 +0200156static const intel_limit_t intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400157 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200158 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200159 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400160 .m = { .min = 96, .max = 140 },
161 .m1 = { .min = 18, .max = 26 },
162 .m2 = { .min = 6, .max = 16 },
163 .p = { .min = 4, .max = 128 },
164 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700165 .p2 = { .dot_limit = 165000,
166 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700167};
168
Daniel Vetter5d536e22013-07-06 12:52:06 +0200169static const intel_limit_t intel_limits_i8xx_dvo = {
170 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200171 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200172 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200173 .m = { .min = 96, .max = 140 },
174 .m1 = { .min = 18, .max = 26 },
175 .m2 = { .min = 6, .max = 16 },
176 .p = { .min = 4, .max = 128 },
177 .p1 = { .min = 2, .max = 33 },
178 .p2 = { .dot_limit = 165000,
179 .p2_slow = 4, .p2_fast = 4 },
180};
181
Keith Packarde4b36692009-06-05 19:22:17 -0700182static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400183 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200184 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200185 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400186 .m = { .min = 96, .max = 140 },
187 .m1 = { .min = 18, .max = 26 },
188 .m2 = { .min = 6, .max = 16 },
189 .p = { .min = 4, .max = 128 },
190 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700191 .p2 = { .dot_limit = 165000,
192 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700193};
Eric Anholt273e27c2011-03-30 13:01:10 -0700194
Keith Packarde4b36692009-06-05 19:22:17 -0700195static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400196 .dot = { .min = 20000, .max = 400000 },
197 .vco = { .min = 1400000, .max = 2800000 },
198 .n = { .min = 1, .max = 6 },
199 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100200 .m1 = { .min = 8, .max = 18 },
201 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400202 .p = { .min = 5, .max = 80 },
203 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700204 .p2 = { .dot_limit = 200000,
205 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700206};
207
208static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400209 .dot = { .min = 20000, .max = 400000 },
210 .vco = { .min = 1400000, .max = 2800000 },
211 .n = { .min = 1, .max = 6 },
212 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100213 .m1 = { .min = 8, .max = 18 },
214 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400215 .p = { .min = 7, .max = 98 },
216 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700217 .p2 = { .dot_limit = 112000,
218 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700219};
220
Eric Anholt273e27c2011-03-30 13:01:10 -0700221
Keith Packarde4b36692009-06-05 19:22:17 -0700222static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700223 .dot = { .min = 25000, .max = 270000 },
224 .vco = { .min = 1750000, .max = 3500000},
225 .n = { .min = 1, .max = 4 },
226 .m = { .min = 104, .max = 138 },
227 .m1 = { .min = 17, .max = 23 },
228 .m2 = { .min = 5, .max = 11 },
229 .p = { .min = 10, .max = 30 },
230 .p1 = { .min = 1, .max = 3},
231 .p2 = { .dot_limit = 270000,
232 .p2_slow = 10,
233 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800234 },
Keith Packarde4b36692009-06-05 19:22:17 -0700235};
236
237static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700238 .dot = { .min = 22000, .max = 400000 },
239 .vco = { .min = 1750000, .max = 3500000},
240 .n = { .min = 1, .max = 4 },
241 .m = { .min = 104, .max = 138 },
242 .m1 = { .min = 16, .max = 23 },
243 .m2 = { .min = 5, .max = 11 },
244 .p = { .min = 5, .max = 80 },
245 .p1 = { .min = 1, .max = 8},
246 .p2 = { .dot_limit = 165000,
247 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700248};
249
250static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700251 .dot = { .min = 20000, .max = 115000 },
252 .vco = { .min = 1750000, .max = 3500000 },
253 .n = { .min = 1, .max = 3 },
254 .m = { .min = 104, .max = 138 },
255 .m1 = { .min = 17, .max = 23 },
256 .m2 = { .min = 5, .max = 11 },
257 .p = { .min = 28, .max = 112 },
258 .p1 = { .min = 2, .max = 8 },
259 .p2 = { .dot_limit = 0,
260 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800261 },
Keith Packarde4b36692009-06-05 19:22:17 -0700262};
263
264static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700265 .dot = { .min = 80000, .max = 224000 },
266 .vco = { .min = 1750000, .max = 3500000 },
267 .n = { .min = 1, .max = 3 },
268 .m = { .min = 104, .max = 138 },
269 .m1 = { .min = 17, .max = 23 },
270 .m2 = { .min = 5, .max = 11 },
271 .p = { .min = 14, .max = 42 },
272 .p1 = { .min = 2, .max = 6 },
273 .p2 = { .dot_limit = 0,
274 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800275 },
Keith Packarde4b36692009-06-05 19:22:17 -0700276};
277
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500278static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400279 .dot = { .min = 20000, .max = 400000},
280 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700281 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400282 .n = { .min = 3, .max = 6 },
283 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700284 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400285 .m1 = { .min = 0, .max = 0 },
286 .m2 = { .min = 0, .max = 254 },
287 .p = { .min = 5, .max = 80 },
288 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700289 .p2 = { .dot_limit = 200000,
290 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700291};
292
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500293static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400294 .dot = { .min = 20000, .max = 400000 },
295 .vco = { .min = 1700000, .max = 3500000 },
296 .n = { .min = 3, .max = 6 },
297 .m = { .min = 2, .max = 256 },
298 .m1 = { .min = 0, .max = 0 },
299 .m2 = { .min = 0, .max = 254 },
300 .p = { .min = 7, .max = 112 },
301 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700302 .p2 = { .dot_limit = 112000,
303 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700304};
305
Eric Anholt273e27c2011-03-30 13:01:10 -0700306/* Ironlake / Sandybridge
307 *
308 * We calculate clock using (register_value + 2) for N/M1/M2, so here
309 * the range value for them is (actual_value - 2).
310 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800311static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700312 .dot = { .min = 25000, .max = 350000 },
313 .vco = { .min = 1760000, .max = 3510000 },
314 .n = { .min = 1, .max = 5 },
315 .m = { .min = 79, .max = 127 },
316 .m1 = { .min = 12, .max = 22 },
317 .m2 = { .min = 5, .max = 9 },
318 .p = { .min = 5, .max = 80 },
319 .p1 = { .min = 1, .max = 8 },
320 .p2 = { .dot_limit = 225000,
321 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700322};
323
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800324static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700325 .dot = { .min = 25000, .max = 350000 },
326 .vco = { .min = 1760000, .max = 3510000 },
327 .n = { .min = 1, .max = 3 },
328 .m = { .min = 79, .max = 118 },
329 .m1 = { .min = 12, .max = 22 },
330 .m2 = { .min = 5, .max = 9 },
331 .p = { .min = 28, .max = 112 },
332 .p1 = { .min = 2, .max = 8 },
333 .p2 = { .dot_limit = 225000,
334 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800335};
336
337static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700338 .dot = { .min = 25000, .max = 350000 },
339 .vco = { .min = 1760000, .max = 3510000 },
340 .n = { .min = 1, .max = 3 },
341 .m = { .min = 79, .max = 127 },
342 .m1 = { .min = 12, .max = 22 },
343 .m2 = { .min = 5, .max = 9 },
344 .p = { .min = 14, .max = 56 },
345 .p1 = { .min = 2, .max = 8 },
346 .p2 = { .dot_limit = 225000,
347 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800348};
349
Eric Anholt273e27c2011-03-30 13:01:10 -0700350/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800351static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700352 .dot = { .min = 25000, .max = 350000 },
353 .vco = { .min = 1760000, .max = 3510000 },
354 .n = { .min = 1, .max = 2 },
355 .m = { .min = 79, .max = 126 },
356 .m1 = { .min = 12, .max = 22 },
357 .m2 = { .min = 5, .max = 9 },
358 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400359 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700360 .p2 = { .dot_limit = 225000,
361 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800362};
363
364static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700365 .dot = { .min = 25000, .max = 350000 },
366 .vco = { .min = 1760000, .max = 3510000 },
367 .n = { .min = 1, .max = 3 },
368 .m = { .min = 79, .max = 126 },
369 .m1 = { .min = 12, .max = 22 },
370 .m2 = { .min = 5, .max = 9 },
371 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400372 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700373 .p2 = { .dot_limit = 225000,
374 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800375};
376
Ville Syrjälädc730512013-09-24 21:26:30 +0300377static const intel_limit_t intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300378 /*
379 * These are the data rate limits (measured in fast clocks)
380 * since those are the strictest limits we have. The fast
381 * clock and actual rate limits are more relaxed, so checking
382 * them would make no difference.
383 */
384 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200385 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700386 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700387 .m1 = { .min = 2, .max = 3 },
388 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300389 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300390 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700391};
392
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300393static const intel_limit_t intel_limits_chv = {
394 /*
395 * These are the data rate limits (measured in fast clocks)
396 * since those are the strictest limits we have. The fast
397 * clock and actual rate limits are more relaxed, so checking
398 * them would make no difference.
399 */
400 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200401 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300402 .n = { .min = 1, .max = 1 },
403 .m1 = { .min = 2, .max = 2 },
404 .m2 = { .min = 24 << 22, .max = 175 << 22 },
405 .p1 = { .min = 2, .max = 4 },
406 .p2 = { .p2_slow = 1, .p2_fast = 14 },
407};
408
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200409static const intel_limit_t intel_limits_bxt = {
410 /* FIXME: find real dot limits */
411 .dot = { .min = 0, .max = INT_MAX },
412 .vco = { .min = 4800000, .max = 6480000 },
413 .n = { .min = 1, .max = 1 },
414 .m1 = { .min = 2, .max = 2 },
415 /* FIXME: find real m2 limits */
416 .m2 = { .min = 2 << 22, .max = 255 << 22 },
417 .p1 = { .min = 2, .max = 4 },
418 .p2 = { .p2_slow = 1, .p2_fast = 20 },
419};
420
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300421static void vlv_clock(int refclk, intel_clock_t *clock)
422{
423 clock->m = clock->m1 * clock->m2;
424 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200425 if (WARN_ON(clock->n == 0 || clock->p == 0))
426 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300427 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
428 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300429}
430
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200431static bool
432needs_modeset(struct drm_crtc_state *state)
433{
434 return state->mode_changed || state->active_changed;
435}
436
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300437/**
438 * Returns whether any output on the specified pipe is of the specified type
439 */
Damien Lespiau40935612014-10-29 11:16:59 +0000440bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300441{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300442 struct drm_device *dev = crtc->base.dev;
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300443 struct intel_encoder *encoder;
444
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300445 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300446 if (encoder->type == type)
447 return true;
448
449 return false;
450}
451
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200452/**
453 * Returns whether any output on the specified pipe will have the specified
454 * type after a staged modeset is complete, i.e., the same as
455 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
456 * encoder->crtc.
457 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200458static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
459 int type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200460{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200461 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300462 struct drm_connector *connector;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200463 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200464 struct intel_encoder *encoder;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200465 int i, num_connectors = 0;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200466
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300467 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200468 if (connector_state->crtc != crtc_state->base.crtc)
469 continue;
470
471 num_connectors++;
472
473 encoder = to_intel_encoder(connector_state->best_encoder);
474 if (encoder->type == type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200475 return true;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200476 }
477
478 WARN_ON(num_connectors == 0);
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200479
480 return false;
481}
482
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200483static const intel_limit_t *
484intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800485{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200486 struct drm_device *dev = crtc_state->base.crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800487 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800488
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200489 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100490 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000491 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800492 limit = &intel_limits_ironlake_dual_lvds_100m;
493 else
494 limit = &intel_limits_ironlake_dual_lvds;
495 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000496 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800497 limit = &intel_limits_ironlake_single_lvds_100m;
498 else
499 limit = &intel_limits_ironlake_single_lvds;
500 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200501 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800502 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800503
504 return limit;
505}
506
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200507static const intel_limit_t *
508intel_g4x_limit(struct intel_crtc_state *crtc_state)
Ma Ling044c7c42009-03-18 20:13:23 +0800509{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200510 struct drm_device *dev = crtc_state->base.crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800511 const intel_limit_t *limit;
512
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200513 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100514 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700515 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800516 else
Keith Packarde4b36692009-06-05 19:22:17 -0700517 limit = &intel_limits_g4x_single_channel_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200518 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
519 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700520 limit = &intel_limits_g4x_hdmi;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200521 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700522 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800523 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700524 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800525
526 return limit;
527}
528
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200529static const intel_limit_t *
530intel_limit(struct intel_crtc_state *crtc_state, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800531{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200532 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800533 const intel_limit_t *limit;
534
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200535 if (IS_BROXTON(dev))
536 limit = &intel_limits_bxt;
537 else if (HAS_PCH_SPLIT(dev))
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200538 limit = intel_ironlake_limit(crtc_state, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800539 else if (IS_G4X(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200540 limit = intel_g4x_limit(crtc_state);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500541 } else if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200542 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500543 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800544 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500545 limit = &intel_limits_pineview_sdvo;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300546 } else if (IS_CHERRYVIEW(dev)) {
547 limit = &intel_limits_chv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700548 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjälädc730512013-09-24 21:26:30 +0300549 limit = &intel_limits_vlv;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100550 } else if (!IS_GEN2(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200551 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100552 limit = &intel_limits_i9xx_lvds;
553 else
554 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800555 } else {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200556 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700557 limit = &intel_limits_i8xx_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200558 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Keith Packarde4b36692009-06-05 19:22:17 -0700559 limit = &intel_limits_i8xx_dvo;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200560 else
561 limit = &intel_limits_i8xx_dac;
Jesse Barnes79e53942008-11-07 14:24:08 -0800562 }
563 return limit;
564}
565
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500566/* m1 is reserved as 0 in Pineview, n is a ring counter */
567static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800568{
Shaohua Li21778322009-02-23 15:19:16 +0800569 clock->m = clock->m2 + 2;
570 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200571 if (WARN_ON(clock->n == 0 || clock->p == 0))
572 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300573 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
574 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Shaohua Li21778322009-02-23 15:19:16 +0800575}
576
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200577static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
578{
579 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
580}
581
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200582static void i9xx_clock(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800583{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200584 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800585 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200586 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
587 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300588 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
589 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Jesse Barnes79e53942008-11-07 14:24:08 -0800590}
591
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300592static void chv_clock(int refclk, intel_clock_t *clock)
593{
594 clock->m = clock->m1 * clock->m2;
595 clock->p = clock->p1 * clock->p2;
596 if (WARN_ON(clock->n == 0 || clock->p == 0))
597 return;
598 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
599 clock->n << 22);
600 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
601}
602
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800603#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800604/**
605 * Returns whether the given set of divisors are valid for a given refclk with
606 * the given connectors.
607 */
608
Chris Wilson1b894b52010-12-14 20:04:54 +0000609static bool intel_PLL_is_valid(struct drm_device *dev,
610 const intel_limit_t *limit,
611 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800612{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300613 if (clock->n < limit->n.min || limit->n.max < clock->n)
614 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800615 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400616 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800617 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400618 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800619 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400620 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300621
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200622 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300623 if (clock->m1 <= clock->m2)
624 INTELPllInvalid("m1 <= m2\n");
625
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200626 if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300627 if (clock->p < limit->p.min || limit->p.max < clock->p)
628 INTELPllInvalid("p out of range\n");
629 if (clock->m < limit->m.min || limit->m.max < clock->m)
630 INTELPllInvalid("m out of range\n");
631 }
632
Jesse Barnes79e53942008-11-07 14:24:08 -0800633 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400634 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800635 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
636 * connector, etc., rather than just a single range.
637 */
638 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400639 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800640
641 return true;
642}
643
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300644static int
645i9xx_select_p2_div(const intel_limit_t *limit,
646 const struct intel_crtc_state *crtc_state,
647 int target)
Jesse Barnes79e53942008-11-07 14:24:08 -0800648{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300649 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800650
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200651 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800652 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100653 * For LVDS just rely on its current settings for dual-channel.
654 * We haven't figured out how to reliably set up different
655 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800656 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100657 if (intel_is_dual_link_lvds(dev))
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300658 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800659 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300660 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800661 } else {
662 if (target < limit->p2.dot_limit)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300663 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800664 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300665 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800666 }
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300667}
668
669static bool
670i9xx_find_best_dpll(const intel_limit_t *limit,
671 struct intel_crtc_state *crtc_state,
672 int target, int refclk, intel_clock_t *match_clock,
673 intel_clock_t *best_clock)
674{
675 struct drm_device *dev = crtc_state->base.crtc->dev;
676 intel_clock_t clock;
677 int err = target;
Jesse Barnes79e53942008-11-07 14:24:08 -0800678
Akshay Joshi0206e352011-08-16 15:34:10 -0400679 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800680
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300681 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
682
Zhao Yakui42158662009-11-20 11:24:18 +0800683 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
684 clock.m1++) {
685 for (clock.m2 = limit->m2.min;
686 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200687 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800688 break;
689 for (clock.n = limit->n.min;
690 clock.n <= limit->n.max; clock.n++) {
691 for (clock.p1 = limit->p1.min;
692 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800693 int this_err;
694
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200695 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000696 if (!intel_PLL_is_valid(dev, limit,
697 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800698 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800699 if (match_clock &&
700 clock.p != match_clock->p)
701 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800702
703 this_err = abs(clock.dot - target);
704 if (this_err < err) {
705 *best_clock = clock;
706 err = this_err;
707 }
708 }
709 }
710 }
711 }
712
713 return (err != target);
714}
715
Ma Lingd4906092009-03-18 20:13:27 +0800716static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200717pnv_find_best_dpll(const intel_limit_t *limit,
718 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200719 int target, int refclk, intel_clock_t *match_clock,
720 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200721{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300722 struct drm_device *dev = crtc_state->base.crtc->dev;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200723 intel_clock_t clock;
724 int err = target;
725
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200726 memset(best_clock, 0, sizeof(*best_clock));
727
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300728 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
729
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200730 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
731 clock.m1++) {
732 for (clock.m2 = limit->m2.min;
733 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200734 for (clock.n = limit->n.min;
735 clock.n <= limit->n.max; clock.n++) {
736 for (clock.p1 = limit->p1.min;
737 clock.p1 <= limit->p1.max; clock.p1++) {
738 int this_err;
739
740 pineview_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800741 if (!intel_PLL_is_valid(dev, limit,
742 &clock))
743 continue;
744 if (match_clock &&
745 clock.p != match_clock->p)
746 continue;
747
748 this_err = abs(clock.dot - target);
749 if (this_err < err) {
750 *best_clock = clock;
751 err = this_err;
752 }
753 }
754 }
755 }
756 }
757
758 return (err != target);
759}
760
Ma Lingd4906092009-03-18 20:13:27 +0800761static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200762g4x_find_best_dpll(const intel_limit_t *limit,
763 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200764 int target, int refclk, intel_clock_t *match_clock,
765 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800766{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300767 struct drm_device *dev = crtc_state->base.crtc->dev;
Ma Lingd4906092009-03-18 20:13:27 +0800768 intel_clock_t clock;
769 int max_n;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300770 bool found = false;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400771 /* approximately equals target * 0.00585 */
772 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800773
774 memset(best_clock, 0, sizeof(*best_clock));
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300775
776 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
777
Ma Lingd4906092009-03-18 20:13:27 +0800778 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200779 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800780 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200781 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800782 for (clock.m1 = limit->m1.max;
783 clock.m1 >= limit->m1.min; clock.m1--) {
784 for (clock.m2 = limit->m2.max;
785 clock.m2 >= limit->m2.min; clock.m2--) {
786 for (clock.p1 = limit->p1.max;
787 clock.p1 >= limit->p1.min; clock.p1--) {
788 int this_err;
789
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200790 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000791 if (!intel_PLL_is_valid(dev, limit,
792 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800793 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000794
795 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800796 if (this_err < err_most) {
797 *best_clock = clock;
798 err_most = this_err;
799 max_n = clock.n;
800 found = true;
801 }
802 }
803 }
804 }
805 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800806 return found;
807}
Ma Lingd4906092009-03-18 20:13:27 +0800808
Imre Deakd5dd62b2015-03-17 11:40:03 +0200809/*
810 * Check if the calculated PLL configuration is more optimal compared to the
811 * best configuration and error found so far. Return the calculated error.
812 */
813static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
814 const intel_clock_t *calculated_clock,
815 const intel_clock_t *best_clock,
816 unsigned int best_error_ppm,
817 unsigned int *error_ppm)
818{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200819 /*
820 * For CHV ignore the error and consider only the P value.
821 * Prefer a bigger P value based on HW requirements.
822 */
823 if (IS_CHERRYVIEW(dev)) {
824 *error_ppm = 0;
825
826 return calculated_clock->p > best_clock->p;
827 }
828
Imre Deak24be4e42015-03-17 11:40:04 +0200829 if (WARN_ON_ONCE(!target_freq))
830 return false;
831
Imre Deakd5dd62b2015-03-17 11:40:03 +0200832 *error_ppm = div_u64(1000000ULL *
833 abs(target_freq - calculated_clock->dot),
834 target_freq);
835 /*
836 * Prefer a better P value over a better (smaller) error if the error
837 * is small. Ensure this preference for future configurations too by
838 * setting the error to 0.
839 */
840 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
841 *error_ppm = 0;
842
843 return true;
844 }
845
846 return *error_ppm + 10 < best_error_ppm;
847}
848
Zhenyu Wang2c072452009-06-05 15:38:42 +0800849static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200850vlv_find_best_dpll(const intel_limit_t *limit,
851 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200852 int target, int refclk, intel_clock_t *match_clock,
853 intel_clock_t *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700854{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200855 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300856 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300857 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300858 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300859 /* min update 19.2 MHz */
860 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300861 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700862
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300863 target *= 5; /* fast clock */
864
865 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700866
867 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300868 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300869 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300870 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300871 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300872 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700873 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300874 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200875 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300876
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300877 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
878 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300879
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300880 vlv_clock(refclk, &clock);
881
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300882 if (!intel_PLL_is_valid(dev, limit,
883 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300884 continue;
885
Imre Deakd5dd62b2015-03-17 11:40:03 +0200886 if (!vlv_PLL_is_optimal(dev, target,
887 &clock,
888 best_clock,
889 bestppm, &ppm))
890 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300891
Imre Deakd5dd62b2015-03-17 11:40:03 +0200892 *best_clock = clock;
893 bestppm = ppm;
894 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700895 }
896 }
897 }
898 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700899
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300900 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700901}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700902
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300903static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200904chv_find_best_dpll(const intel_limit_t *limit,
905 struct intel_crtc_state *crtc_state,
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300906 int target, int refclk, intel_clock_t *match_clock,
907 intel_clock_t *best_clock)
908{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200909 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300910 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200911 unsigned int best_error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300912 intel_clock_t clock;
913 uint64_t m2;
914 int found = false;
915
916 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200917 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300918
919 /*
920 * Based on hardware doc, the n always set to 1, and m1 always
921 * set to 2. If requires to support 200Mhz refclk, we need to
922 * revisit this because n may not 1 anymore.
923 */
924 clock.n = 1, clock.m1 = 2;
925 target *= 5; /* fast clock */
926
927 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
928 for (clock.p2 = limit->p2.p2_fast;
929 clock.p2 >= limit->p2.p2_slow;
930 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +0200931 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300932
933 clock.p = clock.p1 * clock.p2;
934
935 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
936 clock.n) << 22, refclk * clock.m1);
937
938 if (m2 > INT_MAX/clock.m1)
939 continue;
940
941 clock.m2 = m2;
942
943 chv_clock(refclk, &clock);
944
945 if (!intel_PLL_is_valid(dev, limit, &clock))
946 continue;
947
Imre Deak9ca3ba02015-03-17 11:40:05 +0200948 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
949 best_error_ppm, &error_ppm))
950 continue;
951
952 *best_clock = clock;
953 best_error_ppm = error_ppm;
954 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300955 }
956 }
957
958 return found;
959}
960
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200961bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
962 intel_clock_t *best_clock)
963{
964 int refclk = i9xx_get_refclk(crtc_state, 0);
965
966 return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
967 target_clock, refclk, NULL, best_clock);
968}
969
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300970bool intel_crtc_active(struct drm_crtc *crtc)
971{
972 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
973
974 /* Be paranoid as we can arrive here with only partial
975 * state retrieved from the hardware during setup.
976 *
Damien Lespiau241bfc32013-09-25 16:45:37 +0100977 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300978 * as Haswell has gained clock readout/fastboot support.
979 *
Dave Airlie66e514c2014-04-03 07:51:54 +1000980 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300981 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -0700982 *
983 * FIXME: The intel_crtc->active here should be switched to
984 * crtc->state->active once we have proper CRTC states wired up
985 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300986 */
Matt Roperc3d1f432015-03-09 10:19:23 -0700987 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200988 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300989}
990
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200991enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
992 enum pipe pipe)
993{
994 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
995 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
996
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200997 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200998}
999
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001000static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1001{
1002 struct drm_i915_private *dev_priv = dev->dev_private;
1003 u32 reg = PIPEDSL(pipe);
1004 u32 line1, line2;
1005 u32 line_mask;
1006
1007 if (IS_GEN2(dev))
1008 line_mask = DSL_LINEMASK_GEN2;
1009 else
1010 line_mask = DSL_LINEMASK_GEN3;
1011
1012 line1 = I915_READ(reg) & line_mask;
1013 mdelay(5);
1014 line2 = I915_READ(reg) & line_mask;
1015
1016 return line1 == line2;
1017}
1018
Keith Packardab7ad7f2010-10-03 00:33:06 -07001019/*
1020 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001021 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001022 *
1023 * After disabling a pipe, we can't wait for vblank in the usual way,
1024 * spinning on the vblank interrupt status bit, since we won't actually
1025 * see an interrupt when the pipe is disabled.
1026 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001027 * On Gen4 and above:
1028 * wait for the pipe register state bit to turn off
1029 *
1030 * Otherwise:
1031 * wait for the display line value to settle (it usually
1032 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001033 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001034 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001035static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001036{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001037 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001038 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001039 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001040 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001041
Keith Packardab7ad7f2010-10-03 00:33:06 -07001042 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001043 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001044
Keith Packardab7ad7f2010-10-03 00:33:06 -07001045 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +01001046 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1047 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001048 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001049 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001050 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001051 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001052 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001053 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001054}
1055
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001056/*
1057 * ibx_digital_port_connected - is the specified port connected?
1058 * @dev_priv: i915 private structure
1059 * @port: the port to test
1060 *
1061 * Returns true if @port is connected, false otherwise.
1062 */
1063bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
1064 struct intel_digital_port *port)
1065{
1066 u32 bit;
1067
Damien Lespiauc36346e2012-12-13 16:09:03 +00001068 if (HAS_PCH_IBX(dev_priv->dev)) {
Robin Schroereba905b2014-05-18 02:24:50 +02001069 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +00001070 case PORT_B:
1071 bit = SDE_PORTB_HOTPLUG;
1072 break;
1073 case PORT_C:
1074 bit = SDE_PORTC_HOTPLUG;
1075 break;
1076 case PORT_D:
1077 bit = SDE_PORTD_HOTPLUG;
1078 break;
1079 default:
1080 return true;
1081 }
1082 } else {
Robin Schroereba905b2014-05-18 02:24:50 +02001083 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +00001084 case PORT_B:
1085 bit = SDE_PORTB_HOTPLUG_CPT;
1086 break;
1087 case PORT_C:
1088 bit = SDE_PORTC_HOTPLUG_CPT;
1089 break;
1090 case PORT_D:
1091 bit = SDE_PORTD_HOTPLUG_CPT;
1092 break;
1093 default:
1094 return true;
1095 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001096 }
1097
1098 return I915_READ(SDEISR) & bit;
1099}
1100
Jesse Barnesb24e7172011-01-04 15:09:30 -08001101static const char *state_string(bool enabled)
1102{
1103 return enabled ? "on" : "off";
1104}
1105
1106/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001107void assert_pll(struct drm_i915_private *dev_priv,
1108 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001109{
1110 int reg;
1111 u32 val;
1112 bool cur_state;
1113
1114 reg = DPLL(pipe);
1115 val = I915_READ(reg);
1116 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001117 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001118 "PLL state assertion failure (expected %s, current %s)\n",
1119 state_string(state), state_string(cur_state));
1120}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001121
Jani Nikula23538ef2013-08-27 15:12:22 +03001122/* XXX: the dsi pll is shared between MIPI DSI ports */
1123static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1124{
1125 u32 val;
1126 bool cur_state;
1127
Ville Syrjäläa5805162015-05-26 20:42:30 +03001128 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001129 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001130 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001131
1132 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001133 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001134 "DSI PLL state assertion failure (expected %s, current %s)\n",
1135 state_string(state), state_string(cur_state));
1136}
1137#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1138#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1139
Daniel Vetter55607e82013-06-16 21:42:39 +02001140struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +02001141intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -08001142{
Daniel Vettere2b78262013-06-07 23:10:03 +02001143 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1144
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001145 if (crtc->config->shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +02001146 return NULL;
1147
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001148 return &dev_priv->shared_dplls[crtc->config->shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +02001149}
1150
Jesse Barnesb24e7172011-01-04 15:09:30 -08001151/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +02001152void assert_shared_dpll(struct drm_i915_private *dev_priv,
1153 struct intel_shared_dpll *pll,
1154 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001155{
Jesse Barnes040484a2011-01-03 12:14:26 -08001156 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +02001157 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001158
Chris Wilson92b27b02012-05-20 18:10:50 +01001159 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +02001160 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001161 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001162
Daniel Vetter53589012013-06-05 13:34:16 +02001163 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Rob Clarke2c719b2014-12-15 13:56:32 -05001164 I915_STATE_WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +02001165 "%s assertion failure (expected %s, current %s)\n",
1166 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001167}
Jesse Barnes040484a2011-01-03 12:14:26 -08001168
1169static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1170 enum pipe pipe, bool state)
1171{
1172 int reg;
1173 u32 val;
1174 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001175 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1176 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001177
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001178 if (HAS_DDI(dev_priv->dev)) {
1179 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -02001180 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001181 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -02001182 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001183 } else {
1184 reg = FDI_TX_CTL(pipe);
1185 val = I915_READ(reg);
1186 cur_state = !!(val & FDI_TX_ENABLE);
1187 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001188 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001189 "FDI TX state assertion failure (expected %s, current %s)\n",
1190 state_string(state), state_string(cur_state));
1191}
1192#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1193#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1194
1195static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1196 enum pipe pipe, bool state)
1197{
1198 int reg;
1199 u32 val;
1200 bool cur_state;
1201
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001202 reg = FDI_RX_CTL(pipe);
1203 val = I915_READ(reg);
1204 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001205 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001206 "FDI RX state assertion failure (expected %s, current %s)\n",
1207 state_string(state), state_string(cur_state));
1208}
1209#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1210#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1211
1212static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1213 enum pipe pipe)
1214{
1215 int reg;
1216 u32 val;
1217
1218 /* ILK FDI PLL is always enabled */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001219 if (INTEL_INFO(dev_priv->dev)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001220 return;
1221
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001222 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001223 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001224 return;
1225
Jesse Barnes040484a2011-01-03 12:14:26 -08001226 reg = FDI_TX_CTL(pipe);
1227 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001228 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
Jesse Barnes040484a2011-01-03 12:14:26 -08001229}
1230
Daniel Vetter55607e82013-06-16 21:42:39 +02001231void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1232 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001233{
1234 int reg;
1235 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001236 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001237
1238 reg = FDI_RX_CTL(pipe);
1239 val = I915_READ(reg);
Daniel Vetter55607e82013-06-16 21:42:39 +02001240 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001241 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001242 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1243 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001244}
1245
Daniel Vetterb680c372014-09-19 18:27:27 +02001246void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1247 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001248{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001249 struct drm_device *dev = dev_priv->dev;
1250 int pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001251 u32 val;
1252 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001253 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001254
Jani Nikulabedd4db2014-08-22 15:04:13 +03001255 if (WARN_ON(HAS_DDI(dev)))
1256 return;
1257
1258 if (HAS_PCH_SPLIT(dev)) {
1259 u32 port_sel;
1260
Jesse Barnesea0760c2011-01-04 15:09:32 -08001261 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001262 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1263
1264 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1265 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1266 panel_pipe = PIPE_B;
1267 /* XXX: else fix for eDP */
1268 } else if (IS_VALLEYVIEW(dev)) {
1269 /* presumably write lock depends on pipe, not port select */
1270 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1271 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001272 } else {
1273 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001274 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1275 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001276 }
1277
1278 val = I915_READ(pp_reg);
1279 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001280 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001281 locked = false;
1282
Rob Clarke2c719b2014-12-15 13:56:32 -05001283 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001284 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001285 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001286}
1287
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001288static void assert_cursor(struct drm_i915_private *dev_priv,
1289 enum pipe pipe, bool state)
1290{
1291 struct drm_device *dev = dev_priv->dev;
1292 bool cur_state;
1293
Paulo Zanonid9d82082014-02-27 16:30:56 -03001294 if (IS_845G(dev) || IS_I865G(dev))
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001295 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001296 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001297 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001298
Rob Clarke2c719b2014-12-15 13:56:32 -05001299 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001300 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1301 pipe_name(pipe), state_string(state), state_string(cur_state));
1302}
1303#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1304#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1305
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001306void assert_pipe(struct drm_i915_private *dev_priv,
1307 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001308{
1309 int reg;
1310 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001311 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001312 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1313 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001314
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001315 /* if we need the pipe quirk it must be always on */
1316 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1317 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001318 state = true;
1319
Daniel Vetterf458ebb2014-09-30 10:56:39 +02001320 if (!intel_display_power_is_enabled(dev_priv,
Paulo Zanonib97186f2013-05-03 12:15:36 -03001321 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001322 cur_state = false;
1323 } else {
1324 reg = PIPECONF(cpu_transcoder);
1325 val = I915_READ(reg);
1326 cur_state = !!(val & PIPECONF_ENABLE);
1327 }
1328
Rob Clarke2c719b2014-12-15 13:56:32 -05001329 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001330 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001331 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001332}
1333
Chris Wilson931872f2012-01-16 23:01:13 +00001334static void assert_plane(struct drm_i915_private *dev_priv,
1335 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001336{
1337 int reg;
1338 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001339 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001340
1341 reg = DSPCNTR(plane);
1342 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001343 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001344 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001345 "plane %c assertion failure (expected %s, current %s)\n",
1346 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001347}
1348
Chris Wilson931872f2012-01-16 23:01:13 +00001349#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1350#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1351
Jesse Barnesb24e7172011-01-04 15:09:30 -08001352static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1353 enum pipe pipe)
1354{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001355 struct drm_device *dev = dev_priv->dev;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001356 int reg, i;
1357 u32 val;
1358 int cur_pipe;
1359
Ville Syrjälä653e1022013-06-04 13:49:05 +03001360 /* Primary planes are fixed to pipes on gen4+ */
1361 if (INTEL_INFO(dev)->gen >= 4) {
Adam Jackson28c057942011-10-07 14:38:42 -04001362 reg = DSPCNTR(pipe);
1363 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001364 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001365 "plane %c assertion failure, should be disabled but not\n",
1366 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001367 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001368 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001369
Jesse Barnesb24e7172011-01-04 15:09:30 -08001370 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001371 for_each_pipe(dev_priv, i) {
Jesse Barnesb24e7172011-01-04 15:09:30 -08001372 reg = DSPCNTR(i);
1373 val = I915_READ(reg);
1374 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1375 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001376 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001377 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1378 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001379 }
1380}
1381
Jesse Barnes19332d72013-03-28 09:55:38 -07001382static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1383 enum pipe pipe)
1384{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001385 struct drm_device *dev = dev_priv->dev;
Damien Lespiau1fe47782014-03-03 17:31:47 +00001386 int reg, sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001387 u32 val;
1388
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001389 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001390 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001391 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001392 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001393 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1394 sprite, pipe_name(pipe));
1395 }
1396 } else if (IS_VALLEYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001397 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +00001398 reg = SPCNTR(pipe, sprite);
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001399 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001400 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001401 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001402 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001403 }
1404 } else if (INTEL_INFO(dev)->gen >= 7) {
1405 reg = SPRCTL(pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001406 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001407 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001408 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001409 plane_name(pipe), pipe_name(pipe));
1410 } else if (INTEL_INFO(dev)->gen >= 5) {
1411 reg = DVSCNTR(pipe);
1412 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001413 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001414 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1415 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001416 }
1417}
1418
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001419static void assert_vblank_disabled(struct drm_crtc *crtc)
1420{
Rob Clarke2c719b2014-12-15 13:56:32 -05001421 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001422 drm_crtc_vblank_put(crtc);
1423}
1424
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001425static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
Jesse Barnes92f25842011-01-04 15:09:34 -08001426{
1427 u32 val;
1428 bool enabled;
1429
Rob Clarke2c719b2014-12-15 13:56:32 -05001430 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001431
Jesse Barnes92f25842011-01-04 15:09:34 -08001432 val = I915_READ(PCH_DREF_CONTROL);
1433 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1434 DREF_SUPERSPREAD_SOURCE_MASK));
Rob Clarke2c719b2014-12-15 13:56:32 -05001435 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
Jesse Barnes92f25842011-01-04 15:09:34 -08001436}
1437
Daniel Vetterab9412b2013-05-03 11:49:46 +02001438static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1439 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001440{
1441 int reg;
1442 u32 val;
1443 bool enabled;
1444
Daniel Vetterab9412b2013-05-03 11:49:46 +02001445 reg = PCH_TRANSCONF(pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001446 val = I915_READ(reg);
1447 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001448 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001449 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1450 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001451}
1452
Keith Packard4e634382011-08-06 10:39:45 -07001453static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1454 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001455{
1456 if ((val & DP_PORT_EN) == 0)
1457 return false;
1458
1459 if (HAS_PCH_CPT(dev_priv->dev)) {
1460 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1461 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1462 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1463 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001464 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1465 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1466 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001467 } else {
1468 if ((val & DP_PIPE_MASK) != (pipe << 30))
1469 return false;
1470 }
1471 return true;
1472}
1473
Keith Packard1519b992011-08-06 10:35:34 -07001474static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1475 enum pipe pipe, u32 val)
1476{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001477 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001478 return false;
1479
1480 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001481 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001482 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001483 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1484 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1485 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001486 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001487 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001488 return false;
1489 }
1490 return true;
1491}
1492
1493static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1494 enum pipe pipe, u32 val)
1495{
1496 if ((val & LVDS_PORT_EN) == 0)
1497 return false;
1498
1499 if (HAS_PCH_CPT(dev_priv->dev)) {
1500 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1501 return false;
1502 } else {
1503 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1504 return false;
1505 }
1506 return true;
1507}
1508
1509static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1510 enum pipe pipe, u32 val)
1511{
1512 if ((val & ADPA_DAC_ENABLE) == 0)
1513 return false;
1514 if (HAS_PCH_CPT(dev_priv->dev)) {
1515 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1516 return false;
1517 } else {
1518 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1519 return false;
1520 }
1521 return true;
1522}
1523
Jesse Barnes291906f2011-02-02 12:28:03 -08001524static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001525 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001526{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001527 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001528 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001529 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001530 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001531
Rob Clarke2c719b2014-12-15 13:56:32 -05001532 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001533 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001534 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001535}
1536
1537static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1538 enum pipe pipe, int reg)
1539{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001540 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001541 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001542 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001543 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001544
Rob Clarke2c719b2014-12-15 13:56:32 -05001545 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001546 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001547 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001548}
1549
1550static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1551 enum pipe pipe)
1552{
1553 int reg;
1554 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001555
Keith Packardf0575e92011-07-25 22:12:43 -07001556 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1557 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1558 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001559
1560 reg = PCH_ADPA;
1561 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001562 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001563 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001564 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001565
1566 reg = PCH_LVDS;
1567 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001568 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001569 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001570 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001571
Paulo Zanonie2debe92013-02-18 19:00:27 -03001572 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1573 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1574 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001575}
1576
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001577static void intel_init_dpio(struct drm_device *dev)
1578{
1579 struct drm_i915_private *dev_priv = dev->dev_private;
1580
1581 if (!IS_VALLEYVIEW(dev))
1582 return;
1583
Chon Ming Leea09cadd2014-04-09 13:28:14 +03001584 /*
1585 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1586 * CHV x1 PHY (DP/HDMI D)
1587 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1588 */
1589 if (IS_CHERRYVIEW(dev)) {
1590 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1591 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1592 } else {
1593 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1594 }
Jesse Barnes5382f5f352013-12-16 16:34:24 -08001595}
1596
Ville Syrjäläd288f652014-10-28 13:20:22 +02001597static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001598 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001599{
Daniel Vetter426115c2013-07-11 22:13:42 +02001600 struct drm_device *dev = crtc->base.dev;
1601 struct drm_i915_private *dev_priv = dev->dev_private;
1602 int reg = DPLL(crtc->pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02001603 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001604
Daniel Vetter426115c2013-07-11 22:13:42 +02001605 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001606
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001607 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001608 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1609
1610 /* PLL is protected by panel, make sure we can write it */
Jani Nikula6a9e7362014-08-22 15:06:35 +03001611 if (IS_MOBILE(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001612 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001613
Daniel Vetter426115c2013-07-11 22:13:42 +02001614 I915_WRITE(reg, dpll);
1615 POSTING_READ(reg);
1616 udelay(150);
1617
1618 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1619 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1620
Ville Syrjäläd288f652014-10-28 13:20:22 +02001621 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
Daniel Vetter426115c2013-07-11 22:13:42 +02001622 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001623
1624 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001625 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001626 POSTING_READ(reg);
1627 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001628 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001629 POSTING_READ(reg);
1630 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001631 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001632 POSTING_READ(reg);
1633 udelay(150); /* wait for warmup */
1634}
1635
Ville Syrjäläd288f652014-10-28 13:20:22 +02001636static void chv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001637 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001638{
1639 struct drm_device *dev = crtc->base.dev;
1640 struct drm_i915_private *dev_priv = dev->dev_private;
1641 int pipe = crtc->pipe;
1642 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001643 u32 tmp;
1644
1645 assert_pipe_disabled(dev_priv, crtc->pipe);
1646
1647 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1648
Ville Syrjäläa5805162015-05-26 20:42:30 +03001649 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001650
1651 /* Enable back the 10bit clock to display controller */
1652 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1653 tmp |= DPIO_DCLKP_EN;
1654 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1655
Ville Syrjälä54433e92015-05-26 20:42:31 +03001656 mutex_unlock(&dev_priv->sb_lock);
1657
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001658 /*
1659 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1660 */
1661 udelay(1);
1662
1663 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001664 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001665
1666 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001667 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001668 DRM_ERROR("PLL %d failed to lock\n", pipe);
1669
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001670 /* not sure when this should be written */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001671 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001672 POSTING_READ(DPLL_MD(pipe));
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001673}
1674
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001675static int intel_num_dvo_pipes(struct drm_device *dev)
1676{
1677 struct intel_crtc *crtc;
1678 int count = 0;
1679
1680 for_each_intel_crtc(dev, crtc)
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001681 count += crtc->base.state->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001682 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001683
1684 return count;
1685}
1686
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001687static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001688{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001689 struct drm_device *dev = crtc->base.dev;
1690 struct drm_i915_private *dev_priv = dev->dev_private;
1691 int reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001692 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001693
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001694 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001695
1696 /* No really, not for ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001697 BUG_ON(INTEL_INFO(dev)->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001698
1699 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001700 if (IS_MOBILE(dev) && !IS_I830(dev))
1701 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001702
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001703 /* Enable DVO 2x clock on both PLLs if necessary */
1704 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1705 /*
1706 * It appears to be important that we don't enable this
1707 * for the current pipe before otherwise configuring the
1708 * PLL. No idea how this should be handled if multiple
1709 * DVO outputs are enabled simultaneosly.
1710 */
1711 dpll |= DPLL_DVO_2X_MODE;
1712 I915_WRITE(DPLL(!crtc->pipe),
1713 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1714 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001715
1716 /* Wait for the clocks to stabilize. */
1717 POSTING_READ(reg);
1718 udelay(150);
1719
1720 if (INTEL_INFO(dev)->gen >= 4) {
1721 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001722 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001723 } else {
1724 /* The pixel multiplier can only be updated once the
1725 * DPLL is enabled and the clocks are stable.
1726 *
1727 * So write it again.
1728 */
1729 I915_WRITE(reg, dpll);
1730 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001731
1732 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001733 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001734 POSTING_READ(reg);
1735 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001736 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001737 POSTING_READ(reg);
1738 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001739 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001740 POSTING_READ(reg);
1741 udelay(150); /* wait for warmup */
1742}
1743
1744/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001745 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001746 * @dev_priv: i915 private structure
1747 * @pipe: pipe PLL to disable
1748 *
1749 * Disable the PLL for @pipe, making sure the pipe is off first.
1750 *
1751 * Note! This is for pre-ILK only.
1752 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001753static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001754{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001755 struct drm_device *dev = crtc->base.dev;
1756 struct drm_i915_private *dev_priv = dev->dev_private;
1757 enum pipe pipe = crtc->pipe;
1758
1759 /* Disable DVO 2x clock on both PLLs if necessary */
1760 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001761 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001762 !intel_num_dvo_pipes(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001763 I915_WRITE(DPLL(PIPE_B),
1764 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1765 I915_WRITE(DPLL(PIPE_A),
1766 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1767 }
1768
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001769 /* Don't disable pipe or pipe PLLs if needed */
1770 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1771 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001772 return;
1773
1774 /* Make sure the pipe isn't still relying on us */
1775 assert_pipe_disabled(dev_priv, pipe);
1776
Daniel Vetter50b44a42013-06-05 13:34:33 +02001777 I915_WRITE(DPLL(pipe), 0);
1778 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001779}
1780
Jesse Barnesf6071162013-10-01 10:41:38 -07001781static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1782{
1783 u32 val = 0;
1784
1785 /* Make sure the pipe isn't still relying on us */
1786 assert_pipe_disabled(dev_priv, pipe);
1787
Imre Deake5cbfbf2014-01-09 17:08:16 +02001788 /*
1789 * Leave integrated clock source and reference clock enabled for pipe B.
1790 * The latter is needed for VGA hotplug / manual detection.
1791 */
Jesse Barnesf6071162013-10-01 10:41:38 -07001792 if (pipe == PIPE_B)
Imre Deake5cbfbf2014-01-09 17:08:16 +02001793 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07001794 I915_WRITE(DPLL(pipe), val);
1795 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001796
1797}
1798
1799static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1800{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001801 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001802 u32 val;
1803
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001804 /* Make sure the pipe isn't still relying on us */
1805 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001806
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001807 /* Set PLL en = 0 */
Ville Syrjäläd17ec4c2014-06-28 02:03:59 +03001808 val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001809 if (pipe != PIPE_A)
1810 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1811 I915_WRITE(DPLL(pipe), val);
1812 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001813
Ville Syrjäläa5805162015-05-26 20:42:30 +03001814 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001815
1816 /* Disable 10bit clock to display controller */
1817 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1818 val &= ~DPIO_DCLKP_EN;
1819 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1820
Ville Syrjälä61407f62014-05-27 16:32:55 +03001821 /* disable left/right clock distribution */
1822 if (pipe != PIPE_B) {
1823 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1824 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1825 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1826 } else {
1827 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1828 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1829 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1830 }
1831
Ville Syrjäläa5805162015-05-26 20:42:30 +03001832 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001833}
1834
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001835void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001836 struct intel_digital_port *dport,
1837 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001838{
1839 u32 port_mask;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001840 int dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001841
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001842 switch (dport->port) {
1843 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001844 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001845 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001846 break;
1847 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001848 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001849 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001850 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001851 break;
1852 case PORT_D:
1853 port_mask = DPLL_PORTD_READY_MASK;
1854 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001855 break;
1856 default:
1857 BUG();
1858 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001859
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001860 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1861 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1862 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001863}
1864
Daniel Vetterb14b1052014-04-24 23:55:13 +02001865static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1866{
1867 struct drm_device *dev = crtc->base.dev;
1868 struct drm_i915_private *dev_priv = dev->dev_private;
1869 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1870
Chris Wilsonbe19f0f2014-05-28 16:16:42 +01001871 if (WARN_ON(pll == NULL))
1872 return;
1873
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001874 WARN_ON(!pll->config.crtc_mask);
Daniel Vetterb14b1052014-04-24 23:55:13 +02001875 if (pll->active == 0) {
1876 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1877 WARN_ON(pll->on);
1878 assert_shared_dpll_disabled(dev_priv, pll);
1879
1880 pll->mode_set(dev_priv, pll);
1881 }
1882}
1883
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001884/**
Daniel Vetter85b38942014-04-24 23:55:14 +02001885 * intel_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001886 * @dev_priv: i915 private structure
1887 * @pipe: pipe PLL to enable
1888 *
1889 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1890 * drives the transcoder clock.
1891 */
Daniel Vetter85b38942014-04-24 23:55:14 +02001892static void intel_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001893{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001894 struct drm_device *dev = crtc->base.dev;
1895 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001896 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001897
Daniel Vetter87a875b2013-06-05 13:34:19 +02001898 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001899 return;
1900
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001901 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001902 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001903
Damien Lespiau74dd6922014-07-29 18:06:17 +01001904 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
Daniel Vetter46edb022013-06-05 13:34:12 +02001905 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001906 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001907
Daniel Vettercdbd2312013-06-05 13:34:03 +02001908 if (pll->active++) {
1909 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001910 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001911 return;
1912 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001913 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001914
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001915 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1916
Daniel Vetter46edb022013-06-05 13:34:12 +02001917 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001918 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001919 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001920}
1921
Damien Lespiauf6daaec2014-08-09 23:00:56 +01001922static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001923{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001924 struct drm_device *dev = crtc->base.dev;
1925 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001926 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001927
Jesse Barnes92f25842011-01-04 15:09:34 -08001928 /* PCH only available on ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001929 BUG_ON(INTEL_INFO(dev)->gen < 5);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02001930 if (pll == NULL)
1931 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001932
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02001933 if (WARN_ON(!(pll->config.crtc_mask & (1 << drm_crtc_index(&crtc->base)))))
Chris Wilson48da64a2012-05-13 20:16:12 +01001934 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001935
Daniel Vetter46edb022013-06-05 13:34:12 +02001936 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1937 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001938 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001939
Chris Wilson48da64a2012-05-13 20:16:12 +01001940 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001941 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001942 return;
1943 }
1944
Daniel Vettere9d69442013-06-05 13:34:15 +02001945 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001946 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001947 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001948 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001949
Daniel Vetter46edb022013-06-05 13:34:12 +02001950 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001951 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001952 pll->on = false;
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001953
1954 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
Jesse Barnes92f25842011-01-04 15:09:34 -08001955}
1956
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001957static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1958 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001959{
Daniel Vetter23670b322012-11-01 09:15:30 +01001960 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001961 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001962 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001963 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001964
1965 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001966 BUG_ON(!HAS_PCH_SPLIT(dev));
Jesse Barnes040484a2011-01-03 12:14:26 -08001967
1968 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001969 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001970 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001971
1972 /* FDI must be feeding us bits for PCH ports */
1973 assert_fdi_tx_enabled(dev_priv, pipe);
1974 assert_fdi_rx_enabled(dev_priv, pipe);
1975
Daniel Vetter23670b322012-11-01 09:15:30 +01001976 if (HAS_PCH_CPT(dev)) {
1977 /* Workaround: Set the timing override bit before enabling the
1978 * pch transcoder. */
1979 reg = TRANS_CHICKEN2(pipe);
1980 val = I915_READ(reg);
1981 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1982 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001983 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001984
Daniel Vetterab9412b2013-05-03 11:49:46 +02001985 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001986 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001987 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001988
1989 if (HAS_PCH_IBX(dev_priv->dev)) {
1990 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001991 * Make the BPC in transcoder be consistent with
1992 * that in pipeconf reg. For HDMI we must use 8bpc
1993 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07001994 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001995 val &= ~PIPECONF_BPC_MASK;
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001996 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
1997 val |= PIPECONF_8BPC;
1998 else
1999 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07002000 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002001
2002 val &= ~TRANS_INTERLACE_MASK;
2003 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002004 if (HAS_PCH_IBX(dev_priv->dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002005 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002006 val |= TRANS_LEGACY_INTERLACED_ILK;
2007 else
2008 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002009 else
2010 val |= TRANS_PROGRESSIVE;
2011
Jesse Barnes040484a2011-01-03 12:14:26 -08002012 I915_WRITE(reg, val | TRANS_ENABLE);
2013 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002014 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08002015}
2016
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002017static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02002018 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08002019{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002020 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002021
2022 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03002023 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002024
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002025 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01002026 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02002027 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002028
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002029 /* Workaround: set timing override bit. */
2030 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002031 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002032 I915_WRITE(_TRANSA_CHICKEN2, val);
2033
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02002034 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02002035 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002036
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02002037 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
2038 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02002039 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002040 else
2041 val |= TRANS_PROGRESSIVE;
2042
Daniel Vetterab9412b2013-05-03 11:49:46 +02002043 I915_WRITE(LPT_TRANSCONF, val);
2044 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02002045 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002046}
2047
Paulo Zanonib8a4f402012-10-31 18:12:42 -02002048static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
2049 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08002050{
Daniel Vetter23670b322012-11-01 09:15:30 +01002051 struct drm_device *dev = dev_priv->dev;
2052 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08002053
2054 /* FDI relies on the transcoder */
2055 assert_fdi_tx_disabled(dev_priv, pipe);
2056 assert_fdi_rx_disabled(dev_priv, pipe);
2057
Jesse Barnes291906f2011-02-02 12:28:03 -08002058 /* Ports must be off as well */
2059 assert_pch_ports_disabled(dev_priv, pipe);
2060
Daniel Vetterab9412b2013-05-03 11:49:46 +02002061 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002062 val = I915_READ(reg);
2063 val &= ~TRANS_ENABLE;
2064 I915_WRITE(reg, val);
2065 /* wait for PCH transcoder off, transcoder state */
2066 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002067 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01002068
2069 if (!HAS_PCH_IBX(dev)) {
2070 /* Workaround: Clear the timing override chicken bit again. */
2071 reg = TRANS_CHICKEN2(pipe);
2072 val = I915_READ(reg);
2073 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2074 I915_WRITE(reg, val);
2075 }
Jesse Barnes040484a2011-01-03 12:14:26 -08002076}
2077
Paulo Zanoniab4d9662012-10-31 18:12:55 -02002078static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002079{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002080 u32 val;
2081
Daniel Vetterab9412b2013-05-03 11:49:46 +02002082 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002083 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02002084 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002085 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02002086 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02002087 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002088
2089 /* Workaround: clear timing override bit. */
2090 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002091 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002092 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08002093}
2094
2095/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002096 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02002097 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08002098 *
Paulo Zanoni03722642014-01-17 13:51:09 -02002099 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08002100 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002101 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02002102static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002103{
Paulo Zanoni03722642014-01-17 13:51:09 -02002104 struct drm_device *dev = crtc->base.dev;
2105 struct drm_i915_private *dev_priv = dev->dev_private;
2106 enum pipe pipe = crtc->pipe;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002107 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2108 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002109 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002110 int reg;
2111 u32 val;
2112
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002113 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
2114
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002115 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002116 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002117 assert_sprites_disabled(dev_priv, pipe);
2118
Paulo Zanoni681e5812012-12-06 11:12:38 -02002119 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002120 pch_transcoder = TRANSCODER_A;
2121 else
2122 pch_transcoder = pipe;
2123
Jesse Barnesb24e7172011-01-04 15:09:30 -08002124 /*
2125 * A pipe without a PLL won't actually be able to drive bits from
2126 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2127 * need the check.
2128 */
Imre Deak50360402015-01-16 00:55:16 -08002129 if (HAS_GMCH_DISPLAY(dev_priv->dev))
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002130 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03002131 assert_dsi_pll_enabled(dev_priv);
2132 else
2133 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002134 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002135 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002136 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002137 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002138 assert_fdi_tx_pll_enabled(dev_priv,
2139 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08002140 }
2141 /* FIXME: assert CPU port conditions for SNB+ */
2142 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08002143
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002144 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002145 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002146 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002147 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2148 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002149 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002150 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002151
2152 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002153 POSTING_READ(reg);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002154}
2155
2156/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002157 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002158 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002159 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002160 * Disable the pipe of @crtc, making sure that various hardware
2161 * specific requirements are met, if applicable, e.g. plane
2162 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002163 *
2164 * Will wait until the pipe has shut down before returning.
2165 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002166static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002167{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002168 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002169 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002170 enum pipe pipe = crtc->pipe;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002171 int reg;
2172 u32 val;
2173
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002174 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2175
Jesse Barnesb24e7172011-01-04 15:09:30 -08002176 /*
2177 * Make sure planes won't keep trying to pump pixels to us,
2178 * or we might hang the display.
2179 */
2180 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002181 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002182 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002183
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002184 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002185 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002186 if ((val & PIPECONF_ENABLE) == 0)
2187 return;
2188
Ville Syrjälä67adc642014-08-15 01:21:57 +03002189 /*
2190 * Double wide has implications for planes
2191 * so best keep it disabled when not needed.
2192 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002193 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002194 val &= ~PIPECONF_DOUBLE_WIDE;
2195
2196 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002197 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2198 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002199 val &= ~PIPECONF_ENABLE;
2200
2201 I915_WRITE(reg, val);
2202 if ((val & PIPECONF_ENABLE) == 0)
2203 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002204}
2205
Chris Wilson693db182013-03-05 14:52:39 +00002206static bool need_vtd_wa(struct drm_device *dev)
2207{
2208#ifdef CONFIG_INTEL_IOMMU
2209 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2210 return true;
2211#endif
2212 return false;
2213}
2214
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002215unsigned int
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002216intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
2217 uint64_t fb_format_modifier)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002218{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002219 unsigned int tile_height;
2220 uint32_t pixel_bytes;
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002221
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002222 switch (fb_format_modifier) {
2223 case DRM_FORMAT_MOD_NONE:
2224 tile_height = 1;
2225 break;
2226 case I915_FORMAT_MOD_X_TILED:
2227 tile_height = IS_GEN2(dev) ? 16 : 8;
2228 break;
2229 case I915_FORMAT_MOD_Y_TILED:
2230 tile_height = 32;
2231 break;
2232 case I915_FORMAT_MOD_Yf_TILED:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002233 pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
2234 switch (pixel_bytes) {
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002235 default:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002236 case 1:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002237 tile_height = 64;
2238 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002239 case 2:
2240 case 4:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002241 tile_height = 32;
2242 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002243 case 8:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002244 tile_height = 16;
2245 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002246 case 16:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002247 WARN_ONCE(1,
2248 "128-bit pixels are not supported for display!");
2249 tile_height = 16;
2250 break;
2251 }
2252 break;
2253 default:
2254 MISSING_CASE(fb_format_modifier);
2255 tile_height = 1;
2256 break;
2257 }
Daniel Vetter091df6c2015-02-10 17:16:10 +00002258
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002259 return tile_height;
2260}
2261
2262unsigned int
2263intel_fb_align_height(struct drm_device *dev, unsigned int height,
2264 uint32_t pixel_format, uint64_t fb_format_modifier)
2265{
2266 return ALIGN(height, intel_tile_height(dev, pixel_format,
2267 fb_format_modifier));
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002268}
2269
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002270static int
2271intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
2272 const struct drm_plane_state *plane_state)
2273{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002274 struct intel_rotation_info *info = &view->rotation_info;
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002275 unsigned int tile_height, tile_pitch;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002276
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002277 *view = i915_ggtt_view_normal;
2278
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002279 if (!plane_state)
2280 return 0;
2281
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002282 if (!intel_rotation_90_or_270(plane_state->rotation))
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002283 return 0;
2284
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002285 *view = i915_ggtt_view_rotated;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002286
2287 info->height = fb->height;
2288 info->pixel_format = fb->pixel_format;
2289 info->pitch = fb->pitches[0];
2290 info->fb_modifier = fb->modifier[0];
2291
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002292 tile_height = intel_tile_height(fb->dev, fb->pixel_format,
2293 fb->modifier[0]);
2294 tile_pitch = PAGE_SIZE / tile_height;
2295 info->width_pages = DIV_ROUND_UP(fb->pitches[0], tile_pitch);
2296 info->height_pages = DIV_ROUND_UP(fb->height, tile_height);
2297 info->size = info->width_pages * info->height_pages * PAGE_SIZE;
2298
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002299 return 0;
2300}
2301
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002302static unsigned int intel_linear_alignment(struct drm_i915_private *dev_priv)
2303{
2304 if (INTEL_INFO(dev_priv)->gen >= 9)
2305 return 256 * 1024;
Ville Syrjälä985b8bb2015-06-11 16:31:15 +03002306 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
2307 IS_VALLEYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002308 return 128 * 1024;
2309 else if (INTEL_INFO(dev_priv)->gen >= 4)
2310 return 4 * 1024;
2311 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03002312 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002313}
2314
Chris Wilson127bd2a2010-07-23 23:32:05 +01002315int
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002316intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2317 struct drm_framebuffer *fb,
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002318 const struct drm_plane_state *plane_state,
John Harrison91af1272015-06-18 13:14:56 +01002319 struct intel_engine_cs *pipelined,
2320 struct drm_i915_gem_request **pipelined_request)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002321{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002322 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002323 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002324 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002325 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002326 u32 alignment;
2327 int ret;
2328
Matt Roperebcdd392014-07-09 16:22:11 -07002329 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2330
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002331 switch (fb->modifier[0]) {
2332 case DRM_FORMAT_MOD_NONE:
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002333 alignment = intel_linear_alignment(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002334 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002335 case I915_FORMAT_MOD_X_TILED:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002336 if (INTEL_INFO(dev)->gen >= 9)
2337 alignment = 256 * 1024;
2338 else {
2339 /* pin() will align the object as required by fence */
2340 alignment = 0;
2341 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002342 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002343 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiau1327b9a2015-02-27 11:15:20 +00002344 case I915_FORMAT_MOD_Yf_TILED:
2345 if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2346 "Y tiling bo slipped through, driver bug!\n"))
2347 return -EINVAL;
2348 alignment = 1 * 1024 * 1024;
2349 break;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002350 default:
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002351 MISSING_CASE(fb->modifier[0]);
2352 return -EINVAL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002353 }
2354
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002355 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2356 if (ret)
2357 return ret;
2358
Chris Wilson693db182013-03-05 14:52:39 +00002359 /* Note that the w/a also requires 64 PTE of padding following the
2360 * bo. We currently fill all unused PTE with the shadow page and so
2361 * we should always have valid PTE following the scanout preventing
2362 * the VT-d warning.
2363 */
2364 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2365 alignment = 256 * 1024;
2366
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002367 /*
2368 * Global gtt pte registers are special registers which actually forward
2369 * writes to a chunk of system memory. Which means that there is no risk
2370 * that the register values disappear as soon as we call
2371 * intel_runtime_pm_put(), so it is correct to wrap only the
2372 * pin/unpin/fence and not more.
2373 */
2374 intel_runtime_pm_get(dev_priv);
2375
Chris Wilsonce453d82011-02-21 14:43:56 +00002376 dev_priv->mm.interruptible = false;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00002377 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
John Harrison91af1272015-06-18 13:14:56 +01002378 pipelined_request, &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002379 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00002380 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002381
2382 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2383 * fence, whereas 965+ only requires a fence if using
2384 * framebuffer compression. For simplicity, we always install
2385 * a fence as the cost is not that onerous.
2386 */
Chris Wilson06d98132012-04-17 15:31:24 +01002387 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002388 if (ret)
2389 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002390
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002391 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002392
Chris Wilsonce453d82011-02-21 14:43:56 +00002393 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002394 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002395 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002396
2397err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002398 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilsonce453d82011-02-21 14:43:56 +00002399err_interruptible:
2400 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002401 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002402 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002403}
2404
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002405static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
2406 const struct drm_plane_state *plane_state)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002407{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002408 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002409 struct i915_ggtt_view view;
2410 int ret;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002411
Matt Roperebcdd392014-07-09 16:22:11 -07002412 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2413
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002414 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2415 WARN_ONCE(ret, "Couldn't get view from plane state!");
2416
Chris Wilson1690e1e2011-12-14 13:57:08 +01002417 i915_gem_object_unpin_fence(obj);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002418 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002419}
2420
Daniel Vetterc2c75132012-07-05 12:17:30 +02002421/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2422 * is assumed to be a power-of-two. */
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002423unsigned long intel_gen4_compute_page_offset(struct drm_i915_private *dev_priv,
2424 int *x, int *y,
Chris Wilsonbc752862013-02-21 20:04:31 +00002425 unsigned int tiling_mode,
2426 unsigned int cpp,
2427 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002428{
Chris Wilsonbc752862013-02-21 20:04:31 +00002429 if (tiling_mode != I915_TILING_NONE) {
2430 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002431
Chris Wilsonbc752862013-02-21 20:04:31 +00002432 tile_rows = *y / 8;
2433 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002434
Chris Wilsonbc752862013-02-21 20:04:31 +00002435 tiles = *x / (512/cpp);
2436 *x %= 512/cpp;
2437
2438 return tile_rows * pitch * 8 + tiles * 4096;
2439 } else {
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002440 unsigned int alignment = intel_linear_alignment(dev_priv) - 1;
Chris Wilsonbc752862013-02-21 20:04:31 +00002441 unsigned int offset;
2442
2443 offset = *y * pitch + *x * cpp;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002444 *y = (offset & alignment) / pitch;
2445 *x = ((offset & alignment) - *y * pitch) / cpp;
2446 return offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002447 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002448}
2449
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002450static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002451{
2452 switch (format) {
2453 case DISPPLANE_8BPP:
2454 return DRM_FORMAT_C8;
2455 case DISPPLANE_BGRX555:
2456 return DRM_FORMAT_XRGB1555;
2457 case DISPPLANE_BGRX565:
2458 return DRM_FORMAT_RGB565;
2459 default:
2460 case DISPPLANE_BGRX888:
2461 return DRM_FORMAT_XRGB8888;
2462 case DISPPLANE_RGBX888:
2463 return DRM_FORMAT_XBGR8888;
2464 case DISPPLANE_BGRX101010:
2465 return DRM_FORMAT_XRGB2101010;
2466 case DISPPLANE_RGBX101010:
2467 return DRM_FORMAT_XBGR2101010;
2468 }
2469}
2470
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002471static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2472{
2473 switch (format) {
2474 case PLANE_CTL_FORMAT_RGB_565:
2475 return DRM_FORMAT_RGB565;
2476 default:
2477 case PLANE_CTL_FORMAT_XRGB_8888:
2478 if (rgb_order) {
2479 if (alpha)
2480 return DRM_FORMAT_ABGR8888;
2481 else
2482 return DRM_FORMAT_XBGR8888;
2483 } else {
2484 if (alpha)
2485 return DRM_FORMAT_ARGB8888;
2486 else
2487 return DRM_FORMAT_XRGB8888;
2488 }
2489 case PLANE_CTL_FORMAT_XRGB_2101010:
2490 if (rgb_order)
2491 return DRM_FORMAT_XBGR2101010;
2492 else
2493 return DRM_FORMAT_XRGB2101010;
2494 }
2495}
2496
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002497static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002498intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2499 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002500{
2501 struct drm_device *dev = crtc->base.dev;
2502 struct drm_i915_gem_object *obj = NULL;
2503 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002504 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002505 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2506 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2507 PAGE_SIZE);
2508
2509 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002510
Chris Wilsonff2652e2014-03-10 08:07:02 +00002511 if (plane_config->size == 0)
2512 return false;
2513
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002514 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2515 base_aligned,
2516 base_aligned,
2517 size_aligned);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002518 if (!obj)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002519 return false;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002520
Damien Lespiau49af4492015-01-20 12:51:44 +00002521 obj->tiling_mode = plane_config->tiling;
2522 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002523 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002524
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002525 mode_cmd.pixel_format = fb->pixel_format;
2526 mode_cmd.width = fb->width;
2527 mode_cmd.height = fb->height;
2528 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002529 mode_cmd.modifier[0] = fb->modifier[0];
2530 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002531
2532 mutex_lock(&dev->struct_mutex);
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002533 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002534 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002535 DRM_DEBUG_KMS("intel fb init failed\n");
2536 goto out_unref_obj;
2537 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002538 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002539
Daniel Vetterf6936e22015-03-26 12:17:05 +01002540 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002541 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002542
2543out_unref_obj:
2544 drm_gem_object_unreference(&obj->base);
2545 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002546 return false;
2547}
2548
Matt Roperafd65eb2015-02-03 13:10:04 -08002549/* Update plane->state->fb to match plane->fb after driver-internal updates */
2550static void
2551update_state_fb(struct drm_plane *plane)
2552{
2553 if (plane->fb == plane->state->fb)
2554 return;
2555
2556 if (plane->state->fb)
2557 drm_framebuffer_unreference(plane->state->fb);
2558 plane->state->fb = plane->fb;
2559 if (plane->state->fb)
2560 drm_framebuffer_reference(plane->state->fb);
2561}
2562
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002563static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002564intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2565 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002566{
2567 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002568 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002569 struct drm_crtc *c;
2570 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002571 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002572 struct drm_plane *primary = intel_crtc->base.primary;
2573 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002574
Damien Lespiau2d140302015-02-05 17:22:18 +00002575 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002576 return;
2577
Daniel Vetterf6936e22015-03-26 12:17:05 +01002578 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002579 fb = &plane_config->fb->base;
2580 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002581 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002582
Damien Lespiau2d140302015-02-05 17:22:18 +00002583 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002584
2585 /*
2586 * Failed to alloc the obj, check to see if we should share
2587 * an fb with another CRTC instead
2588 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002589 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002590 i = to_intel_crtc(c);
2591
2592 if (c == &intel_crtc->base)
2593 continue;
2594
Matt Roper2ff8fde2014-07-08 07:50:07 -07002595 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002596 continue;
2597
Daniel Vetter88595ac2015-03-26 12:42:24 +01002598 fb = c->primary->fb;
2599 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002600 continue;
2601
Daniel Vetter88595ac2015-03-26 12:42:24 +01002602 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002603 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002604 drm_framebuffer_reference(fb);
2605 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002606 }
2607 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002608
2609 return;
2610
2611valid_fb:
2612 obj = intel_fb_obj(fb);
2613 if (obj->tiling_mode != I915_TILING_NONE)
2614 dev_priv->preserve_bios_swizzle = true;
2615
2616 primary->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002617 primary->crtc = primary->state->crtc = &intel_crtc->base;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002618 update_state_fb(primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002619 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
Ville Syrjäläa9ff8712015-06-24 21:59:34 +03002620 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002621}
2622
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002623static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2624 struct drm_framebuffer *fb,
2625 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002626{
2627 struct drm_device *dev = crtc->dev;
2628 struct drm_i915_private *dev_priv = dev->dev_private;
2629 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002630 struct drm_plane *primary = crtc->primary;
2631 bool visible = to_intel_plane_state(primary->state)->visible;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002632 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002633 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002634 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002635 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002636 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302637 int pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002638
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002639 if (!visible || !fb) {
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002640 I915_WRITE(reg, 0);
2641 if (INTEL_INFO(dev)->gen >= 4)
2642 I915_WRITE(DSPSURF(plane), 0);
2643 else
2644 I915_WRITE(DSPADDR(plane), 0);
2645 POSTING_READ(reg);
2646 return;
2647 }
2648
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002649 obj = intel_fb_obj(fb);
2650 if (WARN_ON(obj == NULL))
2651 return;
2652
2653 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2654
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002655 dspcntr = DISPPLANE_GAMMA_ENABLE;
2656
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002657 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002658
2659 if (INTEL_INFO(dev)->gen < 4) {
2660 if (intel_crtc->pipe == PIPE_B)
2661 dspcntr |= DISPPLANE_SEL_PIPE_B;
2662
2663 /* pipesrc and dspsize control the size that is scaled from,
2664 * which should always be the user's requested size.
2665 */
2666 I915_WRITE(DSPSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002667 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2668 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002669 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002670 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2671 I915_WRITE(PRIMSIZE(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äc14b0482014-10-16 20:52:34 +03002674 I915_WRITE(PRIMPOS(plane), 0);
2675 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002676 }
2677
Ville Syrjälä57779d02012-10-31 17:50:14 +02002678 switch (fb->pixel_format) {
2679 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002680 dspcntr |= DISPPLANE_8BPP;
2681 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002682 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002683 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002684 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002685 case DRM_FORMAT_RGB565:
2686 dspcntr |= DISPPLANE_BGRX565;
2687 break;
2688 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002689 dspcntr |= DISPPLANE_BGRX888;
2690 break;
2691 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002692 dspcntr |= DISPPLANE_RGBX888;
2693 break;
2694 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002695 dspcntr |= DISPPLANE_BGRX101010;
2696 break;
2697 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002698 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002699 break;
2700 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002701 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002702 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002703
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002704 if (INTEL_INFO(dev)->gen >= 4 &&
2705 obj->tiling_mode != I915_TILING_NONE)
2706 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002707
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002708 if (IS_G4X(dev))
2709 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2710
Ville Syrjäläb98971272014-08-27 16:51:22 +03002711 linear_offset = y * fb->pitches[0] + x * pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002712
Daniel Vetterc2c75132012-07-05 12:17:30 +02002713 if (INTEL_INFO(dev)->gen >= 4) {
2714 intel_crtc->dspaddr_offset =
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002715 intel_gen4_compute_page_offset(dev_priv,
2716 &x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002717 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002718 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002719 linear_offset -= intel_crtc->dspaddr_offset;
2720 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002721 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002722 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002723
Matt Roper8e7d6882015-01-21 16:35:41 -08002724 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302725 dspcntr |= DISPPLANE_ROTATE_180;
2726
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002727 x += (intel_crtc->config->pipe_src_w - 1);
2728 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302729
2730 /* Finding the last pixel of the last line of the display
2731 data and adding to linear_offset*/
2732 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002733 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2734 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302735 }
2736
2737 I915_WRITE(reg, dspcntr);
2738
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002739 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002740 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002741 I915_WRITE(DSPSURF(plane),
2742 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002743 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002744 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002745 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002746 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002747 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002748}
2749
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002750static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2751 struct drm_framebuffer *fb,
2752 int x, int y)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002753{
2754 struct drm_device *dev = crtc->dev;
2755 struct drm_i915_private *dev_priv = dev->dev_private;
2756 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002757 struct drm_plane *primary = crtc->primary;
2758 bool visible = to_intel_plane_state(primary->state)->visible;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002759 struct drm_i915_gem_object *obj;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002760 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002761 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002762 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002763 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302764 int pixel_size;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002765
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002766 if (!visible || !fb) {
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002767 I915_WRITE(reg, 0);
2768 I915_WRITE(DSPSURF(plane), 0);
2769 POSTING_READ(reg);
2770 return;
2771 }
2772
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002773 obj = intel_fb_obj(fb);
2774 if (WARN_ON(obj == NULL))
2775 return;
2776
2777 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2778
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002779 dspcntr = DISPPLANE_GAMMA_ENABLE;
2780
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002781 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002782
2783 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2784 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2785
Ville Syrjälä57779d02012-10-31 17:50:14 +02002786 switch (fb->pixel_format) {
2787 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002788 dspcntr |= DISPPLANE_8BPP;
2789 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002790 case DRM_FORMAT_RGB565:
2791 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002792 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002793 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002794 dspcntr |= DISPPLANE_BGRX888;
2795 break;
2796 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002797 dspcntr |= DISPPLANE_RGBX888;
2798 break;
2799 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002800 dspcntr |= DISPPLANE_BGRX101010;
2801 break;
2802 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002803 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002804 break;
2805 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002806 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002807 }
2808
2809 if (obj->tiling_mode != I915_TILING_NONE)
2810 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002811
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002812 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002813 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002814
Ville Syrjäläb98971272014-08-27 16:51:22 +03002815 linear_offset = y * fb->pitches[0] + x * pixel_size;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002816 intel_crtc->dspaddr_offset =
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002817 intel_gen4_compute_page_offset(dev_priv,
2818 &x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002819 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002820 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002821 linear_offset -= intel_crtc->dspaddr_offset;
Matt Roper8e7d6882015-01-21 16:35:41 -08002822 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302823 dspcntr |= DISPPLANE_ROTATE_180;
2824
2825 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002826 x += (intel_crtc->config->pipe_src_w - 1);
2827 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302828
2829 /* Finding the last pixel of the last line of the display
2830 data and adding to linear_offset*/
2831 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002832 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2833 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302834 }
2835 }
2836
2837 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002838
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002839 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002840 I915_WRITE(DSPSURF(plane),
2841 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002842 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002843 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2844 } else {
2845 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2846 I915_WRITE(DSPLINOFF(plane), linear_offset);
2847 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002848 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002849}
2850
Damien Lespiaub3218032015-02-27 11:15:18 +00002851u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2852 uint32_t pixel_format)
2853{
2854 u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2855
2856 /*
2857 * The stride is either expressed as a multiple of 64 bytes
2858 * chunks for linear buffers or in number of tiles for tiled
2859 * buffers.
2860 */
2861 switch (fb_modifier) {
2862 case DRM_FORMAT_MOD_NONE:
2863 return 64;
2864 case I915_FORMAT_MOD_X_TILED:
2865 if (INTEL_INFO(dev)->gen == 2)
2866 return 128;
2867 return 512;
2868 case I915_FORMAT_MOD_Y_TILED:
2869 /* No need to check for old gens and Y tiling since this is
2870 * about the display engine and those will be blocked before
2871 * we get here.
2872 */
2873 return 128;
2874 case I915_FORMAT_MOD_Yf_TILED:
2875 if (bits_per_pixel == 8)
2876 return 64;
2877 else
2878 return 128;
2879 default:
2880 MISSING_CASE(fb_modifier);
2881 return 64;
2882 }
2883}
2884
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002885unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
2886 struct drm_i915_gem_object *obj)
2887{
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002888 const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002889
2890 if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002891 view = &i915_ggtt_view_rotated;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002892
2893 return i915_gem_obj_ggtt_offset_view(obj, view);
2894}
2895
Chandra Kondurua1b22782015-04-07 15:28:45 -07002896/*
2897 * This function detaches (aka. unbinds) unused scalers in hardware
2898 */
Maarten Lankhorst05832362015-06-15 12:33:48 +02002899static void skl_detach_scalers(struct intel_crtc *intel_crtc)
Chandra Kondurua1b22782015-04-07 15:28:45 -07002900{
2901 struct drm_device *dev;
2902 struct drm_i915_private *dev_priv;
2903 struct intel_crtc_scaler_state *scaler_state;
2904 int i;
2905
Chandra Kondurua1b22782015-04-07 15:28:45 -07002906 dev = intel_crtc->base.dev;
2907 dev_priv = dev->dev_private;
2908 scaler_state = &intel_crtc->config->scaler_state;
2909
2910 /* loop through and disable scalers that aren't in use */
2911 for (i = 0; i < intel_crtc->num_scalers; i++) {
2912 if (!scaler_state->scalers[i].in_use) {
2913 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, i), 0);
2914 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, i), 0);
2915 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, i), 0);
2916 DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
2917 intel_crtc->base.base.id, intel_crtc->pipe, i);
2918 }
2919 }
2920}
2921
Chandra Konduru6156a452015-04-27 13:48:39 -07002922u32 skl_plane_ctl_format(uint32_t pixel_format)
2923{
Chandra Konduru6156a452015-04-27 13:48:39 -07002924 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01002925 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002926 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07002927 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002928 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07002929 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002930 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07002931 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002932 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07002933 /*
2934 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2935 * to be already pre-multiplied. We need to add a knob (or a different
2936 * DRM_FORMAT) for user-space to configure that.
2937 */
2938 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002939 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07002940 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002941 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002942 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07002943 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002944 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002945 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002946 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002947 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002948 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002949 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07002950 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002951 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07002952 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002953 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002954 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002955 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002956 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01002957 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07002958 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002959
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002960 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002961}
2962
2963u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2964{
Chandra Konduru6156a452015-04-27 13:48:39 -07002965 switch (fb_modifier) {
2966 case DRM_FORMAT_MOD_NONE:
2967 break;
2968 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002969 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07002970 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002971 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07002972 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002973 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07002974 default:
2975 MISSING_CASE(fb_modifier);
2976 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002977
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002978 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002979}
2980
2981u32 skl_plane_ctl_rotation(unsigned int rotation)
2982{
Chandra Konduru6156a452015-04-27 13:48:39 -07002983 switch (rotation) {
2984 case BIT(DRM_ROTATE_0):
2985 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05302986 /*
2987 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
2988 * while i915 HW rotation is clockwise, thats why this swapping.
2989 */
Chandra Konduru6156a452015-04-27 13:48:39 -07002990 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05302991 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07002992 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002993 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07002994 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05302995 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07002996 default:
2997 MISSING_CASE(rotation);
2998 }
2999
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003000 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003001}
3002
Damien Lespiau70d21f02013-07-03 21:06:04 +01003003static void skylake_update_primary_plane(struct drm_crtc *crtc,
3004 struct drm_framebuffer *fb,
3005 int x, int y)
3006{
3007 struct drm_device *dev = crtc->dev;
3008 struct drm_i915_private *dev_priv = dev->dev_private;
3009 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03003010 struct drm_plane *plane = crtc->primary;
3011 bool visible = to_intel_plane_state(plane->state)->visible;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003012 struct drm_i915_gem_object *obj;
3013 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303014 u32 plane_ctl, stride_div, stride;
3015 u32 tile_height, plane_offset, plane_size;
3016 unsigned int rotation;
3017 int x_offset, y_offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003018 unsigned long surf_addr;
Chandra Konduru6156a452015-04-27 13:48:39 -07003019 struct intel_crtc_state *crtc_state = intel_crtc->config;
3020 struct intel_plane_state *plane_state;
3021 int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
3022 int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
3023 int scaler_id = -1;
3024
Chandra Konduru6156a452015-04-27 13:48:39 -07003025 plane_state = to_intel_plane_state(plane->state);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003026
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03003027 if (!visible || !fb) {
Damien Lespiau70d21f02013-07-03 21:06:04 +01003028 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3029 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3030 POSTING_READ(PLANE_CTL(pipe, 0));
3031 return;
3032 }
3033
3034 plane_ctl = PLANE_CTL_ENABLE |
3035 PLANE_CTL_PIPE_GAMMA_ENABLE |
3036 PLANE_CTL_PIPE_CSC_ENABLE;
3037
Chandra Konduru6156a452015-04-27 13:48:39 -07003038 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3039 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003040 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303041
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303042 rotation = plane->state->rotation;
Chandra Konduru6156a452015-04-27 13:48:39 -07003043 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003044
Damien Lespiaub3218032015-02-27 11:15:18 +00003045 obj = intel_fb_obj(fb);
3046 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3047 fb->pixel_format);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303048 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
3049
Chandra Konduru6156a452015-04-27 13:48:39 -07003050 /*
3051 * FIXME: intel_plane_state->src, dst aren't set when transitional
3052 * update_plane helpers are called from legacy paths.
3053 * Once full atomic crtc is available, below check can be avoided.
3054 */
3055 if (drm_rect_width(&plane_state->src)) {
3056 scaler_id = plane_state->scaler_id;
3057 src_x = plane_state->src.x1 >> 16;
3058 src_y = plane_state->src.y1 >> 16;
3059 src_w = drm_rect_width(&plane_state->src) >> 16;
3060 src_h = drm_rect_height(&plane_state->src) >> 16;
3061 dst_x = plane_state->dst.x1;
3062 dst_y = plane_state->dst.y1;
3063 dst_w = drm_rect_width(&plane_state->dst);
3064 dst_h = drm_rect_height(&plane_state->dst);
3065
3066 WARN_ON(x != src_x || y != src_y);
3067 } else {
3068 src_w = intel_crtc->config->pipe_src_w;
3069 src_h = intel_crtc->config->pipe_src_h;
3070 }
3071
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303072 if (intel_rotation_90_or_270(rotation)) {
3073 /* stride = Surface height in tiles */
Chandra Konduru2614f172015-05-08 20:22:46 -07003074 tile_height = intel_tile_height(dev, fb->pixel_format,
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303075 fb->modifier[0]);
3076 stride = DIV_ROUND_UP(fb->height, tile_height);
Chandra Konduru6156a452015-04-27 13:48:39 -07003077 x_offset = stride * tile_height - y - src_h;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303078 y_offset = x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003079 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303080 } else {
3081 stride = fb->pitches[0] / stride_div;
3082 x_offset = x;
3083 y_offset = y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003084 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303085 }
3086 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003087
Damien Lespiau70d21f02013-07-03 21:06:04 +01003088 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303089 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3090 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3091 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003092
3093 if (scaler_id >= 0) {
3094 uint32_t ps_ctrl = 0;
3095
3096 WARN_ON(!dst_w || !dst_h);
3097 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3098 crtc_state->scaler_state.scalers[scaler_id].mode;
3099 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3100 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3101 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3102 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3103 I915_WRITE(PLANE_POS(pipe, 0), 0);
3104 } else {
3105 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3106 }
3107
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003108 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003109
3110 POSTING_READ(PLANE_SURF(pipe, 0));
3111}
3112
Jesse Barnes17638cd2011-06-24 12:19:23 -07003113/* Assume fb object is pinned & idle & fenced and just update base pointers */
3114static int
3115intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3116 int x, int y, enum mode_set_atomic state)
3117{
3118 struct drm_device *dev = crtc->dev;
3119 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003120
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01003121 if (dev_priv->display.disable_fbc)
3122 dev_priv->display.disable_fbc(dev);
Jesse Barnes81255562010-08-02 12:07:50 -07003123
Daniel Vetter29b9bde2014-04-24 23:55:01 +02003124 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3125
3126 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07003127}
3128
Ville Syrjälä75147472014-11-24 18:28:11 +02003129static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003130{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003131 struct drm_crtc *crtc;
3132
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003133 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003134 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3135 enum plane plane = intel_crtc->plane;
3136
3137 intel_prepare_page_flip(dev, plane);
3138 intel_finish_page_flip_plane(dev, plane);
3139 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003140}
3141
3142static void intel_update_primary_planes(struct drm_device *dev)
3143{
3144 struct drm_i915_private *dev_priv = dev->dev_private;
3145 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003146
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003147 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003148 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3149
Rob Clark51fd3712013-11-19 12:10:12 -05003150 drm_modeset_lock(&crtc->mutex, NULL);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003151 /*
3152 * FIXME: Once we have proper support for primary planes (and
3153 * disabling them without disabling the entire crtc) allow again
Dave Airlie66e514c2014-04-03 07:51:54 +10003154 * a NULL crtc->primary->fb.
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003155 */
Matt Roperf4510a22014-04-01 15:22:40 -07003156 if (intel_crtc->active && crtc->primary->fb)
Matt Roper262ca2b2014-03-18 17:22:55 -07003157 dev_priv->display.update_primary_plane(crtc,
Dave Airlie66e514c2014-04-03 07:51:54 +10003158 crtc->primary->fb,
Matt Roper262ca2b2014-03-18 17:22:55 -07003159 crtc->x,
3160 crtc->y);
Rob Clark51fd3712013-11-19 12:10:12 -05003161 drm_modeset_unlock(&crtc->mutex);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003162 }
3163}
3164
Ville Syrjälä75147472014-11-24 18:28:11 +02003165void intel_prepare_reset(struct drm_device *dev)
3166{
3167 /* no reset support for gen2 */
3168 if (IS_GEN2(dev))
3169 return;
3170
3171 /* reset doesn't touch the display */
3172 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3173 return;
3174
3175 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003176 /*
3177 * Disabling the crtcs gracefully seems nicer. Also the
3178 * g33 docs say we should at least disable all the planes.
3179 */
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02003180 intel_display_suspend(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003181}
3182
3183void intel_finish_reset(struct drm_device *dev)
3184{
3185 struct drm_i915_private *dev_priv = to_i915(dev);
3186
3187 /*
3188 * Flips in the rings will be nuked by the reset,
3189 * so complete all pending flips so that user space
3190 * will get its events and not get stuck.
3191 */
3192 intel_complete_page_flips(dev);
3193
3194 /* no reset support for gen2 */
3195 if (IS_GEN2(dev))
3196 return;
3197
3198 /* reset doesn't touch the display */
3199 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3200 /*
3201 * Flips in the rings have been nuked by the reset,
3202 * so update the base address of all primary
3203 * planes to the the last fb to make sure we're
3204 * showing the correct fb after a reset.
3205 */
3206 intel_update_primary_planes(dev);
3207 return;
3208 }
3209
3210 /*
3211 * The display has been reset as well,
3212 * so need a full re-initialization.
3213 */
3214 intel_runtime_pm_disable_interrupts(dev_priv);
3215 intel_runtime_pm_enable_interrupts(dev_priv);
3216
3217 intel_modeset_init_hw(dev);
3218
3219 spin_lock_irq(&dev_priv->irq_lock);
3220 if (dev_priv->display.hpd_irq_setup)
3221 dev_priv->display.hpd_irq_setup(dev);
3222 spin_unlock_irq(&dev_priv->irq_lock);
3223
3224 intel_modeset_setup_hw_state(dev, true);
3225
3226 intel_hpd_init(dev_priv);
3227
3228 drm_modeset_unlock_all(dev);
3229}
3230
Chris Wilson2e2f3512015-04-27 13:41:14 +01003231static void
Chris Wilson14667a42012-04-03 17:58:35 +01003232intel_finish_fb(struct drm_framebuffer *old_fb)
3233{
Matt Roper2ff8fde2014-07-08 07:50:07 -07003234 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
Chris Wilson2e2f3512015-04-27 13:41:14 +01003235 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilson14667a42012-04-03 17:58:35 +01003236 bool was_interruptible = dev_priv->mm.interruptible;
3237 int ret;
3238
Chris Wilson14667a42012-04-03 17:58:35 +01003239 /* Big Hammer, we also need to ensure that any pending
3240 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3241 * current scanout is retired before unpinning the old
Chris Wilson2e2f3512015-04-27 13:41:14 +01003242 * framebuffer. Note that we rely on userspace rendering
3243 * into the buffer attached to the pipe they are waiting
3244 * on. If not, userspace generates a GPU hang with IPEHR
3245 * point to the MI_WAIT_FOR_EVENT.
Chris Wilson14667a42012-04-03 17:58:35 +01003246 *
3247 * This should only fail upon a hung GPU, in which case we
3248 * can safely continue.
3249 */
3250 dev_priv->mm.interruptible = false;
Chris Wilson2e2f3512015-04-27 13:41:14 +01003251 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson14667a42012-04-03 17:58:35 +01003252 dev_priv->mm.interruptible = was_interruptible;
3253
Chris Wilson2e2f3512015-04-27 13:41:14 +01003254 WARN_ON(ret);
Chris Wilson14667a42012-04-03 17:58:35 +01003255}
3256
Chris Wilson7d5e3792014-03-04 13:15:08 +00003257static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3258{
3259 struct drm_device *dev = crtc->dev;
3260 struct drm_i915_private *dev_priv = dev->dev_private;
3261 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003262 bool pending;
3263
3264 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3265 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3266 return false;
3267
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003268 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003269 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003270 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003271
3272 return pending;
3273}
3274
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003275static void intel_update_pipe_size(struct intel_crtc *crtc)
3276{
3277 struct drm_device *dev = crtc->base.dev;
3278 struct drm_i915_private *dev_priv = dev->dev_private;
3279 const struct drm_display_mode *adjusted_mode;
3280
3281 if (!i915.fastboot)
3282 return;
3283
3284 /*
3285 * Update pipe size and adjust fitter if needed: the reason for this is
3286 * that in compute_mode_changes we check the native mode (not the pfit
3287 * mode) to see if we can flip rather than do a full mode set. In the
3288 * fastboot case, we'll flip, but if we don't update the pipesrc and
3289 * pfit state, we'll end up with a big fb scanned out into the wrong
3290 * sized surface.
3291 *
3292 * To fix this properly, we need to hoist the checks up into
3293 * compute_mode_changes (or above), check the actual pfit state and
3294 * whether the platform allows pfit disable with pipe active, and only
3295 * then update the pipesrc and pfit state, even on the flip path.
3296 */
3297
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003298 adjusted_mode = &crtc->config->base.adjusted_mode;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003299
3300 I915_WRITE(PIPESRC(crtc->pipe),
3301 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3302 (adjusted_mode->crtc_vdisplay - 1));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003303 if (!crtc->config->pch_pfit.enabled &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03003304 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3305 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003306 I915_WRITE(PF_CTL(crtc->pipe), 0);
3307 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3308 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3309 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003310 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3311 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003312}
3313
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003314static void intel_fdi_normal_train(struct drm_crtc *crtc)
3315{
3316 struct drm_device *dev = crtc->dev;
3317 struct drm_i915_private *dev_priv = dev->dev_private;
3318 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3319 int pipe = intel_crtc->pipe;
3320 u32 reg, temp;
3321
3322 /* enable normal train */
3323 reg = FDI_TX_CTL(pipe);
3324 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003325 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003326 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3327 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003328 } else {
3329 temp &= ~FDI_LINK_TRAIN_NONE;
3330 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003331 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003332 I915_WRITE(reg, temp);
3333
3334 reg = FDI_RX_CTL(pipe);
3335 temp = I915_READ(reg);
3336 if (HAS_PCH_CPT(dev)) {
3337 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3338 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3339 } else {
3340 temp &= ~FDI_LINK_TRAIN_NONE;
3341 temp |= FDI_LINK_TRAIN_NONE;
3342 }
3343 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3344
3345 /* wait one idle pattern time */
3346 POSTING_READ(reg);
3347 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003348
3349 /* IVB wants error correction enabled */
3350 if (IS_IVYBRIDGE(dev))
3351 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3352 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003353}
3354
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003355/* The FDI link training functions for ILK/Ibexpeak. */
3356static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3357{
3358 struct drm_device *dev = crtc->dev;
3359 struct drm_i915_private *dev_priv = dev->dev_private;
3360 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3361 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003362 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003363
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003364 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003365 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003366
Adam Jacksone1a44742010-06-25 15:32:14 -04003367 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3368 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003369 reg = FDI_RX_IMR(pipe);
3370 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003371 temp &= ~FDI_RX_SYMBOL_LOCK;
3372 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003373 I915_WRITE(reg, temp);
3374 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003375 udelay(150);
3376
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003377 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003378 reg = FDI_TX_CTL(pipe);
3379 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003380 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003381 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003382 temp &= ~FDI_LINK_TRAIN_NONE;
3383 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003384 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003385
Chris Wilson5eddb702010-09-11 13:48:45 +01003386 reg = FDI_RX_CTL(pipe);
3387 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003388 temp &= ~FDI_LINK_TRAIN_NONE;
3389 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003390 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3391
3392 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003393 udelay(150);
3394
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003395 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003396 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3397 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3398 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003399
Chris Wilson5eddb702010-09-11 13:48:45 +01003400 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003401 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003402 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003403 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3404
3405 if ((temp & FDI_RX_BIT_LOCK)) {
3406 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003407 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003408 break;
3409 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003410 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003411 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003412 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003413
3414 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003415 reg = FDI_TX_CTL(pipe);
3416 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003417 temp &= ~FDI_LINK_TRAIN_NONE;
3418 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003419 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003420
Chris Wilson5eddb702010-09-11 13:48:45 +01003421 reg = FDI_RX_CTL(pipe);
3422 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003423 temp &= ~FDI_LINK_TRAIN_NONE;
3424 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003425 I915_WRITE(reg, temp);
3426
3427 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003428 udelay(150);
3429
Chris Wilson5eddb702010-09-11 13:48:45 +01003430 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003431 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003432 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003433 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3434
3435 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003436 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003437 DRM_DEBUG_KMS("FDI train 2 done.\n");
3438 break;
3439 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003440 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003441 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003442 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003443
3444 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003445
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003446}
3447
Akshay Joshi0206e352011-08-16 15:34:10 -04003448static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003449 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3450 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3451 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3452 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3453};
3454
3455/* The FDI link training functions for SNB/Cougarpoint. */
3456static void gen6_fdi_link_train(struct drm_crtc *crtc)
3457{
3458 struct drm_device *dev = crtc->dev;
3459 struct drm_i915_private *dev_priv = dev->dev_private;
3460 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3461 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05003462 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003463
Adam Jacksone1a44742010-06-25 15:32:14 -04003464 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3465 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003466 reg = FDI_RX_IMR(pipe);
3467 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003468 temp &= ~FDI_RX_SYMBOL_LOCK;
3469 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003470 I915_WRITE(reg, temp);
3471
3472 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003473 udelay(150);
3474
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003475 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003476 reg = FDI_TX_CTL(pipe);
3477 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003478 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003479 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003480 temp &= ~FDI_LINK_TRAIN_NONE;
3481 temp |= FDI_LINK_TRAIN_PATTERN_1;
3482 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3483 /* SNB-B */
3484 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003485 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003486
Daniel Vetterd74cf322012-10-26 10:58:13 +02003487 I915_WRITE(FDI_RX_MISC(pipe),
3488 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3489
Chris Wilson5eddb702010-09-11 13:48:45 +01003490 reg = FDI_RX_CTL(pipe);
3491 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003492 if (HAS_PCH_CPT(dev)) {
3493 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3494 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3495 } else {
3496 temp &= ~FDI_LINK_TRAIN_NONE;
3497 temp |= FDI_LINK_TRAIN_PATTERN_1;
3498 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003499 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3500
3501 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003502 udelay(150);
3503
Akshay Joshi0206e352011-08-16 15:34:10 -04003504 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003505 reg = FDI_TX_CTL(pipe);
3506 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003507 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3508 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003509 I915_WRITE(reg, temp);
3510
3511 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003512 udelay(500);
3513
Sean Paulfa37d392012-03-02 12:53:39 -05003514 for (retry = 0; retry < 5; retry++) {
3515 reg = FDI_RX_IIR(pipe);
3516 temp = I915_READ(reg);
3517 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3518 if (temp & FDI_RX_BIT_LOCK) {
3519 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3520 DRM_DEBUG_KMS("FDI train 1 done.\n");
3521 break;
3522 }
3523 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003524 }
Sean Paulfa37d392012-03-02 12:53:39 -05003525 if (retry < 5)
3526 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003527 }
3528 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003529 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003530
3531 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003532 reg = FDI_TX_CTL(pipe);
3533 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003534 temp &= ~FDI_LINK_TRAIN_NONE;
3535 temp |= FDI_LINK_TRAIN_PATTERN_2;
3536 if (IS_GEN6(dev)) {
3537 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3538 /* SNB-B */
3539 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3540 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003541 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003542
Chris Wilson5eddb702010-09-11 13:48:45 +01003543 reg = FDI_RX_CTL(pipe);
3544 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003545 if (HAS_PCH_CPT(dev)) {
3546 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3547 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3548 } else {
3549 temp &= ~FDI_LINK_TRAIN_NONE;
3550 temp |= FDI_LINK_TRAIN_PATTERN_2;
3551 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003552 I915_WRITE(reg, temp);
3553
3554 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003555 udelay(150);
3556
Akshay Joshi0206e352011-08-16 15:34:10 -04003557 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003558 reg = FDI_TX_CTL(pipe);
3559 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003560 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3561 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003562 I915_WRITE(reg, temp);
3563
3564 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003565 udelay(500);
3566
Sean Paulfa37d392012-03-02 12:53:39 -05003567 for (retry = 0; retry < 5; retry++) {
3568 reg = FDI_RX_IIR(pipe);
3569 temp = I915_READ(reg);
3570 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3571 if (temp & FDI_RX_SYMBOL_LOCK) {
3572 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3573 DRM_DEBUG_KMS("FDI train 2 done.\n");
3574 break;
3575 }
3576 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003577 }
Sean Paulfa37d392012-03-02 12:53:39 -05003578 if (retry < 5)
3579 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003580 }
3581 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003582 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003583
3584 DRM_DEBUG_KMS("FDI train done.\n");
3585}
3586
Jesse Barnes357555c2011-04-28 15:09:55 -07003587/* Manual link training for Ivy Bridge A0 parts */
3588static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3589{
3590 struct drm_device *dev = crtc->dev;
3591 struct drm_i915_private *dev_priv = dev->dev_private;
3592 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3593 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003594 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003595
3596 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3597 for train result */
3598 reg = FDI_RX_IMR(pipe);
3599 temp = I915_READ(reg);
3600 temp &= ~FDI_RX_SYMBOL_LOCK;
3601 temp &= ~FDI_RX_BIT_LOCK;
3602 I915_WRITE(reg, temp);
3603
3604 POSTING_READ(reg);
3605 udelay(150);
3606
Daniel Vetter01a415f2012-10-27 15:58:40 +02003607 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3608 I915_READ(FDI_RX_IIR(pipe)));
3609
Jesse Barnes139ccd32013-08-19 11:04:55 -07003610 /* Try each vswing and preemphasis setting twice before moving on */
3611 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3612 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003613 reg = FDI_TX_CTL(pipe);
3614 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003615 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3616 temp &= ~FDI_TX_ENABLE;
3617 I915_WRITE(reg, temp);
3618
3619 reg = FDI_RX_CTL(pipe);
3620 temp = I915_READ(reg);
3621 temp &= ~FDI_LINK_TRAIN_AUTO;
3622 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3623 temp &= ~FDI_RX_ENABLE;
3624 I915_WRITE(reg, temp);
3625
3626 /* enable CPU FDI TX and PCH FDI RX */
3627 reg = FDI_TX_CTL(pipe);
3628 temp = I915_READ(reg);
3629 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003630 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003631 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003632 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003633 temp |= snb_b_fdi_train_param[j/2];
3634 temp |= FDI_COMPOSITE_SYNC;
3635 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3636
3637 I915_WRITE(FDI_RX_MISC(pipe),
3638 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3639
3640 reg = FDI_RX_CTL(pipe);
3641 temp = I915_READ(reg);
3642 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3643 temp |= FDI_COMPOSITE_SYNC;
3644 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3645
3646 POSTING_READ(reg);
3647 udelay(1); /* should be 0.5us */
3648
3649 for (i = 0; i < 4; i++) {
3650 reg = FDI_RX_IIR(pipe);
3651 temp = I915_READ(reg);
3652 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3653
3654 if (temp & FDI_RX_BIT_LOCK ||
3655 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3656 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3657 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3658 i);
3659 break;
3660 }
3661 udelay(1); /* should be 0.5us */
3662 }
3663 if (i == 4) {
3664 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3665 continue;
3666 }
3667
3668 /* Train 2 */
3669 reg = FDI_TX_CTL(pipe);
3670 temp = I915_READ(reg);
3671 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3672 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3673 I915_WRITE(reg, temp);
3674
3675 reg = FDI_RX_CTL(pipe);
3676 temp = I915_READ(reg);
3677 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3678 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003679 I915_WRITE(reg, temp);
3680
3681 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003682 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003683
Jesse Barnes139ccd32013-08-19 11:04:55 -07003684 for (i = 0; i < 4; i++) {
3685 reg = FDI_RX_IIR(pipe);
3686 temp = I915_READ(reg);
3687 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003688
Jesse Barnes139ccd32013-08-19 11:04:55 -07003689 if (temp & FDI_RX_SYMBOL_LOCK ||
3690 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3691 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3692 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3693 i);
3694 goto train_done;
3695 }
3696 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003697 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003698 if (i == 4)
3699 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003700 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003701
Jesse Barnes139ccd32013-08-19 11:04:55 -07003702train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003703 DRM_DEBUG_KMS("FDI train done.\n");
3704}
3705
Daniel Vetter88cefb62012-08-12 19:27:14 +02003706static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003707{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003708 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003709 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003710 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003711 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003712
Jesse Barnesc64e3112010-09-10 11:27:03 -07003713
Jesse Barnes0e23b992010-09-10 11:10:00 -07003714 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003715 reg = FDI_RX_CTL(pipe);
3716 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003717 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003718 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003719 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003720 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3721
3722 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003723 udelay(200);
3724
3725 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003726 temp = I915_READ(reg);
3727 I915_WRITE(reg, temp | FDI_PCDCLK);
3728
3729 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003730 udelay(200);
3731
Paulo Zanoni20749732012-11-23 15:30:38 -02003732 /* Enable CPU FDI TX PLL, always on for Ironlake */
3733 reg = FDI_TX_CTL(pipe);
3734 temp = I915_READ(reg);
3735 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3736 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003737
Paulo Zanoni20749732012-11-23 15:30:38 -02003738 POSTING_READ(reg);
3739 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003740 }
3741}
3742
Daniel Vetter88cefb62012-08-12 19:27:14 +02003743static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3744{
3745 struct drm_device *dev = intel_crtc->base.dev;
3746 struct drm_i915_private *dev_priv = dev->dev_private;
3747 int pipe = intel_crtc->pipe;
3748 u32 reg, temp;
3749
3750 /* Switch from PCDclk to Rawclk */
3751 reg = FDI_RX_CTL(pipe);
3752 temp = I915_READ(reg);
3753 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3754
3755 /* Disable CPU FDI TX PLL */
3756 reg = FDI_TX_CTL(pipe);
3757 temp = I915_READ(reg);
3758 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3759
3760 POSTING_READ(reg);
3761 udelay(100);
3762
3763 reg = FDI_RX_CTL(pipe);
3764 temp = I915_READ(reg);
3765 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3766
3767 /* Wait for the clocks to turn off. */
3768 POSTING_READ(reg);
3769 udelay(100);
3770}
3771
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003772static void ironlake_fdi_disable(struct drm_crtc *crtc)
3773{
3774 struct drm_device *dev = crtc->dev;
3775 struct drm_i915_private *dev_priv = dev->dev_private;
3776 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3777 int pipe = intel_crtc->pipe;
3778 u32 reg, temp;
3779
3780 /* disable CPU FDI tx and PCH FDI rx */
3781 reg = FDI_TX_CTL(pipe);
3782 temp = I915_READ(reg);
3783 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3784 POSTING_READ(reg);
3785
3786 reg = FDI_RX_CTL(pipe);
3787 temp = I915_READ(reg);
3788 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003789 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003790 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3791
3792 POSTING_READ(reg);
3793 udelay(100);
3794
3795 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003796 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003797 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003798
3799 /* still set train pattern 1 */
3800 reg = FDI_TX_CTL(pipe);
3801 temp = I915_READ(reg);
3802 temp &= ~FDI_LINK_TRAIN_NONE;
3803 temp |= FDI_LINK_TRAIN_PATTERN_1;
3804 I915_WRITE(reg, temp);
3805
3806 reg = FDI_RX_CTL(pipe);
3807 temp = I915_READ(reg);
3808 if (HAS_PCH_CPT(dev)) {
3809 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3810 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3811 } else {
3812 temp &= ~FDI_LINK_TRAIN_NONE;
3813 temp |= FDI_LINK_TRAIN_PATTERN_1;
3814 }
3815 /* BPC in FDI rx is consistent with that in PIPECONF */
3816 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003817 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003818 I915_WRITE(reg, temp);
3819
3820 POSTING_READ(reg);
3821 udelay(100);
3822}
3823
Chris Wilson5dce5b932014-01-20 10:17:36 +00003824bool intel_has_pending_fb_unpin(struct drm_device *dev)
3825{
3826 struct intel_crtc *crtc;
3827
3828 /* Note that we don't need to be called with mode_config.lock here
3829 * as our list of CRTC objects is static for the lifetime of the
3830 * device and so cannot disappear as we iterate. Similarly, we can
3831 * happily treat the predicates as racy, atomic checks as userspace
3832 * cannot claim and pin a new fb without at least acquring the
3833 * struct_mutex and so serialising with us.
3834 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003835 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003836 if (atomic_read(&crtc->unpin_work_count) == 0)
3837 continue;
3838
3839 if (crtc->unpin_work)
3840 intel_wait_for_vblank(dev, crtc->pipe);
3841
3842 return true;
3843 }
3844
3845 return false;
3846}
3847
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003848static void page_flip_completed(struct intel_crtc *intel_crtc)
3849{
3850 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3851 struct intel_unpin_work *work = intel_crtc->unpin_work;
3852
3853 /* ensure that the unpin work is consistent wrt ->pending. */
3854 smp_rmb();
3855 intel_crtc->unpin_work = NULL;
3856
3857 if (work->event)
3858 drm_send_vblank_event(intel_crtc->base.dev,
3859 intel_crtc->pipe,
3860 work->event);
3861
3862 drm_crtc_vblank_put(&intel_crtc->base);
3863
3864 wake_up_all(&dev_priv->pending_flip_queue);
3865 queue_work(dev_priv->wq, &work->work);
3866
3867 trace_i915_flip_complete(intel_crtc->plane,
3868 work->pending_flip_obj);
3869}
3870
Ville Syrjälä46a55d32014-05-21 14:04:46 +03003871void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003872{
Chris Wilson0f911282012-04-17 10:05:38 +01003873 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003874 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003875
Daniel Vetter2c10d572012-12-20 21:24:07 +01003876 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Chris Wilson9c787942014-09-05 07:13:25 +01003877 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3878 !intel_crtc_has_pending_flip(crtc),
3879 60*HZ) == 0)) {
3880 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003881
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003882 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003883 if (intel_crtc->unpin_work) {
3884 WARN_ONCE(1, "Removing stuck page flip\n");
3885 page_flip_completed(intel_crtc);
3886 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003887 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003888 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003889
Chris Wilson975d5682014-08-20 13:13:34 +01003890 if (crtc->primary->fb) {
3891 mutex_lock(&dev->struct_mutex);
3892 intel_finish_fb(crtc->primary->fb);
3893 mutex_unlock(&dev->struct_mutex);
3894 }
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003895}
3896
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003897/* Program iCLKIP clock to the desired frequency */
3898static void lpt_program_iclkip(struct drm_crtc *crtc)
3899{
3900 struct drm_device *dev = crtc->dev;
3901 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003902 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003903 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3904 u32 temp;
3905
Ville Syrjäläa5805162015-05-26 20:42:30 +03003906 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01003907
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003908 /* It is necessary to ungate the pixclk gate prior to programming
3909 * the divisors, and gate it back when it is done.
3910 */
3911 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3912
3913 /* Disable SSCCTL */
3914 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003915 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3916 SBI_SSCCTL_DISABLE,
3917 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003918
3919 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003920 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003921 auxdiv = 1;
3922 divsel = 0x41;
3923 phaseinc = 0x20;
3924 } else {
3925 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003926 * but the adjusted_mode->crtc_clock in in KHz. To get the
3927 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003928 * convert the virtual clock precision to KHz here for higher
3929 * precision.
3930 */
3931 u32 iclk_virtual_root_freq = 172800 * 1000;
3932 u32 iclk_pi_range = 64;
3933 u32 desired_divisor, msb_divisor_value, pi_value;
3934
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003935 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003936 msb_divisor_value = desired_divisor / iclk_pi_range;
3937 pi_value = desired_divisor % iclk_pi_range;
3938
3939 auxdiv = 0;
3940 divsel = msb_divisor_value - 2;
3941 phaseinc = pi_value;
3942 }
3943
3944 /* This should not happen with any sane values */
3945 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3946 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3947 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3948 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3949
3950 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 +03003951 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003952 auxdiv,
3953 divsel,
3954 phasedir,
3955 phaseinc);
3956
3957 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003958 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003959 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3960 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3961 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3962 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3963 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3964 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003965 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003966
3967 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003968 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003969 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3970 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003971 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003972
3973 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003974 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003975 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003976 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003977
3978 /* Wait for initialization time */
3979 udelay(24);
3980
3981 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01003982
Ville Syrjäläa5805162015-05-26 20:42:30 +03003983 mutex_unlock(&dev_priv->sb_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003984}
3985
Daniel Vetter275f01b22013-05-03 11:49:47 +02003986static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3987 enum pipe pch_transcoder)
3988{
3989 struct drm_device *dev = crtc->base.dev;
3990 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003991 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02003992
3993 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3994 I915_READ(HTOTAL(cpu_transcoder)));
3995 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3996 I915_READ(HBLANK(cpu_transcoder)));
3997 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3998 I915_READ(HSYNC(cpu_transcoder)));
3999
4000 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4001 I915_READ(VTOTAL(cpu_transcoder)));
4002 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4003 I915_READ(VBLANK(cpu_transcoder)));
4004 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4005 I915_READ(VSYNC(cpu_transcoder)));
4006 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4007 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4008}
4009
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004010static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004011{
4012 struct drm_i915_private *dev_priv = dev->dev_private;
4013 uint32_t temp;
4014
4015 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004016 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004017 return;
4018
4019 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4020 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4021
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004022 temp &= ~FDI_BC_BIFURCATION_SELECT;
4023 if (enable)
4024 temp |= FDI_BC_BIFURCATION_SELECT;
4025
4026 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004027 I915_WRITE(SOUTH_CHICKEN1, temp);
4028 POSTING_READ(SOUTH_CHICKEN1);
4029}
4030
4031static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4032{
4033 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004034
4035 switch (intel_crtc->pipe) {
4036 case PIPE_A:
4037 break;
4038 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004039 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004040 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004041 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004042 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004043
4044 break;
4045 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004046 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004047
4048 break;
4049 default:
4050 BUG();
4051 }
4052}
4053
Jesse Barnesf67a5592011-01-05 10:31:48 -08004054/*
4055 * Enable PCH resources required for PCH ports:
4056 * - PCH PLLs
4057 * - FDI training & RX/TX
4058 * - update transcoder timings
4059 * - DP transcoding bits
4060 * - transcoder
4061 */
4062static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004063{
4064 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004065 struct drm_i915_private *dev_priv = dev->dev_private;
4066 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4067 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004068 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004069
Daniel Vetterab9412b2013-05-03 11:49:46 +02004070 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004071
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004072 if (IS_IVYBRIDGE(dev))
4073 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4074
Daniel Vettercd986ab2012-10-26 10:58:12 +02004075 /* Write the TU size bits before fdi link training, so that error
4076 * detection works. */
4077 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4078 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4079
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004080 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004081 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004082
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004083 /* We need to program the right clock selection before writing the pixel
4084 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004085 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004086 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004087
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004088 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004089 temp |= TRANS_DPLL_ENABLE(pipe);
4090 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004091 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004092 temp |= sel;
4093 else
4094 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004095 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004096 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004097
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004098 /* XXX: pch pll's can be enabled any time before we enable the PCH
4099 * transcoder, and we actually should do this to not upset any PCH
4100 * transcoder that already use the clock when we share it.
4101 *
4102 * Note that enable_shared_dpll tries to do the right thing, but
4103 * get_shared_dpll unconditionally resets the pll - we need that to have
4104 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004105 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004106
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004107 /* set transcoder timing, panel must allow it */
4108 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004109 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004110
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004111 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004112
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004113 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004114 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004115 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01004116 reg = TRANS_DP_CTL(pipe);
4117 temp = I915_READ(reg);
4118 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004119 TRANS_DP_SYNC_MASK |
4120 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004121 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004122 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004123
4124 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004125 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004126 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004127 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004128
4129 switch (intel_trans_dp_port_sel(crtc)) {
4130 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004131 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004132 break;
4133 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004134 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004135 break;
4136 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004137 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004138 break;
4139 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004140 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004141 }
4142
Chris Wilson5eddb702010-09-11 13:48:45 +01004143 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004144 }
4145
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004146 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004147}
4148
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004149static void lpt_pch_enable(struct drm_crtc *crtc)
4150{
4151 struct drm_device *dev = crtc->dev;
4152 struct drm_i915_private *dev_priv = dev->dev_private;
4153 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004154 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004155
Daniel Vetterab9412b2013-05-03 11:49:46 +02004156 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004157
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004158 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004159
Paulo Zanoni0540e482012-10-31 18:12:40 -02004160 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004161 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004162
Paulo Zanoni937bb612012-10-31 18:12:47 -02004163 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004164}
4165
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004166struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4167 struct intel_crtc_state *crtc_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004168{
Daniel Vettere2b78262013-06-07 23:10:03 +02004169 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004170 struct intel_shared_dpll *pll;
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004171 struct intel_shared_dpll_config *shared_dpll;
Daniel Vettere2b78262013-06-07 23:10:03 +02004172 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004173
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004174 shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
4175
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004176 if (HAS_PCH_IBX(dev_priv->dev)) {
4177 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02004178 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004179 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004180
Daniel Vetter46edb022013-06-05 13:34:12 +02004181 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4182 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004183
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004184 WARN_ON(shared_dpll[i].crtc_mask);
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004185
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004186 goto found;
4187 }
4188
Satheeshakrishna Mbcddf6102014-08-22 09:49:10 +05304189 if (IS_BROXTON(dev_priv->dev)) {
4190 /* PLL is attached to port in bxt */
4191 struct intel_encoder *encoder;
4192 struct intel_digital_port *intel_dig_port;
4193
4194 encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
4195 if (WARN_ON(!encoder))
4196 return NULL;
4197
4198 intel_dig_port = enc_to_dig_port(&encoder->base);
4199 /* 1:1 mapping between ports and PLLs */
4200 i = (enum intel_dpll_id)intel_dig_port->port;
4201 pll = &dev_priv->shared_dplls[i];
4202 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4203 crtc->base.base.id, pll->name);
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004204 WARN_ON(shared_dpll[i].crtc_mask);
Satheeshakrishna Mbcddf6102014-08-22 09:49:10 +05304205
4206 goto found;
4207 }
4208
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004209 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4210 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004211
4212 /* Only want to check enabled timings first */
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004213 if (shared_dpll[i].crtc_mask == 0)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004214 continue;
4215
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004216 if (memcmp(&crtc_state->dpll_hw_state,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004217 &shared_dpll[i].hw_state,
4218 sizeof(crtc_state->dpll_hw_state)) == 0) {
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004219 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004220 crtc->base.base.id, pll->name,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004221 shared_dpll[i].crtc_mask,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004222 pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004223 goto found;
4224 }
4225 }
4226
4227 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004228 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4229 pll = &dev_priv->shared_dplls[i];
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004230 if (shared_dpll[i].crtc_mask == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02004231 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4232 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004233 goto found;
4234 }
4235 }
4236
4237 return NULL;
4238
4239found:
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004240 if (shared_dpll[i].crtc_mask == 0)
4241 shared_dpll[i].hw_state =
4242 crtc_state->dpll_hw_state;
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004243
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004244 crtc_state->shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02004245 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4246 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02004247
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004248 shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004249
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004250 return pll;
4251}
4252
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004253static void intel_shared_dpll_commit(struct drm_atomic_state *state)
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004254{
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004255 struct drm_i915_private *dev_priv = to_i915(state->dev);
4256 struct intel_shared_dpll_config *shared_dpll;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004257 struct intel_shared_dpll *pll;
4258 enum intel_dpll_id i;
4259
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004260 if (!to_intel_atomic_state(state)->dpll_set)
4261 return;
4262
4263 shared_dpll = to_intel_atomic_state(state)->shared_dpll;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004264 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4265 pll = &dev_priv->shared_dplls[i];
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004266 pll->config = shared_dpll[i];
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004267 }
4268}
4269
Daniel Vettera1520312013-05-03 11:49:50 +02004270static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004271{
4272 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01004273 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004274 u32 temp;
4275
4276 temp = I915_READ(dslreg);
4277 udelay(500);
4278 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004279 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004280 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004281 }
4282}
4283
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004284static int
4285skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4286 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4287 int src_w, int src_h, int dst_w, int dst_h)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004288{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004289 struct intel_crtc_scaler_state *scaler_state =
4290 &crtc_state->scaler_state;
4291 struct intel_crtc *intel_crtc =
4292 to_intel_crtc(crtc_state->base.crtc);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004293 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004294
4295 need_scaling = intel_rotation_90_or_270(rotation) ?
4296 (src_h != dst_w || src_w != dst_h):
4297 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004298
4299 /*
4300 * if plane is being disabled or scaler is no more required or force detach
4301 * - free scaler binded to this plane/crtc
4302 * - in order to do this, update crtc->scaler_usage
4303 *
4304 * Here scaler state in crtc_state is set free so that
4305 * scaler can be assigned to other user. Actual register
4306 * update to free the scaler is done in plane/panel-fit programming.
4307 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4308 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004309 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004310 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004311 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004312 scaler_state->scalers[*scaler_id].in_use = 0;
4313
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004314 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4315 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4316 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004317 scaler_state->scaler_users);
4318 *scaler_id = -1;
4319 }
4320 return 0;
4321 }
4322
4323 /* range checks */
4324 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4325 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4326
4327 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4328 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004329 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004330 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004331 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004332 return -EINVAL;
4333 }
4334
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004335 /* mark this plane as a scaler user in crtc_state */
4336 scaler_state->scaler_users |= (1 << scaler_user);
4337 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4338 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4339 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4340 scaler_state->scaler_users);
4341
4342 return 0;
4343}
4344
4345/**
4346 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4347 *
4348 * @state: crtc's scaler state
4349 * @force_detach: whether to forcibly disable scaler
4350 *
4351 * Return
4352 * 0 - scaler_usage updated successfully
4353 * error - requested scaling cannot be supported or other error condition
4354 */
4355int skl_update_scaler_crtc(struct intel_crtc_state *state, int force_detach)
4356{
4357 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
4358 struct drm_display_mode *adjusted_mode =
4359 &state->base.adjusted_mode;
4360
4361 DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
4362 intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
4363
4364 return skl_update_scaler(state, force_detach, SKL_CRTC_INDEX,
4365 &state->scaler_state.scaler_id, DRM_ROTATE_0,
4366 state->pipe_src_w, state->pipe_src_h,
Imre Deak8c6cda22015-06-23 20:40:27 +03004367 adjusted_mode->hdisplay, adjusted_mode->vdisplay);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004368}
4369
4370/**
4371 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4372 *
4373 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004374 * @plane_state: atomic plane state to update
4375 *
4376 * Return
4377 * 0 - scaler_usage updated successfully
4378 * error - requested scaling cannot be supported or other error condition
4379 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004380static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4381 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004382{
4383
4384 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004385 struct intel_plane *intel_plane =
4386 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004387 struct drm_framebuffer *fb = plane_state->base.fb;
4388 int ret;
4389
4390 bool force_detach = !fb || !plane_state->visible;
4391
4392 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
4393 intel_plane->base.base.id, intel_crtc->pipe,
4394 drm_plane_index(&intel_plane->base));
4395
4396 ret = skl_update_scaler(crtc_state, force_detach,
4397 drm_plane_index(&intel_plane->base),
4398 &plane_state->scaler_id,
4399 plane_state->base.rotation,
4400 drm_rect_width(&plane_state->src) >> 16,
4401 drm_rect_height(&plane_state->src) >> 16,
4402 drm_rect_width(&plane_state->dst),
4403 drm_rect_height(&plane_state->dst));
4404
4405 if (ret || plane_state->scaler_id < 0)
4406 return ret;
4407
Chandra Kondurua1b22782015-04-07 15:28:45 -07004408 /* check colorkey */
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004409 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004410 DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004411 intel_plane->base.base.id);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004412 return -EINVAL;
4413 }
4414
4415 /* Check src format */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004416 switch (fb->pixel_format) {
4417 case DRM_FORMAT_RGB565:
4418 case DRM_FORMAT_XBGR8888:
4419 case DRM_FORMAT_XRGB8888:
4420 case DRM_FORMAT_ABGR8888:
4421 case DRM_FORMAT_ARGB8888:
4422 case DRM_FORMAT_XRGB2101010:
4423 case DRM_FORMAT_XBGR2101010:
4424 case DRM_FORMAT_YUYV:
4425 case DRM_FORMAT_YVYU:
4426 case DRM_FORMAT_UYVY:
4427 case DRM_FORMAT_VYUY:
4428 break;
4429 default:
4430 DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
4431 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4432 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004433 }
4434
Chandra Kondurua1b22782015-04-07 15:28:45 -07004435 return 0;
4436}
4437
4438static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004439{
4440 struct drm_device *dev = crtc->base.dev;
4441 struct drm_i915_private *dev_priv = dev->dev_private;
4442 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004443 struct intel_crtc_scaler_state *scaler_state =
4444 &crtc->config->scaler_state;
4445
4446 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4447
4448 /* To update pfit, first update scaler state */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004449 skl_update_scaler_crtc(crtc->config, !enable);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004450 intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
4451 skl_detach_scalers(crtc);
4452 if (!enable)
4453 return;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004454
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004455 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004456 int id;
4457
4458 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4459 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4460 return;
4461 }
4462
4463 id = scaler_state->scaler_id;
4464 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4465 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4466 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4467 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4468
4469 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004470 }
4471}
4472
Jesse Barnesb074cec2013-04-25 12:55:02 -07004473static void ironlake_pfit_enable(struct intel_crtc *crtc)
4474{
4475 struct drm_device *dev = crtc->base.dev;
4476 struct drm_i915_private *dev_priv = dev->dev_private;
4477 int pipe = crtc->pipe;
4478
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004479 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004480 /* Force use of hard-coded filter coefficients
4481 * as some pre-programmed values are broken,
4482 * e.g. x201.
4483 */
4484 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4485 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4486 PF_PIPE_SEL_IVB(pipe));
4487 else
4488 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004489 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4490 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004491 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004492}
4493
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004494void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004495{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004496 struct drm_device *dev = crtc->base.dev;
4497 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004498
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004499 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004500 return;
4501
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004502 /* We can only enable IPS after we enable a plane and wait for a vblank */
4503 intel_wait_for_vblank(dev, crtc->pipe);
4504
Paulo Zanonid77e4532013-09-24 13:52:55 -03004505 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004506 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004507 mutex_lock(&dev_priv->rps.hw_lock);
4508 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4509 mutex_unlock(&dev_priv->rps.hw_lock);
4510 /* Quoting Art Runyan: "its not safe to expect any particular
4511 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004512 * mailbox." Moreover, the mailbox may return a bogus state,
4513 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004514 */
4515 } else {
4516 I915_WRITE(IPS_CTL, IPS_ENABLE);
4517 /* The bit only becomes 1 in the next vblank, so this wait here
4518 * is essentially intel_wait_for_vblank. If we don't have this
4519 * and don't wait for vblanks until the end of crtc_enable, then
4520 * the HW state readout code will complain that the expected
4521 * IPS_CTL value is not the one we read. */
4522 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4523 DRM_ERROR("Timed out waiting for IPS enable\n");
4524 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004525}
4526
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004527void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004528{
4529 struct drm_device *dev = crtc->base.dev;
4530 struct drm_i915_private *dev_priv = dev->dev_private;
4531
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004532 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004533 return;
4534
4535 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004536 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004537 mutex_lock(&dev_priv->rps.hw_lock);
4538 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4539 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004540 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4541 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4542 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004543 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004544 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004545 POSTING_READ(IPS_CTL);
4546 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004547
4548 /* We need to wait for a vblank before we can disable the plane. */
4549 intel_wait_for_vblank(dev, crtc->pipe);
4550}
4551
4552/** Loads the palette/gamma unit for the CRTC with the prepared values */
4553static void intel_crtc_load_lut(struct drm_crtc *crtc)
4554{
4555 struct drm_device *dev = crtc->dev;
4556 struct drm_i915_private *dev_priv = dev->dev_private;
4557 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4558 enum pipe pipe = intel_crtc->pipe;
4559 int palreg = PALETTE(pipe);
4560 int i;
4561 bool reenable_ips = false;
4562
4563 /* The clocks have to be on to load the palette. */
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004564 if (!crtc->state->active)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004565 return;
4566
Imre Deak50360402015-01-16 00:55:16 -08004567 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03004568 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004569 assert_dsi_pll_enabled(dev_priv);
4570 else
4571 assert_pll_enabled(dev_priv, pipe);
4572 }
4573
4574 /* use legacy palette for Ironlake */
Sonika Jindal7a1db492014-07-22 11:18:27 +05304575 if (!HAS_GMCH_DISPLAY(dev))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004576 palreg = LGC_PALETTE(pipe);
4577
4578 /* Workaround : Do not read or write the pipe palette/gamma data while
4579 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4580 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004581 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03004582 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4583 GAMMA_MODE_MODE_SPLIT)) {
4584 hsw_disable_ips(intel_crtc);
4585 reenable_ips = true;
4586 }
4587
4588 for (i = 0; i < 256; i++) {
4589 I915_WRITE(palreg + 4 * i,
4590 (intel_crtc->lut_r[i] << 16) |
4591 (intel_crtc->lut_g[i] << 8) |
4592 intel_crtc->lut_b[i]);
4593 }
4594
4595 if (reenable_ips)
4596 hsw_enable_ips(intel_crtc);
4597}
4598
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004599static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004600{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004601 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004602 struct drm_device *dev = intel_crtc->base.dev;
4603 struct drm_i915_private *dev_priv = dev->dev_private;
4604
4605 mutex_lock(&dev->struct_mutex);
4606 dev_priv->mm.interruptible = false;
4607 (void) intel_overlay_switch_off(intel_crtc->overlay);
4608 dev_priv->mm.interruptible = true;
4609 mutex_unlock(&dev->struct_mutex);
4610 }
4611
4612 /* Let userspace switch the overlay on again. In most cases userspace
4613 * has to recompute where to put it anyway.
4614 */
4615}
4616
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004617/**
4618 * intel_post_enable_primary - Perform operations after enabling primary plane
4619 * @crtc: the CRTC whose primary plane was just enabled
4620 *
4621 * Performs potentially sleeping operations that must be done after the primary
4622 * plane is enabled, such as updating FBC and IPS. Note that this may be
4623 * called due to an explicit primary plane update, or due to an implicit
4624 * re-enable that is caused when a sprite plane is updated to no longer
4625 * completely hide the primary plane.
4626 */
4627static void
4628intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004629{
4630 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004631 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004632 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4633 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004634
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004635 /*
4636 * BDW signals flip done immediately if the plane
4637 * is disabled, even if the plane enable is already
4638 * armed to occur at the next vblank :(
4639 */
4640 if (IS_BROADWELL(dev))
4641 intel_wait_for_vblank(dev, pipe);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004642
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004643 /*
4644 * FIXME IPS should be fine as long as one plane is
4645 * enabled, but in practice it seems to have problems
4646 * when going from primary only to sprite only and vice
4647 * versa.
4648 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004649 hsw_enable_ips(intel_crtc);
4650
Daniel Vetterf99d7062014-06-19 16:01:59 +02004651 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004652 * Gen2 reports pipe underruns whenever all planes are disabled.
4653 * So don't enable underrun reporting before at least some planes
4654 * are enabled.
4655 * FIXME: Need to fix the logic to work when we turn off all planes
4656 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004657 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004658 if (IS_GEN2(dev))
4659 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4660
4661 /* Underruns don't raise interrupts, so check manually. */
4662 if (HAS_GMCH_DISPLAY(dev))
4663 i9xx_check_fifo_underruns(dev_priv);
4664}
4665
4666/**
4667 * intel_pre_disable_primary - Perform operations before disabling primary plane
4668 * @crtc: the CRTC whose primary plane is to be disabled
4669 *
4670 * Performs potentially sleeping operations that must be done before the
4671 * primary plane is disabled, such as updating FBC and IPS. Note that this may
4672 * be called due to an explicit primary plane update, or due to an implicit
4673 * disable that is caused when a sprite plane completely hides the primary
4674 * plane.
4675 */
4676static void
4677intel_pre_disable_primary(struct drm_crtc *crtc)
4678{
4679 struct drm_device *dev = crtc->dev;
4680 struct drm_i915_private *dev_priv = dev->dev_private;
4681 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4682 int pipe = intel_crtc->pipe;
4683
4684 /*
4685 * Gen2 reports pipe underruns whenever all planes are disabled.
4686 * So diasble underrun reporting before all the planes get disabled.
4687 * FIXME: Need to fix the logic to work when we turn off all planes
4688 * but leave the pipe running.
4689 */
4690 if (IS_GEN2(dev))
4691 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4692
4693 /*
4694 * Vblank time updates from the shadow to live plane control register
4695 * are blocked if the memory self-refresh mode is active at that
4696 * moment. So to make sure the plane gets truly disabled, disable
4697 * first the self-refresh mode. The self-refresh enable bit in turn
4698 * will be checked/applied by the HW only at the next frame start
4699 * event which is after the vblank start event, so we need to have a
4700 * wait-for-vblank between disabling the plane and the pipe.
4701 */
4702 if (HAS_GMCH_DISPLAY(dev))
4703 intel_set_memory_cxsr(dev_priv, false);
4704
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004705 /*
4706 * FIXME IPS should be fine as long as one plane is
4707 * enabled, but in practice it seems to have problems
4708 * when going from primary only to sprite only and vice
4709 * versa.
4710 */
4711 hsw_disable_ips(intel_crtc);
4712}
4713
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004714static void intel_post_plane_update(struct intel_crtc *crtc)
4715{
4716 struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
4717 struct drm_device *dev = crtc->base.dev;
4718 struct drm_plane *plane;
4719
4720 if (atomic->wait_vblank)
4721 intel_wait_for_vblank(dev, crtc->pipe);
4722
4723 intel_frontbuffer_flip(dev, atomic->fb_bits);
4724
4725 if (atomic->update_fbc) {
4726 mutex_lock(&dev->struct_mutex);
4727 intel_fbc_update(dev);
4728 mutex_unlock(&dev->struct_mutex);
4729 }
4730
4731 if (atomic->post_enable_primary)
4732 intel_post_enable_primary(&crtc->base);
4733
4734 drm_for_each_plane_mask(plane, dev, atomic->update_sprite_watermarks)
4735 intel_update_sprite_watermarks(plane, &crtc->base,
4736 0, 0, 0, false, false);
4737
4738 memset(atomic, 0, sizeof(*atomic));
4739}
4740
4741static void intel_pre_plane_update(struct intel_crtc *crtc)
4742{
4743 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02004744 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004745 struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
4746 struct drm_plane *p;
4747
4748 /* Track fb's for any planes being disabled */
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004749 drm_for_each_plane_mask(p, dev, atomic->disabled_planes) {
4750 struct intel_plane *plane = to_intel_plane(p);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004751
4752 mutex_lock(&dev->struct_mutex);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +03004753 i915_gem_track_fb(intel_fb_obj(plane->base.fb), NULL,
4754 plane->frontbuffer_bit);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004755 mutex_unlock(&dev->struct_mutex);
4756 }
4757
4758 if (atomic->wait_for_flips)
4759 intel_crtc_wait_for_pending_flips(&crtc->base);
4760
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02004761 if (atomic->disable_fbc &&
4762 dev_priv->fbc.crtc == crtc) {
4763 mutex_lock(&dev->struct_mutex);
4764 if (dev_priv->fbc.crtc == crtc)
4765 intel_fbc_disable(dev);
4766 mutex_unlock(&dev->struct_mutex);
4767 }
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004768
Rodrigo Vivi066cf552015-06-26 13:55:54 -07004769 if (crtc->atomic.disable_ips)
4770 hsw_disable_ips(crtc);
4771
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004772 if (atomic->pre_disable_primary)
4773 intel_pre_disable_primary(&crtc->base);
4774}
4775
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004776static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004777{
4778 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004779 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004780 struct drm_plane *p;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004781 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004782
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004783 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004784
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004785 drm_for_each_plane_mask(p, dev, plane_mask)
4786 to_intel_plane(p)->disable_plane(p, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004787
Daniel Vetterf99d7062014-06-19 16:01:59 +02004788 /*
4789 * FIXME: Once we grow proper nuclear flip support out of this we need
4790 * to compute the mask of flip planes precisely. For the time being
4791 * consider this a flip to a NULL plane.
4792 */
4793 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004794}
4795
Jesse Barnesf67a5592011-01-05 10:31:48 -08004796static void ironlake_crtc_enable(struct drm_crtc *crtc)
4797{
4798 struct drm_device *dev = crtc->dev;
4799 struct drm_i915_private *dev_priv = dev->dev_private;
4800 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004801 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004802 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004803
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004804 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08004805 return;
4806
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004807 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004808 intel_prepare_shared_dpll(intel_crtc);
4809
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004810 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304811 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004812
4813 intel_set_pipe_timings(intel_crtc);
4814
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004815 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004816 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004817 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004818 }
4819
4820 ironlake_set_pipeconf(crtc);
4821
Jesse Barnesf67a5592011-01-05 10:31:48 -08004822 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004823
Daniel Vettera72e4c92014-09-30 10:56:47 +02004824 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4825 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004826
Daniel Vetterf6736a12013-06-05 13:34:30 +02004827 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004828 if (encoder->pre_enable)
4829 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004830
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004831 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004832 /* Note: FDI PLL enabling _must_ be done before we enable the
4833 * cpu pipes, hence this is separate from all the other fdi/pch
4834 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004835 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004836 } else {
4837 assert_fdi_tx_disabled(dev_priv, pipe);
4838 assert_fdi_rx_disabled(dev_priv, pipe);
4839 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004840
Jesse Barnesb074cec2013-04-25 12:55:02 -07004841 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004842
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004843 /*
4844 * On ILK+ LUT must be loaded before the pipe is running but with
4845 * clocks enabled
4846 */
4847 intel_crtc_load_lut(crtc);
4848
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004849 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004850 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004851
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004852 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004853 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004854
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004855 assert_vblank_disabled(crtc);
4856 drm_crtc_vblank_on(crtc);
4857
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004858 for_each_encoder_on_crtc(dev, crtc, encoder)
4859 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004860
4861 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004862 cpt_verify_modeset(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004863}
4864
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004865/* IPS only exists on ULT machines and is tied to pipe A. */
4866static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4867{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004868 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004869}
4870
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004871static void haswell_crtc_enable(struct drm_crtc *crtc)
4872{
4873 struct drm_device *dev = crtc->dev;
4874 struct drm_i915_private *dev_priv = dev->dev_private;
4875 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4876 struct intel_encoder *encoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004877 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4878 struct intel_crtc_state *pipe_config =
4879 to_intel_crtc_state(crtc->state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004880
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004881 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004882 return;
4883
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004884 if (intel_crtc_to_shared_dpll(intel_crtc))
4885 intel_enable_shared_dpll(intel_crtc);
4886
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004887 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304888 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004889
4890 intel_set_pipe_timings(intel_crtc);
4891
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004892 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
4893 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
4894 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004895 }
4896
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004897 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004898 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004899 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004900 }
4901
4902 haswell_set_pipeconf(crtc);
4903
4904 intel_set_pipe_csc(crtc);
4905
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004906 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004907
Daniel Vettera72e4c92014-09-30 10:56:47 +02004908 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004909 for_each_encoder_on_crtc(dev, crtc, encoder)
4910 if (encoder->pre_enable)
4911 encoder->pre_enable(encoder);
4912
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004913 if (intel_crtc->config->has_pch_encoder) {
Daniel Vettera72e4c92014-09-30 10:56:47 +02004914 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4915 true);
Imre Deak4fe94672014-06-25 22:01:49 +03004916 dev_priv->display.fdi_link_train(crtc);
4917 }
4918
Paulo Zanoni1f544382012-10-24 11:32:00 -02004919 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004920
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004921 if (INTEL_INFO(dev)->gen == 9)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004922 skylake_pfit_update(intel_crtc, 1);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004923 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004924 ironlake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004925 else
4926 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004927
4928 /*
4929 * On ILK+ LUT must be loaded before the pipe is running but with
4930 * clocks enabled
4931 */
4932 intel_crtc_load_lut(crtc);
4933
Paulo Zanoni1f544382012-10-24 11:32:00 -02004934 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00004935 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004936
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004937 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004938 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004939
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004940 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004941 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004942
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004943 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10004944 intel_ddi_set_vc_payload_alloc(crtc, true);
4945
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004946 assert_vblank_disabled(crtc);
4947 drm_crtc_vblank_on(crtc);
4948
Jani Nikula8807e552013-08-30 19:40:32 +03004949 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004950 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004951 intel_opregion_notify_encoder(encoder, true);
4952 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004953
Paulo Zanonie4916942013-09-20 16:21:19 -03004954 /* If we change the relative order between pipe/planes enabling, we need
4955 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004956 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4957 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4958 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4959 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4960 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004961}
4962
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004963static void ironlake_pfit_disable(struct intel_crtc *crtc)
4964{
4965 struct drm_device *dev = crtc->base.dev;
4966 struct drm_i915_private *dev_priv = dev->dev_private;
4967 int pipe = crtc->pipe;
4968
4969 /* To avoid upsetting the power well on haswell only disable the pfit if
4970 * it's in use. The hw state code will make sure we get this right. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004971 if (crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004972 I915_WRITE(PF_CTL(pipe), 0);
4973 I915_WRITE(PF_WIN_POS(pipe), 0);
4974 I915_WRITE(PF_WIN_SZ(pipe), 0);
4975 }
4976}
4977
Jesse Barnes6be4a602010-09-10 10:26:01 -07004978static void ironlake_crtc_disable(struct drm_crtc *crtc)
4979{
4980 struct drm_device *dev = crtc->dev;
4981 struct drm_i915_private *dev_priv = dev->dev_private;
4982 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004983 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004984 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01004985 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004986
Daniel Vetterea9d7582012-07-10 10:42:52 +02004987 for_each_encoder_on_crtc(dev, crtc, encoder)
4988 encoder->disable(encoder);
4989
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004990 drm_crtc_vblank_off(crtc);
4991 assert_vblank_disabled(crtc);
4992
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004993 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02004994 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Daniel Vetterd925c592013-06-05 13:34:04 +02004995
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004996 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004997
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004998 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004999
Ville Syrjälä5a74f702015-05-05 17:17:38 +03005000 if (intel_crtc->config->has_pch_encoder)
5001 ironlake_fdi_disable(crtc);
5002
Daniel Vetterbf49ec82012-09-06 22:15:40 +02005003 for_each_encoder_on_crtc(dev, crtc, encoder)
5004 if (encoder->post_disable)
5005 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005006
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005007 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005008 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005009
Daniel Vetterd925c592013-06-05 13:34:04 +02005010 if (HAS_PCH_CPT(dev)) {
5011 /* disable TRANS_DP_CTL */
5012 reg = TRANS_DP_CTL(pipe);
5013 temp = I915_READ(reg);
5014 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5015 TRANS_DP_PORT_SEL_MASK);
5016 temp |= TRANS_DP_PORT_SEL_NONE;
5017 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005018
Daniel Vetterd925c592013-06-05 13:34:04 +02005019 /* disable DPLL_SEL */
5020 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005021 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005022 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005023 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005024
Daniel Vetterd925c592013-06-05 13:34:04 +02005025 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005026 }
Jesse Barnes6be4a602010-09-10 10:26:01 -07005027}
5028
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005029static void haswell_crtc_disable(struct drm_crtc *crtc)
5030{
5031 struct drm_device *dev = crtc->dev;
5032 struct drm_i915_private *dev_priv = dev->dev_private;
5033 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5034 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005035 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005036
Jani Nikula8807e552013-08-30 19:40:32 +03005037 for_each_encoder_on_crtc(dev, crtc, encoder) {
5038 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005039 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005040 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005041
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005042 drm_crtc_vblank_off(crtc);
5043 assert_vblank_disabled(crtc);
5044
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005045 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005046 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5047 false);
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005048 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005049
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005050 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005051 intel_ddi_set_vc_payload_alloc(crtc, false);
5052
Paulo Zanoniad80a812012-10-24 16:06:19 -02005053 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005054
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005055 if (INTEL_INFO(dev)->gen == 9)
Chandra Kondurua1b22782015-04-07 15:28:45 -07005056 skylake_pfit_update(intel_crtc, 0);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005057 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00005058 ironlake_pfit_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005059 else
5060 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005061
Paulo Zanoni1f544382012-10-24 11:32:00 -02005062 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005063
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005064 if (intel_crtc->config->has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02005065 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02005066 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02005067 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005068
Imre Deak97b040a2014-06-25 22:01:50 +03005069 for_each_encoder_on_crtc(dev, crtc, encoder)
5070 if (encoder->post_disable)
5071 encoder->post_disable(encoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005072}
5073
Jesse Barnes2dd24552013-04-25 12:55:01 -07005074static void i9xx_pfit_enable(struct intel_crtc *crtc)
5075{
5076 struct drm_device *dev = crtc->base.dev;
5077 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005078 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005079
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005080 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005081 return;
5082
Daniel Vetterc0b03412013-05-28 12:05:54 +02005083 /*
5084 * The panel fitter should only be adjusted whilst the pipe is disabled,
5085 * according to register description and PRM.
5086 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005087 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5088 assert_pipe_disabled(dev_priv, crtc->pipe);
5089
Jesse Barnesb074cec2013-04-25 12:55:02 -07005090 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5091 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005092
5093 /* Border color in case we don't scale up to the full screen. Black by
5094 * default, change to something else for debugging. */
5095 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005096}
5097
Dave Airlied05410f2014-06-05 13:22:59 +10005098static enum intel_display_power_domain port_to_power_domain(enum port port)
5099{
5100 switch (port) {
5101 case PORT_A:
5102 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
5103 case PORT_B:
5104 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
5105 case PORT_C:
5106 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
5107 case PORT_D:
5108 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
5109 default:
5110 WARN_ON_ONCE(1);
5111 return POWER_DOMAIN_PORT_OTHER;
5112 }
5113}
5114
Imre Deak77d22dc2014-03-05 16:20:52 +02005115#define for_each_power_domain(domain, mask) \
5116 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
5117 if ((1 << (domain)) & (mask))
5118
Imre Deak319be8a2014-03-04 19:22:57 +02005119enum intel_display_power_domain
5120intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005121{
Imre Deak319be8a2014-03-04 19:22:57 +02005122 struct drm_device *dev = intel_encoder->base.dev;
5123 struct intel_digital_port *intel_dig_port;
5124
5125 switch (intel_encoder->type) {
5126 case INTEL_OUTPUT_UNKNOWN:
5127 /* Only DDI platforms should ever use this output type */
5128 WARN_ON_ONCE(!HAS_DDI(dev));
5129 case INTEL_OUTPUT_DISPLAYPORT:
5130 case INTEL_OUTPUT_HDMI:
5131 case INTEL_OUTPUT_EDP:
5132 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005133 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005134 case INTEL_OUTPUT_DP_MST:
5135 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5136 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005137 case INTEL_OUTPUT_ANALOG:
5138 return POWER_DOMAIN_PORT_CRT;
5139 case INTEL_OUTPUT_DSI:
5140 return POWER_DOMAIN_PORT_DSI;
5141 default:
5142 return POWER_DOMAIN_PORT_OTHER;
5143 }
5144}
5145
5146static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
5147{
5148 struct drm_device *dev = crtc->dev;
5149 struct intel_encoder *intel_encoder;
5150 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5151 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005152 unsigned long mask;
5153 enum transcoder transcoder;
5154
5155 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
5156
5157 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5158 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005159 if (intel_crtc->config->pch_pfit.enabled ||
5160 intel_crtc->config->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005161 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5162
Imre Deak319be8a2014-03-04 19:22:57 +02005163 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5164 mask |= BIT(intel_display_port_power_domain(intel_encoder));
5165
Imre Deak77d22dc2014-03-05 16:20:52 +02005166 return mask;
5167}
5168
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005169static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
Imre Deak77d22dc2014-03-05 16:20:52 +02005170{
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005171 struct drm_device *dev = state->dev;
Imre Deak77d22dc2014-03-05 16:20:52 +02005172 struct drm_i915_private *dev_priv = dev->dev_private;
5173 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
5174 struct intel_crtc *crtc;
5175
5176 /*
5177 * First get all needed power domains, then put all unneeded, to avoid
5178 * any unnecessary toggling of the power wells.
5179 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005180 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005181 enum intel_display_power_domain domain;
5182
Matt Roper83d65732015-02-25 13:12:16 -08005183 if (!crtc->base.state->enable)
Imre Deak77d22dc2014-03-05 16:20:52 +02005184 continue;
5185
Imre Deak319be8a2014-03-04 19:22:57 +02005186 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
Imre Deak77d22dc2014-03-05 16:20:52 +02005187
5188 for_each_power_domain(domain, pipe_domains[crtc->pipe])
5189 intel_display_power_get(dev_priv, domain);
5190 }
5191
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005192 if (dev_priv->display.modeset_commit_cdclk) {
5193 unsigned int cdclk = to_intel_atomic_state(state)->cdclk;
5194
5195 if (cdclk != dev_priv->cdclk_freq &&
5196 !WARN_ON(!state->allow_modeset))
5197 dev_priv->display.modeset_commit_cdclk(state);
5198 }
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005199
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005200 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005201 enum intel_display_power_domain domain;
5202
5203 for_each_power_domain(domain, crtc->enabled_power_domains)
5204 intel_display_power_put(dev_priv, domain);
5205
5206 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
5207 }
5208
5209 intel_display_set_init_power(dev_priv, false);
5210}
5211
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005212static void intel_update_max_cdclk(struct drm_device *dev)
5213{
5214 struct drm_i915_private *dev_priv = dev->dev_private;
5215
5216 if (IS_SKYLAKE(dev)) {
5217 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5218
5219 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5220 dev_priv->max_cdclk_freq = 675000;
5221 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5222 dev_priv->max_cdclk_freq = 540000;
5223 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5224 dev_priv->max_cdclk_freq = 450000;
5225 else
5226 dev_priv->max_cdclk_freq = 337500;
5227 } else if (IS_BROADWELL(dev)) {
5228 /*
5229 * FIXME with extra cooling we can allow
5230 * 540 MHz for ULX and 675 Mhz for ULT.
5231 * How can we know if extra cooling is
5232 * available? PCI ID, VTB, something else?
5233 */
5234 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5235 dev_priv->max_cdclk_freq = 450000;
5236 else if (IS_BDW_ULX(dev))
5237 dev_priv->max_cdclk_freq = 450000;
5238 else if (IS_BDW_ULT(dev))
5239 dev_priv->max_cdclk_freq = 540000;
5240 else
5241 dev_priv->max_cdclk_freq = 675000;
Mika Kahola0904dea2015-06-12 10:11:32 +03005242 } else if (IS_CHERRYVIEW(dev)) {
5243 dev_priv->max_cdclk_freq = 320000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005244 } else if (IS_VALLEYVIEW(dev)) {
5245 dev_priv->max_cdclk_freq = 400000;
5246 } else {
5247 /* otherwise assume cdclk is fixed */
5248 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5249 }
5250
5251 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5252 dev_priv->max_cdclk_freq);
5253}
5254
5255static void intel_update_cdclk(struct drm_device *dev)
5256{
5257 struct drm_i915_private *dev_priv = dev->dev_private;
5258
5259 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5260 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5261 dev_priv->cdclk_freq);
5262
5263 /*
5264 * Program the gmbus_freq based on the cdclk frequency.
5265 * BSpec erroneously claims we should aim for 4MHz, but
5266 * in fact 1MHz is the correct frequency.
5267 */
5268 if (IS_VALLEYVIEW(dev)) {
5269 /*
5270 * Program the gmbus_freq based on the cdclk frequency.
5271 * BSpec erroneously claims we should aim for 4MHz, but
5272 * in fact 1MHz is the correct frequency.
5273 */
5274 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5275 }
5276
5277 if (dev_priv->max_cdclk_freq == 0)
5278 intel_update_max_cdclk(dev);
5279}
5280
Damien Lespiau70d0c572015-06-04 18:21:29 +01005281static void broxton_set_cdclk(struct drm_device *dev, int frequency)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305282{
5283 struct drm_i915_private *dev_priv = dev->dev_private;
5284 uint32_t divider;
5285 uint32_t ratio;
5286 uint32_t current_freq;
5287 int ret;
5288
5289 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5290 switch (frequency) {
5291 case 144000:
5292 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5293 ratio = BXT_DE_PLL_RATIO(60);
5294 break;
5295 case 288000:
5296 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5297 ratio = BXT_DE_PLL_RATIO(60);
5298 break;
5299 case 384000:
5300 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5301 ratio = BXT_DE_PLL_RATIO(60);
5302 break;
5303 case 576000:
5304 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5305 ratio = BXT_DE_PLL_RATIO(60);
5306 break;
5307 case 624000:
5308 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5309 ratio = BXT_DE_PLL_RATIO(65);
5310 break;
5311 case 19200:
5312 /*
5313 * Bypass frequency with DE PLL disabled. Init ratio, divider
5314 * to suppress GCC warning.
5315 */
5316 ratio = 0;
5317 divider = 0;
5318 break;
5319 default:
5320 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5321
5322 return;
5323 }
5324
5325 mutex_lock(&dev_priv->rps.hw_lock);
5326 /* Inform power controller of upcoming frequency change */
5327 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5328 0x80000000);
5329 mutex_unlock(&dev_priv->rps.hw_lock);
5330
5331 if (ret) {
5332 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5333 ret, frequency);
5334 return;
5335 }
5336
5337 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5338 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5339 current_freq = current_freq * 500 + 1000;
5340
5341 /*
5342 * DE PLL has to be disabled when
5343 * - setting to 19.2MHz (bypass, PLL isn't used)
5344 * - before setting to 624MHz (PLL needs toggling)
5345 * - before setting to any frequency from 624MHz (PLL needs toggling)
5346 */
5347 if (frequency == 19200 || frequency == 624000 ||
5348 current_freq == 624000) {
5349 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5350 /* Timeout 200us */
5351 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5352 1))
5353 DRM_ERROR("timout waiting for DE PLL unlock\n");
5354 }
5355
5356 if (frequency != 19200) {
5357 uint32_t val;
5358
5359 val = I915_READ(BXT_DE_PLL_CTL);
5360 val &= ~BXT_DE_PLL_RATIO_MASK;
5361 val |= ratio;
5362 I915_WRITE(BXT_DE_PLL_CTL, val);
5363
5364 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5365 /* Timeout 200us */
5366 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5367 DRM_ERROR("timeout waiting for DE PLL lock\n");
5368
5369 val = I915_READ(CDCLK_CTL);
5370 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5371 val |= divider;
5372 /*
5373 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5374 * enable otherwise.
5375 */
5376 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5377 if (frequency >= 500000)
5378 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5379
5380 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5381 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5382 val |= (frequency - 1000) / 500;
5383 I915_WRITE(CDCLK_CTL, val);
5384 }
5385
5386 mutex_lock(&dev_priv->rps.hw_lock);
5387 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5388 DIV_ROUND_UP(frequency, 25000));
5389 mutex_unlock(&dev_priv->rps.hw_lock);
5390
5391 if (ret) {
5392 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5393 ret, frequency);
5394 return;
5395 }
5396
Damien Lespiaua47871b2015-06-04 18:21:34 +01005397 intel_update_cdclk(dev);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305398}
5399
5400void broxton_init_cdclk(struct drm_device *dev)
5401{
5402 struct drm_i915_private *dev_priv = dev->dev_private;
5403 uint32_t val;
5404
5405 /*
5406 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5407 * or else the reset will hang because there is no PCH to respond.
5408 * Move the handshake programming to initialization sequence.
5409 * Previously was left up to BIOS.
5410 */
5411 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5412 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5413 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5414
5415 /* Enable PG1 for cdclk */
5416 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5417
5418 /* check if cd clock is enabled */
5419 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5420 DRM_DEBUG_KMS("Display already initialized\n");
5421 return;
5422 }
5423
5424 /*
5425 * FIXME:
5426 * - The initial CDCLK needs to be read from VBT.
5427 * Need to make this change after VBT has changes for BXT.
5428 * - check if setting the max (or any) cdclk freq is really necessary
5429 * here, it belongs to modeset time
5430 */
5431 broxton_set_cdclk(dev, 624000);
5432
5433 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005434 POSTING_READ(DBUF_CTL);
5435
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305436 udelay(10);
5437
5438 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5439 DRM_ERROR("DBuf power enable timeout!\n");
5440}
5441
5442void broxton_uninit_cdclk(struct drm_device *dev)
5443{
5444 struct drm_i915_private *dev_priv = dev->dev_private;
5445
5446 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005447 POSTING_READ(DBUF_CTL);
5448
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305449 udelay(10);
5450
5451 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5452 DRM_ERROR("DBuf power disable timeout!\n");
5453
5454 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5455 broxton_set_cdclk(dev, 19200);
5456
5457 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5458}
5459
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005460static const struct skl_cdclk_entry {
5461 unsigned int freq;
5462 unsigned int vco;
5463} skl_cdclk_frequencies[] = {
5464 { .freq = 308570, .vco = 8640 },
5465 { .freq = 337500, .vco = 8100 },
5466 { .freq = 432000, .vco = 8640 },
5467 { .freq = 450000, .vco = 8100 },
5468 { .freq = 540000, .vco = 8100 },
5469 { .freq = 617140, .vco = 8640 },
5470 { .freq = 675000, .vco = 8100 },
5471};
5472
5473static unsigned int skl_cdclk_decimal(unsigned int freq)
5474{
5475 return (freq - 1000) / 500;
5476}
5477
5478static unsigned int skl_cdclk_get_vco(unsigned int freq)
5479{
5480 unsigned int i;
5481
5482 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5483 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5484
5485 if (e->freq == freq)
5486 return e->vco;
5487 }
5488
5489 return 8100;
5490}
5491
5492static void
5493skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5494{
5495 unsigned int min_freq;
5496 u32 val;
5497
5498 /* select the minimum CDCLK before enabling DPLL 0 */
5499 val = I915_READ(CDCLK_CTL);
5500 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5501 val |= CDCLK_FREQ_337_308;
5502
5503 if (required_vco == 8640)
5504 min_freq = 308570;
5505 else
5506 min_freq = 337500;
5507
5508 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5509
5510 I915_WRITE(CDCLK_CTL, val);
5511 POSTING_READ(CDCLK_CTL);
5512
5513 /*
5514 * We always enable DPLL0 with the lowest link rate possible, but still
5515 * taking into account the VCO required to operate the eDP panel at the
5516 * desired frequency. The usual DP link rates operate with a VCO of
5517 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5518 * The modeset code is responsible for the selection of the exact link
5519 * rate later on, with the constraint of choosing a frequency that
5520 * works with required_vco.
5521 */
5522 val = I915_READ(DPLL_CTRL1);
5523
5524 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5525 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5526 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5527 if (required_vco == 8640)
5528 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5529 SKL_DPLL0);
5530 else
5531 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5532 SKL_DPLL0);
5533
5534 I915_WRITE(DPLL_CTRL1, val);
5535 POSTING_READ(DPLL_CTRL1);
5536
5537 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5538
5539 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5540 DRM_ERROR("DPLL0 not locked\n");
5541}
5542
5543static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5544{
5545 int ret;
5546 u32 val;
5547
5548 /* inform PCU we want to change CDCLK */
5549 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5550 mutex_lock(&dev_priv->rps.hw_lock);
5551 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5552 mutex_unlock(&dev_priv->rps.hw_lock);
5553
5554 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5555}
5556
5557static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5558{
5559 unsigned int i;
5560
5561 for (i = 0; i < 15; i++) {
5562 if (skl_cdclk_pcu_ready(dev_priv))
5563 return true;
5564 udelay(10);
5565 }
5566
5567 return false;
5568}
5569
5570static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5571{
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005572 struct drm_device *dev = dev_priv->dev;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005573 u32 freq_select, pcu_ack;
5574
5575 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5576
5577 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5578 DRM_ERROR("failed to inform PCU about cdclk change\n");
5579 return;
5580 }
5581
5582 /* set CDCLK_CTL */
5583 switch(freq) {
5584 case 450000:
5585 case 432000:
5586 freq_select = CDCLK_FREQ_450_432;
5587 pcu_ack = 1;
5588 break;
5589 case 540000:
5590 freq_select = CDCLK_FREQ_540;
5591 pcu_ack = 2;
5592 break;
5593 case 308570:
5594 case 337500:
5595 default:
5596 freq_select = CDCLK_FREQ_337_308;
5597 pcu_ack = 0;
5598 break;
5599 case 617140:
5600 case 675000:
5601 freq_select = CDCLK_FREQ_675_617;
5602 pcu_ack = 3;
5603 break;
5604 }
5605
5606 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5607 POSTING_READ(CDCLK_CTL);
5608
5609 /* inform PCU of the change */
5610 mutex_lock(&dev_priv->rps.hw_lock);
5611 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5612 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005613
5614 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005615}
5616
5617void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5618{
5619 /* disable DBUF power */
5620 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5621 POSTING_READ(DBUF_CTL);
5622
5623 udelay(10);
5624
5625 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5626 DRM_ERROR("DBuf power disable timeout\n");
5627
5628 /* disable DPLL0 */
5629 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5630 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5631 DRM_ERROR("Couldn't disable DPLL0\n");
5632
5633 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5634}
5635
5636void skl_init_cdclk(struct drm_i915_private *dev_priv)
5637{
5638 u32 val;
5639 unsigned int required_vco;
5640
5641 /* enable PCH reset handshake */
5642 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5643 I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
5644
5645 /* enable PG1 and Misc I/O */
5646 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5647
5648 /* DPLL0 already enabed !? */
5649 if (I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE) {
5650 DRM_DEBUG_DRIVER("DPLL0 already running\n");
5651 return;
5652 }
5653
5654 /* enable DPLL0 */
5655 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5656 skl_dpll0_enable(dev_priv, required_vco);
5657
5658 /* set CDCLK to the frequency the BIOS chose */
5659 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5660
5661 /* enable DBUF power */
5662 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5663 POSTING_READ(DBUF_CTL);
5664
5665 udelay(10);
5666
5667 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5668 DRM_ERROR("DBuf power enable timeout\n");
5669}
5670
Ville Syrjälädfcab172014-06-13 13:37:47 +03005671/* returns HPLL frequency in kHz */
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005672static int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005673{
Jesse Barnes586f49d2013-11-04 16:06:59 -08005674 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08005675
Jesse Barnes586f49d2013-11-04 16:06:59 -08005676 /* Obtain SKU information */
Ville Syrjäläa5805162015-05-26 20:42:30 +03005677 mutex_lock(&dev_priv->sb_lock);
Jesse Barnes586f49d2013-11-04 16:06:59 -08005678 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
5679 CCK_FUSE_HPLL_FREQ_MASK;
Ville Syrjäläa5805162015-05-26 20:42:30 +03005680 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005681
Ville Syrjälädfcab172014-06-13 13:37:47 +03005682 return vco_freq[hpll_freq] * 1000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005683}
5684
5685/* Adjust CDclk dividers to allow high res or save power if possible */
5686static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5687{
5688 struct drm_i915_private *dev_priv = dev->dev_private;
5689 u32 val, cmd;
5690
Vandana Kannan164dfd22014-11-24 13:37:41 +05305691 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5692 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005693
Ville Syrjälädfcab172014-06-13 13:37:47 +03005694 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005695 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005696 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005697 cmd = 1;
5698 else
5699 cmd = 0;
5700
5701 mutex_lock(&dev_priv->rps.hw_lock);
5702 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5703 val &= ~DSPFREQGUAR_MASK;
5704 val |= (cmd << DSPFREQGUAR_SHIFT);
5705 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5706 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5707 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5708 50)) {
5709 DRM_ERROR("timed out waiting for CDclk change\n");
5710 }
5711 mutex_unlock(&dev_priv->rps.hw_lock);
5712
Ville Syrjälä54433e92015-05-26 20:42:31 +03005713 mutex_lock(&dev_priv->sb_lock);
5714
Ville Syrjälädfcab172014-06-13 13:37:47 +03005715 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005716 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005717
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005718 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005719
Jesse Barnes30a970c2013-11-04 13:48:12 -08005720 /* adjust cdclk divider */
5721 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjälä9cf33db2014-06-13 13:37:48 +03005722 val &= ~DISPLAY_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005723 val |= divider;
5724 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005725
5726 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5727 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5728 50))
5729 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005730 }
5731
Jesse Barnes30a970c2013-11-04 13:48:12 -08005732 /* adjust self-refresh exit latency value */
5733 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5734 val &= ~0x7f;
5735
5736 /*
5737 * For high bandwidth configs, we set a higher latency in the bunit
5738 * so that the core display fetch happens in time to avoid underruns.
5739 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005740 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005741 val |= 4500 / 250; /* 4.5 usec */
5742 else
5743 val |= 3000 / 250; /* 3.0 usec */
5744 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03005745
Ville Syrjäläa5805162015-05-26 20:42:30 +03005746 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005747
Ville Syrjäläb6283052015-06-03 15:45:07 +03005748 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005749}
5750
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005751static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5752{
5753 struct drm_i915_private *dev_priv = dev->dev_private;
5754 u32 val, cmd;
5755
Vandana Kannan164dfd22014-11-24 13:37:41 +05305756 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5757 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005758
5759 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005760 case 333333:
5761 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005762 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005763 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005764 break;
5765 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005766 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005767 return;
5768 }
5769
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005770 /*
5771 * Specs are full of misinformation, but testing on actual
5772 * hardware has shown that we just need to write the desired
5773 * CCK divider into the Punit register.
5774 */
5775 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5776
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005777 mutex_lock(&dev_priv->rps.hw_lock);
5778 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5779 val &= ~DSPFREQGUAR_MASK_CHV;
5780 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5781 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5782 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5783 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5784 50)) {
5785 DRM_ERROR("timed out waiting for CDclk change\n");
5786 }
5787 mutex_unlock(&dev_priv->rps.hw_lock);
5788
Ville Syrjäläb6283052015-06-03 15:45:07 +03005789 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005790}
5791
Jesse Barnes30a970c2013-11-04 13:48:12 -08005792static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5793 int max_pixclk)
5794{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005795 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005796 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005797
Jesse Barnes30a970c2013-11-04 13:48:12 -08005798 /*
5799 * Really only a few cases to deal with, as only 4 CDclks are supported:
5800 * 200MHz
5801 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005802 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005803 * 400MHz (VLV only)
5804 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5805 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005806 *
5807 * We seem to get an unstable or solid color picture at 200MHz.
5808 * Not sure what's wrong. For now use 200MHz only when all pipes
5809 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005810 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005811 if (!IS_CHERRYVIEW(dev_priv) &&
5812 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005813 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005814 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005815 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005816 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005817 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005818 else
5819 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005820}
5821
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305822static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5823 int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005824{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305825 /*
5826 * FIXME:
5827 * - remove the guardband, it's not needed on BXT
5828 * - set 19.2MHz bypass frequency if there are no active pipes
5829 */
5830 if (max_pixclk > 576000*9/10)
5831 return 624000;
5832 else if (max_pixclk > 384000*9/10)
5833 return 576000;
5834 else if (max_pixclk > 288000*9/10)
5835 return 384000;
5836 else if (max_pixclk > 144000*9/10)
5837 return 288000;
5838 else
5839 return 144000;
5840}
5841
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005842/* Compute the max pixel clock for new configuration. Uses atomic state if
5843 * that's non-NULL, look at current state otherwise. */
5844static int intel_mode_max_pixclk(struct drm_device *dev,
5845 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005846{
Jesse Barnes30a970c2013-11-04 13:48:12 -08005847 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005848 struct intel_crtc_state *crtc_state;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005849 int max_pixclk = 0;
5850
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005851 for_each_intel_crtc(dev, intel_crtc) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005852 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005853 if (IS_ERR(crtc_state))
5854 return PTR_ERR(crtc_state);
5855
5856 if (!crtc_state->base.enable)
5857 continue;
5858
5859 max_pixclk = max(max_pixclk,
5860 crtc_state->base.adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005861 }
5862
5863 return max_pixclk;
5864}
5865
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005866static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005867{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005868 struct drm_device *dev = state->dev;
5869 struct drm_i915_private *dev_priv = dev->dev_private;
5870 int max_pixclk = intel_mode_max_pixclk(dev, state);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005871
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005872 if (max_pixclk < 0)
5873 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005874
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005875 to_intel_atomic_state(state)->cdclk =
5876 valleyview_calc_cdclk(dev_priv, max_pixclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305877
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005878 return 0;
5879}
Jesse Barnes30a970c2013-11-04 13:48:12 -08005880
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005881static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
5882{
5883 struct drm_device *dev = state->dev;
5884 struct drm_i915_private *dev_priv = dev->dev_private;
5885 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005886
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005887 if (max_pixclk < 0)
5888 return max_pixclk;
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005889
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005890 to_intel_atomic_state(state)->cdclk =
5891 broxton_calc_cdclk(dev_priv, max_pixclk);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005892
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005893 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005894}
5895
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005896static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5897{
5898 unsigned int credits, default_credits;
5899
5900 if (IS_CHERRYVIEW(dev_priv))
5901 default_credits = PFI_CREDIT(12);
5902 else
5903 default_credits = PFI_CREDIT(8);
5904
Vandana Kannan164dfd22014-11-24 13:37:41 +05305905 if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005906 /* CHV suggested value is 31 or 63 */
5907 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjäläfcc00082015-05-26 20:22:40 +03005908 credits = PFI_CREDIT_63;
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005909 else
5910 credits = PFI_CREDIT(15);
5911 } else {
5912 credits = default_credits;
5913 }
5914
5915 /*
5916 * WA - write default credits before re-programming
5917 * FIXME: should we also set the resend bit here?
5918 */
5919 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5920 default_credits);
5921
5922 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5923 credits | PFI_CREDIT_RESEND);
5924
5925 /*
5926 * FIXME is this guaranteed to clear
5927 * immediately or should we poll for it?
5928 */
5929 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
5930}
5931
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005932static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005933{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005934 struct drm_device *dev = old_state->dev;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005935 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005936 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005937
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005938 /*
5939 * FIXME: We can end up here with all power domains off, yet
5940 * with a CDCLK frequency other than the minimum. To account
5941 * for this take the PIPE-A power domain, which covers the HW
5942 * blocks needed for the following programming. This can be
5943 * removed once it's guaranteed that we get here either with
5944 * the minimum CDCLK set, or the required power domains
5945 * enabled.
5946 */
5947 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005948
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005949 if (IS_CHERRYVIEW(dev))
5950 cherryview_set_cdclk(dev, req_cdclk);
5951 else
5952 valleyview_set_cdclk(dev, req_cdclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005953
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005954 vlv_program_pfi_credits(dev_priv);
Imre Deak738c05c2014-11-19 16:25:37 +02005955
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005956 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005957}
5958
Jesse Barnes89b667f2013-04-18 14:51:36 -07005959static void valleyview_crtc_enable(struct drm_crtc *crtc)
5960{
5961 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02005962 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005963 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5964 struct intel_encoder *encoder;
5965 int pipe = intel_crtc->pipe;
Jani Nikula23538ef2013-08-27 15:12:22 +03005966 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005967
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02005968 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07005969 return;
5970
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005971 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
Shobhit Kumar8525a232014-06-25 12:20:39 +05305972
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005973 if (!is_dsi) {
5974 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005975 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005976 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005977 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005978 }
Daniel Vetter5b18e572014-04-24 23:55:06 +02005979
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005980 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05305981 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005982
5983 intel_set_pipe_timings(intel_crtc);
5984
Ville Syrjäläc14b0482014-10-16 20:52:34 +03005985 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
5986 struct drm_i915_private *dev_priv = dev->dev_private;
5987
5988 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
5989 I915_WRITE(CHV_CANVAS(pipe), 0);
5990 }
5991
Daniel Vetter5b18e572014-04-24 23:55:06 +02005992 i9xx_set_pipeconf(intel_crtc);
5993
Jesse Barnes89b667f2013-04-18 14:51:36 -07005994 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005995
Daniel Vettera72e4c92014-09-30 10:56:47 +02005996 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005997
Jesse Barnes89b667f2013-04-18 14:51:36 -07005998 for_each_encoder_on_crtc(dev, crtc, encoder)
5999 if (encoder->pre_pll_enable)
6000 encoder->pre_pll_enable(encoder);
6001
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006002 if (!is_dsi) {
6003 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006004 chv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006005 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006006 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006007 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006008
6009 for_each_encoder_on_crtc(dev, crtc, encoder)
6010 if (encoder->pre_enable)
6011 encoder->pre_enable(encoder);
6012
Jesse Barnes2dd24552013-04-25 12:55:01 -07006013 i9xx_pfit_enable(intel_crtc);
6014
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006015 intel_crtc_load_lut(crtc);
6016
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006017 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006018 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006019
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006020 assert_vblank_disabled(crtc);
6021 drm_crtc_vblank_on(crtc);
6022
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006023 for_each_encoder_on_crtc(dev, crtc, encoder)
6024 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006025}
6026
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006027static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6028{
6029 struct drm_device *dev = crtc->base.dev;
6030 struct drm_i915_private *dev_priv = dev->dev_private;
6031
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006032 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6033 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006034}
6035
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006036static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006037{
6038 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006039 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006040 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006041 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006042 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006043
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006044 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006045 return;
6046
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006047 i9xx_set_pll_dividers(intel_crtc);
6048
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006049 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306050 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006051
6052 intel_set_pipe_timings(intel_crtc);
6053
Daniel Vetter5b18e572014-04-24 23:55:06 +02006054 i9xx_set_pipeconf(intel_crtc);
6055
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006056 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006057
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006058 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006059 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006060
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006061 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006062 if (encoder->pre_enable)
6063 encoder->pre_enable(encoder);
6064
Daniel Vetterf6736a12013-06-05 13:34:30 +02006065 i9xx_enable_pll(intel_crtc);
6066
Jesse Barnes2dd24552013-04-25 12:55:01 -07006067 i9xx_pfit_enable(intel_crtc);
6068
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006069 intel_crtc_load_lut(crtc);
6070
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006071 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006072 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006073
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006074 assert_vblank_disabled(crtc);
6075 drm_crtc_vblank_on(crtc);
6076
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006077 for_each_encoder_on_crtc(dev, crtc, encoder)
6078 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006079}
6080
Daniel Vetter87476d62013-04-11 16:29:06 +02006081static void i9xx_pfit_disable(struct intel_crtc *crtc)
6082{
6083 struct drm_device *dev = crtc->base.dev;
6084 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02006085
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006086 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006087 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006088
6089 assert_pipe_disabled(dev_priv, crtc->pipe);
6090
Daniel Vetter328d8e82013-05-08 10:36:31 +02006091 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6092 I915_READ(PFIT_CONTROL));
6093 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006094}
6095
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006096static void i9xx_crtc_disable(struct drm_crtc *crtc)
6097{
6098 struct drm_device *dev = crtc->dev;
6099 struct drm_i915_private *dev_priv = dev->dev_private;
6100 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006101 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006102 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006103
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006104 /*
6105 * On gen2 planes are double buffered but the pipe isn't, so we must
6106 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03006107 * We also need to wait on all gmch platforms because of the
6108 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006109 */
Imre Deak564ed192014-06-13 14:54:21 +03006110 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006111
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006112 for_each_encoder_on_crtc(dev, crtc, encoder)
6113 encoder->disable(encoder);
6114
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006115 drm_crtc_vblank_off(crtc);
6116 assert_vblank_disabled(crtc);
6117
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006118 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006119
Daniel Vetter87476d62013-04-11 16:29:06 +02006120 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006121
Jesse Barnes89b667f2013-04-18 14:51:36 -07006122 for_each_encoder_on_crtc(dev, crtc, encoder)
6123 if (encoder->post_disable)
6124 encoder->post_disable(encoder);
6125
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006126 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006127 if (IS_CHERRYVIEW(dev))
6128 chv_disable_pll(dev_priv, pipe);
6129 else if (IS_VALLEYVIEW(dev))
6130 vlv_disable_pll(dev_priv, pipe);
6131 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006132 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006133 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006134
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006135 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006136 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006137}
6138
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006139static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006140{
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006141 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006142 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006143 enum intel_display_power_domain domain;
6144 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006145
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006146 if (!intel_crtc->active)
6147 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006148
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006149 if (to_intel_plane_state(crtc->primary->state)->visible) {
6150 intel_crtc_wait_for_pending_flips(crtc);
6151 intel_pre_disable_primary(crtc);
6152 }
6153
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02006154 intel_crtc_disable_planes(crtc, crtc->state->plane_mask);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006155 dev_priv->display.crtc_disable(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006156
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006157 domains = intel_crtc->enabled_power_domains;
6158 for_each_power_domain(domain, domains)
6159 intel_display_power_put(dev_priv, domain);
6160 intel_crtc->enabled_power_domains = 0;
6161}
6162
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006163/*
6164 * turn all crtc's off, but do not adjust state
6165 * This has to be paired with a call to intel_modeset_setup_hw_state.
6166 */
Maarten Lankhorst9716c692015-06-10 10:24:19 +02006167void intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006168{
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006169 struct drm_crtc *crtc;
6170
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006171 for_each_crtc(dev, crtc)
6172 intel_crtc_disable_noatomic(crtc);
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006173}
6174
Chris Wilsoncdd59982010-09-08 16:30:16 +01006175/* Master function to enable/disable CRTC and corresponding power wells */
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006176int intel_crtc_control(struct drm_crtc *crtc, bool enable)
Daniel Vetter976f8a22012-07-08 22:34:21 +02006177{
6178 struct drm_device *dev = crtc->dev;
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006179 struct drm_mode_config *config = &dev->mode_config;
6180 struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006181 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006182 struct intel_crtc_state *pipe_config;
6183 struct drm_atomic_state *state;
6184 int ret;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006185
Maarten Lankhorst1b509252015-06-01 12:49:48 +02006186 if (enable == intel_crtc->active)
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006187 return 0;
Maarten Lankhorst1b509252015-06-01 12:49:48 +02006188
6189 if (enable && !crtc->state->enable)
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006190 return 0;
Maarten Lankhorst1b509252015-06-01 12:49:48 +02006191
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006192 /* this function should be called with drm_modeset_lock_all for now */
6193 if (WARN_ON(!ctx))
6194 return -EIO;
6195 lockdep_assert_held(&ctx->ww_ctx);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006196
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006197 state = drm_atomic_state_alloc(dev);
6198 if (WARN_ON(!state))
6199 return -ENOMEM;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006200
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006201 state->acquire_ctx = ctx;
6202 state->allow_modeset = true;
6203
6204 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
6205 if (IS_ERR(pipe_config)) {
6206 ret = PTR_ERR(pipe_config);
6207 goto err;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006208 }
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006209 pipe_config->base.active = enable;
6210
6211 ret = intel_set_mode(state);
6212 if (!ret)
6213 return ret;
6214
6215err:
6216 DRM_ERROR("Updating crtc active failed with %i\n", ret);
6217 drm_atomic_state_free(state);
6218 return ret;
Borun Fub04c5bd2014-07-12 10:02:27 +05306219}
6220
6221/**
6222 * Sets the power management mode of the pipe and plane.
6223 */
6224void intel_crtc_update_dpms(struct drm_crtc *crtc)
6225{
6226 struct drm_device *dev = crtc->dev;
6227 struct intel_encoder *intel_encoder;
6228 bool enable = false;
6229
6230 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
6231 enable |= intel_encoder->connectors_active;
6232
6233 intel_crtc_control(crtc, enable);
Chris Wilsoncdd59982010-09-08 16:30:16 +01006234}
6235
Chris Wilsonea5b2132010-08-04 13:50:23 +01006236void intel_encoder_destroy(struct drm_encoder *encoder)
6237{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006238 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006239
Chris Wilsonea5b2132010-08-04 13:50:23 +01006240 drm_encoder_cleanup(encoder);
6241 kfree(intel_encoder);
6242}
6243
Damien Lespiau92373292013-08-08 22:28:57 +01006244/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006245 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
6246 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01006247static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006248{
6249 if (mode == DRM_MODE_DPMS_ON) {
6250 encoder->connectors_active = true;
6251
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006252 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006253 } else {
6254 encoder->connectors_active = false;
6255
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006256 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006257 }
6258}
6259
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006260/* Cross check the actual hw state with our own modeset state tracking (and it's
6261 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02006262static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006263{
6264 if (connector->get_hw_state(connector)) {
6265 struct intel_encoder *encoder = connector->encoder;
6266 struct drm_crtc *crtc;
6267 bool encoder_enabled;
6268 enum pipe pipe;
6269
6270 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6271 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03006272 connector->base.name);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006273
Dave Airlie0e32b392014-05-02 14:02:48 +10006274 /* there is no real hw state for MST connectors */
6275 if (connector->mst_port)
6276 return;
6277
Rob Clarke2c719b2014-12-15 13:56:32 -05006278 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006279 "wrong connector dpms state\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006280 I915_STATE_WARN(connector->base.encoder != &encoder->base,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006281 "active connector not linked to encoder\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006282
Dave Airlie36cd7442014-05-02 13:44:18 +10006283 if (encoder) {
Rob Clarke2c719b2014-12-15 13:56:32 -05006284 I915_STATE_WARN(!encoder->connectors_active,
Dave Airlie36cd7442014-05-02 13:44:18 +10006285 "encoder->connectors_active not set\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006286
Dave Airlie36cd7442014-05-02 13:44:18 +10006287 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -05006288 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
6289 if (I915_STATE_WARN_ON(!encoder->base.crtc))
Dave Airlie36cd7442014-05-02 13:44:18 +10006290 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006291
Dave Airlie36cd7442014-05-02 13:44:18 +10006292 crtc = encoder->base.crtc;
6293
Matt Roper83d65732015-02-25 13:12:16 -08006294 I915_STATE_WARN(!crtc->state->enable,
6295 "crtc not enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006296 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
6297 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
Dave Airlie36cd7442014-05-02 13:44:18 +10006298 "encoder active on the wrong pipe\n");
6299 }
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006300 }
6301}
6302
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006303int intel_connector_init(struct intel_connector *connector)
6304{
6305 struct drm_connector_state *connector_state;
6306
6307 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6308 if (!connector_state)
6309 return -ENOMEM;
6310
6311 connector->base.state = connector_state;
6312 return 0;
6313}
6314
6315struct intel_connector *intel_connector_alloc(void)
6316{
6317 struct intel_connector *connector;
6318
6319 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6320 if (!connector)
6321 return NULL;
6322
6323 if (intel_connector_init(connector) < 0) {
6324 kfree(connector);
6325 return NULL;
6326 }
6327
6328 return connector;
6329}
6330
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006331/* Even simpler default implementation, if there's really no special case to
6332 * consider. */
6333void intel_connector_dpms(struct drm_connector *connector, int mode)
6334{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006335 /* All the simple cases only support two dpms states. */
6336 if (mode != DRM_MODE_DPMS_ON)
6337 mode = DRM_MODE_DPMS_OFF;
6338
6339 if (mode == connector->dpms)
6340 return;
6341
6342 connector->dpms = mode;
6343
6344 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dc2013-09-29 19:15:07 +01006345 if (connector->encoder)
6346 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006347
Daniel Vetterb9805142012-08-31 17:37:33 +02006348 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006349}
6350
Daniel Vetterf0947c32012-07-02 13:10:34 +02006351/* Simple connector->get_hw_state implementation for encoders that support only
6352 * one connector and no cloning and hence the encoder state determines the state
6353 * of the connector. */
6354bool intel_connector_get_hw_state(struct intel_connector *connector)
6355{
Daniel Vetter24929352012-07-02 20:28:59 +02006356 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006357 struct intel_encoder *encoder = connector->encoder;
6358
6359 return encoder->get_hw_state(encoder, &pipe);
6360}
6361
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006362static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006363{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006364 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6365 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006366
6367 return 0;
6368}
6369
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006370static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006371 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006372{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006373 struct drm_atomic_state *state = pipe_config->base.state;
6374 struct intel_crtc *other_crtc;
6375 struct intel_crtc_state *other_crtc_state;
6376
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006377 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6378 pipe_name(pipe), pipe_config->fdi_lanes);
6379 if (pipe_config->fdi_lanes > 4) {
6380 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6381 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006382 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006383 }
6384
Paulo Zanonibafb6552013-11-02 21:07:44 -07006385 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006386 if (pipe_config->fdi_lanes > 2) {
6387 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6388 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006389 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006390 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006391 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006392 }
6393 }
6394
6395 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006396 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006397
6398 /* Ivybridge 3 pipe is really complicated */
6399 switch (pipe) {
6400 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006401 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006402 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006403 if (pipe_config->fdi_lanes <= 2)
6404 return 0;
6405
6406 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6407 other_crtc_state =
6408 intel_atomic_get_crtc_state(state, other_crtc);
6409 if (IS_ERR(other_crtc_state))
6410 return PTR_ERR(other_crtc_state);
6411
6412 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006413 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6414 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006415 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006416 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006417 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006418 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006419 if (pipe_config->fdi_lanes > 2) {
6420 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6421 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006422 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006423 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006424
6425 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6426 other_crtc_state =
6427 intel_atomic_get_crtc_state(state, other_crtc);
6428 if (IS_ERR(other_crtc_state))
6429 return PTR_ERR(other_crtc_state);
6430
6431 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006432 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006433 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006434 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006435 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006436 default:
6437 BUG();
6438 }
6439}
6440
Daniel Vettere29c22c2013-02-21 00:00:16 +01006441#define RETRY 1
6442static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006443 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006444{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006445 struct drm_device *dev = intel_crtc->base.dev;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006446 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006447 int lane, link_bw, fdi_dotclock, ret;
6448 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006449
Daniel Vettere29c22c2013-02-21 00:00:16 +01006450retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006451 /* FDI is a binary signal running at ~2.7GHz, encoding
6452 * each output octet as 10 bits. The actual frequency
6453 * is stored as a divider into a 100MHz clock, and the
6454 * mode pixel clock is stored in units of 1KHz.
6455 * Hence the bw of each lane in terms of the mode signal
6456 * is:
6457 */
6458 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6459
Damien Lespiau241bfc32013-09-25 16:45:37 +01006460 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006461
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006462 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006463 pipe_config->pipe_bpp);
6464
6465 pipe_config->fdi_lanes = lane;
6466
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006467 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006468 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006469
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006470 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6471 intel_crtc->pipe, pipe_config);
6472 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006473 pipe_config->pipe_bpp -= 2*3;
6474 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6475 pipe_config->pipe_bpp);
6476 needs_recompute = true;
6477 pipe_config->bw_constrained = true;
6478
6479 goto retry;
6480 }
6481
6482 if (needs_recompute)
6483 return RETRY;
6484
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006485 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006486}
6487
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006488static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6489 struct intel_crtc_state *pipe_config)
6490{
6491 if (pipe_config->pipe_bpp > 24)
6492 return false;
6493
6494 /* HSW can handle pixel rate up to cdclk? */
6495 if (IS_HASWELL(dev_priv->dev))
6496 return true;
6497
6498 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006499 * We compare against max which means we must take
6500 * the increased cdclk requirement into account when
6501 * calculating the new cdclk.
6502 *
6503 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006504 */
6505 return ilk_pipe_pixel_rate(pipe_config) <=
6506 dev_priv->max_cdclk_freq * 95 / 100;
6507}
6508
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006509static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006510 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006511{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006512 struct drm_device *dev = crtc->base.dev;
6513 struct drm_i915_private *dev_priv = dev->dev_private;
6514
Jani Nikulad330a952014-01-21 11:24:25 +02006515 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006516 hsw_crtc_supports_ips(crtc) &&
6517 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006518}
6519
Daniel Vettera43f6e02013-06-07 23:10:32 +02006520static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006521 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006522{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006523 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006524 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006525 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01006526
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006527 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006528 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä44913152015-06-03 15:45:10 +03006529 int clock_limit = dev_priv->max_cdclk_freq;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006530
6531 /*
6532 * Enable pixel doubling when the dot clock
6533 * is > 90% of the (display) core speed.
6534 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03006535 * GDG double wide on either pipe,
6536 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006537 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03006538 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01006539 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006540 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006541 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006542 }
6543
Damien Lespiau241bfc32013-09-25 16:45:37 +01006544 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006545 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006546 }
Chris Wilson89749352010-09-12 18:25:19 +01006547
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006548 /*
6549 * Pipe horizontal size must be even in:
6550 * - DVO ganged mode
6551 * - LVDS dual channel mode
6552 * - Double wide pipe
6553 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006554 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006555 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6556 pipe_config->pipe_src_w &= ~1;
6557
Damien Lespiau8693a822013-05-03 18:48:11 +01006558 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6559 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006560 */
6561 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
6562 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006563 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006564
Damien Lespiauf5adf942013-06-24 18:29:34 +01006565 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006566 hsw_compute_ips_config(crtc, pipe_config);
6567
Daniel Vetter877d48d2013-04-19 11:24:43 +02006568 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006569 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006570
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006571 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006572}
6573
Ville Syrjälä1652d192015-03-31 14:12:01 +03006574static int skylake_get_display_clock_speed(struct drm_device *dev)
6575{
6576 struct drm_i915_private *dev_priv = to_i915(dev);
6577 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6578 uint32_t cdctl = I915_READ(CDCLK_CTL);
6579 uint32_t linkrate;
6580
Damien Lespiau414355a2015-06-04 18:21:31 +01006581 if (!(lcpll1 & LCPLL_PLL_ENABLE))
Ville Syrjälä1652d192015-03-31 14:12:01 +03006582 return 24000; /* 24MHz is the cd freq with NSSC ref */
Ville Syrjälä1652d192015-03-31 14:12:01 +03006583
6584 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6585 return 540000;
6586
6587 linkrate = (I915_READ(DPLL_CTRL1) &
Damien Lespiau71cd8422015-04-30 16:39:17 +01006588 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006589
Damien Lespiau71cd8422015-04-30 16:39:17 +01006590 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6591 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006592 /* vco 8640 */
6593 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6594 case CDCLK_FREQ_450_432:
6595 return 432000;
6596 case CDCLK_FREQ_337_308:
6597 return 308570;
6598 case CDCLK_FREQ_675_617:
6599 return 617140;
6600 default:
6601 WARN(1, "Unknown cd freq selection\n");
6602 }
6603 } else {
6604 /* vco 8100 */
6605 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6606 case CDCLK_FREQ_450_432:
6607 return 450000;
6608 case CDCLK_FREQ_337_308:
6609 return 337500;
6610 case CDCLK_FREQ_675_617:
6611 return 675000;
6612 default:
6613 WARN(1, "Unknown cd freq selection\n");
6614 }
6615 }
6616
6617 /* error case, do as if DPLL0 isn't enabled */
6618 return 24000;
6619}
6620
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006621static int broxton_get_display_clock_speed(struct drm_device *dev)
6622{
6623 struct drm_i915_private *dev_priv = to_i915(dev);
6624 uint32_t cdctl = I915_READ(CDCLK_CTL);
6625 uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
6626 uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
6627 int cdclk;
6628
6629 if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
6630 return 19200;
6631
6632 cdclk = 19200 * pll_ratio / 2;
6633
6634 switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
6635 case BXT_CDCLK_CD2X_DIV_SEL_1:
6636 return cdclk; /* 576MHz or 624MHz */
6637 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
6638 return cdclk * 2 / 3; /* 384MHz */
6639 case BXT_CDCLK_CD2X_DIV_SEL_2:
6640 return cdclk / 2; /* 288MHz */
6641 case BXT_CDCLK_CD2X_DIV_SEL_4:
6642 return cdclk / 4; /* 144MHz */
6643 }
6644
6645 /* error case, do as if DE PLL isn't enabled */
6646 return 19200;
6647}
6648
Ville Syrjälä1652d192015-03-31 14:12:01 +03006649static int broadwell_get_display_clock_speed(struct drm_device *dev)
6650{
6651 struct drm_i915_private *dev_priv = dev->dev_private;
6652 uint32_t lcpll = I915_READ(LCPLL_CTL);
6653 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6654
6655 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6656 return 800000;
6657 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6658 return 450000;
6659 else if (freq == LCPLL_CLK_FREQ_450)
6660 return 450000;
6661 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6662 return 540000;
6663 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6664 return 337500;
6665 else
6666 return 675000;
6667}
6668
6669static int haswell_get_display_clock_speed(struct drm_device *dev)
6670{
6671 struct drm_i915_private *dev_priv = dev->dev_private;
6672 uint32_t lcpll = I915_READ(LCPLL_CTL);
6673 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6674
6675 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6676 return 800000;
6677 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6678 return 450000;
6679 else if (freq == LCPLL_CLK_FREQ_450)
6680 return 450000;
6681 else if (IS_HSW_ULT(dev))
6682 return 337500;
6683 else
6684 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006685}
6686
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006687static int valleyview_get_display_clock_speed(struct drm_device *dev)
6688{
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006689 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006690 u32 val;
6691 int divider;
6692
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006693 if (dev_priv->hpll_freq == 0)
6694 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
6695
Ville Syrjäläa5805162015-05-26 20:42:30 +03006696 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006697 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03006698 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006699
6700 divider = val & DISPLAY_FREQUENCY_VALUES;
6701
Ville Syrjälä7d007f42014-06-13 13:37:53 +03006702 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
6703 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
6704 "cdclk change in progress\n");
6705
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006706 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006707}
6708
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006709static int ilk_get_display_clock_speed(struct drm_device *dev)
6710{
6711 return 450000;
6712}
6713
Jesse Barnese70236a2009-09-21 10:42:27 -07006714static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006715{
Jesse Barnese70236a2009-09-21 10:42:27 -07006716 return 400000;
6717}
Jesse Barnes79e53942008-11-07 14:24:08 -08006718
Jesse Barnese70236a2009-09-21 10:42:27 -07006719static int i915_get_display_clock_speed(struct drm_device *dev)
6720{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006721 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006722}
Jesse Barnes79e53942008-11-07 14:24:08 -08006723
Jesse Barnese70236a2009-09-21 10:42:27 -07006724static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6725{
6726 return 200000;
6727}
Jesse Barnes79e53942008-11-07 14:24:08 -08006728
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006729static int pnv_get_display_clock_speed(struct drm_device *dev)
6730{
6731 u16 gcfgc = 0;
6732
6733 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6734
6735 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6736 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006737 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006738 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006739 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006740 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006741 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006742 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6743 return 200000;
6744 default:
6745 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6746 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006747 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006748 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006749 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006750 }
6751}
6752
Jesse Barnese70236a2009-09-21 10:42:27 -07006753static int i915gm_get_display_clock_speed(struct drm_device *dev)
6754{
6755 u16 gcfgc = 0;
6756
6757 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6758
6759 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006760 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006761 else {
6762 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6763 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006764 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006765 default:
6766 case GC_DISPLAY_CLOCK_190_200_MHZ:
6767 return 190000;
6768 }
6769 }
6770}
Jesse Barnes79e53942008-11-07 14:24:08 -08006771
Jesse Barnese70236a2009-09-21 10:42:27 -07006772static int i865_get_display_clock_speed(struct drm_device *dev)
6773{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006774 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006775}
6776
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006777static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07006778{
6779 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006780
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03006781 /*
6782 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6783 * encoding is different :(
6784 * FIXME is this the right way to detect 852GM/852GMV?
6785 */
6786 if (dev->pdev->revision == 0x1)
6787 return 133333;
6788
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006789 pci_bus_read_config_word(dev->pdev->bus,
6790 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6791
Jesse Barnese70236a2009-09-21 10:42:27 -07006792 /* Assume that the hardware is in the high speed state. This
6793 * should be the default.
6794 */
6795 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6796 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006797 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07006798 case GC_CLOCK_100_200:
6799 return 200000;
6800 case GC_CLOCK_166_250:
6801 return 250000;
6802 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006803 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006804 case GC_CLOCK_133_266:
6805 case GC_CLOCK_133_266_2:
6806 case GC_CLOCK_166_266:
6807 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006808 }
6809
6810 /* Shouldn't happen */
6811 return 0;
6812}
6813
6814static int i830_get_display_clock_speed(struct drm_device *dev)
6815{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006816 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006817}
6818
Ville Syrjälä34edce22015-05-22 11:22:33 +03006819static unsigned int intel_hpll_vco(struct drm_device *dev)
6820{
6821 struct drm_i915_private *dev_priv = dev->dev_private;
6822 static const unsigned int blb_vco[8] = {
6823 [0] = 3200000,
6824 [1] = 4000000,
6825 [2] = 5333333,
6826 [3] = 4800000,
6827 [4] = 6400000,
6828 };
6829 static const unsigned int pnv_vco[8] = {
6830 [0] = 3200000,
6831 [1] = 4000000,
6832 [2] = 5333333,
6833 [3] = 4800000,
6834 [4] = 2666667,
6835 };
6836 static const unsigned int cl_vco[8] = {
6837 [0] = 3200000,
6838 [1] = 4000000,
6839 [2] = 5333333,
6840 [3] = 6400000,
6841 [4] = 3333333,
6842 [5] = 3566667,
6843 [6] = 4266667,
6844 };
6845 static const unsigned int elk_vco[8] = {
6846 [0] = 3200000,
6847 [1] = 4000000,
6848 [2] = 5333333,
6849 [3] = 4800000,
6850 };
6851 static const unsigned int ctg_vco[8] = {
6852 [0] = 3200000,
6853 [1] = 4000000,
6854 [2] = 5333333,
6855 [3] = 6400000,
6856 [4] = 2666667,
6857 [5] = 4266667,
6858 };
6859 const unsigned int *vco_table;
6860 unsigned int vco;
6861 uint8_t tmp = 0;
6862
6863 /* FIXME other chipsets? */
6864 if (IS_GM45(dev))
6865 vco_table = ctg_vco;
6866 else if (IS_G4X(dev))
6867 vco_table = elk_vco;
6868 else if (IS_CRESTLINE(dev))
6869 vco_table = cl_vco;
6870 else if (IS_PINEVIEW(dev))
6871 vco_table = pnv_vco;
6872 else if (IS_G33(dev))
6873 vco_table = blb_vco;
6874 else
6875 return 0;
6876
6877 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6878
6879 vco = vco_table[tmp & 0x7];
6880 if (vco == 0)
6881 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6882 else
6883 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6884
6885 return vco;
6886}
6887
6888static int gm45_get_display_clock_speed(struct drm_device *dev)
6889{
6890 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6891 uint16_t tmp = 0;
6892
6893 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6894
6895 cdclk_sel = (tmp >> 12) & 0x1;
6896
6897 switch (vco) {
6898 case 2666667:
6899 case 4000000:
6900 case 5333333:
6901 return cdclk_sel ? 333333 : 222222;
6902 case 3200000:
6903 return cdclk_sel ? 320000 : 228571;
6904 default:
6905 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6906 return 222222;
6907 }
6908}
6909
6910static int i965gm_get_display_clock_speed(struct drm_device *dev)
6911{
6912 static const uint8_t div_3200[] = { 16, 10, 8 };
6913 static const uint8_t div_4000[] = { 20, 12, 10 };
6914 static const uint8_t div_5333[] = { 24, 16, 14 };
6915 const uint8_t *div_table;
6916 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6917 uint16_t tmp = 0;
6918
6919 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6920
6921 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
6922
6923 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6924 goto fail;
6925
6926 switch (vco) {
6927 case 3200000:
6928 div_table = div_3200;
6929 break;
6930 case 4000000:
6931 div_table = div_4000;
6932 break;
6933 case 5333333:
6934 div_table = div_5333;
6935 break;
6936 default:
6937 goto fail;
6938 }
6939
6940 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6941
Damien Lespiaucaf4e252015-06-04 16:56:18 +01006942fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03006943 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
6944 return 200000;
6945}
6946
6947static int g33_get_display_clock_speed(struct drm_device *dev)
6948{
6949 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
6950 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
6951 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
6952 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
6953 const uint8_t *div_table;
6954 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6955 uint16_t tmp = 0;
6956
6957 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6958
6959 cdclk_sel = (tmp >> 4) & 0x7;
6960
6961 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6962 goto fail;
6963
6964 switch (vco) {
6965 case 3200000:
6966 div_table = div_3200;
6967 break;
6968 case 4000000:
6969 div_table = div_4000;
6970 break;
6971 case 4800000:
6972 div_table = div_4800;
6973 break;
6974 case 5333333:
6975 div_table = div_5333;
6976 break;
6977 default:
6978 goto fail;
6979 }
6980
6981 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6982
Damien Lespiaucaf4e252015-06-04 16:56:18 +01006983fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03006984 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
6985 return 190476;
6986}
6987
Zhenyu Wang2c072452009-06-05 15:38:42 +08006988static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006989intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006990{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006991 while (*num > DATA_LINK_M_N_MASK ||
6992 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08006993 *num >>= 1;
6994 *den >>= 1;
6995 }
6996}
6997
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006998static void compute_m_n(unsigned int m, unsigned int n,
6999 uint32_t *ret_m, uint32_t *ret_n)
7000{
7001 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7002 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7003 intel_reduce_m_n_ratio(ret_m, ret_n);
7004}
7005
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007006void
7007intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7008 int pixel_clock, int link_clock,
7009 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007010{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007011 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007012
7013 compute_m_n(bits_per_pixel * pixel_clock,
7014 link_clock * nlanes * 8,
7015 &m_n->gmch_m, &m_n->gmch_n);
7016
7017 compute_m_n(pixel_clock, link_clock,
7018 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007019}
7020
Chris Wilsona7615032011-01-12 17:04:08 +00007021static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7022{
Jani Nikulad330a952014-01-21 11:24:25 +02007023 if (i915.panel_use_ssc >= 0)
7024 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007025 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007026 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007027}
7028
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007029static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
7030 int num_connectors)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007031{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007032 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007033 struct drm_i915_private *dev_priv = dev->dev_private;
7034 int refclk;
7035
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007036 WARN_ON(!crtc_state->base.state);
7037
Imre Deak5ab7b0b2015-03-06 03:29:25 +02007038 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02007039 refclk = 100000;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007040 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007041 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007042 refclk = dev_priv->vbt.lvds_ssc_freq;
7043 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007044 } else if (!IS_GEN2(dev)) {
7045 refclk = 96000;
7046 } else {
7047 refclk = 48000;
7048 }
7049
7050 return refclk;
7051}
7052
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007053static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007054{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007055 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007056}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007057
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007058static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7059{
7060 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007061}
7062
Daniel Vetterf47709a2013-03-28 10:42:02 +01007063static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007064 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08007065 intel_clock_t *reduced_clock)
7066{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007067 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007068 u32 fp, fp2 = 0;
7069
7070 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007071 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007072 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007073 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007074 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007075 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007076 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007077 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007078 }
7079
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007080 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007081
Daniel Vetterf47709a2013-03-28 10:42:02 +01007082 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007083 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007084 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007085 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007086 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007087 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007088 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007089 }
7090}
7091
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007092static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7093 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007094{
7095 u32 reg_val;
7096
7097 /*
7098 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7099 * and set it to a reasonable value instead.
7100 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007101 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007102 reg_val &= 0xffffff00;
7103 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007104 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007105
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007106 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007107 reg_val &= 0x8cffffff;
7108 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007109 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007110
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007111 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007112 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007113 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007114
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007115 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007116 reg_val &= 0x00ffffff;
7117 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007118 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007119}
7120
Daniel Vetterb5518422013-05-03 11:49:48 +02007121static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7122 struct intel_link_m_n *m_n)
7123{
7124 struct drm_device *dev = crtc->base.dev;
7125 struct drm_i915_private *dev_priv = dev->dev_private;
7126 int pipe = crtc->pipe;
7127
Daniel Vettere3b95f12013-05-03 11:49:49 +02007128 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7129 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7130 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7131 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007132}
7133
7134static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007135 struct intel_link_m_n *m_n,
7136 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007137{
7138 struct drm_device *dev = crtc->base.dev;
7139 struct drm_i915_private *dev_priv = dev->dev_private;
7140 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007141 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007142
7143 if (INTEL_INFO(dev)->gen >= 5) {
7144 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7145 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7146 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7147 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007148 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7149 * for gen < 8) and if DRRS is supported (to make sure the
7150 * registers are not unnecessarily accessed).
7151 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307152 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007153 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007154 I915_WRITE(PIPE_DATA_M2(transcoder),
7155 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7156 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7157 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7158 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7159 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007160 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007161 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7162 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7163 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7164 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007165 }
7166}
7167
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307168void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007169{
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307170 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7171
7172 if (m_n == M1_N1) {
7173 dp_m_n = &crtc->config->dp_m_n;
7174 dp_m2_n2 = &crtc->config->dp_m2_n2;
7175 } else if (m_n == M2_N2) {
7176
7177 /*
7178 * M2_N2 registers are not supported. Hence m2_n2 divider value
7179 * needs to be programmed into M1_N1.
7180 */
7181 dp_m_n = &crtc->config->dp_m2_n2;
7182 } else {
7183 DRM_ERROR("Unsupported divider value\n");
7184 return;
7185 }
7186
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007187 if (crtc->config->has_pch_encoder)
7188 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007189 else
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307190 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007191}
7192
Daniel Vetter251ac862015-06-18 10:30:24 +02007193static void vlv_compute_dpll(struct intel_crtc *crtc,
7194 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007195{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007196 u32 dpll, dpll_md;
7197
7198 /*
7199 * Enable DPIO clock input. We should never disable the reference
7200 * clock for pipe B, since VGA hotplug / manual detection depends
7201 * on it.
7202 */
7203 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
7204 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
7205 /* We should never disable this, set it here for state tracking */
7206 if (crtc->pipe == PIPE_B)
7207 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7208 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007209 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007210
Ville Syrjäläd288f652014-10-28 13:20:22 +02007211 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007212 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007213 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007214}
7215
Ville Syrjäläd288f652014-10-28 13:20:22 +02007216static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007217 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007218{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007219 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007220 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007221 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007222 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007223 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007224 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007225
Ville Syrjäläa5805162015-05-26 20:42:30 +03007226 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007227
Ville Syrjäläd288f652014-10-28 13:20:22 +02007228 bestn = pipe_config->dpll.n;
7229 bestm1 = pipe_config->dpll.m1;
7230 bestm2 = pipe_config->dpll.m2;
7231 bestp1 = pipe_config->dpll.p1;
7232 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007233
Jesse Barnes89b667f2013-04-18 14:51:36 -07007234 /* See eDP HDMI DPIO driver vbios notes doc */
7235
7236 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007237 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007238 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007239
7240 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007241 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007242
7243 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007244 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007245 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007246 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007247
7248 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007249 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007250
7251 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007252 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7253 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7254 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007255 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007256
7257 /*
7258 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7259 * but we don't support that).
7260 * Note: don't use the DAC post divider as it seems unstable.
7261 */
7262 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007263 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007264
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007265 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007266 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007267
Jesse Barnes89b667f2013-04-18 14:51:36 -07007268 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007269 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007270 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7271 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007272 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03007273 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007274 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007275 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007276 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007277
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007278 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007279 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007280 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007281 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007282 0x0df40000);
7283 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007284 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007285 0x0df70000);
7286 } else { /* HDMI or VGA */
7287 /* Use bend 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 0x0df70000);
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 0x0df40000);
7294 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007295
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007296 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007297 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007298 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7299 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007300 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007301 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007302
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007303 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007304 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007305}
7306
Daniel Vetter251ac862015-06-18 10:30:24 +02007307static void chv_compute_dpll(struct intel_crtc *crtc,
7308 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007309{
Ville Syrjäläd288f652014-10-28 13:20:22 +02007310 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007311 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
7312 DPLL_VCO_ENABLE;
7313 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007314 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007315
Ville Syrjäläd288f652014-10-28 13:20:22 +02007316 pipe_config->dpll_hw_state.dpll_md =
7317 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007318}
7319
Ville Syrjäläd288f652014-10-28 13:20:22 +02007320static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007321 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007322{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007323 struct drm_device *dev = crtc->base.dev;
7324 struct drm_i915_private *dev_priv = dev->dev_private;
7325 int pipe = crtc->pipe;
7326 int dpll_reg = DPLL(crtc->pipe);
7327 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307328 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007329 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307330 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307331 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007332
Ville Syrjäläd288f652014-10-28 13:20:22 +02007333 bestn = pipe_config->dpll.n;
7334 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7335 bestm1 = pipe_config->dpll.m1;
7336 bestm2 = pipe_config->dpll.m2 >> 22;
7337 bestp1 = pipe_config->dpll.p1;
7338 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307339 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307340 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307341 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007342
7343 /*
7344 * Enable Refclk and SSC
7345 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007346 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007347 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007348
Ville Syrjäläa5805162015-05-26 20:42:30 +03007349 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007350
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007351 /* p1 and p2 divider */
7352 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7353 5 << DPIO_CHV_S1_DIV_SHIFT |
7354 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7355 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7356 1 << DPIO_CHV_K_DIV_SHIFT);
7357
7358 /* Feedback post-divider - m2 */
7359 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7360
7361 /* Feedback refclk divider - n and m1 */
7362 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7363 DPIO_CHV_M1_DIV_BY_2 |
7364 1 << DPIO_CHV_N_DIV_SHIFT);
7365
7366 /* M2 fraction division */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307367 if (bestm2_frac)
7368 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007369
7370 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307371 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7372 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7373 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7374 if (bestm2_frac)
7375 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7376 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007377
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307378 /* Program digital lock detect threshold */
7379 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7380 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7381 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7382 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7383 if (!bestm2_frac)
7384 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7385 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7386
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007387 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307388 if (vco == 5400000) {
7389 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7390 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7391 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7392 tribuf_calcntr = 0x9;
7393 } else if (vco <= 6200000) {
7394 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7395 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7396 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7397 tribuf_calcntr = 0x9;
7398 } else if (vco <= 6480000) {
7399 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7400 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7401 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7402 tribuf_calcntr = 0x8;
7403 } else {
7404 /* Not supported. Apply the same limits as in the max case */
7405 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7406 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7407 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7408 tribuf_calcntr = 0;
7409 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007410 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7411
Ville Syrjälä968040b2015-03-11 22:52:08 +02007412 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307413 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7414 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7415 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7416
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007417 /* AFC Recal */
7418 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7419 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7420 DPIO_AFC_RECAL);
7421
Ville Syrjäläa5805162015-05-26 20:42:30 +03007422 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007423}
7424
Ville Syrjäläd288f652014-10-28 13:20:22 +02007425/**
7426 * vlv_force_pll_on - forcibly enable just the PLL
7427 * @dev_priv: i915 private structure
7428 * @pipe: pipe PLL to enable
7429 * @dpll: PLL configuration
7430 *
7431 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7432 * in cases where we need the PLL enabled even when @pipe is not going to
7433 * be enabled.
7434 */
7435void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7436 const struct dpll *dpll)
7437{
7438 struct intel_crtc *crtc =
7439 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007440 struct intel_crtc_state pipe_config = {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007441 .base.crtc = &crtc->base,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007442 .pixel_multiplier = 1,
7443 .dpll = *dpll,
7444 };
7445
7446 if (IS_CHERRYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02007447 chv_compute_dpll(crtc, &pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007448 chv_prepare_pll(crtc, &pipe_config);
7449 chv_enable_pll(crtc, &pipe_config);
7450 } else {
Daniel Vetter251ac862015-06-18 10:30:24 +02007451 vlv_compute_dpll(crtc, &pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007452 vlv_prepare_pll(crtc, &pipe_config);
7453 vlv_enable_pll(crtc, &pipe_config);
7454 }
7455}
7456
7457/**
7458 * vlv_force_pll_off - forcibly disable just the PLL
7459 * @dev_priv: i915 private structure
7460 * @pipe: pipe PLL to disable
7461 *
7462 * Disable the PLL for @pipe. To be used in cases where we need
7463 * the PLL enabled even when @pipe is not going to be enabled.
7464 */
7465void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7466{
7467 if (IS_CHERRYVIEW(dev))
7468 chv_disable_pll(to_i915(dev), pipe);
7469 else
7470 vlv_disable_pll(to_i915(dev), pipe);
7471}
7472
Daniel Vetter251ac862015-06-18 10:30:24 +02007473static void i9xx_compute_dpll(struct intel_crtc *crtc,
7474 struct intel_crtc_state *crtc_state,
7475 intel_clock_t *reduced_clock,
7476 int num_connectors)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007477{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007478 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007479 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007480 u32 dpll;
7481 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007482 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007483
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007484 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307485
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007486 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7487 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007488
7489 dpll = DPLL_VGA_MODE_DIS;
7490
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007491 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007492 dpll |= DPLLB_MODE_LVDS;
7493 else
7494 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007495
Daniel Vetteref1b4602013-06-01 17:17:04 +02007496 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007497 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007498 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007499 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007500
7501 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007502 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007503
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007504 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007505 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007506
7507 /* compute bitmask from p1 value */
7508 if (IS_PINEVIEW(dev))
7509 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7510 else {
7511 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7512 if (IS_G4X(dev) && reduced_clock)
7513 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7514 }
7515 switch (clock->p2) {
7516 case 5:
7517 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7518 break;
7519 case 7:
7520 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7521 break;
7522 case 10:
7523 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7524 break;
7525 case 14:
7526 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7527 break;
7528 }
7529 if (INTEL_INFO(dev)->gen >= 4)
7530 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7531
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007532 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007533 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007534 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007535 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7536 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7537 else
7538 dpll |= PLL_REF_INPUT_DREFCLK;
7539
7540 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007541 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007542
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007543 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007544 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007545 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007546 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007547 }
7548}
7549
Daniel Vetter251ac862015-06-18 10:30:24 +02007550static void i8xx_compute_dpll(struct intel_crtc *crtc,
7551 struct intel_crtc_state *crtc_state,
7552 intel_clock_t *reduced_clock,
7553 int num_connectors)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007554{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007555 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007556 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007557 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007558 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007559
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007560 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307561
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007562 dpll = DPLL_VGA_MODE_DIS;
7563
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007564 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007565 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7566 } else {
7567 if (clock->p1 == 2)
7568 dpll |= PLL_P1_DIVIDE_BY_TWO;
7569 else
7570 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7571 if (clock->p2 == 4)
7572 dpll |= PLL_P2_DIVIDE_BY_4;
7573 }
7574
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007575 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007576 dpll |= DPLL_DVO_2X_MODE;
7577
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007578 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007579 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7580 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7581 else
7582 dpll |= PLL_REF_INPUT_DREFCLK;
7583
7584 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007585 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007586}
7587
Daniel Vetter8a654f32013-06-01 17:16:22 +02007588static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007589{
7590 struct drm_device *dev = intel_crtc->base.dev;
7591 struct drm_i915_private *dev_priv = dev->dev_private;
7592 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007593 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02007594 struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007595 &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007596 uint32_t crtc_vtotal, crtc_vblank_end;
7597 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007598
7599 /* We need to be careful not to changed the adjusted mode, for otherwise
7600 * the hw state checker will get angry at the mismatch. */
7601 crtc_vtotal = adjusted_mode->crtc_vtotal;
7602 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007603
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007604 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007605 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007606 crtc_vtotal -= 1;
7607 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007608
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007609 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007610 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7611 else
7612 vsyncshift = adjusted_mode->crtc_hsync_start -
7613 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007614 if (vsyncshift < 0)
7615 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007616 }
7617
7618 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007619 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007620
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007621 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007622 (adjusted_mode->crtc_hdisplay - 1) |
7623 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007624 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007625 (adjusted_mode->crtc_hblank_start - 1) |
7626 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007627 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007628 (adjusted_mode->crtc_hsync_start - 1) |
7629 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7630
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007631 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007632 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007633 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007634 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007635 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007636 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007637 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007638 (adjusted_mode->crtc_vsync_start - 1) |
7639 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7640
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007641 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7642 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7643 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7644 * bits. */
7645 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7646 (pipe == PIPE_B || pipe == PIPE_C))
7647 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7648
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007649 /* pipesrc controls the size that is scaled from, which should
7650 * always be the user's requested size.
7651 */
7652 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007653 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7654 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007655}
7656
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007657static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007658 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007659{
7660 struct drm_device *dev = crtc->base.dev;
7661 struct drm_i915_private *dev_priv = dev->dev_private;
7662 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7663 uint32_t tmp;
7664
7665 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007666 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7667 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007668 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007669 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7670 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007671 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007672 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7673 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007674
7675 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007676 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7677 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007678 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007679 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7680 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007681 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007682 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7683 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007684
7685 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007686 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7687 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7688 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007689 }
7690
7691 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007692 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7693 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7694
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007695 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7696 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007697}
7698
Daniel Vetterf6a83282014-02-11 15:28:57 -08007699void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007700 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007701{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007702 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7703 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7704 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7705 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007706
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007707 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7708 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7709 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7710 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007711
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007712 mode->flags = pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007713
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007714 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7715 mode->flags |= pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007716}
7717
Daniel Vetter84b046f2013-02-19 18:48:54 +01007718static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7719{
7720 struct drm_device *dev = intel_crtc->base.dev;
7721 struct drm_i915_private *dev_priv = dev->dev_private;
7722 uint32_t pipeconf;
7723
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007724 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007725
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007726 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7727 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7728 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007729
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007730 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007731 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007732
Daniel Vetterff9ce462013-04-24 14:57:17 +02007733 /* only g4x and later have fancy bpc/dither controls */
7734 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007735 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007736 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007737 pipeconf |= PIPECONF_DITHER_EN |
7738 PIPECONF_DITHER_TYPE_SP;
7739
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007740 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007741 case 18:
7742 pipeconf |= PIPECONF_6BPC;
7743 break;
7744 case 24:
7745 pipeconf |= PIPECONF_8BPC;
7746 break;
7747 case 30:
7748 pipeconf |= PIPECONF_10BPC;
7749 break;
7750 default:
7751 /* Case prevented by intel_choose_pipe_bpp_dither. */
7752 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007753 }
7754 }
7755
7756 if (HAS_PIPE_CXSR(dev)) {
7757 if (intel_crtc->lowfreq_avail) {
7758 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7759 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7760 } else {
7761 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007762 }
7763 }
7764
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007765 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007766 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007767 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007768 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7769 else
7770 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7771 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007772 pipeconf |= PIPECONF_PROGRESSIVE;
7773
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007774 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007775 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007776
Daniel Vetter84b046f2013-02-19 18:48:54 +01007777 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7778 POSTING_READ(PIPECONF(intel_crtc->pipe));
7779}
7780
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007781static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7782 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007783{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007784 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007785 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc751ce42010-03-25 11:48:48 -07007786 int refclk, num_connectors = 0;
Daniel Vetterc329a4e2015-06-18 10:30:23 +02007787 intel_clock_t clock;
7788 bool ok;
7789 bool is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01007790 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08007791 const intel_limit_t *limit;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007792 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007793 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007794 struct drm_connector_state *connector_state;
7795 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -08007796
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007797 memset(&crtc_state->dpll_hw_state, 0,
7798 sizeof(crtc_state->dpll_hw_state));
7799
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007800 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007801 if (connector_state->crtc != &crtc->base)
7802 continue;
7803
7804 encoder = to_intel_encoder(connector_state->best_encoder);
7805
Chris Wilson5eddb702010-09-11 13:48:45 +01007806 switch (encoder->type) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007807 case INTEL_OUTPUT_DSI:
7808 is_dsi = true;
7809 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007810 default:
7811 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007812 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05007813
Eric Anholtc751ce42010-03-25 11:48:48 -07007814 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08007815 }
7816
Jani Nikulaf2335332013-09-13 11:03:09 +03007817 if (is_dsi)
Daniel Vetter5b18e572014-04-24 23:55:06 +02007818 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007819
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007820 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007821 refclk = i9xx_get_refclk(crtc_state, num_connectors);
Jani Nikulaf2335332013-09-13 11:03:09 +03007822
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007823 /*
7824 * Returns a set of divisors for the desired target clock with
7825 * the given refclk, or FALSE. The returned values represent
7826 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7827 * 2) / p1 / p2.
7828 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007829 limit = intel_limit(crtc_state, refclk);
7830 ok = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007831 crtc_state->port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007832 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03007833 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007834 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7835 return -EINVAL;
7836 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007837
Jani Nikulaf2335332013-09-13 11:03:09 +03007838 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007839 crtc_state->dpll.n = clock.n;
7840 crtc_state->dpll.m1 = clock.m1;
7841 crtc_state->dpll.m2 = clock.m2;
7842 crtc_state->dpll.p1 = clock.p1;
7843 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007844 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007845
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007846 if (IS_GEN2(dev)) {
Daniel Vetterc329a4e2015-06-18 10:30:23 +02007847 i8xx_compute_dpll(crtc, crtc_state, NULL,
Daniel Vetter251ac862015-06-18 10:30:24 +02007848 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007849 } else if (IS_CHERRYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02007850 chv_compute_dpll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007851 } else if (IS_VALLEYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02007852 vlv_compute_dpll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007853 } else {
Daniel Vetterc329a4e2015-06-18 10:30:23 +02007854 i9xx_compute_dpll(crtc, crtc_state, NULL,
Daniel Vetter251ac862015-06-18 10:30:24 +02007855 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007856 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007857
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007858 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007859}
7860
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007861static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007862 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007863{
7864 struct drm_device *dev = crtc->base.dev;
7865 struct drm_i915_private *dev_priv = dev->dev_private;
7866 uint32_t tmp;
7867
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007868 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7869 return;
7870
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007871 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007872 if (!(tmp & PFIT_ENABLE))
7873 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007874
Daniel Vetter06922822013-07-11 13:35:40 +02007875 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007876 if (INTEL_INFO(dev)->gen < 4) {
7877 if (crtc->pipe != PIPE_B)
7878 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007879 } else {
7880 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7881 return;
7882 }
7883
Daniel Vetter06922822013-07-11 13:35:40 +02007884 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007885 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7886 if (INTEL_INFO(dev)->gen < 5)
7887 pipe_config->gmch_pfit.lvds_border_bits =
7888 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7889}
7890
Jesse Barnesacbec812013-09-20 11:29:32 -07007891static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007892 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07007893{
7894 struct drm_device *dev = crtc->base.dev;
7895 struct drm_i915_private *dev_priv = dev->dev_private;
7896 int pipe = pipe_config->cpu_transcoder;
7897 intel_clock_t clock;
7898 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07007899 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07007900
Shobhit Kumarf573de52014-07-30 20:32:37 +05307901 /* In case of MIPI DPLL will not even be used */
7902 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7903 return;
7904
Ville Syrjäläa5805162015-05-26 20:42:30 +03007905 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007906 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03007907 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007908
7909 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7910 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7911 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7912 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7913 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7914
Ville Syrjäläf6466282013-10-14 14:50:31 +03007915 vlv_clock(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007916
Ville Syrjäläf6466282013-10-14 14:50:31 +03007917 /* clock.dot is the fast clock */
7918 pipe_config->port_clock = clock.dot / 5;
Jesse Barnesacbec812013-09-20 11:29:32 -07007919}
7920
Damien Lespiau5724dbd2015-01-20 12:51:52 +00007921static void
7922i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7923 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007924{
7925 struct drm_device *dev = crtc->base.dev;
7926 struct drm_i915_private *dev_priv = dev->dev_private;
7927 u32 val, base, offset;
7928 int pipe = crtc->pipe, plane = crtc->plane;
7929 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00007930 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007931 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00007932 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007933
Damien Lespiau42a7b082015-02-05 19:35:13 +00007934 val = I915_READ(DSPCNTR(plane));
7935 if (!(val & DISPLAY_PLANE_ENABLE))
7936 return;
7937
Damien Lespiaud9806c92015-01-21 14:07:19 +00007938 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00007939 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007940 DRM_DEBUG_KMS("failed to alloc fb\n");
7941 return;
7942 }
7943
Damien Lespiau1b842c82015-01-21 13:50:54 +00007944 fb = &intel_fb->base;
7945
Daniel Vetter18c52472015-02-10 17:16:09 +00007946 if (INTEL_INFO(dev)->gen >= 4) {
7947 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007948 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00007949 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
7950 }
7951 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007952
7953 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00007954 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007955 fb->pixel_format = fourcc;
7956 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007957
7958 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007959 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007960 offset = I915_READ(DSPTILEOFF(plane));
7961 else
7962 offset = I915_READ(DSPLINOFF(plane));
7963 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7964 } else {
7965 base = I915_READ(DSPADDR(plane));
7966 }
7967 plane_config->base = base;
7968
7969 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007970 fb->width = ((val >> 16) & 0xfff) + 1;
7971 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007972
7973 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007974 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007975
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007976 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00007977 fb->pixel_format,
7978 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007979
Daniel Vetterf37b5c22015-02-10 23:12:27 +01007980 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007981
Damien Lespiau2844a922015-01-20 12:51:48 +00007982 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7983 pipe_name(pipe), plane, fb->width, fb->height,
7984 fb->bits_per_pixel, base, fb->pitches[0],
7985 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007986
Damien Lespiau2d140302015-02-05 17:22:18 +00007987 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007988}
7989
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007990static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007991 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007992{
7993 struct drm_device *dev = crtc->base.dev;
7994 struct drm_i915_private *dev_priv = dev->dev_private;
7995 int pipe = pipe_config->cpu_transcoder;
7996 enum dpio_channel port = vlv_pipe_to_channel(pipe);
7997 intel_clock_t clock;
7998 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
7999 int refclk = 100000;
8000
Ville Syrjäläa5805162015-05-26 20:42:30 +03008001 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008002 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8003 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8004 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8005 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008006 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008007
8008 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
8009 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
8010 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8011 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8012 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8013
8014 chv_clock(refclk, &clock);
8015
8016 /* clock.dot is the fast clock */
8017 pipe_config->port_clock = clock.dot / 5;
8018}
8019
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008020static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008021 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008022{
8023 struct drm_device *dev = crtc->base.dev;
8024 struct drm_i915_private *dev_priv = dev->dev_private;
8025 uint32_t tmp;
8026
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008027 if (!intel_display_power_is_enabled(dev_priv,
8028 POWER_DOMAIN_PIPE(crtc->pipe)))
Imre Deakb5482bd2014-03-05 16:20:55 +02008029 return false;
8030
Daniel Vettere143a212013-07-04 12:01:15 +02008031 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008032 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02008033
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008034 tmp = I915_READ(PIPECONF(crtc->pipe));
8035 if (!(tmp & PIPECONF_ENABLE))
8036 return false;
8037
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008038 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
8039 switch (tmp & PIPECONF_BPC_MASK) {
8040 case PIPECONF_6BPC:
8041 pipe_config->pipe_bpp = 18;
8042 break;
8043 case PIPECONF_8BPC:
8044 pipe_config->pipe_bpp = 24;
8045 break;
8046 case PIPECONF_10BPC:
8047 pipe_config->pipe_bpp = 30;
8048 break;
8049 default:
8050 break;
8051 }
8052 }
8053
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008054 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
8055 pipe_config->limited_color_range = true;
8056
Ville Syrjälä282740f2013-09-04 18:30:03 +03008057 if (INTEL_INFO(dev)->gen < 4)
8058 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8059
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008060 intel_get_pipe_timings(crtc, pipe_config);
8061
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008062 i9xx_get_pfit_config(crtc, pipe_config);
8063
Daniel Vetter6c49f242013-06-06 12:45:25 +02008064 if (INTEL_INFO(dev)->gen >= 4) {
8065 tmp = I915_READ(DPLL_MD(crtc->pipe));
8066 pipe_config->pixel_multiplier =
8067 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8068 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008069 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008070 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8071 tmp = I915_READ(DPLL(crtc->pipe));
8072 pipe_config->pixel_multiplier =
8073 ((tmp & SDVO_MULTIPLIER_MASK)
8074 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8075 } else {
8076 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8077 * port and will be fixed up in the encoder->get_config
8078 * function. */
8079 pipe_config->pixel_multiplier = 1;
8080 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008081 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
8082 if (!IS_VALLEYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008083 /*
8084 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8085 * on 830. Filter it out here so that we don't
8086 * report errors due to that.
8087 */
8088 if (IS_I830(dev))
8089 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8090
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008091 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8092 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008093 } else {
8094 /* Mask out read-only status bits. */
8095 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8096 DPLL_PORTC_READY_MASK |
8097 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008098 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008099
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008100 if (IS_CHERRYVIEW(dev))
8101 chv_crtc_clock_get(crtc, pipe_config);
8102 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008103 vlv_crtc_clock_get(crtc, pipe_config);
8104 else
8105 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008106
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008107 return true;
8108}
8109
Paulo Zanonidde86e22012-12-01 12:04:25 -02008110static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008111{
8112 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008113 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008114 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008115 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008116 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008117 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008118 bool has_ck505 = false;
8119 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008120
8121 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008122 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008123 switch (encoder->type) {
8124 case INTEL_OUTPUT_LVDS:
8125 has_panel = true;
8126 has_lvds = true;
8127 break;
8128 case INTEL_OUTPUT_EDP:
8129 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008130 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008131 has_cpu_edp = true;
8132 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008133 default:
8134 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008135 }
8136 }
8137
Keith Packard99eb6a02011-09-26 14:29:12 -07008138 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008139 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008140 can_ssc = has_ck505;
8141 } else {
8142 has_ck505 = false;
8143 can_ssc = true;
8144 }
8145
Imre Deak2de69052013-05-08 13:14:04 +03008146 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8147 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008148
8149 /* Ironlake: try to setup display ref clock before DPLL
8150 * enabling. This is only under driver's control after
8151 * PCH B stepping, previous chipset stepping should be
8152 * ignoring this setting.
8153 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008154 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008155
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008156 /* As we must carefully and slowly disable/enable each source in turn,
8157 * compute the final state we want first and check if we need to
8158 * make any changes at all.
8159 */
8160 final = val;
8161 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008162 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008163 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008164 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008165 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8166
8167 final &= ~DREF_SSC_SOURCE_MASK;
8168 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8169 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008170
Keith Packard199e5d72011-09-22 12:01:57 -07008171 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008172 final |= DREF_SSC_SOURCE_ENABLE;
8173
8174 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8175 final |= DREF_SSC1_ENABLE;
8176
8177 if (has_cpu_edp) {
8178 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8179 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8180 else
8181 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8182 } else
8183 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8184 } else {
8185 final |= DREF_SSC_SOURCE_DISABLE;
8186 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8187 }
8188
8189 if (final == val)
8190 return;
8191
8192 /* Always enable nonspread source */
8193 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8194
8195 if (has_ck505)
8196 val |= DREF_NONSPREAD_CK505_ENABLE;
8197 else
8198 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8199
8200 if (has_panel) {
8201 val &= ~DREF_SSC_SOURCE_MASK;
8202 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008203
Keith Packard199e5d72011-09-22 12:01:57 -07008204 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008205 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008206 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008207 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008208 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008209 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008210
8211 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008212 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008213 POSTING_READ(PCH_DREF_CONTROL);
8214 udelay(200);
8215
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008216 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008217
8218 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008219 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008220 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008221 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008222 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008223 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008224 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008225 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008226 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008227
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008228 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008229 POSTING_READ(PCH_DREF_CONTROL);
8230 udelay(200);
8231 } else {
8232 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8233
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008234 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008235
8236 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008237 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008238
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008239 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008240 POSTING_READ(PCH_DREF_CONTROL);
8241 udelay(200);
8242
8243 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008244 val &= ~DREF_SSC_SOURCE_MASK;
8245 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008246
8247 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008248 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008249
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008250 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008251 POSTING_READ(PCH_DREF_CONTROL);
8252 udelay(200);
8253 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008254
8255 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008256}
8257
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008258static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008259{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008260 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008261
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008262 tmp = I915_READ(SOUTH_CHICKEN2);
8263 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8264 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008265
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008266 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8267 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8268 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008269
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008270 tmp = I915_READ(SOUTH_CHICKEN2);
8271 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8272 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008273
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008274 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8275 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8276 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008277}
8278
8279/* WaMPhyProgramming:hsw */
8280static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8281{
8282 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008283
8284 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8285 tmp &= ~(0xFF << 24);
8286 tmp |= (0x12 << 24);
8287 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8288
Paulo Zanonidde86e22012-12-01 12:04:25 -02008289 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8290 tmp |= (1 << 11);
8291 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8292
8293 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8294 tmp |= (1 << 11);
8295 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8296
Paulo Zanonidde86e22012-12-01 12:04:25 -02008297 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8298 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8299 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8300
8301 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8302 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8303 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8304
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008305 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8306 tmp &= ~(7 << 13);
8307 tmp |= (5 << 13);
8308 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008309
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008310 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8311 tmp &= ~(7 << 13);
8312 tmp |= (5 << 13);
8313 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008314
8315 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8316 tmp &= ~0xFF;
8317 tmp |= 0x1C;
8318 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8319
8320 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8321 tmp &= ~0xFF;
8322 tmp |= 0x1C;
8323 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8324
8325 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8326 tmp &= ~(0xFF << 16);
8327 tmp |= (0x1C << 16);
8328 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8329
8330 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8331 tmp &= ~(0xFF << 16);
8332 tmp |= (0x1C << 16);
8333 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8334
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008335 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8336 tmp |= (1 << 27);
8337 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008338
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008339 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8340 tmp |= (1 << 27);
8341 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008342
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008343 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8344 tmp &= ~(0xF << 28);
8345 tmp |= (4 << 28);
8346 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008347
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008348 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8349 tmp &= ~(0xF << 28);
8350 tmp |= (4 << 28);
8351 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008352}
8353
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008354/* Implements 3 different sequences from BSpec chapter "Display iCLK
8355 * Programming" based on the parameters passed:
8356 * - Sequence to enable CLKOUT_DP
8357 * - Sequence to enable CLKOUT_DP without spread
8358 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8359 */
8360static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8361 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008362{
8363 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008364 uint32_t reg, tmp;
8365
8366 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8367 with_spread = true;
8368 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
8369 with_fdi, "LP PCH doesn't have FDI\n"))
8370 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008371
Ville Syrjäläa5805162015-05-26 20:42:30 +03008372 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008373
8374 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8375 tmp &= ~SBI_SSCCTL_DISABLE;
8376 tmp |= SBI_SSCCTL_PATHALT;
8377 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8378
8379 udelay(24);
8380
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008381 if (with_spread) {
8382 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8383 tmp &= ~SBI_SSCCTL_PATHALT;
8384 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008385
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008386 if (with_fdi) {
8387 lpt_reset_fdi_mphy(dev_priv);
8388 lpt_program_fdi_mphy(dev_priv);
8389 }
8390 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008391
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008392 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8393 SBI_GEN0 : SBI_DBUFF0;
8394 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8395 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8396 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008397
Ville Syrjäläa5805162015-05-26 20:42:30 +03008398 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008399}
8400
Paulo Zanoni47701c32013-07-23 11:19:25 -03008401/* Sequence to disable CLKOUT_DP */
8402static void lpt_disable_clkout_dp(struct drm_device *dev)
8403{
8404 struct drm_i915_private *dev_priv = dev->dev_private;
8405 uint32_t reg, tmp;
8406
Ville Syrjäläa5805162015-05-26 20:42:30 +03008407 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008408
8409 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8410 SBI_GEN0 : SBI_DBUFF0;
8411 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8412 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8413 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8414
8415 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8416 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8417 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8418 tmp |= SBI_SSCCTL_PATHALT;
8419 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8420 udelay(32);
8421 }
8422 tmp |= SBI_SSCCTL_DISABLE;
8423 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8424 }
8425
Ville Syrjäläa5805162015-05-26 20:42:30 +03008426 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008427}
8428
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008429static void lpt_init_pch_refclk(struct drm_device *dev)
8430{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008431 struct intel_encoder *encoder;
8432 bool has_vga = false;
8433
Damien Lespiaub2784e12014-08-05 11:29:37 +01008434 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008435 switch (encoder->type) {
8436 case INTEL_OUTPUT_ANALOG:
8437 has_vga = true;
8438 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008439 default:
8440 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008441 }
8442 }
8443
Paulo Zanoni47701c32013-07-23 11:19:25 -03008444 if (has_vga)
8445 lpt_enable_clkout_dp(dev, true, true);
8446 else
8447 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008448}
8449
Paulo Zanonidde86e22012-12-01 12:04:25 -02008450/*
8451 * Initialize reference clocks when the driver loads
8452 */
8453void intel_init_pch_refclk(struct drm_device *dev)
8454{
8455 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8456 ironlake_init_pch_refclk(dev);
8457 else if (HAS_PCH_LPT(dev))
8458 lpt_init_pch_refclk(dev);
8459}
8460
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008461static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008462{
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008463 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008464 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008465 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008466 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008467 struct drm_connector_state *connector_state;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008468 struct intel_encoder *encoder;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008469 int num_connectors = 0, i;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008470 bool is_lvds = false;
8471
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008472 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008473 if (connector_state->crtc != crtc_state->base.crtc)
8474 continue;
8475
8476 encoder = to_intel_encoder(connector_state->best_encoder);
8477
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008478 switch (encoder->type) {
8479 case INTEL_OUTPUT_LVDS:
8480 is_lvds = true;
8481 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008482 default:
8483 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008484 }
8485 num_connectors++;
8486 }
8487
8488 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008489 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008490 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008491 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008492 }
8493
8494 return 120000;
8495}
8496
Daniel Vetter6ff93602013-04-19 11:24:36 +02008497static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008498{
8499 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8500 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8501 int pipe = intel_crtc->pipe;
8502 uint32_t val;
8503
Daniel Vetter78114072013-06-13 00:54:57 +02008504 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008505
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008506 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008507 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008508 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008509 break;
8510 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008511 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008512 break;
8513 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008514 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008515 break;
8516 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008517 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008518 break;
8519 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008520 /* Case prevented by intel_choose_pipe_bpp_dither. */
8521 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008522 }
8523
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008524 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008525 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8526
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008527 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008528 val |= PIPECONF_INTERLACED_ILK;
8529 else
8530 val |= PIPECONF_PROGRESSIVE;
8531
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008532 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008533 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008534
Paulo Zanonic8203562012-09-12 10:06:29 -03008535 I915_WRITE(PIPECONF(pipe), val);
8536 POSTING_READ(PIPECONF(pipe));
8537}
8538
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008539/*
8540 * Set up the pipe CSC unit.
8541 *
8542 * Currently only full range RGB to limited range RGB conversion
8543 * is supported, but eventually this should handle various
8544 * RGB<->YCbCr scenarios as well.
8545 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01008546static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008547{
8548 struct drm_device *dev = crtc->dev;
8549 struct drm_i915_private *dev_priv = dev->dev_private;
8550 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8551 int pipe = intel_crtc->pipe;
8552 uint16_t coeff = 0x7800; /* 1.0 */
8553
8554 /*
8555 * TODO: Check what kind of values actually come out of the pipe
8556 * with these coeff/postoff values and adjust to get the best
8557 * accuracy. Perhaps we even need to take the bpc value into
8558 * consideration.
8559 */
8560
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008561 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008562 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8563
8564 /*
8565 * GY/GU and RY/RU should be the other way around according
8566 * to BSpec, but reality doesn't agree. Just set them up in
8567 * a way that results in the correct picture.
8568 */
8569 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8570 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8571
8572 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8573 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8574
8575 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8576 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8577
8578 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8579 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8580 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8581
8582 if (INTEL_INFO(dev)->gen > 6) {
8583 uint16_t postoff = 0;
8584
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008585 if (intel_crtc->config->limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02008586 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008587
8588 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8589 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8590 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8591
8592 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8593 } else {
8594 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8595
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008596 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008597 mode |= CSC_BLACK_SCREEN_OFFSET;
8598
8599 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8600 }
8601}
8602
Daniel Vetter6ff93602013-04-19 11:24:36 +02008603static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008604{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008605 struct drm_device *dev = crtc->dev;
8606 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008607 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008608 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008609 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008610 uint32_t val;
8611
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008612 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008613
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008614 if (IS_HASWELL(dev) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008615 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8616
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008617 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008618 val |= PIPECONF_INTERLACED_ILK;
8619 else
8620 val |= PIPECONF_PROGRESSIVE;
8621
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008622 I915_WRITE(PIPECONF(cpu_transcoder), val);
8623 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008624
8625 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8626 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008627
Satheeshakrishna M3cdf122c2014-04-08 15:46:53 +05308628 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008629 val = 0;
8630
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008631 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008632 case 18:
8633 val |= PIPEMISC_DITHER_6_BPC;
8634 break;
8635 case 24:
8636 val |= PIPEMISC_DITHER_8_BPC;
8637 break;
8638 case 30:
8639 val |= PIPEMISC_DITHER_10_BPC;
8640 break;
8641 case 36:
8642 val |= PIPEMISC_DITHER_12_BPC;
8643 break;
8644 default:
8645 /* Case prevented by pipe_config_set_bpp. */
8646 BUG();
8647 }
8648
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008649 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008650 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8651
8652 I915_WRITE(PIPEMISC(pipe), val);
8653 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008654}
8655
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008656static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008657 struct intel_crtc_state *crtc_state,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008658 intel_clock_t *clock,
8659 bool *has_reduced_clock,
8660 intel_clock_t *reduced_clock)
8661{
8662 struct drm_device *dev = crtc->dev;
8663 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008664 int refclk;
8665 const intel_limit_t *limit;
Daniel Vetterc329a4e2015-06-18 10:30:23 +02008666 bool ret;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008667
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008668 refclk = ironlake_get_refclk(crtc_state);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008669
8670 /*
8671 * Returns a set of divisors for the desired target clock with the given
8672 * refclk, or FALSE. The returned values represent the clock equation:
8673 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8674 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008675 limit = intel_limit(crtc_state, refclk);
8676 ret = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008677 crtc_state->port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008678 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008679 if (!ret)
8680 return false;
8681
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008682 return true;
8683}
8684
Paulo Zanonid4b19312012-11-29 11:29:32 -02008685int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8686{
8687 /*
8688 * Account for spread spectrum to avoid
8689 * oversubscribing the link. Max center spread
8690 * is 2.5%; use 5% for safety's sake.
8691 */
8692 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008693 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008694}
8695
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008696static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008697{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008698 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008699}
8700
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008701static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008702 struct intel_crtc_state *crtc_state,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008703 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008704 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008705{
8706 struct drm_crtc *crtc = &intel_crtc->base;
8707 struct drm_device *dev = crtc->dev;
8708 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008709 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008710 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008711 struct drm_connector_state *connector_state;
8712 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008713 uint32_t dpll;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008714 int factor, num_connectors = 0, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008715 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008716
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008717 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008718 if (connector_state->crtc != crtc_state->base.crtc)
8719 continue;
8720
8721 encoder = to_intel_encoder(connector_state->best_encoder);
8722
8723 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008724 case INTEL_OUTPUT_LVDS:
8725 is_lvds = true;
8726 break;
8727 case INTEL_OUTPUT_SDVO:
8728 case INTEL_OUTPUT_HDMI:
8729 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008730 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008731 default:
8732 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008733 }
8734
8735 num_connectors++;
8736 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008737
Chris Wilsonc1858122010-12-03 21:35:48 +00008738 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008739 factor = 21;
8740 if (is_lvds) {
8741 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008742 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008743 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008744 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008745 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008746 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008747
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008748 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02008749 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00008750
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008751 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8752 *fp2 |= FP_CB_TUNE;
8753
Chris Wilson5eddb702010-09-11 13:48:45 +01008754 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008755
Eric Anholta07d6782011-03-30 13:01:08 -07008756 if (is_lvds)
8757 dpll |= DPLLB_MODE_LVDS;
8758 else
8759 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008760
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008761 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008762 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008763
8764 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008765 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008766 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008767 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008768
Eric Anholta07d6782011-03-30 13:01:08 -07008769 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008770 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008771 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008772 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008773
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008774 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008775 case 5:
8776 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8777 break;
8778 case 7:
8779 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8780 break;
8781 case 10:
8782 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8783 break;
8784 case 14:
8785 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8786 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008787 }
8788
Daniel Vetterb4c09f32013-04-30 14:01:42 +02008789 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008790 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008791 else
8792 dpll |= PLL_REF_INPUT_DREFCLK;
8793
Daniel Vetter959e16d2013-06-05 13:34:21 +02008794 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008795}
8796
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008797static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8798 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008799{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008800 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008801 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008802 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03008803 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01008804 bool is_lvds = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008805 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008806
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008807 memset(&crtc_state->dpll_hw_state, 0,
8808 sizeof(crtc_state->dpll_hw_state));
8809
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03008810 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
Jesse Barnes79e53942008-11-07 14:24:08 -08008811
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008812 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8813 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
8814
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008815 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008816 &has_reduced_clock, &reduced_clock);
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008817 if (!ok && !crtc_state->clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008818 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8819 return -EINVAL;
8820 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01008821 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008822 if (!crtc_state->clock_set) {
8823 crtc_state->dpll.n = clock.n;
8824 crtc_state->dpll.m1 = clock.m1;
8825 crtc_state->dpll.m2 = clock.m2;
8826 crtc_state->dpll.p1 = clock.p1;
8827 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008828 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008829
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008830 /* 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 +02008831 if (crtc_state->has_pch_encoder) {
8832 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008833 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008834 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008835
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008836 dpll = ironlake_compute_dpll(crtc, crtc_state,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008837 &fp, &reduced_clock,
8838 has_reduced_clock ? &fp2 : NULL);
8839
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008840 crtc_state->dpll_hw_state.dpll = dpll;
8841 crtc_state->dpll_hw_state.fp0 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008842 if (has_reduced_clock)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008843 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008844 else
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008845 crtc_state->dpll_hw_state.fp1 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008846
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008847 pll = intel_get_shared_dpll(crtc, crtc_state);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008848 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03008849 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008850 pipe_name(crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07008851 return -EINVAL;
8852 }
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008853 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008854
Rodrigo Viviab585de2015-03-24 12:40:09 -07008855 if (is_lvds && has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008856 crtc->lowfreq_avail = true;
Daniel Vetterbcd644e2013-06-05 13:34:22 +02008857 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008858 crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008859
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008860 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008861}
8862
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008863static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8864 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008865{
8866 struct drm_device *dev = crtc->base.dev;
8867 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008868 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008869
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008870 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8871 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8872 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8873 & ~TU_SIZE_MASK;
8874 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8875 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8876 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8877}
8878
8879static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8880 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008881 struct intel_link_m_n *m_n,
8882 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008883{
8884 struct drm_device *dev = crtc->base.dev;
8885 struct drm_i915_private *dev_priv = dev->dev_private;
8886 enum pipe pipe = crtc->pipe;
8887
8888 if (INTEL_INFO(dev)->gen >= 5) {
8889 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8890 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8891 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8892 & ~TU_SIZE_MASK;
8893 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8894 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8895 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008896 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8897 * gen < 8) and if DRRS is supported (to make sure the
8898 * registers are not unnecessarily read).
8899 */
8900 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008901 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008902 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8903 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8904 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8905 & ~TU_SIZE_MASK;
8906 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8907 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8908 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8909 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008910 } else {
8911 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8912 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8913 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8914 & ~TU_SIZE_MASK;
8915 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8916 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8917 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8918 }
8919}
8920
8921void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008922 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008923{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02008924 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008925 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8926 else
8927 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008928 &pipe_config->dp_m_n,
8929 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008930}
8931
Daniel Vetter72419202013-04-04 13:28:53 +02008932static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008933 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02008934{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008935 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008936 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02008937}
8938
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008939static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008940 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008941{
8942 struct drm_device *dev = crtc->base.dev;
8943 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07008944 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8945 uint32_t ps_ctrl = 0;
8946 int id = -1;
8947 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008948
Chandra Kondurua1b22782015-04-07 15:28:45 -07008949 /* find scaler attached to this pipe */
8950 for (i = 0; i < crtc->num_scalers; i++) {
8951 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8952 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8953 id = i;
8954 pipe_config->pch_pfit.enabled = true;
8955 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8956 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8957 break;
8958 }
8959 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008960
Chandra Kondurua1b22782015-04-07 15:28:45 -07008961 scaler_state->scaler_id = id;
8962 if (id >= 0) {
8963 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8964 } else {
8965 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008966 }
8967}
8968
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008969static void
8970skylake_get_initial_plane_config(struct intel_crtc *crtc,
8971 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008972{
8973 struct drm_device *dev = crtc->base.dev;
8974 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00008975 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008976 int pipe = crtc->pipe;
8977 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008978 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008979 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008980 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008981
Damien Lespiaud9806c92015-01-21 14:07:19 +00008982 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008983 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008984 DRM_DEBUG_KMS("failed to alloc fb\n");
8985 return;
8986 }
8987
Damien Lespiau1b842c82015-01-21 13:50:54 +00008988 fb = &intel_fb->base;
8989
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008990 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00008991 if (!(val & PLANE_CTL_ENABLE))
8992 goto error;
8993
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008994 pixel_format = val & PLANE_CTL_FORMAT_MASK;
8995 fourcc = skl_format_to_fourcc(pixel_format,
8996 val & PLANE_CTL_ORDER_RGBX,
8997 val & PLANE_CTL_ALPHA_MASK);
8998 fb->pixel_format = fourcc;
8999 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9000
Damien Lespiau40f46282015-02-27 11:15:21 +00009001 tiling = val & PLANE_CTL_TILED_MASK;
9002 switch (tiling) {
9003 case PLANE_CTL_TILED_LINEAR:
9004 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9005 break;
9006 case PLANE_CTL_TILED_X:
9007 plane_config->tiling = I915_TILING_X;
9008 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9009 break;
9010 case PLANE_CTL_TILED_Y:
9011 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9012 break;
9013 case PLANE_CTL_TILED_YF:
9014 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9015 break;
9016 default:
9017 MISSING_CASE(tiling);
9018 goto error;
9019 }
9020
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009021 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9022 plane_config->base = base;
9023
9024 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9025
9026 val = I915_READ(PLANE_SIZE(pipe, 0));
9027 fb->height = ((val >> 16) & 0xfff) + 1;
9028 fb->width = ((val >> 0) & 0x1fff) + 1;
9029
9030 val = I915_READ(PLANE_STRIDE(pipe, 0));
Damien Lespiau40f46282015-02-27 11:15:21 +00009031 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
9032 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009033 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9034
9035 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009036 fb->pixel_format,
9037 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009038
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009039 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009040
9041 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9042 pipe_name(pipe), fb->width, fb->height,
9043 fb->bits_per_pixel, base, fb->pitches[0],
9044 plane_config->size);
9045
Damien Lespiau2d140302015-02-05 17:22:18 +00009046 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009047 return;
9048
9049error:
9050 kfree(fb);
9051}
9052
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009053static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009054 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009055{
9056 struct drm_device *dev = crtc->base.dev;
9057 struct drm_i915_private *dev_priv = dev->dev_private;
9058 uint32_t tmp;
9059
9060 tmp = I915_READ(PF_CTL(crtc->pipe));
9061
9062 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009063 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009064 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9065 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009066
9067 /* We currently do not free assignements of panel fitters on
9068 * ivb/hsw (since we don't use the higher upscaling modes which
9069 * differentiates them) so just WARN about this case for now. */
9070 if (IS_GEN7(dev)) {
9071 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9072 PF_PIPE_SEL_IVB(crtc->pipe));
9073 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009074 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009075}
9076
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009077static void
9078ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9079 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009080{
9081 struct drm_device *dev = crtc->base.dev;
9082 struct drm_i915_private *dev_priv = dev->dev_private;
9083 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009084 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009085 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009086 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009087 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009088 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009089
Damien Lespiau42a7b082015-02-05 19:35:13 +00009090 val = I915_READ(DSPCNTR(pipe));
9091 if (!(val & DISPLAY_PLANE_ENABLE))
9092 return;
9093
Damien Lespiaud9806c92015-01-21 14:07:19 +00009094 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009095 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009096 DRM_DEBUG_KMS("failed to alloc fb\n");
9097 return;
9098 }
9099
Damien Lespiau1b842c82015-01-21 13:50:54 +00009100 fb = &intel_fb->base;
9101
Daniel Vetter18c52472015-02-10 17:16:09 +00009102 if (INTEL_INFO(dev)->gen >= 4) {
9103 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009104 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009105 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9106 }
9107 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009108
9109 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009110 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009111 fb->pixel_format = fourcc;
9112 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009113
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009114 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009115 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009116 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009117 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009118 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009119 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009120 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009121 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009122 }
9123 plane_config->base = base;
9124
9125 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009126 fb->width = ((val >> 16) & 0xfff) + 1;
9127 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009128
9129 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009130 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009131
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009132 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009133 fb->pixel_format,
9134 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009135
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009136 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009137
Damien Lespiau2844a922015-01-20 12:51:48 +00009138 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9139 pipe_name(pipe), fb->width, fb->height,
9140 fb->bits_per_pixel, base, fb->pitches[0],
9141 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009142
Damien Lespiau2d140302015-02-05 17:22:18 +00009143 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009144}
9145
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009146static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009147 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009148{
9149 struct drm_device *dev = crtc->base.dev;
9150 struct drm_i915_private *dev_priv = dev->dev_private;
9151 uint32_t tmp;
9152
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009153 if (!intel_display_power_is_enabled(dev_priv,
9154 POWER_DOMAIN_PIPE(crtc->pipe)))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009155 return false;
9156
Daniel Vettere143a212013-07-04 12:01:15 +02009157 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009158 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02009159
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009160 tmp = I915_READ(PIPECONF(crtc->pipe));
9161 if (!(tmp & PIPECONF_ENABLE))
9162 return false;
9163
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009164 switch (tmp & PIPECONF_BPC_MASK) {
9165 case PIPECONF_6BPC:
9166 pipe_config->pipe_bpp = 18;
9167 break;
9168 case PIPECONF_8BPC:
9169 pipe_config->pipe_bpp = 24;
9170 break;
9171 case PIPECONF_10BPC:
9172 pipe_config->pipe_bpp = 30;
9173 break;
9174 case PIPECONF_12BPC:
9175 pipe_config->pipe_bpp = 36;
9176 break;
9177 default:
9178 break;
9179 }
9180
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009181 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9182 pipe_config->limited_color_range = true;
9183
Daniel Vetterab9412b2013-05-03 11:49:46 +02009184 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009185 struct intel_shared_dpll *pll;
9186
Daniel Vetter88adfff2013-03-28 10:42:01 +01009187 pipe_config->has_pch_encoder = true;
9188
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009189 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9190 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9191 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009192
9193 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009194
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009195 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02009196 pipe_config->shared_dpll =
9197 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009198 } else {
9199 tmp = I915_READ(PCH_DPLL_SEL);
9200 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
9201 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
9202 else
9203 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
9204 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009205
9206 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9207
9208 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9209 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009210
9211 tmp = pipe_config->dpll_hw_state.dpll;
9212 pipe_config->pixel_multiplier =
9213 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9214 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009215
9216 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009217 } else {
9218 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009219 }
9220
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009221 intel_get_pipe_timings(crtc, pipe_config);
9222
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009223 ironlake_get_pfit_config(crtc, pipe_config);
9224
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009225 return true;
9226}
9227
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009228static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9229{
9230 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009231 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009232
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009233 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009234 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009235 pipe_name(crtc->pipe));
9236
Rob Clarke2c719b2014-12-15 13:56:32 -05009237 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9238 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
9239 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9240 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
9241 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9242 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009243 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009244 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009245 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009246 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009247 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009248 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009249 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009250 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009251 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009252
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009253 /*
9254 * In theory we can still leave IRQs enabled, as long as only the HPD
9255 * interrupts remain enabled. We used to check for that, but since it's
9256 * gen-specific and since we only disable LCPLL after we fully disable
9257 * the interrupts, the check below should be enough.
9258 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009259 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009260}
9261
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009262static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9263{
9264 struct drm_device *dev = dev_priv->dev;
9265
9266 if (IS_HASWELL(dev))
9267 return I915_READ(D_COMP_HSW);
9268 else
9269 return I915_READ(D_COMP_BDW);
9270}
9271
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009272static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9273{
9274 struct drm_device *dev = dev_priv->dev;
9275
9276 if (IS_HASWELL(dev)) {
9277 mutex_lock(&dev_priv->rps.hw_lock);
9278 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9279 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009280 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009281 mutex_unlock(&dev_priv->rps.hw_lock);
9282 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009283 I915_WRITE(D_COMP_BDW, val);
9284 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009285 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009286}
9287
9288/*
9289 * This function implements pieces of two sequences from BSpec:
9290 * - Sequence for display software to disable LCPLL
9291 * - Sequence for display software to allow package C8+
9292 * The steps implemented here are just the steps that actually touch the LCPLL
9293 * register. Callers should take care of disabling all the display engine
9294 * functions, doing the mode unset, fixing interrupts, etc.
9295 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009296static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9297 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009298{
9299 uint32_t val;
9300
9301 assert_can_disable_lcpll(dev_priv);
9302
9303 val = I915_READ(LCPLL_CTL);
9304
9305 if (switch_to_fclk) {
9306 val |= LCPLL_CD_SOURCE_FCLK;
9307 I915_WRITE(LCPLL_CTL, val);
9308
9309 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9310 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9311 DRM_ERROR("Switching to FCLK failed\n");
9312
9313 val = I915_READ(LCPLL_CTL);
9314 }
9315
9316 val |= LCPLL_PLL_DISABLE;
9317 I915_WRITE(LCPLL_CTL, val);
9318 POSTING_READ(LCPLL_CTL);
9319
9320 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9321 DRM_ERROR("LCPLL still locked\n");
9322
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009323 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009324 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009325 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009326 ndelay(100);
9327
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009328 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9329 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009330 DRM_ERROR("D_COMP RCOMP still in progress\n");
9331
9332 if (allow_power_down) {
9333 val = I915_READ(LCPLL_CTL);
9334 val |= LCPLL_POWER_DOWN_ALLOW;
9335 I915_WRITE(LCPLL_CTL, val);
9336 POSTING_READ(LCPLL_CTL);
9337 }
9338}
9339
9340/*
9341 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9342 * source.
9343 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009344static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009345{
9346 uint32_t val;
9347
9348 val = I915_READ(LCPLL_CTL);
9349
9350 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9351 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9352 return;
9353
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009354 /*
9355 * Make sure we're not on PC8 state before disabling PC8, otherwise
9356 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009357 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009358 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009359
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009360 if (val & LCPLL_POWER_DOWN_ALLOW) {
9361 val &= ~LCPLL_POWER_DOWN_ALLOW;
9362 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009363 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009364 }
9365
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009366 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009367 val |= D_COMP_COMP_FORCE;
9368 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009369 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009370
9371 val = I915_READ(LCPLL_CTL);
9372 val &= ~LCPLL_PLL_DISABLE;
9373 I915_WRITE(LCPLL_CTL, val);
9374
9375 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9376 DRM_ERROR("LCPLL not locked yet\n");
9377
9378 if (val & LCPLL_CD_SOURCE_FCLK) {
9379 val = I915_READ(LCPLL_CTL);
9380 val &= ~LCPLL_CD_SOURCE_FCLK;
9381 I915_WRITE(LCPLL_CTL, val);
9382
9383 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9384 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9385 DRM_ERROR("Switching back to LCPLL failed\n");
9386 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009387
Mika Kuoppala59bad942015-01-16 11:34:40 +02009388 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjäläb6283052015-06-03 15:45:07 +03009389 intel_update_cdclk(dev_priv->dev);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009390}
9391
Paulo Zanoni765dab672014-03-07 20:08:18 -03009392/*
9393 * Package states C8 and deeper are really deep PC states that can only be
9394 * reached when all the devices on the system allow it, so even if the graphics
9395 * device allows PC8+, it doesn't mean the system will actually get to these
9396 * states. Our driver only allows PC8+ when going into runtime PM.
9397 *
9398 * The requirements for PC8+ are that all the outputs are disabled, the power
9399 * well is disabled and most interrupts are disabled, and these are also
9400 * requirements for runtime PM. When these conditions are met, we manually do
9401 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9402 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9403 * hang the machine.
9404 *
9405 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9406 * the state of some registers, so when we come back from PC8+ we need to
9407 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9408 * need to take care of the registers kept by RC6. Notice that this happens even
9409 * if we don't put the device in PCI D3 state (which is what currently happens
9410 * because of the runtime PM support).
9411 *
9412 * For more, read "Display Sequences for Package C8" on the hardware
9413 * documentation.
9414 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009415void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009416{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009417 struct drm_device *dev = dev_priv->dev;
9418 uint32_t val;
9419
Paulo Zanonic67a4702013-08-19 13:18:09 -03009420 DRM_DEBUG_KMS("Enabling package C8+\n");
9421
Paulo Zanonic67a4702013-08-19 13:18:09 -03009422 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9423 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9424 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9425 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9426 }
9427
9428 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009429 hsw_disable_lcpll(dev_priv, true, true);
9430}
9431
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009432void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009433{
9434 struct drm_device *dev = dev_priv->dev;
9435 uint32_t val;
9436
Paulo Zanonic67a4702013-08-19 13:18:09 -03009437 DRM_DEBUG_KMS("Disabling package C8+\n");
9438
9439 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009440 lpt_init_pch_refclk(dev);
9441
9442 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9443 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9444 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9445 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9446 }
9447
9448 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009449}
9450
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009451static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309452{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009453 struct drm_device *dev = old_state->dev;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009454 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309455
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009456 broxton_set_cdclk(dev, req_cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309457}
9458
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009459/* compute the max rate for new configuration */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009460static int ilk_max_pixel_rate(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009461{
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009462 struct intel_crtc *intel_crtc;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009463 struct intel_crtc_state *crtc_state;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009464 int max_pixel_rate = 0;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009465
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009466 for_each_intel_crtc(state->dev, intel_crtc) {
9467 int pixel_rate;
9468
9469 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
9470 if (IS_ERR(crtc_state))
9471 return PTR_ERR(crtc_state);
9472
9473 if (!crtc_state->base.enable)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009474 continue;
9475
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009476 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009477
9478 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009479 if (IS_BROADWELL(state->dev) && crtc_state->ips_enabled)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009480 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9481
9482 max_pixel_rate = max(max_pixel_rate, pixel_rate);
9483 }
9484
9485 return max_pixel_rate;
9486}
9487
9488static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9489{
9490 struct drm_i915_private *dev_priv = dev->dev_private;
9491 uint32_t val, data;
9492 int ret;
9493
9494 if (WARN((I915_READ(LCPLL_CTL) &
9495 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9496 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9497 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9498 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9499 "trying to change cdclk frequency with cdclk not enabled\n"))
9500 return;
9501
9502 mutex_lock(&dev_priv->rps.hw_lock);
9503 ret = sandybridge_pcode_write(dev_priv,
9504 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9505 mutex_unlock(&dev_priv->rps.hw_lock);
9506 if (ret) {
9507 DRM_ERROR("failed to inform pcode about cdclk change\n");
9508 return;
9509 }
9510
9511 val = I915_READ(LCPLL_CTL);
9512 val |= LCPLL_CD_SOURCE_FCLK;
9513 I915_WRITE(LCPLL_CTL, val);
9514
9515 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9516 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9517 DRM_ERROR("Switching to FCLK failed\n");
9518
9519 val = I915_READ(LCPLL_CTL);
9520 val &= ~LCPLL_CLK_FREQ_MASK;
9521
9522 switch (cdclk) {
9523 case 450000:
9524 val |= LCPLL_CLK_FREQ_450;
9525 data = 0;
9526 break;
9527 case 540000:
9528 val |= LCPLL_CLK_FREQ_54O_BDW;
9529 data = 1;
9530 break;
9531 case 337500:
9532 val |= LCPLL_CLK_FREQ_337_5_BDW;
9533 data = 2;
9534 break;
9535 case 675000:
9536 val |= LCPLL_CLK_FREQ_675_BDW;
9537 data = 3;
9538 break;
9539 default:
9540 WARN(1, "invalid cdclk frequency\n");
9541 return;
9542 }
9543
9544 I915_WRITE(LCPLL_CTL, val);
9545
9546 val = I915_READ(LCPLL_CTL);
9547 val &= ~LCPLL_CD_SOURCE_FCLK;
9548 I915_WRITE(LCPLL_CTL, val);
9549
9550 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9551 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9552 DRM_ERROR("Switching back to LCPLL failed\n");
9553
9554 mutex_lock(&dev_priv->rps.hw_lock);
9555 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9556 mutex_unlock(&dev_priv->rps.hw_lock);
9557
9558 intel_update_cdclk(dev);
9559
9560 WARN(cdclk != dev_priv->cdclk_freq,
9561 "cdclk requested %d kHz but got %d kHz\n",
9562 cdclk, dev_priv->cdclk_freq);
9563}
9564
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009565static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009566{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009567 struct drm_i915_private *dev_priv = to_i915(state->dev);
9568 int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009569 int cdclk;
9570
9571 /*
9572 * FIXME should also account for plane ratio
9573 * once 64bpp pixel formats are supported.
9574 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009575 if (max_pixclk > 540000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009576 cdclk = 675000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009577 else if (max_pixclk > 450000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009578 cdclk = 540000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009579 else if (max_pixclk > 337500)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009580 cdclk = 450000;
9581 else
9582 cdclk = 337500;
9583
9584 /*
9585 * FIXME move the cdclk caclulation to
9586 * compute_config() so we can fail gracegully.
9587 */
9588 if (cdclk > dev_priv->max_cdclk_freq) {
9589 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9590 cdclk, dev_priv->max_cdclk_freq);
9591 cdclk = dev_priv->max_cdclk_freq;
9592 }
9593
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009594 to_intel_atomic_state(state)->cdclk = cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009595
9596 return 0;
9597}
9598
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009599static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009600{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009601 struct drm_device *dev = old_state->dev;
9602 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009603
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009604 broadwell_set_cdclk(dev, req_cdclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009605}
9606
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009607static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9608 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009609{
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009610 if (!intel_ddi_pll_select(crtc, crtc_state))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009611 return -EINVAL;
Daniel Vetter716c2e52014-06-25 22:02:02 +03009612
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009613 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009614
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009615 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009616}
9617
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309618static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9619 enum port port,
9620 struct intel_crtc_state *pipe_config)
9621{
9622 switch (port) {
9623 case PORT_A:
9624 pipe_config->ddi_pll_sel = SKL_DPLL0;
9625 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9626 break;
9627 case PORT_B:
9628 pipe_config->ddi_pll_sel = SKL_DPLL1;
9629 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9630 break;
9631 case PORT_C:
9632 pipe_config->ddi_pll_sel = SKL_DPLL2;
9633 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9634 break;
9635 default:
9636 DRM_ERROR("Incorrect port type\n");
9637 }
9638}
9639
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009640static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9641 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009642 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009643{
Damien Lespiau3148ade2014-11-21 16:14:56 +00009644 u32 temp, dpll_ctl1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009645
9646 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9647 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9648
9649 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009650 case SKL_DPLL0:
9651 /*
9652 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9653 * of the shared DPLL framework and thus needs to be read out
9654 * separately
9655 */
9656 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9657 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9658 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009659 case SKL_DPLL1:
9660 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9661 break;
9662 case SKL_DPLL2:
9663 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9664 break;
9665 case SKL_DPLL3:
9666 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9667 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009668 }
9669}
9670
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009671static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9672 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009673 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009674{
9675 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9676
9677 switch (pipe_config->ddi_pll_sel) {
9678 case PORT_CLK_SEL_WRPLL1:
9679 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9680 break;
9681 case PORT_CLK_SEL_WRPLL2:
9682 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9683 break;
9684 }
9685}
9686
Daniel Vetter26804af2014-06-25 22:01:55 +03009687static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009688 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009689{
9690 struct drm_device *dev = crtc->base.dev;
9691 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009692 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009693 enum port port;
9694 uint32_t tmp;
9695
9696 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9697
9698 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9699
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009700 if (IS_SKYLAKE(dev))
9701 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309702 else if (IS_BROXTON(dev))
9703 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009704 else
9705 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009706
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009707 if (pipe_config->shared_dpll >= 0) {
9708 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9709
9710 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9711 &pipe_config->dpll_hw_state));
9712 }
9713
Daniel Vetter26804af2014-06-25 22:01:55 +03009714 /*
9715 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9716 * DDI E. So just check whether this pipe is wired to DDI E and whether
9717 * the PCH transcoder is on.
9718 */
Damien Lespiauca370452013-12-03 13:56:24 +00009719 if (INTEL_INFO(dev)->gen < 9 &&
9720 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009721 pipe_config->has_pch_encoder = true;
9722
9723 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9724 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9725 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9726
9727 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9728 }
9729}
9730
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009731static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009732 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009733{
9734 struct drm_device *dev = crtc->base.dev;
9735 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009736 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009737 uint32_t tmp;
9738
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009739 if (!intel_display_power_is_enabled(dev_priv,
Imre Deakb5482bd2014-03-05 16:20:55 +02009740 POWER_DOMAIN_PIPE(crtc->pipe)))
9741 return false;
9742
Daniel Vettere143a212013-07-04 12:01:15 +02009743 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009744 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9745
Daniel Vettereccb1402013-05-22 00:50:22 +02009746 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9747 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9748 enum pipe trans_edp_pipe;
9749 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9750 default:
9751 WARN(1, "unknown pipe linked to edp transcoder\n");
9752 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9753 case TRANS_DDI_EDP_INPUT_A_ON:
9754 trans_edp_pipe = PIPE_A;
9755 break;
9756 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9757 trans_edp_pipe = PIPE_B;
9758 break;
9759 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9760 trans_edp_pipe = PIPE_C;
9761 break;
9762 }
9763
9764 if (trans_edp_pipe == crtc->pipe)
9765 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9766 }
9767
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009768 if (!intel_display_power_is_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02009769 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03009770 return false;
9771
Daniel Vettereccb1402013-05-22 00:50:22 +02009772 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009773 if (!(tmp & PIPECONF_ENABLE))
9774 return false;
9775
Daniel Vetter26804af2014-06-25 22:01:55 +03009776 haswell_get_ddi_port_state(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009777
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009778 intel_get_pipe_timings(crtc, pipe_config);
9779
Chandra Kondurua1b22782015-04-07 15:28:45 -07009780 if (INTEL_INFO(dev)->gen >= 9) {
9781 skl_init_scalers(dev, crtc, pipe_config);
9782 }
9783
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009784 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Chandra Konduruaf99ced2015-05-11 14:35:47 -07009785
9786 if (INTEL_INFO(dev)->gen >= 9) {
9787 pipe_config->scaler_state.scaler_id = -1;
9788 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9789 }
9790
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009791 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009792 if (INTEL_INFO(dev)->gen == 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009793 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009794 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009795 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009796 else
9797 MISSING_CASE(INTEL_INFO(dev)->gen);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009798 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01009799
Jesse Barnese59150d2014-01-07 13:30:45 -08009800 if (IS_HASWELL(dev))
9801 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9802 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009803
Clint Taylorebb69c92014-09-30 10:30:22 -07009804 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
9805 pipe_config->pixel_multiplier =
9806 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9807 } else {
9808 pipe_config->pixel_multiplier = 1;
9809 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009810
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009811 return true;
9812}
9813
Chris Wilson560b85b2010-08-07 11:01:38 +01009814static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
9815{
9816 struct drm_device *dev = crtc->dev;
9817 struct drm_i915_private *dev_priv = dev->dev_private;
9818 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +03009819 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01009820
Ville Syrjälädc41c152014-08-13 11:57:05 +03009821 if (base) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009822 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
9823 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009824 unsigned int stride = roundup_pow_of_two(width) * 4;
9825
9826 switch (stride) {
9827 default:
9828 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9829 width, stride);
9830 stride = 256;
9831 /* fallthrough */
9832 case 256:
9833 case 512:
9834 case 1024:
9835 case 2048:
9836 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009837 }
9838
Ville Syrjälädc41c152014-08-13 11:57:05 +03009839 cntl |= CURSOR_ENABLE |
9840 CURSOR_GAMMA_ENABLE |
9841 CURSOR_FORMAT_ARGB |
9842 CURSOR_STRIDE(stride);
9843
9844 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009845 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009846
Ville Syrjälädc41c152014-08-13 11:57:05 +03009847 if (intel_crtc->cursor_cntl != 0 &&
9848 (intel_crtc->cursor_base != base ||
9849 intel_crtc->cursor_size != size ||
9850 intel_crtc->cursor_cntl != cntl)) {
9851 /* On these chipsets we can only modify the base/size/stride
9852 * whilst the cursor is disabled.
9853 */
9854 I915_WRITE(_CURACNTR, 0);
9855 POSTING_READ(_CURACNTR);
9856 intel_crtc->cursor_cntl = 0;
9857 }
9858
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009859 if (intel_crtc->cursor_base != base) {
Ville Syrjälädc41c152014-08-13 11:57:05 +03009860 I915_WRITE(_CURABASE, base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009861 intel_crtc->cursor_base = base;
9862 }
Ville Syrjälädc41c152014-08-13 11:57:05 +03009863
9864 if (intel_crtc->cursor_size != size) {
9865 I915_WRITE(CURSIZE, size);
9866 intel_crtc->cursor_size = size;
9867 }
9868
Chris Wilson4b0e3332014-05-30 16:35:26 +03009869 if (intel_crtc->cursor_cntl != cntl) {
9870 I915_WRITE(_CURACNTR, cntl);
9871 POSTING_READ(_CURACNTR);
9872 intel_crtc->cursor_cntl = cntl;
9873 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009874}
9875
9876static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
9877{
9878 struct drm_device *dev = crtc->dev;
9879 struct drm_i915_private *dev_priv = dev->dev_private;
9880 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9881 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009882 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +01009883
Chris Wilson4b0e3332014-05-30 16:35:26 +03009884 cntl = 0;
9885 if (base) {
9886 cntl = MCURSOR_GAMMA_ENABLE;
Matt Roper3dd512f2015-02-27 10:12:00 -08009887 switch (intel_crtc->base.cursor->state->crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309888 case 64:
9889 cntl |= CURSOR_MODE_64_ARGB_AX;
9890 break;
9891 case 128:
9892 cntl |= CURSOR_MODE_128_ARGB_AX;
9893 break;
9894 case 256:
9895 cntl |= CURSOR_MODE_256_ARGB_AX;
9896 break;
9897 default:
Matt Roper3dd512f2015-02-27 10:12:00 -08009898 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309899 return;
Chris Wilson560b85b2010-08-07 11:01:38 +01009900 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009901 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +03009902
9903 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
9904 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson560b85b2010-08-07 11:01:38 +01009905 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009906
Matt Roper8e7d6882015-01-21 16:35:41 -08009907 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009908 cntl |= CURSOR_ROTATE_180;
9909
Chris Wilson4b0e3332014-05-30 16:35:26 +03009910 if (intel_crtc->cursor_cntl != cntl) {
9911 I915_WRITE(CURCNTR(pipe), cntl);
9912 POSTING_READ(CURCNTR(pipe));
9913 intel_crtc->cursor_cntl = cntl;
9914 }
9915
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009916 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009917 I915_WRITE(CURBASE(pipe), base);
9918 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009919
9920 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009921}
9922
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009923/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01009924static void intel_crtc_update_cursor(struct drm_crtc *crtc,
9925 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009926{
9927 struct drm_device *dev = crtc->dev;
9928 struct drm_i915_private *dev_priv = dev->dev_private;
9929 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9930 int pipe = intel_crtc->pipe;
Matt Roper3d7d6512014-06-10 08:28:13 -07009931 int x = crtc->cursor_x;
9932 int y = crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009933 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009934
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009935 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009936 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009937
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009938 if (x >= intel_crtc->config->pipe_src_w)
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009939 base = 0;
9940
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009941 if (y >= intel_crtc->config->pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009942 base = 0;
9943
9944 if (x < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009945 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009946 base = 0;
9947
9948 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
9949 x = -x;
9950 }
9951 pos |= x << CURSOR_X_SHIFT;
9952
9953 if (y < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009954 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009955 base = 0;
9956
9957 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
9958 y = -y;
9959 }
9960 pos |= y << CURSOR_Y_SHIFT;
9961
Chris Wilson4b0e3332014-05-30 16:35:26 +03009962 if (base == 0 && intel_crtc->cursor_base == 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009963 return;
9964
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009965 I915_WRITE(CURPOS(pipe), pos);
9966
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009967 /* ILK+ do this automagically */
9968 if (HAS_GMCH_DISPLAY(dev) &&
Matt Roper8e7d6882015-01-21 16:35:41 -08009969 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009970 base += (intel_crtc->base.cursor->state->crtc_h *
9971 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009972 }
9973
Ville Syrjälä8ac54662014-08-12 19:39:54 +03009974 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009975 i845_update_cursor(crtc, base);
9976 else
9977 i9xx_update_cursor(crtc, base);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009978}
9979
Ville Syrjälädc41c152014-08-13 11:57:05 +03009980static bool cursor_size_ok(struct drm_device *dev,
9981 uint32_t width, uint32_t height)
9982{
9983 if (width == 0 || height == 0)
9984 return false;
9985
9986 /*
9987 * 845g/865g are special in that they are only limited by
9988 * the width of their cursors, the height is arbitrary up to
9989 * the precision of the register. Everything else requires
9990 * square cursors, limited to a few power-of-two sizes.
9991 */
9992 if (IS_845G(dev) || IS_I865G(dev)) {
9993 if ((width & 63) != 0)
9994 return false;
9995
9996 if (width > (IS_845G(dev) ? 64 : 512))
9997 return false;
9998
9999 if (height > 1023)
10000 return false;
10001 } else {
10002 switch (width | height) {
10003 case 256:
10004 case 128:
10005 if (IS_GEN2(dev))
10006 return false;
10007 case 64:
10008 break;
10009 default:
10010 return false;
10011 }
10012 }
10013
10014 return true;
10015}
10016
Jesse Barnes79e53942008-11-07 14:24:08 -080010017static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +010010018 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -080010019{
James Simmons72034252010-08-03 01:33:19 +010010020 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -080010021 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080010022
James Simmons72034252010-08-03 01:33:19 +010010023 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010024 intel_crtc->lut_r[i] = red[i] >> 8;
10025 intel_crtc->lut_g[i] = green[i] >> 8;
10026 intel_crtc->lut_b[i] = blue[i] >> 8;
10027 }
10028
10029 intel_crtc_load_lut(crtc);
10030}
10031
Jesse Barnes79e53942008-11-07 14:24:08 -080010032/* VESA 640x480x72Hz mode to set on the pipe */
10033static struct drm_display_mode load_detect_mode = {
10034 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10035 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10036};
10037
Daniel Vettera8bb6812014-02-10 18:00:39 +010010038struct drm_framebuffer *
10039__intel_framebuffer_create(struct drm_device *dev,
10040 struct drm_mode_fb_cmd2 *mode_cmd,
10041 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010042{
10043 struct intel_framebuffer *intel_fb;
10044 int ret;
10045
10046 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
10047 if (!intel_fb) {
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +030010048 drm_gem_object_unreference(&obj->base);
Chris Wilsond2dff872011-04-19 08:36:26 +010010049 return ERR_PTR(-ENOMEM);
10050 }
10051
10052 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010053 if (ret)
10054 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010055
10056 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010057err:
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +030010058 drm_gem_object_unreference(&obj->base);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010059 kfree(intel_fb);
10060
10061 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010062}
10063
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010064static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010065intel_framebuffer_create(struct drm_device *dev,
10066 struct drm_mode_fb_cmd2 *mode_cmd,
10067 struct drm_i915_gem_object *obj)
10068{
10069 struct drm_framebuffer *fb;
10070 int ret;
10071
10072 ret = i915_mutex_lock_interruptible(dev);
10073 if (ret)
10074 return ERR_PTR(ret);
10075 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10076 mutex_unlock(&dev->struct_mutex);
10077
10078 return fb;
10079}
10080
Chris Wilsond2dff872011-04-19 08:36:26 +010010081static u32
10082intel_framebuffer_pitch_for_width(int width, int bpp)
10083{
10084 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10085 return ALIGN(pitch, 64);
10086}
10087
10088static u32
10089intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10090{
10091 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010092 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010093}
10094
10095static struct drm_framebuffer *
10096intel_framebuffer_create_for_mode(struct drm_device *dev,
10097 struct drm_display_mode *mode,
10098 int depth, int bpp)
10099{
10100 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010101 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010102
10103 obj = i915_gem_alloc_object(dev,
10104 intel_framebuffer_size_for_mode(mode, bpp));
10105 if (obj == NULL)
10106 return ERR_PTR(-ENOMEM);
10107
10108 mode_cmd.width = mode->hdisplay;
10109 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010110 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10111 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010112 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010113
10114 return intel_framebuffer_create(dev, &mode_cmd, obj);
10115}
10116
10117static struct drm_framebuffer *
10118mode_fits_in_fbdev(struct drm_device *dev,
10119 struct drm_display_mode *mode)
10120{
Daniel Vetter4520f532013-10-09 09:18:51 +020010121#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +010010122 struct drm_i915_private *dev_priv = dev->dev_private;
10123 struct drm_i915_gem_object *obj;
10124 struct drm_framebuffer *fb;
10125
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010126 if (!dev_priv->fbdev)
10127 return NULL;
10128
10129 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010130 return NULL;
10131
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010132 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010133 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010134
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010135 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010136 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10137 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010138 return NULL;
10139
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010140 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010141 return NULL;
10142
10143 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010144#else
10145 return NULL;
10146#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010147}
10148
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010149static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10150 struct drm_crtc *crtc,
10151 struct drm_display_mode *mode,
10152 struct drm_framebuffer *fb,
10153 int x, int y)
10154{
10155 struct drm_plane_state *plane_state;
10156 int hdisplay, vdisplay;
10157 int ret;
10158
10159 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10160 if (IS_ERR(plane_state))
10161 return PTR_ERR(plane_state);
10162
10163 if (mode)
10164 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10165 else
10166 hdisplay = vdisplay = 0;
10167
10168 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10169 if (ret)
10170 return ret;
10171 drm_atomic_set_fb_for_plane(plane_state, fb);
10172 plane_state->crtc_x = 0;
10173 plane_state->crtc_y = 0;
10174 plane_state->crtc_w = hdisplay;
10175 plane_state->crtc_h = vdisplay;
10176 plane_state->src_x = x << 16;
10177 plane_state->src_y = y << 16;
10178 plane_state->src_w = hdisplay << 16;
10179 plane_state->src_h = vdisplay << 16;
10180
10181 return 0;
10182}
10183
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010184bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010185 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010186 struct intel_load_detect_pipe *old,
10187 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010188{
10189 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010190 struct intel_encoder *intel_encoder =
10191 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010192 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010193 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010194 struct drm_crtc *crtc = NULL;
10195 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010196 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010197 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010198 struct drm_atomic_state *state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010199 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010200 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010201 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010202
Chris Wilsond2dff872011-04-19 08:36:26 +010010203 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010204 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010205 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010206
Rob Clark51fd3712013-11-19 12:10:12 -050010207retry:
10208 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10209 if (ret)
10210 goto fail_unlock;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010211
Jesse Barnes79e53942008-11-07 14:24:08 -080010212 /*
10213 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010214 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010215 * - if the connector already has an assigned crtc, use it (but make
10216 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010217 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010218 * - try to find the first unused crtc that can drive this connector,
10219 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010220 */
10221
10222 /* See if we already have a CRTC for this connector */
10223 if (encoder->crtc) {
10224 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010225
Rob Clark51fd3712013-11-19 12:10:12 -050010226 ret = drm_modeset_lock(&crtc->mutex, ctx);
10227 if (ret)
10228 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010229 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10230 if (ret)
10231 goto fail_unlock;
Daniel Vetter7b240562012-12-12 00:35:33 +010010232
Daniel Vetter24218aa2012-08-12 19:27:11 +020010233 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +010010234 old->load_detect_temp = false;
10235
10236 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +020010237 if (connector->dpms != DRM_MODE_DPMS_ON)
10238 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +010010239
Chris Wilson71731882011-04-19 23:10:58 +010010240 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -080010241 }
10242
10243 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010244 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010245 i++;
10246 if (!(encoder->possible_crtcs & (1 << i)))
10247 continue;
Matt Roper83d65732015-02-25 13:12:16 -080010248 if (possible_crtc->state->enable)
Ville Syrjäläa4592492014-08-11 13:15:36 +030010249 continue;
10250 /* This can occur when applying the pipe A quirk on resume. */
10251 if (to_intel_crtc(possible_crtc)->new_enabled)
10252 continue;
10253
10254 crtc = possible_crtc;
10255 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010256 }
10257
10258 /*
10259 * If we didn't find an unused CRTC, don't use any.
10260 */
10261 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010262 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Rob Clark51fd3712013-11-19 12:10:12 -050010263 goto fail_unlock;
Jesse Barnes79e53942008-11-07 14:24:08 -080010264 }
10265
Rob Clark51fd3712013-11-19 12:10:12 -050010266 ret = drm_modeset_lock(&crtc->mutex, ctx);
10267 if (ret)
10268 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010269 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10270 if (ret)
10271 goto fail_unlock;
Daniel Vetterfc303102012-07-09 10:40:58 +020010272 intel_encoder->new_crtc = to_intel_crtc(crtc);
10273 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010274
10275 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010276 intel_crtc->new_enabled = true;
Daniel Vetter24218aa2012-08-12 19:27:11 +020010277 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +010010278 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +010010279 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -080010280
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010281 state = drm_atomic_state_alloc(dev);
10282 if (!state)
10283 return false;
10284
10285 state->acquire_ctx = ctx;
10286
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010287 connector_state = drm_atomic_get_connector_state(state, connector);
10288 if (IS_ERR(connector_state)) {
10289 ret = PTR_ERR(connector_state);
10290 goto fail;
10291 }
10292
10293 connector_state->crtc = crtc;
10294 connector_state->best_encoder = &intel_encoder->base;
10295
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010296 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10297 if (IS_ERR(crtc_state)) {
10298 ret = PTR_ERR(crtc_state);
10299 goto fail;
10300 }
10301
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010302 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010303
Chris Wilson64927112011-04-20 07:25:26 +010010304 if (!mode)
10305 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010306
Chris Wilsond2dff872011-04-19 08:36:26 +010010307 /* We need a framebuffer large enough to accommodate all accesses
10308 * that the plane may generate whilst we perform load detection.
10309 * We can not rely on the fbcon either being present (we get called
10310 * during its initialisation to detect all boot displays, or it may
10311 * not even exist) or that it is large enough to satisfy the
10312 * requested mode.
10313 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010314 fb = mode_fits_in_fbdev(dev, mode);
10315 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010316 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010317 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
10318 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010319 } else
10320 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010321 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010322 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010323 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010324 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010325
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010326 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10327 if (ret)
10328 goto fail;
10329
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010330 drm_mode_copy(&crtc_state->base.mode, mode);
10331
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020010332 if (intel_set_mode(state)) {
Chris Wilson64927112011-04-20 07:25:26 +010010333 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +010010334 if (old->release_fb)
10335 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010336 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010337 }
Daniel Vetter9128b042015-03-03 17:31:21 +010010338 crtc->primary->crtc = crtc;
Chris Wilson71731882011-04-19 23:10:58 +010010339
Jesse Barnes79e53942008-11-07 14:24:08 -080010340 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010341 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010342 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010343
10344 fail:
Matt Roper83d65732015-02-25 13:12:16 -080010345 intel_crtc->new_enabled = crtc->state->enable;
Rob Clark51fd3712013-11-19 12:10:12 -050010346fail_unlock:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010347 drm_atomic_state_free(state);
10348 state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010349
Rob Clark51fd3712013-11-19 12:10:12 -050010350 if (ret == -EDEADLK) {
10351 drm_modeset_backoff(ctx);
10352 goto retry;
10353 }
10354
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010355 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010356}
10357
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010358void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010359 struct intel_load_detect_pipe *old,
10360 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010361{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010362 struct drm_device *dev = connector->dev;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010363 struct intel_encoder *intel_encoder =
10364 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010365 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +010010366 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010367 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010368 struct drm_atomic_state *state;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010369 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010370 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010371 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010372
Chris Wilsond2dff872011-04-19 08:36:26 +010010373 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010374 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010375 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010376
Chris Wilson8261b192011-04-19 23:18:09 +010010377 if (old->load_detect_temp) {
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010378 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010379 if (!state)
10380 goto fail;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010381
10382 state->acquire_ctx = ctx;
10383
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010384 connector_state = drm_atomic_get_connector_state(state, connector);
10385 if (IS_ERR(connector_state))
10386 goto fail;
10387
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010388 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10389 if (IS_ERR(crtc_state))
10390 goto fail;
10391
Daniel Vetterfc303102012-07-09 10:40:58 +020010392 to_intel_connector(connector)->new_encoder = NULL;
10393 intel_encoder->new_crtc = NULL;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010394 intel_crtc->new_enabled = false;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010395
10396 connector_state->best_encoder = NULL;
10397 connector_state->crtc = NULL;
10398
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010399 crtc_state->base.enable = crtc_state->base.active = false;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010400
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010401 ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
10402 0, 0);
10403 if (ret)
10404 goto fail;
10405
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020010406 ret = intel_set_mode(state);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030010407 if (ret)
10408 goto fail;
Chris Wilsond2dff872011-04-19 08:36:26 +010010409
Daniel Vetter36206362012-12-10 20:42:17 +010010410 if (old->release_fb) {
10411 drm_framebuffer_unregister_private(old->release_fb);
10412 drm_framebuffer_unreference(old->release_fb);
10413 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010414
Chris Wilson0622a532011-04-21 09:32:11 +010010415 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010416 }
10417
Eric Anholtc751ce42010-03-25 11:48:48 -070010418 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +020010419 if (old->dpms_mode != DRM_MODE_DPMS_ON)
10420 connector->funcs->dpms(connector, old->dpms_mode);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010421
10422 return;
10423fail:
10424 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
10425 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010426}
10427
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010428static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010429 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010430{
10431 struct drm_i915_private *dev_priv = dev->dev_private;
10432 u32 dpll = pipe_config->dpll_hw_state.dpll;
10433
10434 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010435 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010436 else if (HAS_PCH_SPLIT(dev))
10437 return 120000;
10438 else if (!IS_GEN2(dev))
10439 return 96000;
10440 else
10441 return 48000;
10442}
10443
Jesse Barnes79e53942008-11-07 14:24:08 -080010444/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010445static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010446 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010447{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010448 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010449 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010450 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010451 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010452 u32 fp;
10453 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010454 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010455
10456 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010457 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010458 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010459 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010460
10461 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010462 if (IS_PINEVIEW(dev)) {
10463 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10464 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010465 } else {
10466 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10467 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10468 }
10469
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010470 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010471 if (IS_PINEVIEW(dev))
10472 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10473 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010474 else
10475 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010476 DPLL_FPA01_P1_POST_DIV_SHIFT);
10477
10478 switch (dpll & DPLL_MODE_MASK) {
10479 case DPLLB_MODE_DAC_SERIAL:
10480 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10481 5 : 10;
10482 break;
10483 case DPLLB_MODE_LVDS:
10484 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10485 7 : 14;
10486 break;
10487 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010488 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010489 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010490 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010491 }
10492
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010493 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010494 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010495 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010496 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010497 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010498 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010499 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010500
10501 if (is_lvds) {
10502 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10503 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010504
10505 if (lvds & LVDS_CLKB_POWER_UP)
10506 clock.p2 = 7;
10507 else
10508 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010509 } else {
10510 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10511 clock.p1 = 2;
10512 else {
10513 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10514 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10515 }
10516 if (dpll & PLL_P2_DIVIDE_BY_4)
10517 clock.p2 = 4;
10518 else
10519 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010520 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010521
10522 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010523 }
10524
Ville Syrjälä18442d02013-09-13 16:00:08 +030010525 /*
10526 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010527 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010528 * encoder's get_config() function.
10529 */
10530 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010531}
10532
Ville Syrjälä6878da02013-09-13 15:59:11 +030010533int intel_dotclock_calculate(int link_freq,
10534 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010535{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010536 /*
10537 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010538 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010539 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010540 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010541 *
10542 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010543 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010544 */
10545
Ville Syrjälä6878da02013-09-13 15:59:11 +030010546 if (!m_n->link_n)
10547 return 0;
10548
10549 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10550}
10551
Ville Syrjälä18442d02013-09-13 16:00:08 +030010552static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010553 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010554{
10555 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +030010556
10557 /* read out port_clock from the DPLL */
10558 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010559
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010560 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +030010561 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +010010562 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +030010563 * agree once we know their relationship in the encoder's
10564 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010565 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010566 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +030010567 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10568 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010569}
10570
10571/** Returns the currently programmed mode of the given pipe. */
10572struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10573 struct drm_crtc *crtc)
10574{
Jesse Barnes548f2452011-02-17 10:40:53 -080010575 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010576 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010577 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010578 struct drm_display_mode *mode;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010579 struct intel_crtc_state pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010580 int htot = I915_READ(HTOTAL(cpu_transcoder));
10581 int hsync = I915_READ(HSYNC(cpu_transcoder));
10582 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10583 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010584 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010585
10586 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10587 if (!mode)
10588 return NULL;
10589
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010590 /*
10591 * Construct a pipe_config sufficient for getting the clock info
10592 * back out of crtc_clock_get.
10593 *
10594 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10595 * to use a real value here instead.
10596 */
Ville Syrjälä293623f2013-09-13 16:18:46 +030010597 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010598 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010599 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10600 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10601 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010602 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10603
Ville Syrjälä773ae032013-09-23 17:48:20 +030010604 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010605 mode->hdisplay = (htot & 0xffff) + 1;
10606 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10607 mode->hsync_start = (hsync & 0xffff) + 1;
10608 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10609 mode->vdisplay = (vtot & 0xffff) + 1;
10610 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10611 mode->vsync_start = (vsync & 0xffff) + 1;
10612 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10613
10614 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010615
10616 return mode;
10617}
10618
Chris Wilsonf047e392012-07-21 12:31:41 +010010619void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010620{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010621 struct drm_i915_private *dev_priv = dev->dev_private;
10622
Chris Wilsonf62a0072014-02-21 17:55:39 +000010623 if (dev_priv->mm.busy)
10624 return;
10625
Paulo Zanoni43694d62014-03-07 20:08:08 -030010626 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010627 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010628 if (INTEL_INFO(dev)->gen >= 6)
10629 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010630 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010631}
10632
10633void intel_mark_idle(struct drm_device *dev)
10634{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010635 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010636
Chris Wilsonf62a0072014-02-21 17:55:39 +000010637 if (!dev_priv->mm.busy)
10638 return;
10639
10640 dev_priv->mm.busy = false;
10641
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010642 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010643 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010644
Paulo Zanoni43694d62014-03-07 20:08:08 -030010645 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010646}
10647
Jesse Barnes79e53942008-11-07 14:24:08 -080010648static void intel_crtc_destroy(struct drm_crtc *crtc)
10649{
10650 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010651 struct drm_device *dev = crtc->dev;
10652 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010653
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010654 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010655 work = intel_crtc->unpin_work;
10656 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010657 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010658
10659 if (work) {
10660 cancel_work_sync(&work->work);
10661 kfree(work);
10662 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010663
10664 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010665
Jesse Barnes79e53942008-11-07 14:24:08 -080010666 kfree(intel_crtc);
10667}
10668
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010669static void intel_unpin_work_fn(struct work_struct *__work)
10670{
10671 struct intel_unpin_work *work =
10672 container_of(__work, struct intel_unpin_work, work);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010673 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10674 struct drm_device *dev = crtc->base.dev;
10675 struct drm_plane *primary = crtc->base.primary;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010676
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010677 mutex_lock(&dev->struct_mutex);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010678 intel_unpin_fb_obj(work->old_fb, primary->state);
Chris Wilson05394f32010-11-08 19:18:58 +000010679 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010680
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020010681 intel_fbc_update(dev);
John Harrisonf06cc1b2014-11-24 18:49:37 +000010682
10683 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010684 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010685 mutex_unlock(&dev->struct_mutex);
10686
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010687 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
Chris Wilson89ed88b2015-02-16 14:31:49 +000010688 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010689
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010690 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10691 atomic_dec(&crtc->unpin_work_count);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010692
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010693 kfree(work);
10694}
10695
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010696static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010010697 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010698{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010699 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10700 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010701 unsigned long flags;
10702
10703 /* Ignore early vblank irqs */
10704 if (intel_crtc == NULL)
10705 return;
10706
Daniel Vetterf3260382014-09-15 14:55:23 +020010707 /*
10708 * This is called both by irq handlers and the reset code (to complete
10709 * lost pageflips) so needs the full irqsave spinlocks.
10710 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010711 spin_lock_irqsave(&dev->event_lock, flags);
10712 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000010713
10714 /* Ensure we don't miss a work->pending update ... */
10715 smp_rmb();
10716
10717 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010718 spin_unlock_irqrestore(&dev->event_lock, flags);
10719 return;
10720 }
10721
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010722 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010010723
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010724 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010725}
10726
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010727void intel_finish_page_flip(struct drm_device *dev, int pipe)
10728{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010729 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010730 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10731
Mario Kleiner49b14a52010-12-09 07:00:07 +010010732 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010733}
10734
10735void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10736{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010737 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010738 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10739
Mario Kleiner49b14a52010-12-09 07:00:07 +010010740 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010741}
10742
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010743/* Is 'a' after or equal to 'b'? */
10744static bool g4x_flip_count_after_eq(u32 a, u32 b)
10745{
10746 return !((a - b) & 0x80000000);
10747}
10748
10749static bool page_flip_finished(struct intel_crtc *crtc)
10750{
10751 struct drm_device *dev = crtc->base.dev;
10752 struct drm_i915_private *dev_priv = dev->dev_private;
10753
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030010754 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10755 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10756 return true;
10757
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010758 /*
10759 * The relevant registers doen't exist on pre-ctg.
10760 * As the flip done interrupt doesn't trigger for mmio
10761 * flips on gmch platforms, a flip count check isn't
10762 * really needed there. But since ctg has the registers,
10763 * include it in the check anyway.
10764 */
10765 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10766 return true;
10767
10768 /*
10769 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10770 * used the same base address. In that case the mmio flip might
10771 * have completed, but the CS hasn't even executed the flip yet.
10772 *
10773 * A flip count check isn't enough as the CS might have updated
10774 * the base address just after start of vblank, but before we
10775 * managed to process the interrupt. This means we'd complete the
10776 * CS flip too soon.
10777 *
10778 * Combining both checks should get us a good enough result. It may
10779 * still happen that the CS flip has been executed, but has not
10780 * yet actually completed. But in case the base address is the same
10781 * anyway, we don't really care.
10782 */
10783 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10784 crtc->unpin_work->gtt_offset &&
10785 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
10786 crtc->unpin_work->flip_count);
10787}
10788
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010789void intel_prepare_page_flip(struct drm_device *dev, int plane)
10790{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010791 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010792 struct intel_crtc *intel_crtc =
10793 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10794 unsigned long flags;
10795
Daniel Vetterf3260382014-09-15 14:55:23 +020010796
10797 /*
10798 * This is called both by irq handlers and the reset code (to complete
10799 * lost pageflips) so needs the full irqsave spinlocks.
10800 *
10801 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000010802 * generate a page-flip completion irq, i.e. every modeset
10803 * is also accompanied by a spurious intel_prepare_page_flip().
10804 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010805 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010806 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000010807 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010808 spin_unlock_irqrestore(&dev->event_lock, flags);
10809}
10810
Robin Schroereba905b2014-05-18 02:24:50 +020010811static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
Chris Wilsone7d841c2012-12-03 11:36:30 +000010812{
10813 /* Ensure that the work item is consistent when activating it ... */
10814 smp_wmb();
10815 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
10816 /* and that it is marked active as soon as the irq could fire. */
10817 smp_wmb();
10818}
10819
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010820static int intel_gen2_queue_flip(struct drm_device *dev,
10821 struct drm_crtc *crtc,
10822 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010823 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010824 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010825 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010826{
John Harrison6258fbe2015-05-29 17:43:48 +010010827 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010828 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010829 u32 flip_mask;
10830 int ret;
10831
John Harrison5fb9de12015-05-29 17:44:07 +010010832 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010833 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010834 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010835
10836 /* Can't queue multiple flips, so wait for the previous
10837 * one to finish before executing the next.
10838 */
10839 if (intel_crtc->plane)
10840 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10841 else
10842 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010843 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10844 intel_ring_emit(ring, MI_NOOP);
10845 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10846 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10847 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010848 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010849 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000010850
10851 intel_mark_page_flip_active(intel_crtc);
Chris Wilson83d40922012-04-17 19:35:53 +010010852 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010853}
10854
10855static int intel_gen3_queue_flip(struct drm_device *dev,
10856 struct drm_crtc *crtc,
10857 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010858 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010859 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010860 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010861{
John Harrison6258fbe2015-05-29 17:43:48 +010010862 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010863 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010864 u32 flip_mask;
10865 int ret;
10866
John Harrison5fb9de12015-05-29 17:44:07 +010010867 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010868 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010869 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010870
10871 if (intel_crtc->plane)
10872 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10873 else
10874 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010875 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10876 intel_ring_emit(ring, MI_NOOP);
10877 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
10878 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10879 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010880 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010881 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010882
Chris Wilsone7d841c2012-12-03 11:36:30 +000010883 intel_mark_page_flip_active(intel_crtc);
Chris Wilson83d40922012-04-17 19:35:53 +010010884 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010885}
10886
10887static int intel_gen4_queue_flip(struct drm_device *dev,
10888 struct drm_crtc *crtc,
10889 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010890 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010891 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010892 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010893{
John Harrison6258fbe2015-05-29 17:43:48 +010010894 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010895 struct drm_i915_private *dev_priv = dev->dev_private;
10896 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10897 uint32_t pf, pipesrc;
10898 int ret;
10899
John Harrison5fb9de12015-05-29 17:44:07 +010010900 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010901 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010902 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010903
10904 /* i965+ uses the linear or tiled offsets from the
10905 * Display Registers (which do not change across a page-flip)
10906 * so we need only reprogram the base address.
10907 */
Daniel Vetter6d90c952012-04-26 23:28:05 +020010908 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10909 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10910 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010911 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020010912 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010913
10914 /* XXX Enabling the panel-fitter across page-flip is so far
10915 * untested on non-native modes, so ignore it for now.
10916 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
10917 */
10918 pf = 0;
10919 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010920 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010921
10922 intel_mark_page_flip_active(intel_crtc);
Chris Wilson83d40922012-04-17 19:35:53 +010010923 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010924}
10925
10926static int intel_gen6_queue_flip(struct drm_device *dev,
10927 struct drm_crtc *crtc,
10928 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010929 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010930 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010931 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010932{
John Harrison6258fbe2015-05-29 17:43:48 +010010933 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010934 struct drm_i915_private *dev_priv = dev->dev_private;
10935 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10936 uint32_t pf, pipesrc;
10937 int ret;
10938
John Harrison5fb9de12015-05-29 17:44:07 +010010939 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010940 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010941 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010942
Daniel Vetter6d90c952012-04-26 23:28:05 +020010943 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10944 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10945 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010946 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010947
Chris Wilson99d9acd2012-04-17 20:37:00 +010010948 /* Contrary to the suggestions in the documentation,
10949 * "Enable Panel Fitter" does not seem to be required when page
10950 * flipping with a non-native mode, and worse causes a normal
10951 * modeset to fail.
10952 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
10953 */
10954 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010955 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010956 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010957
10958 intel_mark_page_flip_active(intel_crtc);
Chris Wilson83d40922012-04-17 19:35:53 +010010959 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010960}
10961
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010962static int intel_gen7_queue_flip(struct drm_device *dev,
10963 struct drm_crtc *crtc,
10964 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010965 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010966 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010967 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010968{
John Harrison6258fbe2015-05-29 17:43:48 +010010969 struct intel_engine_cs *ring = req->ring;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010970 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010971 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010010972 int len, ret;
10973
Robin Schroereba905b2014-05-18 02:24:50 +020010974 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010975 case PLANE_A:
10976 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
10977 break;
10978 case PLANE_B:
10979 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
10980 break;
10981 case PLANE_C:
10982 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
10983 break;
10984 default:
10985 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010986 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010987 }
10988
Chris Wilsonffe74d72013-08-26 20:58:12 +010010989 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +010010990 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010010991 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010010992 /*
10993 * On Gen 8, SRM is now taking an extra dword to accommodate
10994 * 48bits addresses, and we need a NOOP for the batch size to
10995 * stay even.
10996 */
10997 if (IS_GEN8(dev))
10998 len += 2;
10999 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011000
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011001 /*
11002 * BSpec MI_DISPLAY_FLIP for IVB:
11003 * "The full packet must be contained within the same cache line."
11004 *
11005 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11006 * cacheline, if we ever start emitting more commands before
11007 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11008 * then do the cacheline alignment, and finally emit the
11009 * MI_DISPLAY_FLIP.
11010 */
John Harrisonbba09b12015-05-29 17:44:06 +010011011 ret = intel_ring_cacheline_align(req);
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011012 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011013 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011014
John Harrison5fb9de12015-05-29 17:44:07 +010011015 ret = intel_ring_begin(req, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011016 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011017 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011018
Chris Wilsonffe74d72013-08-26 20:58:12 +010011019 /* Unmask the flip-done completion message. Note that the bspec says that
11020 * we should do this for both the BCS and RCS, and that we must not unmask
11021 * more than one flip event at any time (or ensure that one flip message
11022 * can be sent by waiting for flip-done prior to queueing new flips).
11023 * Experimentation says that BCS works despite DERRMR masking all
11024 * flip-done completion events and that unmasking all planes at once
11025 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11026 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11027 */
11028 if (ring->id == RCS) {
11029 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
11030 intel_ring_emit(ring, DERRMR);
11031 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11032 DERRMR_PIPEB_PRI_FLIP_DONE |
11033 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010011034 if (IS_GEN8(dev))
11035 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
11036 MI_SRM_LRM_GLOBAL_GTT);
11037 else
11038 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
11039 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +010011040 intel_ring_emit(ring, DERRMR);
11041 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010011042 if (IS_GEN8(dev)) {
11043 intel_ring_emit(ring, 0);
11044 intel_ring_emit(ring, MI_NOOP);
11045 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011046 }
11047
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011048 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +020011049 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011050 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011051 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000011052
11053 intel_mark_page_flip_active(intel_crtc);
Chris Wilson83d40922012-04-17 19:35:53 +010011054 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011055}
11056
Sourab Gupta84c33a62014-06-02 16:47:17 +053011057static bool use_mmio_flip(struct intel_engine_cs *ring,
11058 struct drm_i915_gem_object *obj)
11059{
11060 /*
11061 * This is not being used for older platforms, because
11062 * non-availability of flip done interrupt forces us to use
11063 * CS flips. Older platforms derive flip done using some clever
11064 * tricks involving the flip_pending status bits and vblank irqs.
11065 * So using MMIO flips there would disrupt this mechanism.
11066 */
11067
Chris Wilson8e09bf82014-07-08 10:40:30 +010011068 if (ring == NULL)
11069 return true;
11070
Sourab Gupta84c33a62014-06-02 16:47:17 +053011071 if (INTEL_INFO(ring->dev)->gen < 5)
11072 return false;
11073
11074 if (i915.use_mmio_flip < 0)
11075 return false;
11076 else if (i915.use_mmio_flip > 0)
11077 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010011078 else if (i915.enable_execlists)
11079 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011080 else
Chris Wilsonb4716182015-04-27 13:41:17 +010011081 return ring != i915_gem_request_get_ring(obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011082}
11083
Damien Lespiauff944562014-11-20 14:58:16 +000011084static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
11085{
11086 struct drm_device *dev = intel_crtc->base.dev;
11087 struct drm_i915_private *dev_priv = dev->dev_private;
11088 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000011089 const enum pipe pipe = intel_crtc->pipe;
11090 u32 ctl, stride;
11091
11092 ctl = I915_READ(PLANE_CTL(pipe, 0));
11093 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011094 switch (fb->modifier[0]) {
11095 case DRM_FORMAT_MOD_NONE:
11096 break;
11097 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000011098 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011099 break;
11100 case I915_FORMAT_MOD_Y_TILED:
11101 ctl |= PLANE_CTL_TILED_Y;
11102 break;
11103 case I915_FORMAT_MOD_Yf_TILED:
11104 ctl |= PLANE_CTL_TILED_YF;
11105 break;
11106 default:
11107 MISSING_CASE(fb->modifier[0]);
11108 }
Damien Lespiauff944562014-11-20 14:58:16 +000011109
11110 /*
11111 * The stride is either expressed as a multiple of 64 bytes chunks for
11112 * linear buffers or in number of tiles for tiled buffers.
11113 */
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011114 stride = fb->pitches[0] /
11115 intel_fb_stride_alignment(dev, fb->modifier[0],
11116 fb->pixel_format);
Damien Lespiauff944562014-11-20 14:58:16 +000011117
11118 /*
11119 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11120 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11121 */
11122 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11123 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11124
11125 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
11126 POSTING_READ(PLANE_SURF(pipe, 0));
11127}
11128
11129static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011130{
11131 struct drm_device *dev = intel_crtc->base.dev;
11132 struct drm_i915_private *dev_priv = dev->dev_private;
11133 struct intel_framebuffer *intel_fb =
11134 to_intel_framebuffer(intel_crtc->base.primary->fb);
11135 struct drm_i915_gem_object *obj = intel_fb->obj;
11136 u32 dspcntr;
11137 u32 reg;
11138
Sourab Gupta84c33a62014-06-02 16:47:17 +053011139 reg = DSPCNTR(intel_crtc->plane);
11140 dspcntr = I915_READ(reg);
11141
Damien Lespiauc5d97472014-10-25 00:11:11 +010011142 if (obj->tiling_mode != I915_TILING_NONE)
11143 dspcntr |= DISPPLANE_TILED;
11144 else
11145 dspcntr &= ~DISPPLANE_TILED;
11146
Sourab Gupta84c33a62014-06-02 16:47:17 +053011147 I915_WRITE(reg, dspcntr);
11148
11149 I915_WRITE(DSPSURF(intel_crtc->plane),
11150 intel_crtc->unpin_work->gtt_offset);
11151 POSTING_READ(DSPSURF(intel_crtc->plane));
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011152
Damien Lespiauff944562014-11-20 14:58:16 +000011153}
11154
11155/*
11156 * XXX: This is the temporary way to update the plane registers until we get
11157 * around to using the usual plane update functions for MMIO flips
11158 */
11159static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
11160{
11161 struct drm_device *dev = intel_crtc->base.dev;
11162 bool atomic_update;
11163 u32 start_vbl_count;
11164
11165 intel_mark_page_flip_active(intel_crtc);
11166
11167 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
11168
11169 if (INTEL_INFO(dev)->gen >= 9)
11170 skl_do_mmio_flip(intel_crtc);
11171 else
11172 /* use_mmio_flip() retricts MMIO flips to ilk+ */
11173 ilk_do_mmio_flip(intel_crtc);
11174
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011175 if (atomic_update)
11176 intel_pipe_update_end(intel_crtc, start_vbl_count);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011177}
11178
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011179static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011180{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011181 struct intel_mmio_flip *mmio_flip =
11182 container_of(work, struct intel_mmio_flip, work);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011183
Daniel Vettereed29a52015-05-21 14:21:25 +020011184 if (mmio_flip->req)
11185 WARN_ON(__i915_wait_request(mmio_flip->req,
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011186 mmio_flip->crtc->reset_counter,
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011187 false, NULL,
11188 &mmio_flip->i915->rps.mmioflips));
Sourab Gupta84c33a62014-06-02 16:47:17 +053011189
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011190 intel_do_mmio_flip(mmio_flip->crtc);
11191
Daniel Vettereed29a52015-05-21 14:21:25 +020011192 i915_gem_request_unreference__unlocked(mmio_flip->req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011193 kfree(mmio_flip);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011194}
11195
11196static int intel_queue_mmio_flip(struct drm_device *dev,
11197 struct drm_crtc *crtc,
11198 struct drm_framebuffer *fb,
11199 struct drm_i915_gem_object *obj,
11200 struct intel_engine_cs *ring,
11201 uint32_t flags)
11202{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011203 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011204
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011205 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11206 if (mmio_flip == NULL)
11207 return -ENOMEM;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011208
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011209 mmio_flip->i915 = to_i915(dev);
Daniel Vettereed29a52015-05-21 14:21:25 +020011210 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011211 mmio_flip->crtc = to_intel_crtc(crtc);
11212
11213 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11214 schedule_work(&mmio_flip->work);
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020011215
Sourab Gupta84c33a62014-06-02 16:47:17 +053011216 return 0;
11217}
11218
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011219static int intel_default_queue_flip(struct drm_device *dev,
11220 struct drm_crtc *crtc,
11221 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011222 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011223 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011224 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011225{
11226 return -ENODEV;
11227}
11228
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011229static bool __intel_pageflip_stall_check(struct drm_device *dev,
11230 struct drm_crtc *crtc)
11231{
11232 struct drm_i915_private *dev_priv = dev->dev_private;
11233 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11234 struct intel_unpin_work *work = intel_crtc->unpin_work;
11235 u32 addr;
11236
11237 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11238 return true;
11239
11240 if (!work->enable_stall_check)
11241 return false;
11242
11243 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010011244 if (work->flip_queued_req &&
11245 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011246 return false;
11247
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011248 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011249 }
11250
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011251 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011252 return false;
11253
11254 /* Potential stall - if we see that the flip has happened,
11255 * assume a missed interrupt. */
11256 if (INTEL_INFO(dev)->gen >= 4)
11257 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11258 else
11259 addr = I915_READ(DSPADDR(intel_crtc->plane));
11260
11261 /* There is a potential issue here with a false positive after a flip
11262 * to the same address. We could address this by checking for a
11263 * non-incrementing frame counter.
11264 */
11265 return addr == work->gtt_offset;
11266}
11267
11268void intel_check_page_flip(struct drm_device *dev, int pipe)
11269{
11270 struct drm_i915_private *dev_priv = dev->dev_private;
11271 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11272 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011273 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020011274
Dave Gordon6c51d462015-03-06 15:34:26 +000011275 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011276
11277 if (crtc == NULL)
11278 return;
11279
Daniel Vetterf3260382014-09-15 14:55:23 +020011280 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011281 work = intel_crtc->unpin_work;
11282 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011283 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010011284 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011285 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011286 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011287 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010011288 if (work != NULL &&
11289 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11290 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020011291 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011292}
11293
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011294static int intel_crtc_page_flip(struct drm_crtc *crtc,
11295 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011296 struct drm_pending_vblank_event *event,
11297 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011298{
11299 struct drm_device *dev = crtc->dev;
11300 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070011301 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070011302 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011303 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080011304 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020011305 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011306 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011307 struct intel_engine_cs *ring;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011308 bool mmio_flip;
John Harrison91af1272015-06-18 13:14:56 +010011309 struct drm_i915_gem_request *request = NULL;
Chris Wilson52e68632010-08-08 10:15:59 +010011310 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011311
Matt Roper2ff8fde2014-07-08 07:50:07 -070011312 /*
11313 * drm_mode_page_flip_ioctl() should already catch this, but double
11314 * check to be safe. In the future we may enable pageflipping from
11315 * a disabled primary plane.
11316 */
11317 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11318 return -EBUSY;
11319
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011320 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070011321 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011322 return -EINVAL;
11323
11324 /*
11325 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11326 * Note that pitch changes could also affect these register.
11327 */
11328 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070011329 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11330 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011331 return -EINVAL;
11332
Chris Wilsonf900db42014-02-20 09:26:13 +000011333 if (i915_terminally_wedged(&dev_priv->gpu_error))
11334 goto out_hang;
11335
Daniel Vetterb14c5672013-09-19 12:18:32 +020011336 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011337 if (work == NULL)
11338 return -ENOMEM;
11339
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011340 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011341 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011342 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011343 INIT_WORK(&work->work, intel_unpin_work_fn);
11344
Daniel Vetter87b6b102014-05-15 15:33:46 +020011345 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011346 if (ret)
11347 goto free_work;
11348
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011349 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011350 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011351 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011352 /* Before declaring the flip queue wedged, check if
11353 * the hardware completed the operation behind our backs.
11354 */
11355 if (__intel_pageflip_stall_check(dev, crtc)) {
11356 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11357 page_flip_completed(intel_crtc);
11358 } else {
11359 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011360 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010011361
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011362 drm_crtc_vblank_put(crtc);
11363 kfree(work);
11364 return -EBUSY;
11365 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011366 }
11367 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011368 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011369
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011370 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11371 flush_workqueue(dev_priv->wq);
11372
Jesse Barnes75dfca82010-02-10 15:09:44 -080011373 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011374 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000011375 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011376
Matt Roperf4510a22014-04-01 15:22:40 -070011377 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011378 update_state_fb(crtc->primary);
Matt Roper1ed1f962015-01-30 16:22:36 -080011379
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011380 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011381
Chris Wilson89ed88b2015-02-16 14:31:49 +000011382 ret = i915_mutex_lock_interruptible(dev);
11383 if (ret)
11384 goto cleanup;
11385
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011386 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020011387 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011388
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011389 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Daniel Vettera071fa02014-06-18 23:28:09 +020011390 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011391
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011392 if (IS_VALLEYVIEW(dev)) {
11393 ring = &dev_priv->ring[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011394 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011395 /* vlv: DISPLAY_FLIP fails to change tiling */
11396 ring = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000011397 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Chris Wilson2a92d5b2014-07-08 10:40:29 +010011398 ring = &dev_priv->ring[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011399 } else if (INTEL_INFO(dev)->gen >= 7) {
Chris Wilsonb4716182015-04-27 13:41:17 +010011400 ring = i915_gem_request_get_ring(obj->last_write_req);
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011401 if (ring == NULL || ring->id != RCS)
11402 ring = &dev_priv->ring[BCS];
11403 } else {
11404 ring = &dev_priv->ring[RCS];
11405 }
11406
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011407 mmio_flip = use_mmio_flip(ring, obj);
11408
11409 /* When using CS flips, we want to emit semaphores between rings.
11410 * However, when using mmio flips we will create a task to do the
11411 * synchronisation, so all we want here is to pin the framebuffer
11412 * into the display plane and skip any waits.
11413 */
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011414 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011415 crtc->primary->state,
John Harrison91af1272015-06-18 13:14:56 +010011416 mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring, &request);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011417 if (ret)
11418 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011419
Tvrtko Ursulin121920f2015-03-23 11:10:37 +000011420 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
11421 + intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011422
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011423 if (mmio_flip) {
Sourab Gupta84c33a62014-06-02 16:47:17 +053011424 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
11425 page_flip_flags);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011426 if (ret)
11427 goto cleanup_unpin;
11428
John Harrisonf06cc1b2014-11-24 18:49:37 +000011429 i915_gem_request_assign(&work->flip_queued_req,
11430 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011431 } else {
John Harrison6258fbe2015-05-29 17:43:48 +010011432 if (!request) {
11433 ret = i915_gem_request_alloc(ring, ring->default_context, &request);
11434 if (ret)
11435 goto cleanup_unpin;
11436 }
11437
11438 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011439 page_flip_flags);
11440 if (ret)
11441 goto cleanup_unpin;
11442
John Harrison6258fbe2015-05-29 17:43:48 +010011443 i915_gem_request_assign(&work->flip_queued_req, request);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011444 }
11445
John Harrison91af1272015-06-18 13:14:56 +010011446 if (request)
John Harrison75289872015-05-29 17:43:49 +010011447 i915_add_request_no_flush(request);
John Harrison91af1272015-06-18 13:14:56 +010011448
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011449 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011450 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011451
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011452 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011453 to_intel_plane(primary)->frontbuffer_bit);
Daniel Vettera071fa02014-06-18 23:28:09 +020011454
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020011455 intel_fbc_disable(dev);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011456 intel_frontbuffer_flip_prepare(dev,
11457 to_intel_plane(primary)->frontbuffer_bit);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011458 mutex_unlock(&dev->struct_mutex);
11459
Jesse Barnese5510fa2010-07-01 16:48:37 -070011460 trace_i915_flip_request(intel_crtc->plane, obj);
11461
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011462 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010011463
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011464cleanup_unpin:
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011465 intel_unpin_fb_obj(fb, crtc->primary->state);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011466cleanup_pending:
John Harrison91af1272015-06-18 13:14:56 +010011467 if (request)
11468 i915_gem_request_cancel(request);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011469 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000011470 mutex_unlock(&dev->struct_mutex);
11471cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070011472 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011473 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010011474
Chris Wilson89ed88b2015-02-16 14:31:49 +000011475 drm_gem_object_unreference_unlocked(&obj->base);
11476 drm_framebuffer_unreference(work->old_fb);
11477
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011478 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011479 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011480 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011481
Daniel Vetter87b6b102014-05-15 15:33:46 +020011482 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011483free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010011484 kfree(work);
11485
Chris Wilsonf900db42014-02-20 09:26:13 +000011486 if (ret == -EIO) {
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011487 struct drm_atomic_state *state;
11488 struct drm_plane_state *plane_state;
11489
Chris Wilsonf900db42014-02-20 09:26:13 +000011490out_hang:
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011491 state = drm_atomic_state_alloc(dev);
11492 if (!state)
11493 return -ENOMEM;
11494 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11495
11496retry:
11497 plane_state = drm_atomic_get_plane_state(state, primary);
11498 ret = PTR_ERR_OR_ZERO(plane_state);
11499 if (!ret) {
11500 drm_atomic_set_fb_for_plane(plane_state, fb);
11501
11502 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11503 if (!ret)
11504 ret = drm_atomic_commit(state);
11505 }
11506
11507 if (ret == -EDEADLK) {
11508 drm_modeset_backoff(state->acquire_ctx);
11509 drm_atomic_state_clear(state);
11510 goto retry;
11511 }
11512
11513 if (ret)
11514 drm_atomic_state_free(state);
11515
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011516 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011517 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020011518 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011519 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011520 }
Chris Wilsonf900db42014-02-20 09:26:13 +000011521 }
Chris Wilson96b099f2010-06-07 14:03:04 +010011522 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011523}
11524
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011525
11526/**
11527 * intel_wm_need_update - Check whether watermarks need updating
11528 * @plane: drm plane
11529 * @state: new plane state
11530 *
11531 * Check current plane state versus the new one to determine whether
11532 * watermarks need to be recalculated.
11533 *
11534 * Returns true or false.
11535 */
11536static bool intel_wm_need_update(struct drm_plane *plane,
11537 struct drm_plane_state *state)
11538{
11539 /* Update watermarks on tiling changes. */
11540 if (!plane->state->fb || !state->fb ||
11541 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
11542 plane->state->rotation != state->rotation)
11543 return true;
11544
11545 if (plane->state->crtc_w != state->crtc_w)
11546 return true;
11547
11548 return false;
11549}
11550
11551int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11552 struct drm_plane_state *plane_state)
11553{
11554 struct drm_crtc *crtc = crtc_state->crtc;
11555 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11556 struct drm_plane *plane = plane_state->plane;
11557 struct drm_device *dev = crtc->dev;
11558 struct drm_i915_private *dev_priv = dev->dev_private;
11559 struct intel_plane_state *old_plane_state =
11560 to_intel_plane_state(plane->state);
11561 int idx = intel_crtc->base.base.id, ret;
11562 int i = drm_plane_index(plane);
11563 bool mode_changed = needs_modeset(crtc_state);
11564 bool was_crtc_enabled = crtc->state->active;
11565 bool is_crtc_enabled = crtc_state->active;
11566
11567 bool turn_off, turn_on, visible, was_visible;
11568 struct drm_framebuffer *fb = plane_state->fb;
11569
11570 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11571 plane->type != DRM_PLANE_TYPE_CURSOR) {
11572 ret = skl_update_scaler_plane(
11573 to_intel_crtc_state(crtc_state),
11574 to_intel_plane_state(plane_state));
11575 if (ret)
11576 return ret;
11577 }
11578
11579 /*
11580 * Disabling a plane is always okay; we just need to update
11581 * fb tracking in a special way since cleanup_fb() won't
11582 * get called by the plane helpers.
11583 */
11584 if (old_plane_state->base.fb && !fb)
11585 intel_crtc->atomic.disabled_planes |= 1 << i;
11586
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011587 was_visible = old_plane_state->visible;
11588 visible = to_intel_plane_state(plane_state)->visible;
11589
11590 if (!was_crtc_enabled && WARN_ON(was_visible))
11591 was_visible = false;
11592
11593 if (!is_crtc_enabled && WARN_ON(visible))
11594 visible = false;
11595
11596 if (!was_visible && !visible)
11597 return 0;
11598
11599 turn_off = was_visible && (!visible || mode_changed);
11600 turn_on = visible && (!was_visible || mode_changed);
11601
11602 DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
11603 plane->base.id, fb ? fb->base.id : -1);
11604
11605 DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11606 plane->base.id, was_visible, visible,
11607 turn_off, turn_on, mode_changed);
11608
11609 if (intel_wm_need_update(plane, plane_state))
11610 intel_crtc->atomic.update_wm = true;
11611
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011612 if (visible)
11613 intel_crtc->atomic.fb_bits |=
11614 to_intel_plane(plane)->frontbuffer_bit;
11615
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011616 switch (plane->type) {
11617 case DRM_PLANE_TYPE_PRIMARY:
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011618 intel_crtc->atomic.wait_for_flips = true;
11619 intel_crtc->atomic.pre_disable_primary = turn_off;
11620 intel_crtc->atomic.post_enable_primary = turn_on;
11621
Rodrigo Vivi066cf552015-06-26 13:55:54 -070011622 if (turn_off) {
11623 /*
11624 * FIXME: Actually if we will still have any other
11625 * plane enabled on the pipe we could let IPS enabled
11626 * still, but for now lets consider that when we make
11627 * primary invisible by setting DSPCNTR to 0 on
11628 * update_primary_plane function IPS needs to be
11629 * disable.
11630 */
11631 intel_crtc->atomic.disable_ips = true;
11632
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011633 intel_crtc->atomic.disable_fbc = true;
Rodrigo Vivi066cf552015-06-26 13:55:54 -070011634 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011635
11636 /*
11637 * FBC does not work on some platforms for rotated
11638 * planes, so disable it when rotation is not 0 and
11639 * update it when rotation is set back to 0.
11640 *
11641 * FIXME: This is redundant with the fbc update done in
11642 * the primary plane enable function except that that
11643 * one is done too late. We eventually need to unify
11644 * this.
11645 */
11646
11647 if (visible &&
11648 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
11649 dev_priv->fbc.crtc == intel_crtc &&
11650 plane_state->rotation != BIT(DRM_ROTATE_0))
11651 intel_crtc->atomic.disable_fbc = true;
11652
11653 /*
11654 * BDW signals flip done immediately if the plane
11655 * is disabled, even if the plane enable is already
11656 * armed to occur at the next vblank :(
11657 */
11658 if (turn_on && IS_BROADWELL(dev))
11659 intel_crtc->atomic.wait_vblank = true;
11660
11661 intel_crtc->atomic.update_fbc |= visible || mode_changed;
11662 break;
11663 case DRM_PLANE_TYPE_CURSOR:
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011664 break;
11665 case DRM_PLANE_TYPE_OVERLAY:
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020011666 if (turn_off && !mode_changed) {
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011667 intel_crtc->atomic.wait_vblank = true;
11668 intel_crtc->atomic.update_sprite_watermarks |=
11669 1 << i;
11670 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011671 }
11672 return 0;
11673}
11674
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011675static bool encoders_cloneable(const struct intel_encoder *a,
11676 const struct intel_encoder *b)
11677{
11678 /* masks could be asymmetric, so check both ways */
11679 return a == b || (a->cloneable & (1 << b->type) &&
11680 b->cloneable & (1 << a->type));
11681}
11682
11683static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11684 struct intel_crtc *crtc,
11685 struct intel_encoder *encoder)
11686{
11687 struct intel_encoder *source_encoder;
11688 struct drm_connector *connector;
11689 struct drm_connector_state *connector_state;
11690 int i;
11691
11692 for_each_connector_in_state(state, connector, connector_state, i) {
11693 if (connector_state->crtc != &crtc->base)
11694 continue;
11695
11696 source_encoder =
11697 to_intel_encoder(connector_state->best_encoder);
11698 if (!encoders_cloneable(encoder, source_encoder))
11699 return false;
11700 }
11701
11702 return true;
11703}
11704
11705static bool check_encoder_cloning(struct drm_atomic_state *state,
11706 struct intel_crtc *crtc)
11707{
11708 struct intel_encoder *encoder;
11709 struct drm_connector *connector;
11710 struct drm_connector_state *connector_state;
11711 int i;
11712
11713 for_each_connector_in_state(state, connector, connector_state, i) {
11714 if (connector_state->crtc != &crtc->base)
11715 continue;
11716
11717 encoder = to_intel_encoder(connector_state->best_encoder);
11718 if (!check_single_encoder_cloning(state, crtc, encoder))
11719 return false;
11720 }
11721
11722 return true;
11723}
11724
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020011725static void intel_crtc_check_initial_planes(struct drm_crtc *crtc,
11726 struct drm_crtc_state *crtc_state)
11727{
11728 struct intel_crtc_state *pipe_config =
11729 to_intel_crtc_state(crtc_state);
11730 struct drm_plane *p;
11731 unsigned visible_mask = 0;
11732
11733 drm_for_each_plane_mask(p, crtc->dev, crtc_state->plane_mask) {
11734 struct drm_plane_state *plane_state =
11735 drm_atomic_get_existing_plane_state(crtc_state->state, p);
11736
11737 if (WARN_ON(!plane_state))
11738 continue;
11739
11740 if (!plane_state->fb)
11741 crtc_state->plane_mask &=
11742 ~(1 << drm_plane_index(p));
11743 else if (to_intel_plane_state(plane_state)->visible)
11744 visible_mask |= 1 << drm_plane_index(p);
11745 }
11746
11747 if (!visible_mask)
11748 return;
11749
11750 pipe_config->quirks &= ~PIPE_CONFIG_QUIRK_INITIAL_PLANES;
11751}
11752
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011753static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11754 struct drm_crtc_state *crtc_state)
11755{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011756 struct drm_device *dev = crtc->dev;
Maarten Lankhorstad421372015-06-15 12:33:42 +020011757 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011758 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011759 struct intel_crtc_state *pipe_config =
11760 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011761 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorstad421372015-06-15 12:33:42 +020011762 int ret, idx = crtc->base.id;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011763 bool mode_changed = needs_modeset(crtc_state);
11764
11765 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
11766 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11767 return -EINVAL;
11768 }
11769
11770 I915_STATE_WARN(crtc->state->active != intel_crtc->active,
11771 "[CRTC:%i] mismatch between state->active(%i) and crtc->active(%i)\n",
11772 idx, crtc->state->active, intel_crtc->active);
11773
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020011774 /* plane mask is fixed up after all initial planes are calculated */
11775 if (pipe_config->quirks & PIPE_CONFIG_QUIRK_INITIAL_PLANES)
11776 intel_crtc_check_initial_planes(crtc, crtc_state);
11777
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020011778 if (mode_changed)
11779 intel_crtc->atomic.update_wm = !crtc_state->active;
11780
Maarten Lankhorstad421372015-06-15 12:33:42 +020011781 if (mode_changed && crtc_state->enable &&
11782 dev_priv->display.crtc_compute_clock &&
11783 !WARN_ON(pipe_config->shared_dpll != DPLL_ID_PRIVATE)) {
11784 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11785 pipe_config);
11786 if (ret)
11787 return ret;
11788 }
11789
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011790 return intel_atomic_setup_scalers(dev, intel_crtc, pipe_config);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011791}
11792
Jani Nikula65b38e02015-04-13 11:26:56 +030011793static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011794 .mode_set_base_atomic = intel_pipe_set_base_atomic,
11795 .load_lut = intel_crtc_load_lut,
Matt Roperea2c67b2014-12-23 10:41:52 -080011796 .atomic_begin = intel_begin_crtc_commit,
11797 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011798 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011799};
11800
Daniel Vetter9a935852012-07-05 22:34:27 +020011801/**
11802 * intel_modeset_update_staged_output_state
11803 *
11804 * Updates the staged output configuration state, e.g. after we've read out the
11805 * current hw state.
11806 */
11807static void intel_modeset_update_staged_output_state(struct drm_device *dev)
11808{
Ville Syrjälä76688512014-01-10 11:28:06 +020011809 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011810 struct intel_encoder *encoder;
11811 struct intel_connector *connector;
11812
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011813 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011814 connector->new_encoder =
11815 to_intel_encoder(connector->base.encoder);
11816 }
11817
Damien Lespiaub2784e12014-08-05 11:29:37 +010011818 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011819 encoder->new_crtc =
11820 to_intel_crtc(encoder->base.crtc);
11821 }
Ville Syrjälä76688512014-01-10 11:28:06 +020011822
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011823 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011824 crtc->new_enabled = crtc->base.state->enable;
Ville Syrjälä76688512014-01-10 11:28:06 +020011825 }
Daniel Vetter9a935852012-07-05 22:34:27 +020011826}
11827
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011828/* Transitional helper to copy current connector/encoder state to
11829 * connector->state. This is needed so that code that is partially
11830 * converted to atomic does the right thing.
11831 */
11832static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11833{
11834 struct intel_connector *connector;
11835
11836 for_each_intel_connector(dev, connector) {
11837 if (connector->base.encoder) {
11838 connector->base.state->best_encoder =
11839 connector->base.encoder;
11840 connector->base.state->crtc =
11841 connector->base.encoder->crtc;
11842 } else {
11843 connector->base.state->best_encoder = NULL;
11844 connector->base.state->crtc = NULL;
11845 }
11846 }
11847}
11848
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011849static void
Robin Schroereba905b2014-05-18 02:24:50 +020011850connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011851 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011852{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011853 int bpp = pipe_config->pipe_bpp;
11854
11855 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11856 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011857 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011858
11859 /* Don't use an invalid EDID bpc value */
11860 if (connector->base.display_info.bpc &&
11861 connector->base.display_info.bpc * 3 < bpp) {
11862 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11863 bpp, connector->base.display_info.bpc*3);
11864 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11865 }
11866
11867 /* Clamp bpp to 8 on screens without EDID 1.4 */
11868 if (connector->base.display_info.bpc == 0 && bpp > 24) {
11869 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11870 bpp);
11871 pipe_config->pipe_bpp = 24;
11872 }
11873}
11874
11875static int
11876compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011877 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011878{
11879 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011880 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011881 struct drm_connector *connector;
11882 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011883 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011884
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011885 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011886 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011887 else if (INTEL_INFO(dev)->gen >= 5)
11888 bpp = 12*3;
11889 else
11890 bpp = 8*3;
11891
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011892
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011893 pipe_config->pipe_bpp = bpp;
11894
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011895 state = pipe_config->base.state;
11896
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011897 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011898 for_each_connector_in_state(state, connector, connector_state, i) {
11899 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011900 continue;
11901
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011902 connected_sink_compute_bpp(to_intel_connector(connector),
11903 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011904 }
11905
11906 return bpp;
11907}
11908
Daniel Vetter644db712013-09-19 14:53:58 +020011909static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11910{
11911 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11912 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010011913 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020011914 mode->crtc_hdisplay, mode->crtc_hsync_start,
11915 mode->crtc_hsync_end, mode->crtc_htotal,
11916 mode->crtc_vdisplay, mode->crtc_vsync_start,
11917 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11918}
11919
Daniel Vetterc0b03412013-05-28 12:05:54 +020011920static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011921 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020011922 const char *context)
11923{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011924 struct drm_device *dev = crtc->base.dev;
11925 struct drm_plane *plane;
11926 struct intel_plane *intel_plane;
11927 struct intel_plane_state *state;
11928 struct drm_framebuffer *fb;
11929
11930 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
11931 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020011932
11933 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
11934 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
11935 pipe_config->pipe_bpp, pipe_config->dither);
11936 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11937 pipe_config->has_pch_encoder,
11938 pipe_config->fdi_lanes,
11939 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
11940 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
11941 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011942 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11943 pipe_config->has_dp_encoder,
11944 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
11945 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
11946 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011947
11948 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
11949 pipe_config->has_dp_encoder,
11950 pipe_config->dp_m2_n2.gmch_m,
11951 pipe_config->dp_m2_n2.gmch_n,
11952 pipe_config->dp_m2_n2.link_m,
11953 pipe_config->dp_m2_n2.link_n,
11954 pipe_config->dp_m2_n2.tu);
11955
Daniel Vetter55072d12014-11-20 16:10:28 +010011956 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
11957 pipe_config->has_audio,
11958 pipe_config->has_infoframe);
11959
Daniel Vetterc0b03412013-05-28 12:05:54 +020011960 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011961 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011962 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011963 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11964 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030011965 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030011966 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
11967 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010011968 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
11969 crtc->num_scalers,
11970 pipe_config->scaler_state.scaler_users,
11971 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011972 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11973 pipe_config->gmch_pfit.control,
11974 pipe_config->gmch_pfit.pgm_ratios,
11975 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011976 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020011977 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011978 pipe_config->pch_pfit.size,
11979 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030011980 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030011981 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011982
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010011983 if (IS_BROXTON(dev)) {
11984 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, "
11985 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
11986 "pll6: 0x%x, pll8: 0x%x, pcsdw12: 0x%x\n",
11987 pipe_config->ddi_pll_sel,
11988 pipe_config->dpll_hw_state.ebb0,
11989 pipe_config->dpll_hw_state.pll0,
11990 pipe_config->dpll_hw_state.pll1,
11991 pipe_config->dpll_hw_state.pll2,
11992 pipe_config->dpll_hw_state.pll3,
11993 pipe_config->dpll_hw_state.pll6,
11994 pipe_config->dpll_hw_state.pll8,
11995 pipe_config->dpll_hw_state.pcsdw12);
11996 } else if (IS_SKYLAKE(dev)) {
11997 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
11998 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
11999 pipe_config->ddi_pll_sel,
12000 pipe_config->dpll_hw_state.ctrl1,
12001 pipe_config->dpll_hw_state.cfgcr1,
12002 pipe_config->dpll_hw_state.cfgcr2);
12003 } else if (HAS_DDI(dev)) {
12004 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
12005 pipe_config->ddi_pll_sel,
12006 pipe_config->dpll_hw_state.wrpll);
12007 } else {
12008 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12009 "fp0: 0x%x, fp1: 0x%x\n",
12010 pipe_config->dpll_hw_state.dpll,
12011 pipe_config->dpll_hw_state.dpll_md,
12012 pipe_config->dpll_hw_state.fp0,
12013 pipe_config->dpll_hw_state.fp1);
12014 }
12015
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012016 DRM_DEBUG_KMS("planes on this crtc\n");
12017 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12018 intel_plane = to_intel_plane(plane);
12019 if (intel_plane->pipe != crtc->pipe)
12020 continue;
12021
12022 state = to_intel_plane_state(plane->state);
12023 fb = state->base.fb;
12024 if (!fb) {
12025 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
12026 "disabled, scaler_id = %d\n",
12027 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12028 plane->base.id, intel_plane->pipe,
12029 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
12030 drm_plane_index(plane), state->scaler_id);
12031 continue;
12032 }
12033
12034 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
12035 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12036 plane->base.id, intel_plane->pipe,
12037 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
12038 drm_plane_index(plane));
12039 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
12040 fb->base.id, fb->width, fb->height, fb->pixel_format);
12041 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
12042 state->scaler_id,
12043 state->src.x1 >> 16, state->src.y1 >> 16,
12044 drm_rect_width(&state->src) >> 16,
12045 drm_rect_height(&state->src) >> 16,
12046 state->dst.x1, state->dst.y1,
12047 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
12048 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020012049}
12050
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012051static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012052{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012053 struct drm_device *dev = state->dev;
12054 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012055 struct drm_connector *connector;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012056 struct drm_connector_state *connector_state;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012057 unsigned int used_ports = 0;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012058 int i;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012059
12060 /*
12061 * Walk the connector list instead of the encoder
12062 * list to detect the problem on ddi platforms
12063 * where there's just one encoder per digital port.
12064 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012065 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012066 if (!connector_state->best_encoder)
12067 continue;
12068
12069 encoder = to_intel_encoder(connector_state->best_encoder);
12070
12071 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012072
12073 switch (encoder->type) {
12074 unsigned int port_mask;
12075 case INTEL_OUTPUT_UNKNOWN:
12076 if (WARN_ON(!HAS_DDI(dev)))
12077 break;
12078 case INTEL_OUTPUT_DISPLAYPORT:
12079 case INTEL_OUTPUT_HDMI:
12080 case INTEL_OUTPUT_EDP:
12081 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12082
12083 /* the same port mustn't appear more than once */
12084 if (used_ports & port_mask)
12085 return false;
12086
12087 used_ports |= port_mask;
12088 default:
12089 break;
12090 }
12091 }
12092
12093 return true;
12094}
12095
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012096static void
12097clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12098{
12099 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012100 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012101 struct intel_dpll_hw_state dpll_hw_state;
12102 enum intel_dpll_id shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012103 uint32_t ddi_pll_sel;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012104
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012105 /* FIXME: before the switch to atomic started, a new pipe_config was
12106 * kzalloc'd. Code that depends on any field being zero should be
12107 * fixed, so that the crtc_state can be safely duplicated. For now,
12108 * only fields that are know to not cause problems are preserved. */
12109
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012110 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012111 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012112 shared_dpll = crtc_state->shared_dpll;
12113 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012114 ddi_pll_sel = crtc_state->ddi_pll_sel;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012115
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012116 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012117
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012118 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012119 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012120 crtc_state->shared_dpll = shared_dpll;
12121 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012122 crtc_state->ddi_pll_sel = ddi_pll_sel;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012123}
12124
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012125static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012126intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012127 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012128{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012129 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020012130 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012131 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012132 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012133 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012134 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012135 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012136
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012137 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012138
Daniel Vettere143a212013-07-04 12:01:15 +020012139 pipe_config->cpu_transcoder =
12140 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012141
Imre Deak2960bc92013-07-30 13:36:32 +030012142 /*
12143 * Sanitize sync polarity flags based on requested ones. If neither
12144 * positive or negative polarity is requested, treat this as meaning
12145 * negative polarity.
12146 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012147 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012148 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012149 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012150
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012151 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012152 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012153 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012154
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012155 /* Compute a starting value for pipe_config->pipe_bpp taking the source
12156 * plane pixel format and any sink constraints into account. Returns the
12157 * source plane bpp so that dithering can be selected on mismatches
12158 * after encoders and crtc also have had their say. */
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012159 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12160 pipe_config);
12161 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012162 goto fail;
12163
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012164 /*
12165 * Determine the real pipe dimensions. Note that stereo modes can
12166 * increase the actual pipe size due to the frame doubling and
12167 * insertion of additional space for blanks between the frame. This
12168 * is stored in the crtc timings. We use the requested mode to do this
12169 * computation to clearly distinguish it from the adjusted mode, which
12170 * can be changed by the connectors in the below retry loop.
12171 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012172 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080012173 &pipe_config->pipe_src_w,
12174 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012175
Daniel Vettere29c22c2013-02-21 00:00:16 +010012176encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020012177 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020012178 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020012179 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012180
Daniel Vetter135c81b2013-07-21 21:37:09 +020012181 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012182 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12183 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020012184
Daniel Vetter7758a112012-07-08 19:40:39 +020012185 /* Pass our mode to the connectors and the CRTC to give them a chance to
12186 * adjust it according to limitations or connector properties, and also
12187 * a chance to reject the mode entirely.
12188 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012189 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012190 if (connector_state->crtc != crtc)
12191 continue;
12192
12193 encoder = to_intel_encoder(connector_state->best_encoder);
12194
Daniel Vetterefea6e82013-07-21 21:36:59 +020012195 if (!(encoder->compute_config(encoder, pipe_config))) {
12196 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020012197 goto fail;
12198 }
12199 }
12200
Daniel Vetterff9a6752013-06-01 17:16:21 +020012201 /* Set default port clock if not overwritten by the encoder. Needs to be
12202 * done afterwards in case the encoder adjusts the mode. */
12203 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012204 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010012205 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012206
Daniel Vettera43f6e02013-06-07 23:10:32 +020012207 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010012208 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020012209 DRM_DEBUG_KMS("CRTC fixup failed\n");
12210 goto fail;
12211 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010012212
12213 if (ret == RETRY) {
12214 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12215 ret = -EINVAL;
12216 goto fail;
12217 }
12218
12219 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12220 retry = false;
12221 goto encoder_retry;
12222 }
12223
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012224 pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012225 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012226 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012227
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +020012228 /* Check if we need to force a modeset */
12229 if (pipe_config->has_audio !=
Maarten Lankhorst85a96e72015-06-01 12:49:53 +020012230 to_intel_crtc_state(crtc->state)->has_audio) {
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +020012231 pipe_config->base.mode_changed = true;
Maarten Lankhorst85a96e72015-06-01 12:49:53 +020012232 ret = drm_atomic_add_affected_planes(state, crtc);
12233 }
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +020012234
12235 /*
12236 * Note we have an issue here with infoframes: current code
12237 * only updates them on the full mode set path per hw
12238 * requirements. So here we should be checking for any
12239 * required changes and forcing a mode set.
12240 */
Daniel Vetter7758a112012-07-08 19:40:39 +020012241fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012242 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020012243}
12244
Daniel Vetterea9d7582012-07-10 10:42:52 +020012245static bool intel_crtc_in_use(struct drm_crtc *crtc)
12246{
12247 struct drm_encoder *encoder;
12248 struct drm_device *dev = crtc->dev;
12249
12250 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
12251 if (encoder->crtc == crtc)
12252 return true;
12253
12254 return false;
12255}
12256
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012257static void
12258intel_modeset_update_state(struct drm_atomic_state *state)
12259{
12260 struct drm_device *dev = state->dev;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012261 struct intel_encoder *intel_encoder;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012262 struct drm_crtc *crtc;
12263 struct drm_crtc_state *crtc_state;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012264 struct drm_connector *connector;
12265
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020012266 intel_shared_dpll_commit(state);
Daniel Vetterba41c0de2014-11-03 15:04:55 +010012267
Damien Lespiaub2784e12014-08-05 11:29:37 +010012268 for_each_intel_encoder(dev, intel_encoder) {
Daniel Vetterea9d7582012-07-10 10:42:52 +020012269 if (!intel_encoder->base.crtc)
12270 continue;
12271
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012272 crtc = intel_encoder->base.crtc;
12273 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
12274 if (!crtc_state || !needs_modeset(crtc->state))
12275 continue;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012276
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012277 intel_encoder->connectors_active = false;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012278 }
12279
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012280 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorstf7217902015-06-10 10:24:20 +020012281 intel_modeset_update_staged_output_state(state->dev);
Daniel Vetterea9d7582012-07-10 10:42:52 +020012282
Ville Syrjälä76688512014-01-10 11:28:06 +020012283 /* Double check state. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012284 for_each_crtc(dev, crtc) {
12285 WARN_ON(crtc->state->enable != intel_crtc_in_use(crtc));
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012286
12287 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020012288
12289 /* Update hwmode for vblank functions */
12290 if (crtc->state->active)
12291 crtc->hwmode = crtc->state->adjusted_mode;
12292 else
12293 crtc->hwmode.crtc_clock = 0;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012294 }
12295
12296 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
12297 if (!connector->encoder || !connector->encoder->crtc)
12298 continue;
12299
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012300 crtc = connector->encoder->crtc;
12301 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
12302 if (!crtc_state || !needs_modeset(crtc->state))
12303 continue;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012304
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020012305 if (crtc->state->active) {
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012306 struct drm_property *dpms_property =
12307 dev->mode_config.dpms_property;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012308
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012309 connector->dpms = DRM_MODE_DPMS_ON;
12310 drm_object_property_set_value(&connector->base, dpms_property, DRM_MODE_DPMS_ON);
Daniel Vetter68d34722012-09-06 22:08:35 +020012311
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012312 intel_encoder = to_intel_encoder(connector->encoder);
12313 intel_encoder->connectors_active = true;
12314 } else
12315 connector->dpms = DRM_MODE_DPMS_OFF;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012316 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012317}
12318
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012319static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012320{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012321 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012322
12323 if (clock1 == clock2)
12324 return true;
12325
12326 if (!clock1 || !clock2)
12327 return false;
12328
12329 diff = abs(clock1 - clock2);
12330
12331 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12332 return true;
12333
12334 return false;
12335}
12336
Daniel Vetter25c5b262012-07-08 22:08:04 +020012337#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12338 list_for_each_entry((intel_crtc), \
12339 &(dev)->mode_config.crtc_list, \
12340 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +020012341 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020012342
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012343static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012344intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012345 struct intel_crtc_state *current_config,
12346 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012347{
Daniel Vetter66e985c2013-06-05 13:34:20 +020012348#define PIPE_CONF_CHECK_X(name) \
12349 if (current_config->name != pipe_config->name) { \
12350 DRM_ERROR("mismatch in " #name " " \
12351 "(expected 0x%08x, found 0x%08x)\n", \
12352 current_config->name, \
12353 pipe_config->name); \
12354 return false; \
12355 }
12356
Daniel Vetter08a24032013-04-19 11:25:34 +020012357#define PIPE_CONF_CHECK_I(name) \
12358 if (current_config->name != pipe_config->name) { \
12359 DRM_ERROR("mismatch in " #name " " \
12360 "(expected %i, found %i)\n", \
12361 current_config->name, \
12362 pipe_config->name); \
12363 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010012364 }
12365
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012366/* This is required for BDW+ where there is only one set of registers for
12367 * switching between high and low RR.
12368 * This macro can be used whenever a comparison has to be made between one
12369 * hw state and multiple sw state variables.
12370 */
12371#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
12372 if ((current_config->name != pipe_config->name) && \
12373 (current_config->alt_name != pipe_config->name)) { \
12374 DRM_ERROR("mismatch in " #name " " \
12375 "(expected %i or %i, found %i)\n", \
12376 current_config->name, \
12377 current_config->alt_name, \
12378 pipe_config->name); \
12379 return false; \
12380 }
12381
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012382#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12383 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -070012384 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012385 "(expected %i, found %i)\n", \
12386 current_config->name & (mask), \
12387 pipe_config->name & (mask)); \
12388 return false; \
12389 }
12390
Ville Syrjälä5e550652013-09-06 23:29:07 +030012391#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12392 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
12393 DRM_ERROR("mismatch in " #name " " \
12394 "(expected %i, found %i)\n", \
12395 current_config->name, \
12396 pipe_config->name); \
12397 return false; \
12398 }
12399
Daniel Vetterbb760062013-06-06 14:55:52 +020012400#define PIPE_CONF_QUIRK(quirk) \
12401 ((current_config->quirks | pipe_config->quirks) & (quirk))
12402
Daniel Vettereccb1402013-05-22 00:50:22 +020012403 PIPE_CONF_CHECK_I(cpu_transcoder);
12404
Daniel Vetter08a24032013-04-19 11:25:34 +020012405 PIPE_CONF_CHECK_I(has_pch_encoder);
12406 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +020012407 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
12408 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
12409 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
12410 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
12411 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +020012412
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012413 PIPE_CONF_CHECK_I(has_dp_encoder);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012414
12415 if (INTEL_INFO(dev)->gen < 8) {
12416 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
12417 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
12418 PIPE_CONF_CHECK_I(dp_m_n.link_m);
12419 PIPE_CONF_CHECK_I(dp_m_n.link_n);
12420 PIPE_CONF_CHECK_I(dp_m_n.tu);
12421
12422 if (current_config->has_drrs) {
12423 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
12424 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
12425 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
12426 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
12427 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
12428 }
12429 } else {
12430 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
12431 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
12432 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
12433 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
12434 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
12435 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012436
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012437 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12438 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12439 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12440 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12441 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12442 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012443
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012444 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12445 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12446 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12447 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12448 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12449 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012450
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012451 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b52014-04-24 23:54:47 +020012452 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012453 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
12454 IS_VALLEYVIEW(dev))
12455 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012456 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012457
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012458 PIPE_CONF_CHECK_I(has_audio);
12459
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012460 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012461 DRM_MODE_FLAG_INTERLACE);
12462
Daniel Vetterbb760062013-06-06 14:55:52 +020012463 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012464 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012465 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012466 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012467 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012468 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012469 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012470 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012471 DRM_MODE_FLAG_NVSYNC);
12472 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012473
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012474 PIPE_CONF_CHECK_I(pipe_src_w);
12475 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012476
Daniel Vetter99535992014-04-13 12:00:33 +020012477 /*
12478 * FIXME: BIOS likes to set up a cloned config with lvds+external
12479 * screen. Since we don't yet re-compute the pipe config when moving
12480 * just the lvds port away to another pipe the sw tracking won't match.
12481 *
12482 * Proper atomic modesets with recomputed global state will fix this.
12483 * Until then just don't check gmch state for inherited modes.
12484 */
12485 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
12486 PIPE_CONF_CHECK_I(gmch_pfit.control);
12487 /* pfit ratios are autocomputed by the hw on gen4+ */
12488 if (INTEL_INFO(dev)->gen < 4)
12489 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
12490 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
12491 }
12492
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012493 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12494 if (current_config->pch_pfit.enabled) {
12495 PIPE_CONF_CHECK_I(pch_pfit.pos);
12496 PIPE_CONF_CHECK_I(pch_pfit.size);
12497 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012498
Chandra Kondurua1b22782015-04-07 15:28:45 -070012499 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12500
Jesse Barnese59150d2014-01-07 13:30:45 -080012501 /* BDW+ don't expose a synchronous way to read the state */
12502 if (IS_HASWELL(dev))
12503 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012504
Ville Syrjälä282740f2013-09-04 18:30:03 +030012505 PIPE_CONF_CHECK_I(double_wide);
12506
Daniel Vetter26804af2014-06-25 22:01:55 +030012507 PIPE_CONF_CHECK_X(ddi_pll_sel);
12508
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012509 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012510 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012511 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012512 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12513 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012514 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012515 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12516 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12517 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012518
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012519 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12520 PIPE_CONF_CHECK_I(pipe_bpp);
12521
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012522 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012523 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012524
Daniel Vetter66e985c2013-06-05 13:34:20 +020012525#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012526#undef PIPE_CONF_CHECK_I
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012527#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012528#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012529#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012530#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012531
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012532 return true;
12533}
12534
Damien Lespiau08db6652014-11-04 17:06:52 +000012535static void check_wm_state(struct drm_device *dev)
12536{
12537 struct drm_i915_private *dev_priv = dev->dev_private;
12538 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12539 struct intel_crtc *intel_crtc;
12540 int plane;
12541
12542 if (INTEL_INFO(dev)->gen < 9)
12543 return;
12544
12545 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12546 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12547
12548 for_each_intel_crtc(dev, intel_crtc) {
12549 struct skl_ddb_entry *hw_entry, *sw_entry;
12550 const enum pipe pipe = intel_crtc->pipe;
12551
12552 if (!intel_crtc->active)
12553 continue;
12554
12555 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000012556 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012557 hw_entry = &hw_ddb.plane[pipe][plane];
12558 sw_entry = &sw_ddb->plane[pipe][plane];
12559
12560 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12561 continue;
12562
12563 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12564 "(expected (%u,%u), found (%u,%u))\n",
12565 pipe_name(pipe), plane + 1,
12566 sw_entry->start, sw_entry->end,
12567 hw_entry->start, hw_entry->end);
12568 }
12569
12570 /* cursor */
12571 hw_entry = &hw_ddb.cursor[pipe];
12572 sw_entry = &sw_ddb->cursor[pipe];
12573
12574 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12575 continue;
12576
12577 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12578 "(expected (%u,%u), found (%u,%u))\n",
12579 pipe_name(pipe),
12580 sw_entry->start, sw_entry->end,
12581 hw_entry->start, hw_entry->end);
12582 }
12583}
12584
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012585static void
12586check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012587{
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012588 struct intel_connector *connector;
12589
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012590 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012591 /* This also checks the encoder/connector hw state with the
12592 * ->get_hw_state callbacks. */
12593 intel_connector_check_state(connector);
12594
Rob Clarke2c719b2014-12-15 13:56:32 -050012595 I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012596 "connector's staged encoder doesn't match current encoder\n");
12597 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012598}
12599
12600static void
12601check_encoder_state(struct drm_device *dev)
12602{
12603 struct intel_encoder *encoder;
12604 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012605
Damien Lespiaub2784e12014-08-05 11:29:37 +010012606 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012607 bool enabled = false;
12608 bool active = false;
12609 enum pipe pipe, tracked_pipe;
12610
12611 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12612 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012613 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012614
Rob Clarke2c719b2014-12-15 13:56:32 -050012615 I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012616 "encoder's stage crtc doesn't match current crtc\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012617 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012618 "encoder's active_connectors set, but no crtc\n");
12619
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012620 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012621 if (connector->base.encoder != &encoder->base)
12622 continue;
12623 enabled = true;
12624 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
12625 active = true;
12626 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012627 /*
12628 * for MST connectors if we unplug the connector is gone
12629 * away but the encoder is still connected to a crtc
12630 * until a modeset happens in response to the hotplug.
12631 */
12632 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
12633 continue;
12634
Rob Clarke2c719b2014-12-15 13:56:32 -050012635 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012636 "encoder's enabled state mismatch "
12637 "(expected %i, found %i)\n",
12638 !!encoder->base.crtc, enabled);
Rob Clarke2c719b2014-12-15 13:56:32 -050012639 I915_STATE_WARN(active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012640 "active encoder with no crtc\n");
12641
Rob Clarke2c719b2014-12-15 13:56:32 -050012642 I915_STATE_WARN(encoder->connectors_active != active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012643 "encoder's computed active state doesn't match tracked active state "
12644 "(expected %i, found %i)\n", active, encoder->connectors_active);
12645
12646 active = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -050012647 I915_STATE_WARN(active != encoder->connectors_active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012648 "encoder's hw state doesn't match sw tracking "
12649 "(expected %i, found %i)\n",
12650 encoder->connectors_active, active);
12651
12652 if (!encoder->base.crtc)
12653 continue;
12654
12655 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
Rob Clarke2c719b2014-12-15 13:56:32 -050012656 I915_STATE_WARN(active && pipe != tracked_pipe,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012657 "active encoder's pipe doesn't match"
12658 "(expected %i, found %i)\n",
12659 tracked_pipe, pipe);
12660
12661 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012662}
12663
12664static void
12665check_crtc_state(struct drm_device *dev)
12666{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012667 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012668 struct intel_crtc *crtc;
12669 struct intel_encoder *encoder;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012670 struct intel_crtc_state pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012671
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012672 for_each_intel_crtc(dev, crtc) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012673 bool enabled = false;
12674 bool active = false;
12675
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012676 memset(&pipe_config, 0, sizeof(pipe_config));
12677
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012678 DRM_DEBUG_KMS("[CRTC:%d]\n",
12679 crtc->base.base.id);
12680
Matt Roper83d65732015-02-25 13:12:16 -080012681 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012682 "active crtc, but not enabled in sw tracking\n");
12683
Damien Lespiaub2784e12014-08-05 11:29:37 +010012684 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012685 if (encoder->base.crtc != &crtc->base)
12686 continue;
12687 enabled = true;
12688 if (encoder->connectors_active)
12689 active = true;
12690 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020012691
Rob Clarke2c719b2014-12-15 13:56:32 -050012692 I915_STATE_WARN(active != crtc->active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012693 "crtc's computed active state doesn't match tracked active state "
12694 "(expected %i, found %i)\n", active, crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080012695 I915_STATE_WARN(enabled != crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012696 "crtc's computed enabled state doesn't match tracked enabled state "
Matt Roper83d65732015-02-25 13:12:16 -080012697 "(expected %i, found %i)\n", enabled,
12698 crtc->base.state->enable);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012699
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012700 active = dev_priv->display.get_pipe_config(crtc,
12701 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +020012702
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030012703 /* hw state is inconsistent with the pipe quirk */
12704 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12705 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetterd62cf622013-05-29 10:41:29 +020012706 active = crtc->active;
12707
Damien Lespiaub2784e12014-08-05 11:29:37 +010012708 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +030012709 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +020012710 if (encoder->base.crtc != &crtc->base)
12711 continue;
Daniel Vetter1d37b682013-11-18 09:00:59 +010012712 if (encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +020012713 encoder->get_config(encoder, &pipe_config);
12714 }
12715
Rob Clarke2c719b2014-12-15 13:56:32 -050012716 I915_STATE_WARN(crtc->active != active,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012717 "crtc active state doesn't match with hw state "
12718 "(expected %i, found %i)\n", crtc->active, active);
12719
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020012720 I915_STATE_WARN(crtc->active != crtc->base.state->active,
12721 "transitional active state does not match atomic hw state "
12722 "(expected %i, found %i)\n", crtc->base.state->active, crtc->active);
12723
Daniel Vetterc0b03412013-05-28 12:05:54 +020012724 if (active &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012725 !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050012726 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Daniel Vetterc0b03412013-05-28 12:05:54 +020012727 intel_dump_pipe_config(crtc, &pipe_config,
12728 "[hw state]");
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012729 intel_dump_pipe_config(crtc, crtc->config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012730 "[sw state]");
12731 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012732 }
12733}
12734
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012735static void
12736check_shared_dpll_state(struct drm_device *dev)
12737{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012738 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012739 struct intel_crtc *crtc;
12740 struct intel_dpll_hw_state dpll_hw_state;
12741 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020012742
12743 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12744 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12745 int enabled_crtcs = 0, active_crtcs = 0;
12746 bool active;
12747
12748 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12749
12750 DRM_DEBUG_KMS("%s\n", pll->name);
12751
12752 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
12753
Rob Clarke2c719b2014-12-15 13:56:32 -050012754 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
Daniel Vetter53589012013-06-05 13:34:16 +020012755 "more active pll users than references: %i vs %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012756 pll->active, hweight32(pll->config.crtc_mask));
Rob Clarke2c719b2014-12-15 13:56:32 -050012757 I915_STATE_WARN(pll->active && !pll->on,
Daniel Vetter53589012013-06-05 13:34:16 +020012758 "pll in active use but not on in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012759 I915_STATE_WARN(pll->on && !pll->active,
Daniel Vetter35c95372013-07-17 06:55:04 +020012760 "pll in on but not on in use in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012761 I915_STATE_WARN(pll->on != active,
Daniel Vetter53589012013-06-05 13:34:16 +020012762 "pll on state mismatch (expected %i, found %i)\n",
12763 pll->on, active);
12764
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012765 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080012766 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
Daniel Vetter53589012013-06-05 13:34:16 +020012767 enabled_crtcs++;
12768 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12769 active_crtcs++;
12770 }
Rob Clarke2c719b2014-12-15 13:56:32 -050012771 I915_STATE_WARN(pll->active != active_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012772 "pll active crtcs mismatch (expected %i, found %i)\n",
12773 pll->active, active_crtcs);
Rob Clarke2c719b2014-12-15 13:56:32 -050012774 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012775 "pll enabled crtcs mismatch (expected %i, found %i)\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012776 hweight32(pll->config.crtc_mask), enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012777
Rob Clarke2c719b2014-12-15 13:56:32 -050012778 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020012779 sizeof(dpll_hw_state)),
12780 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020012781 }
Daniel Vettere2e1ed42012-07-08 21:14:38 +020012782}
12783
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012784void
12785intel_modeset_check_state(struct drm_device *dev)
12786{
Damien Lespiau08db6652014-11-04 17:06:52 +000012787 check_wm_state(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012788 check_connector_state(dev);
12789 check_encoder_state(dev);
12790 check_crtc_state(dev);
12791 check_shared_dpll_state(dev);
12792}
12793
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012794void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
Ville Syrjälä18442d02013-09-13 16:00:08 +030012795 int dotclock)
12796{
12797 /*
12798 * FDI already provided one idea for the dotclock.
12799 * Yell if the encoder disagrees.
12800 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012801 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030012802 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012803 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030012804}
12805
Ville Syrjälä80715b22014-05-15 20:23:23 +030012806static void update_scanline_offset(struct intel_crtc *crtc)
12807{
12808 struct drm_device *dev = crtc->base.dev;
12809
12810 /*
12811 * The scanline counter increments at the leading edge of hsync.
12812 *
12813 * On most platforms it starts counting from vtotal-1 on the
12814 * first active line. That means the scanline counter value is
12815 * always one less than what we would expect. Ie. just after
12816 * start of vblank, which also occurs at start of hsync (on the
12817 * last active line), the scanline counter will read vblank_start-1.
12818 *
12819 * On gen2 the scanline counter starts counting from 1 instead
12820 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12821 * to keep the value positive), instead of adding one.
12822 *
12823 * On HSW+ the behaviour of the scanline counter depends on the output
12824 * type. For DP ports it behaves like most other platforms, but on HDMI
12825 * there's an extra 1 line difference. So we need to add two instead of
12826 * one to the value.
12827 */
12828 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012829 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012830 int vtotal;
12831
12832 vtotal = mode->crtc_vtotal;
12833 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
12834 vtotal /= 2;
12835
12836 crtc->scanline_offset = vtotal - 1;
12837 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030012838 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030012839 crtc->scanline_offset = 2;
12840 } else
12841 crtc->scanline_offset = 1;
12842}
12843
Maarten Lankhorstad421372015-06-15 12:33:42 +020012844static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012845{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012846 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012847 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstad421372015-06-15 12:33:42 +020012848 struct intel_shared_dpll_config *shared_dpll = NULL;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012849 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012850 struct intel_crtc_state *intel_crtc_state;
12851 struct drm_crtc *crtc;
12852 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012853 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012854
12855 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020012856 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012857
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012858 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020012859 int dpll;
12860
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012861 intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012862 intel_crtc_state = to_intel_crtc_state(crtc_state);
Maarten Lankhorstad421372015-06-15 12:33:42 +020012863 dpll = intel_crtc_state->shared_dpll;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012864
Maarten Lankhorstad421372015-06-15 12:33:42 +020012865 if (!needs_modeset(crtc_state) || dpll == DPLL_ID_PRIVATE)
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012866 continue;
12867
Maarten Lankhorstad421372015-06-15 12:33:42 +020012868 intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012869
Maarten Lankhorstad421372015-06-15 12:33:42 +020012870 if (!shared_dpll)
12871 shared_dpll = intel_atomic_get_shared_dpll_state(state);
12872
12873 shared_dpll[dpll].crtc_mask &= ~(1 << intel_crtc->pipe);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012874 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012875}
12876
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020012877/*
12878 * This implements the workaround described in the "notes" section of the mode
12879 * set sequence documentation. When going from no pipes or single pipe to
12880 * multiple pipes, and planes are enabled after the pipe, we need to wait at
12881 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
12882 */
12883static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
12884{
12885 struct drm_crtc_state *crtc_state;
12886 struct intel_crtc *intel_crtc;
12887 struct drm_crtc *crtc;
12888 struct intel_crtc_state *first_crtc_state = NULL;
12889 struct intel_crtc_state *other_crtc_state = NULL;
12890 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
12891 int i;
12892
12893 /* look at all crtc's that are going to be enabled in during modeset */
12894 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12895 intel_crtc = to_intel_crtc(crtc);
12896
12897 if (!crtc_state->active || !needs_modeset(crtc_state))
12898 continue;
12899
12900 if (first_crtc_state) {
12901 other_crtc_state = to_intel_crtc_state(crtc_state);
12902 break;
12903 } else {
12904 first_crtc_state = to_intel_crtc_state(crtc_state);
12905 first_pipe = intel_crtc->pipe;
12906 }
12907 }
12908
12909 /* No workaround needed? */
12910 if (!first_crtc_state)
12911 return 0;
12912
12913 /* w/a possibly needed, check how many crtc's are already enabled. */
12914 for_each_intel_crtc(state->dev, intel_crtc) {
12915 struct intel_crtc_state *pipe_config;
12916
12917 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
12918 if (IS_ERR(pipe_config))
12919 return PTR_ERR(pipe_config);
12920
12921 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
12922
12923 if (!pipe_config->base.active ||
12924 needs_modeset(&pipe_config->base))
12925 continue;
12926
12927 /* 2 or more enabled crtcs means no need for w/a */
12928 if (enabled_pipe != INVALID_PIPE)
12929 return 0;
12930
12931 enabled_pipe = intel_crtc->pipe;
12932 }
12933
12934 if (enabled_pipe != INVALID_PIPE)
12935 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
12936 else if (other_crtc_state)
12937 other_crtc_state->hsw_workaround_pipe = first_pipe;
12938
12939 return 0;
12940}
12941
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012942static int intel_modeset_all_pipes(struct drm_atomic_state *state)
12943{
12944 struct drm_crtc *crtc;
12945 struct drm_crtc_state *crtc_state;
12946 int ret = 0;
12947
12948 /* add all active pipes to the state */
12949 for_each_crtc(state->dev, crtc) {
12950 crtc_state = drm_atomic_get_crtc_state(state, crtc);
12951 if (IS_ERR(crtc_state))
12952 return PTR_ERR(crtc_state);
12953
12954 if (!crtc_state->active || needs_modeset(crtc_state))
12955 continue;
12956
12957 crtc_state->mode_changed = true;
12958
12959 ret = drm_atomic_add_affected_connectors(state, crtc);
12960 if (ret)
12961 break;
12962
12963 ret = drm_atomic_add_affected_planes(state, crtc);
12964 if (ret)
12965 break;
12966 }
12967
12968 return ret;
12969}
12970
12971
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012972/* Code that should eventually be part of atomic_check() */
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012973static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012974{
12975 struct drm_device *dev = state->dev;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012976 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012977 int ret;
12978
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012979 if (!check_digital_port_conflicts(state)) {
12980 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
12981 return -EINVAL;
12982 }
12983
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012984 /*
12985 * See if the config requires any additional preparation, e.g.
12986 * to adjust global state with pipes off. We need to do this
12987 * here so we can get the modeset_pipe updated config for the new
12988 * mode set on this crtc. For other crtcs we need to use the
12989 * adjusted_mode bits in the crtc directly.
12990 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012991 if (dev_priv->display.modeset_calc_cdclk) {
12992 unsigned int cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030012993
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012994 ret = dev_priv->display.modeset_calc_cdclk(state);
12995
12996 cdclk = to_intel_atomic_state(state)->cdclk;
12997 if (!ret && cdclk != dev_priv->cdclk_freq)
12998 ret = intel_modeset_all_pipes(state);
12999
13000 if (ret < 0)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013001 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013002 } else
13003 to_intel_atomic_state(state)->cdclk = dev_priv->cdclk_freq;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013004
Maarten Lankhorstad421372015-06-15 12:33:42 +020013005 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013006
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013007 if (IS_HASWELL(dev))
Maarten Lankhorstad421372015-06-15 12:33:42 +020013008 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013009
Maarten Lankhorstad421372015-06-15 12:33:42 +020013010 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013011}
13012
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013013static int
13014intel_modeset_compute_config(struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013015{
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013016 struct drm_crtc *crtc;
13017 struct drm_crtc_state *crtc_state;
13018 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013019 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013020
13021 ret = drm_atomic_helper_check_modeset(state->dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013022 if (ret)
13023 return ret;
13024
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013025 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013026 if (!crtc_state->enable) {
13027 if (needs_modeset(crtc_state))
13028 any_ms = true;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013029 continue;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013030 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013031
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020013032 if (to_intel_crtc_state(crtc_state)->quirks &
13033 PIPE_CONFIG_QUIRK_INITIAL_PLANES) {
13034 ret = drm_atomic_add_affected_planes(state, crtc);
13035 if (ret)
13036 return ret;
13037
13038 /*
13039 * We ought to handle i915.fastboot here.
13040 * If no modeset is required and the primary plane has
13041 * a fb, update the members of crtc_state as needed,
13042 * and run the necessary updates during vblank evasion.
13043 */
13044 }
13045
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013046 if (!needs_modeset(crtc_state)) {
13047 ret = drm_atomic_add_affected_connectors(state, crtc);
13048 if (ret)
13049 return ret;
13050 }
13051
13052 ret = intel_modeset_pipe_config(crtc,
13053 to_intel_crtc_state(crtc_state));
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013054 if (ret)
13055 return ret;
13056
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013057 if (needs_modeset(crtc_state))
13058 any_ms = true;
13059
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013060 intel_dump_pipe_config(to_intel_crtc(crtc),
13061 to_intel_crtc_state(crtc_state),
13062 "[modeset]");
13063 }
13064
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013065 if (any_ms) {
13066 ret = intel_modeset_checks(state);
13067
13068 if (ret)
13069 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013070 } else
13071 to_intel_atomic_state(state)->cdclk =
13072 to_i915(state->dev)->cdclk_freq;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013073
13074 return drm_atomic_helper_check_planes(state->dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013075}
13076
Ander Conselvan de Oliveirac72d9692015-06-01 12:49:50 +020013077static int __intel_set_mode(struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013078{
Ander Conselvan de Oliveirac72d9692015-06-01 12:49:50 +020013079 struct drm_device *dev = state->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030013080 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013081 struct drm_crtc *crtc;
13082 struct drm_crtc_state *crtc_state;
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013083 int ret = 0;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013084 int i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013085 bool any_ms = false;
Daniel Vettera6778b32012-07-02 09:56:42 +020013086
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013087 ret = drm_atomic_helper_prepare_planes(dev, state);
13088 if (ret)
13089 return ret;
13090
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013091 drm_atomic_helper_swap_state(dev, state);
13092
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013093 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013094 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13095
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013096 if (!needs_modeset(crtc->state))
13097 continue;
13098
13099 any_ms = true;
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013100 intel_pre_plane_update(intel_crtc);
Daniel Vetter460da9162013-03-27 00:44:51 +010013101
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013102 if (crtc_state->active) {
13103 intel_crtc_disable_planes(crtc, crtc_state->plane_mask);
13104 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013105 intel_crtc->active = false;
13106 intel_disable_shared_dpll(intel_crtc);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013107 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010013108 }
Daniel Vetter7758a112012-07-08 19:40:39 +020013109
Daniel Vetterea9d7582012-07-10 10:42:52 +020013110 /* Only after disabling all output pipelines that will be changed can we
13111 * update the the output configuration. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013112 intel_modeset_update_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020013113
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030013114 /* The state has been swaped above, so state actually contains the
13115 * old state now. */
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013116 if (any_ms)
13117 modeset_update_crtc_power_domains(state);
Daniel Vetter47fab732012-10-26 10:58:18 +020013118
Daniel Vettera6778b32012-07-02 09:56:42 +020013119 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013120 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013121 if (needs_modeset(crtc->state) && crtc->state->active) {
13122 update_scanline_offset(to_intel_crtc(crtc));
13123 dev_priv->display.crtc_enable(crtc);
13124 }
13125
Maarten Lankhorst5ac1c4b2015-06-01 12:50:01 +020013126 drm_atomic_helper_commit_planes_on_crtc(crtc_state);
Ville Syrjälä80715b22014-05-15 20:23:23 +030013127 }
Daniel Vettera6778b32012-07-02 09:56:42 +020013128
Daniel Vettera6778b32012-07-02 09:56:42 +020013129 /* FIXME: add subpixel order */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013130
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013131 drm_atomic_helper_cleanup_planes(dev, state);
13132
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013133 drm_atomic_state_free(state);
13134
Ander Conselvan de Oliveira9eb45f22015-04-21 17:13:07 +030013135 return 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020013136}
13137
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013138static int intel_set_mode_checked(struct drm_atomic_state *state)
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013139{
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013140 struct drm_device *dev = state->dev;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013141 int ret;
13142
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013143 ret = __intel_set_mode(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013144 if (ret == 0)
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013145 intel_modeset_check_state(dev);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013146
13147 return ret;
13148}
13149
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013150static int intel_set_mode(struct drm_atomic_state *state)
Daniel Vetterf30da182013-04-11 20:22:50 +020013151{
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013152 int ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020013153
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013154 ret = intel_modeset_compute_config(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013155 if (ret)
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013156 return ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013157
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013158 return intel_set_mode_checked(state);
Daniel Vetterf30da182013-04-11 20:22:50 +020013159}
13160
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013161void intel_crtc_restore_mode(struct drm_crtc *crtc)
13162{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013163 struct drm_device *dev = crtc->dev;
13164 struct drm_atomic_state *state;
13165 struct intel_encoder *encoder;
13166 struct intel_connector *connector;
13167 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013168 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013169 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013170
13171 state = drm_atomic_state_alloc(dev);
13172 if (!state) {
13173 DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
13174 crtc->base.id);
13175 return;
13176 }
13177
13178 state->acquire_ctx = dev->mode_config.acquire_ctx;
13179
13180 /* The force restore path in the HW readout code relies on the staged
13181 * config still keeping the user requested config while the actual
13182 * state has been overwritten by the configuration read from HW. We
13183 * need to copy the staged config to the atomic state, otherwise the
13184 * mode set will just reapply the state the HW is already in. */
13185 for_each_intel_encoder(dev, encoder) {
13186 if (&encoder->new_crtc->base != crtc)
13187 continue;
13188
13189 for_each_intel_connector(dev, connector) {
13190 if (connector->new_encoder != encoder)
13191 continue;
13192
13193 connector_state = drm_atomic_get_connector_state(state, &connector->base);
13194 if (IS_ERR(connector_state)) {
13195 DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
13196 connector->base.base.id,
13197 connector->base.name,
13198 PTR_ERR(connector_state));
13199 continue;
13200 }
13201
13202 connector_state->crtc = crtc;
13203 connector_state->best_encoder = &encoder->base;
13204 }
13205 }
13206
Ander Conselvan de Oliveira4ed9fb32015-06-16 11:49:45 +030013207 crtc_state = intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));
13208 if (IS_ERR(crtc_state)) {
13209 DRM_DEBUG_KMS("Failed to add [CRTC:%d] to state: %ld\n",
13210 crtc->base.id, PTR_ERR(crtc_state));
13211 drm_atomic_state_free(state);
13212 return;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013213 }
13214
Ander Conselvan de Oliveira4ed9fb32015-06-16 11:49:45 +030013215 crtc_state->base.active = crtc_state->base.enable =
13216 to_intel_crtc(crtc)->new_enabled;
13217
13218 drm_mode_copy(&crtc_state->base.mode, &crtc->mode);
13219
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030013220 intel_modeset_setup_plane_state(state, crtc, &crtc->mode,
13221 crtc->primary->fb, crtc->x, crtc->y);
13222
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013223 ret = intel_set_mode(state);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013224 if (ret)
13225 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013226}
13227
Daniel Vetter25c5b262012-07-08 22:08:04 +020013228#undef for_each_intel_crtc_masked
13229
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013230static bool intel_connector_in_mode_set(struct intel_connector *connector,
13231 struct drm_mode_set *set)
13232{
13233 int ro;
13234
13235 for (ro = 0; ro < set->num_connectors; ro++)
13236 if (set->connectors[ro] == &connector->base)
13237 return true;
13238
13239 return false;
13240}
13241
Daniel Vetter2e431052012-07-04 22:42:15 +020013242static int
Daniel Vetter9a935852012-07-05 22:34:27 +020013243intel_modeset_stage_output_state(struct drm_device *dev,
13244 struct drm_mode_set *set,
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020013245 struct drm_atomic_state *state)
Daniel Vetter50f56112012-07-02 09:35:43 +020013246{
Daniel Vetter9a935852012-07-05 22:34:27 +020013247 struct intel_connector *connector;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013248 struct drm_connector *drm_connector;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020013249 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013250 struct drm_crtc *crtc;
13251 struct drm_crtc_state *crtc_state;
13252 int i, ret;
Daniel Vetter50f56112012-07-02 09:35:43 +020013253
Damien Lespiau9abdda72013-02-13 13:29:23 +000013254 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +020013255 * of connectors. For paranoia, double-check this. */
13256 WARN_ON(!set->fb && (set->num_connectors != 0));
13257 WARN_ON(set->fb && (set->num_connectors == 0));
13258
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020013259 for_each_intel_connector(dev, connector) {
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013260 bool in_mode_set = intel_connector_in_mode_set(connector, set);
13261
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013262 if (!in_mode_set && connector->base.state->crtc != set->crtc)
13263 continue;
13264
13265 connector_state =
13266 drm_atomic_get_connector_state(state, &connector->base);
13267 if (IS_ERR(connector_state))
13268 return PTR_ERR(connector_state);
13269
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013270 if (in_mode_set) {
13271 int pipe = to_intel_crtc(set->crtc)->pipe;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013272 connector_state->best_encoder =
13273 &intel_find_encoder(connector, pipe)->base;
Daniel Vetter50f56112012-07-02 09:35:43 +020013274 }
13275
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013276 if (connector->base.state->crtc != set->crtc)
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013277 continue;
13278
Daniel Vetter9a935852012-07-05 22:34:27 +020013279 /* If we disable the crtc, disable all its connectors. Also, if
13280 * the connector is on the changing crtc but not on the new
13281 * connector list, disable it. */
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013282 if (!set->fb || !in_mode_set) {
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013283 connector_state->best_encoder = NULL;
Daniel Vetter9a935852012-07-05 22:34:27 +020013284
13285 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
13286 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030013287 connector->base.name);
Daniel Vetter9a935852012-07-05 22:34:27 +020013288 }
Daniel Vetter9a935852012-07-05 22:34:27 +020013289 }
13290 /* connector->new_encoder is now updated for all connectors. */
13291
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013292 for_each_connector_in_state(state, drm_connector, connector_state, i) {
13293 connector = to_intel_connector(drm_connector);
Ville Syrjälä76688512014-01-10 11:28:06 +020013294
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013295 if (!connector_state->best_encoder) {
13296 ret = drm_atomic_set_crtc_for_connector(connector_state,
13297 NULL);
13298 if (ret)
13299 return ret;
13300
Daniel Vetter50f56112012-07-02 09:35:43 +020013301 continue;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013302 }
Daniel Vetter50f56112012-07-02 09:35:43 +020013303
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013304 if (intel_connector_in_mode_set(connector, set)) {
13305 struct drm_crtc *crtc = connector->base.state->crtc;
13306
13307 /* If this connector was in a previous crtc, add it
13308 * to the state. We might need to disable it. */
13309 if (crtc) {
13310 crtc_state =
13311 drm_atomic_get_crtc_state(state, crtc);
13312 if (IS_ERR(crtc_state))
13313 return PTR_ERR(crtc_state);
13314 }
13315
13316 ret = drm_atomic_set_crtc_for_connector(connector_state,
13317 set->crtc);
13318 if (ret)
13319 return ret;
13320 }
Daniel Vetter50f56112012-07-02 09:35:43 +020013321
13322 /* Make sure the new CRTC will work with the encoder */
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013323 if (!drm_encoder_crtc_ok(connector_state->best_encoder,
13324 connector_state->crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020013325 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +020013326 }
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020013327
Daniel Vetter9a935852012-07-05 22:34:27 +020013328 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
13329 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030013330 connector->base.name,
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013331 connector_state->crtc->base.id);
13332
13333 if (connector_state->best_encoder != &connector->encoder->base)
13334 connector->encoder =
13335 to_intel_encoder(connector_state->best_encoder);
Daniel Vetter9a935852012-07-05 22:34:27 +020013336 }
13337
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013338 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020013339 bool has_connectors;
13340
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013341 ret = drm_atomic_add_affected_connectors(state, crtc);
13342 if (ret)
13343 return ret;
Paulo Zanoni5a65f352014-01-07 14:55:53 -020013344
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020013345 has_connectors = !!drm_atomic_connectors_for_crtc(state, crtc);
13346 if (has_connectors != crtc_state->enable)
13347 crtc_state->enable =
13348 crtc_state->active = has_connectors;
Ville Syrjälä76688512014-01-10 11:28:06 +020013349 }
13350
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030013351 ret = intel_modeset_setup_plane_state(state, set->crtc, set->mode,
13352 set->fb, set->x, set->y);
13353 if (ret)
13354 return ret;
13355
13356 crtc_state = drm_atomic_get_crtc_state(state, set->crtc);
13357 if (IS_ERR(crtc_state))
13358 return PTR_ERR(crtc_state);
13359
Matt Roperce522992015-06-05 15:08:24 -070013360 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
13361 if (ret)
13362 return ret;
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030013363
13364 if (set->num_connectors)
13365 crtc_state->active = true;
13366
Daniel Vetter2e431052012-07-04 22:42:15 +020013367 return 0;
13368}
13369
13370static int intel_crtc_set_config(struct drm_mode_set *set)
13371{
13372 struct drm_device *dev;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013373 struct drm_atomic_state *state = NULL;
Daniel Vetter2e431052012-07-04 22:42:15 +020013374 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +020013375
Daniel Vetter8d3e3752012-07-05 16:09:09 +020013376 BUG_ON(!set);
13377 BUG_ON(!set->crtc);
13378 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +020013379
Daniel Vetter7e53f3a2013-01-21 10:52:17 +010013380 /* Enforce sane interface api - has been abused by the fb helper. */
13381 BUG_ON(!set->mode && set->fb);
13382 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +020013383
Daniel Vetter2e431052012-07-04 22:42:15 +020013384 if (set->fb) {
13385 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
13386 set->crtc->base.id, set->fb->base.id,
13387 (int)set->num_connectors, set->x, set->y);
13388 } else {
13389 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +020013390 }
13391
13392 dev = set->crtc->dev;
13393
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013394 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013395 if (!state)
13396 return -ENOMEM;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013397
13398 state->acquire_ctx = dev->mode_config.acquire_ctx;
13399
Ander Conselvan de Oliveira462a4252015-04-21 17:13:00 +030013400 ret = intel_modeset_stage_output_state(dev, set, state);
Daniel Vetter2e431052012-07-04 22:42:15 +020013401 if (ret)
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013402 goto out;
Daniel Vetter2e431052012-07-04 22:42:15 +020013403
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013404 ret = intel_modeset_compute_config(state);
13405 if (ret)
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013406 goto out;
Jesse Barnes50f52752014-11-07 13:11:00 -080013407
Jesse Barnes1f9954d2014-11-05 14:26:10 -080013408 intel_update_pipe_size(to_intel_crtc(set->crtc));
13409
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013410 ret = intel_set_mode_checked(state);
Chris Wilson2d05eae2013-05-03 17:36:25 +010013411 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +020013412 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
13413 set->crtc->base.id, ret);
Chris Wilson2d05eae2013-05-03 17:36:25 +010013414 }
Daniel Vetter50f56112012-07-02 09:35:43 +020013415
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013416out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013417 if (ret)
13418 drm_atomic_state_free(state);
Daniel Vetter50f56112012-07-02 09:35:43 +020013419 return ret;
13420}
13421
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013422static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013423 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +020013424 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013425 .destroy = intel_crtc_destroy,
13426 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013427 .atomic_duplicate_state = intel_crtc_duplicate_state,
13428 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013429};
13430
Daniel Vetter53589012013-06-05 13:34:16 +020013431static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
13432 struct intel_shared_dpll *pll,
13433 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013434{
Daniel Vetter53589012013-06-05 13:34:16 +020013435 uint32_t val;
13436
Daniel Vetterf458ebb2014-09-30 10:56:39 +020013437 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030013438 return false;
13439
Daniel Vetter53589012013-06-05 13:34:16 +020013440 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020013441 hw_state->dpll = val;
13442 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
13443 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020013444
13445 return val & DPLL_VCO_ENABLE;
13446}
13447
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013448static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
13449 struct intel_shared_dpll *pll)
13450{
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013451 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
13452 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013453}
13454
Daniel Vettere7b903d2013-06-05 13:34:14 +020013455static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
13456 struct intel_shared_dpll *pll)
13457{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013458 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020013459 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020013460
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013461 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013462
13463 /* Wait for the clocks to stabilize. */
13464 POSTING_READ(PCH_DPLL(pll->id));
13465 udelay(150);
13466
13467 /* The pixel multiplier can only be updated once the
13468 * DPLL is enabled and the clocks are stable.
13469 *
13470 * So write it again.
13471 */
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013472 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013473 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013474 udelay(200);
13475}
13476
13477static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
13478 struct intel_shared_dpll *pll)
13479{
13480 struct drm_device *dev = dev_priv->dev;
13481 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013482
13483 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013484 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020013485 if (intel_crtc_to_shared_dpll(crtc) == pll)
13486 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
13487 }
13488
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013489 I915_WRITE(PCH_DPLL(pll->id), 0);
13490 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013491 udelay(200);
13492}
13493
Daniel Vetter46edb022013-06-05 13:34:12 +020013494static char *ibx_pch_dpll_names[] = {
13495 "PCH DPLL A",
13496 "PCH DPLL B",
13497};
13498
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013499static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013500{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013501 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013502 int i;
13503
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013504 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013505
Daniel Vettere72f9fb2013-06-05 13:34:06 +020013506 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020013507 dev_priv->shared_dplls[i].id = i;
13508 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013509 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013510 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
13511 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020013512 dev_priv->shared_dplls[i].get_hw_state =
13513 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013514 }
13515}
13516
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013517static void intel_shared_dpll_init(struct drm_device *dev)
13518{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013519 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013520
Ville Syrjäläb6283052015-06-03 15:45:07 +030013521 intel_update_cdclk(dev);
13522
Daniel Vetter9cd86932014-06-25 22:01:57 +030013523 if (HAS_DDI(dev))
13524 intel_ddi_pll_init(dev);
13525 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013526 ibx_pch_dpll_init(dev);
13527 else
13528 dev_priv->num_shared_dpll = 0;
13529
13530 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013531}
13532
Matt Roper6beb8c232014-12-01 15:40:14 -080013533/**
13534 * intel_prepare_plane_fb - Prepare fb for usage on plane
13535 * @plane: drm plane to prepare for
13536 * @fb: framebuffer to prepare for presentation
13537 *
13538 * Prepares a framebuffer for usage on a display plane. Generally this
13539 * involves pinning the underlying object and updating the frontbuffer tracking
13540 * bits. Some older platforms need special physical address handling for
13541 * cursor planes.
13542 *
13543 * Returns 0 on success, negative error code on failure.
13544 */
13545int
13546intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013547 struct drm_framebuffer *fb,
13548 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013549{
13550 struct drm_device *dev = plane->dev;
Matt Roper6beb8c232014-12-01 15:40:14 -080013551 struct intel_plane *intel_plane = to_intel_plane(plane);
Matt Roper6beb8c232014-12-01 15:40:14 -080013552 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13553 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
Matt Roper6beb8c232014-12-01 15:40:14 -080013554 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013555
Matt Roperea2c67b2014-12-23 10:41:52 -080013556 if (!obj)
Matt Roper465c1202014-05-29 08:06:54 -070013557 return 0;
13558
Matt Roper4c345742014-07-09 16:22:10 -070013559 mutex_lock(&dev->struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070013560
Matt Roper6beb8c232014-12-01 15:40:14 -080013561 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13562 INTEL_INFO(dev)->cursor_needs_physical) {
13563 int align = IS_I830(dev) ? 16 * 1024 : 256;
13564 ret = i915_gem_object_attach_phys(obj, align);
13565 if (ret)
13566 DRM_DEBUG_KMS("failed to attach phys object\n");
13567 } else {
John Harrison91af1272015-06-18 13:14:56 +010013568 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL, NULL);
Matt Roper6beb8c232014-12-01 15:40:14 -080013569 }
13570
13571 if (ret == 0)
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013572 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Matt Roper6beb8c232014-12-01 15:40:14 -080013573
13574 mutex_unlock(&dev->struct_mutex);
13575
13576 return ret;
13577}
13578
Matt Roper38f3ce32014-12-02 07:45:25 -080013579/**
13580 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13581 * @plane: drm plane to clean up for
13582 * @fb: old framebuffer that was on plane
13583 *
13584 * Cleans up a framebuffer that has just been removed from a plane.
13585 */
13586void
13587intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013588 struct drm_framebuffer *fb,
13589 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013590{
13591 struct drm_device *dev = plane->dev;
13592 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13593
13594 if (WARN_ON(!obj))
13595 return;
13596
13597 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
13598 !INTEL_INFO(dev)->cursor_needs_physical) {
13599 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000013600 intel_unpin_fb_obj(fb, old_state);
Matt Roper38f3ce32014-12-02 07:45:25 -080013601 mutex_unlock(&dev->struct_mutex);
13602 }
Matt Roper465c1202014-05-29 08:06:54 -070013603}
13604
Chandra Konduru6156a452015-04-27 13:48:39 -070013605int
13606skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13607{
13608 int max_scale;
13609 struct drm_device *dev;
13610 struct drm_i915_private *dev_priv;
13611 int crtc_clock, cdclk;
13612
13613 if (!intel_crtc || !crtc_state)
13614 return DRM_PLANE_HELPER_NO_SCALING;
13615
13616 dev = intel_crtc->base.dev;
13617 dev_priv = dev->dev_private;
13618 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013619 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
Chandra Konduru6156a452015-04-27 13:48:39 -070013620
13621 if (!crtc_clock || !cdclk)
13622 return DRM_PLANE_HELPER_NO_SCALING;
13623
13624 /*
13625 * skl max scale is lower of:
13626 * close to 3 but not 3, -1 is for that purpose
13627 * or
13628 * cdclk/crtc_clock
13629 */
13630 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13631
13632 return max_scale;
13633}
13634
Matt Roper465c1202014-05-29 08:06:54 -070013635static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013636intel_check_primary_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013637 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013638 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013639{
Matt Roper2b875c22014-12-01 15:40:13 -080013640 struct drm_crtc *crtc = state->base.crtc;
13641 struct drm_framebuffer *fb = state->base.fb;
Chandra Konduru6156a452015-04-27 13:48:39 -070013642 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013643 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13644 bool can_position = false;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013645
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013646 /* use scaler when colorkey is not required */
13647 if (INTEL_INFO(plane->dev)->gen >= 9 &&
Maarten Lankhorst818ed962015-06-15 12:33:54 +020013648 state->ckey.flags == I915_SET_COLORKEY_NONE) {
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013649 min_scale = 1;
13650 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
Sonika Jindald8106362015-04-10 14:37:28 +053013651 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070013652 }
Sonika Jindald8106362015-04-10 14:37:28 +053013653
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013654 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13655 &state->dst, &state->clip,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013656 min_scale, max_scale,
13657 can_position, true,
13658 &state->visible);
Matt Roper465c1202014-05-29 08:06:54 -070013659}
13660
Gustavo Padovan14af2932014-10-24 14:51:31 +010013661static void
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013662intel_commit_primary_plane(struct drm_plane *plane,
13663 struct intel_plane_state *state)
13664{
Matt Roper2b875c22014-12-01 15:40:13 -080013665 struct drm_crtc *crtc = state->base.crtc;
13666 struct drm_framebuffer *fb = state->base.fb;
13667 struct drm_device *dev = plane->dev;
Sonika Jindal48404c12014-08-22 14:06:04 +053013668 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperea2c67b2014-12-23 10:41:52 -080013669 struct intel_crtc *intel_crtc;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013670 struct drm_rect *src = &state->src;
Matt Ropercf4c7c12014-12-04 10:27:42 -080013671
Matt Roperea2c67b2014-12-23 10:41:52 -080013672 crtc = crtc ? crtc : plane->crtc;
13673 intel_crtc = to_intel_crtc(crtc);
13674
Matt Ropercf4c7c12014-12-04 10:27:42 -080013675 plane->fb = fb;
Matt Roper9dc806f2014-11-17 18:10:38 -080013676 crtc->x = src->x1 >> 16;
13677 crtc->y = src->y1 >> 16;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013678
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013679 if (!crtc->state->active)
Maarten Lankhorst302d19a2015-06-15 12:33:45 +020013680 return;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013681
Maarten Lankhorst302d19a2015-06-15 12:33:45 +020013682 if (state->visible)
13683 /* FIXME: kill this fastboot hack */
13684 intel_update_pipe_size(intel_crtc);
13685
13686 dev_priv->display.update_primary_plane(crtc, fb, crtc->x, crtc->y);
Matt Roper32b7eee2014-12-24 07:59:06 -080013687}
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013688
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013689static void
13690intel_disable_primary_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020013691 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013692{
13693 struct drm_device *dev = plane->dev;
13694 struct drm_i915_private *dev_priv = dev->dev_private;
13695
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013696 dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
13697}
13698
Matt Roper32b7eee2014-12-24 07:59:06 -080013699static void intel_begin_crtc_commit(struct drm_crtc *crtc)
13700{
13701 struct drm_device *dev = crtc->dev;
13702 struct drm_i915_private *dev_priv = dev->dev_private;
13703 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013704
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013705 if (!needs_modeset(crtc->state))
13706 intel_pre_plane_update(intel_crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013707
13708 if (intel_crtc->atomic.update_wm)
13709 intel_update_watermarks(crtc);
13710
13711 intel_runtime_pm_get(dev_priv);
Matt Roperc34c9ee2014-12-23 10:41:50 -080013712
13713 /* Perform vblank evasion around commit operation */
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013714 if (crtc->state->active)
Matt Roperc34c9ee2014-12-23 10:41:50 -080013715 intel_crtc->atomic.evade =
13716 intel_pipe_update_start(intel_crtc,
13717 &intel_crtc->atomic.start_vbl_count);
Maarten Lankhorst05832362015-06-15 12:33:48 +020013718
13719 if (!needs_modeset(crtc->state) && INTEL_INFO(dev)->gen >= 9)
13720 skl_detach_scalers(intel_crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013721}
13722
13723static void intel_finish_crtc_commit(struct drm_crtc *crtc)
13724{
13725 struct drm_device *dev = crtc->dev;
13726 struct drm_i915_private *dev_priv = dev->dev_private;
13727 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013728
Matt Roperc34c9ee2014-12-23 10:41:50 -080013729 if (intel_crtc->atomic.evade)
13730 intel_pipe_update_end(intel_crtc,
13731 intel_crtc->atomic.start_vbl_count);
13732
Matt Roper32b7eee2014-12-24 07:59:06 -080013733 intel_runtime_pm_put(dev_priv);
13734
Maarten Lankhorstac21b222015-06-15 12:33:49 +020013735 intel_post_plane_update(intel_crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013736}
13737
Matt Ropercf4c7c12014-12-04 10:27:42 -080013738/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013739 * intel_plane_destroy - destroy a plane
13740 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013741 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013742 * Common destruction function for all types of planes (primary, cursor,
13743 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013744 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013745void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013746{
13747 struct intel_plane *intel_plane = to_intel_plane(plane);
13748 drm_plane_cleanup(plane);
13749 kfree(intel_plane);
13750}
13751
Matt Roper65a3fea2015-01-21 16:35:42 -080013752const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013753 .update_plane = drm_atomic_helper_update_plane,
13754 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013755 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080013756 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080013757 .atomic_get_property = intel_plane_atomic_get_property,
13758 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013759 .atomic_duplicate_state = intel_plane_duplicate_state,
13760 .atomic_destroy_state = intel_plane_destroy_state,
13761
Matt Roper465c1202014-05-29 08:06:54 -070013762};
13763
13764static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13765 int pipe)
13766{
13767 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080013768 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070013769 const uint32_t *intel_primary_formats;
13770 int num_formats;
13771
13772 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13773 if (primary == NULL)
13774 return NULL;
13775
Matt Roper8e7d6882015-01-21 16:35:41 -080013776 state = intel_create_plane_state(&primary->base);
13777 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013778 kfree(primary);
13779 return NULL;
13780 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013781 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013782
Matt Roper465c1202014-05-29 08:06:54 -070013783 primary->can_scale = false;
13784 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013785 if (INTEL_INFO(dev)->gen >= 9) {
13786 primary->can_scale = true;
Chandra Konduruaf99ced2015-05-11 14:35:47 -070013787 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013788 }
Matt Roper465c1202014-05-29 08:06:54 -070013789 primary->pipe = pipe;
13790 primary->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013791 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080013792 primary->check_plane = intel_check_primary_plane;
13793 primary->commit_plane = intel_commit_primary_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013794 primary->disable_plane = intel_disable_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070013795 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13796 primary->plane = !pipe;
13797
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013798 if (INTEL_INFO(dev)->gen >= 9) {
13799 intel_primary_formats = skl_primary_formats;
13800 num_formats = ARRAY_SIZE(skl_primary_formats);
13801 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010013802 intel_primary_formats = i965_primary_formats;
13803 num_formats = ARRAY_SIZE(i965_primary_formats);
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013804 } else {
13805 intel_primary_formats = i8xx_primary_formats;
13806 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Matt Roper465c1202014-05-29 08:06:54 -070013807 }
13808
13809 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013810 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070013811 intel_primary_formats, num_formats,
13812 DRM_PLANE_TYPE_PRIMARY);
Sonika Jindal48404c12014-08-22 14:06:04 +053013813
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013814 if (INTEL_INFO(dev)->gen >= 4)
13815 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053013816
Matt Roperea2c67b2014-12-23 10:41:52 -080013817 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13818
Matt Roper465c1202014-05-29 08:06:54 -070013819 return &primary->base;
13820}
13821
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013822void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13823{
13824 if (!dev->mode_config.rotation_property) {
13825 unsigned long flags = BIT(DRM_ROTATE_0) |
13826 BIT(DRM_ROTATE_180);
13827
13828 if (INTEL_INFO(dev)->gen >= 9)
13829 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13830
13831 dev->mode_config.rotation_property =
13832 drm_mode_create_rotation_property(dev, flags);
13833 }
13834 if (dev->mode_config.rotation_property)
13835 drm_object_attach_property(&plane->base.base,
13836 dev->mode_config.rotation_property,
13837 plane->base.state->rotation);
13838}
13839
Matt Roper3d7d6512014-06-10 08:28:13 -070013840static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030013841intel_check_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013842 struct intel_crtc_state *crtc_state,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013843 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070013844{
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013845 struct drm_crtc *crtc = crtc_state->base.crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080013846 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013847 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013848 unsigned stride;
13849 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013850
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013851 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13852 &state->dst, &state->clip,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013853 DRM_PLANE_HELPER_NO_SCALING,
13854 DRM_PLANE_HELPER_NO_SCALING,
13855 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013856 if (ret)
13857 return ret;
13858
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013859 /* if we want to turn off the cursor ignore width and height */
13860 if (!obj)
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013861 return 0;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013862
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013863 /* Check for which cursor types we support */
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013864 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013865 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13866 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013867 return -EINVAL;
13868 }
13869
Matt Roperea2c67b2014-12-23 10:41:52 -080013870 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13871 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013872 DRM_DEBUG_KMS("buffer is too small\n");
13873 return -ENOMEM;
13874 }
13875
Ville Syrjälä3a656b52015-03-09 21:08:37 +020013876 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013877 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013878 return -EINVAL;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013879 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013880
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013881 return 0;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013882}
13883
Matt Roperf4a2cf22014-12-01 15:40:12 -080013884static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013885intel_disable_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020013886 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013887{
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013888 intel_crtc_update_cursor(crtc, false);
13889}
13890
13891static void
Gustavo Padovan852e7872014-09-05 17:22:31 -030013892intel_commit_cursor_plane(struct drm_plane *plane,
13893 struct intel_plane_state *state)
13894{
Matt Roper2b875c22014-12-01 15:40:13 -080013895 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013896 struct drm_device *dev = plane->dev;
13897 struct intel_crtc *intel_crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080013898 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080013899 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070013900
Matt Roperea2c67b2014-12-23 10:41:52 -080013901 crtc = crtc ? crtc : plane->crtc;
13902 intel_crtc = to_intel_crtc(crtc);
Sonika Jindala919db92014-10-23 07:41:33 -070013903
Matt Roperea2c67b2014-12-23 10:41:52 -080013904 plane->fb = state->base.fb;
13905 crtc->cursor_x = state->base.crtc_x;
13906 crtc->cursor_y = state->base.crtc_y;
13907
Gustavo Padovana912f122014-12-01 15:40:10 -080013908 if (intel_crtc->cursor_bo == obj)
13909 goto update;
13910
Matt Roperf4a2cf22014-12-01 15:40:12 -080013911 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080013912 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080013913 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080013914 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080013915 else
Gustavo Padovana912f122014-12-01 15:40:10 -080013916 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080013917
Gustavo Padovana912f122014-12-01 15:40:10 -080013918 intel_crtc->cursor_addr = addr;
13919 intel_crtc->cursor_bo = obj;
Gustavo Padovana912f122014-12-01 15:40:10 -080013920
Maarten Lankhorst302d19a2015-06-15 12:33:45 +020013921update:
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013922 if (crtc->state->active)
Gustavo Padovan852e7872014-09-05 17:22:31 -030013923 intel_crtc_update_cursor(crtc, state->visible);
Matt Roper3d7d6512014-06-10 08:28:13 -070013924}
Gustavo Padovan852e7872014-09-05 17:22:31 -030013925
Matt Roper3d7d6512014-06-10 08:28:13 -070013926static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
13927 int pipe)
13928{
13929 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080013930 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070013931
13932 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
13933 if (cursor == NULL)
13934 return NULL;
13935
Matt Roper8e7d6882015-01-21 16:35:41 -080013936 state = intel_create_plane_state(&cursor->base);
13937 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013938 kfree(cursor);
13939 return NULL;
13940 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013941 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013942
Matt Roper3d7d6512014-06-10 08:28:13 -070013943 cursor->can_scale = false;
13944 cursor->max_downscale = 1;
13945 cursor->pipe = pipe;
13946 cursor->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013947 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080013948 cursor->check_plane = intel_check_cursor_plane;
13949 cursor->commit_plane = intel_commit_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013950 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070013951
13952 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013953 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070013954 intel_cursor_formats,
13955 ARRAY_SIZE(intel_cursor_formats),
13956 DRM_PLANE_TYPE_CURSOR);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013957
13958 if (INTEL_INFO(dev)->gen >= 4) {
13959 if (!dev->mode_config.rotation_property)
13960 dev->mode_config.rotation_property =
13961 drm_mode_create_rotation_property(dev,
13962 BIT(DRM_ROTATE_0) |
13963 BIT(DRM_ROTATE_180));
13964 if (dev->mode_config.rotation_property)
13965 drm_object_attach_property(&cursor->base.base,
13966 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080013967 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013968 }
13969
Chandra Konduruaf99ced2015-05-11 14:35:47 -070013970 if (INTEL_INFO(dev)->gen >=9)
13971 state->scaler_id = -1;
13972
Matt Roperea2c67b2014-12-23 10:41:52 -080013973 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
13974
Matt Roper3d7d6512014-06-10 08:28:13 -070013975 return &cursor->base;
13976}
13977
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013978static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
13979 struct intel_crtc_state *crtc_state)
13980{
13981 int i;
13982 struct intel_scaler *intel_scaler;
13983 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
13984
13985 for (i = 0; i < intel_crtc->num_scalers; i++) {
13986 intel_scaler = &scaler_state->scalers[i];
13987 intel_scaler->in_use = 0;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013988 intel_scaler->mode = PS_SCALER_MODE_DYN;
13989 }
13990
13991 scaler_state->scaler_id = -1;
13992}
13993
Hannes Ederb358d0a2008-12-18 21:18:47 +010013994static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080013995{
Jani Nikulafbee40d2014-03-31 14:27:18 +030013996 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080013997 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013998 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070013999 struct drm_plane *primary = NULL;
14000 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014001 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014002
Daniel Vetter955382f2013-09-19 14:05:45 +020014003 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080014004 if (intel_crtc == NULL)
14005 return;
14006
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014007 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14008 if (!crtc_state)
14009 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030014010 intel_crtc->config = crtc_state;
14011 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080014012 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014013
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014014 /* initialize shared scalers */
14015 if (INTEL_INFO(dev)->gen >= 9) {
14016 if (pipe == PIPE_C)
14017 intel_crtc->num_scalers = 1;
14018 else
14019 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14020
14021 skl_init_scalers(dev, intel_crtc, crtc_state);
14022 }
14023
Matt Roper465c1202014-05-29 08:06:54 -070014024 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014025 if (!primary)
14026 goto fail;
14027
14028 cursor = intel_cursor_plane_create(dev, pipe);
14029 if (!cursor)
14030 goto fail;
14031
Matt Roper465c1202014-05-29 08:06:54 -070014032 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Matt Roper3d7d6512014-06-10 08:28:13 -070014033 cursor, &intel_crtc_funcs);
14034 if (ret)
14035 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080014036
14037 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080014038 for (i = 0; i < 256; i++) {
14039 intel_crtc->lut_r[i] = i;
14040 intel_crtc->lut_g[i] = i;
14041 intel_crtc->lut_b[i] = i;
14042 }
14043
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014044 /*
14045 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020014046 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014047 */
Jesse Barnes80824002009-09-10 15:28:06 -070014048 intel_crtc->pipe = pipe;
14049 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010014050 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080014051 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010014052 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070014053 }
14054
Chris Wilson4b0e3332014-05-30 16:35:26 +030014055 intel_crtc->cursor_base = ~0;
14056 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030014057 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030014058
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080014059 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14060 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14061 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14062 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14063
Jesse Barnes79e53942008-11-07 14:24:08 -080014064 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020014065
14066 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014067 return;
14068
14069fail:
14070 if (primary)
14071 drm_plane_cleanup(primary);
14072 if (cursor)
14073 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014074 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014075 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080014076}
14077
Jesse Barnes752aa882013-10-31 18:55:49 +020014078enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14079{
14080 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014081 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020014082
Rob Clark51fd3712013-11-19 12:10:12 -050014083 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020014084
Ville Syrjäläd3babd32014-11-07 11:16:01 +020014085 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020014086 return INVALID_PIPE;
14087
14088 return to_intel_crtc(encoder->crtc)->pipe;
14089}
14090
Carl Worth08d7b3d2009-04-29 14:43:54 -070014091int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000014092 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070014093{
Carl Worth08d7b3d2009-04-29 14:43:54 -070014094 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040014095 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020014096 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014097
Rob Clark7707e652014-07-17 23:30:04 -040014098 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070014099
Rob Clark7707e652014-07-17 23:30:04 -040014100 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070014101 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030014102 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014103 }
14104
Rob Clark7707e652014-07-17 23:30:04 -040014105 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020014106 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014107
Daniel Vetterc05422d2009-08-11 16:05:30 +020014108 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014109}
14110
Daniel Vetter66a92782012-07-12 20:08:18 +020014111static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080014112{
Daniel Vetter66a92782012-07-12 20:08:18 +020014113 struct drm_device *dev = encoder->base.dev;
14114 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080014115 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080014116 int entry = 0;
14117
Damien Lespiaub2784e12014-08-05 11:29:37 +010014118 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020014119 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020014120 index_mask |= (1 << entry);
14121
Jesse Barnes79e53942008-11-07 14:24:08 -080014122 entry++;
14123 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010014124
Jesse Barnes79e53942008-11-07 14:24:08 -080014125 return index_mask;
14126}
14127
Chris Wilson4d302442010-12-14 19:21:29 +000014128static bool has_edp_a(struct drm_device *dev)
14129{
14130 struct drm_i915_private *dev_priv = dev->dev_private;
14131
14132 if (!IS_MOBILE(dev))
14133 return false;
14134
14135 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14136 return false;
14137
Damien Lespiaue3589902014-02-07 19:12:50 +000014138 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000014139 return false;
14140
14141 return true;
14142}
14143
Jesse Barnes84b4e042014-06-25 08:24:29 -070014144static bool intel_crt_present(struct drm_device *dev)
14145{
14146 struct drm_i915_private *dev_priv = dev->dev_private;
14147
Damien Lespiau884497e2013-12-03 13:56:23 +000014148 if (INTEL_INFO(dev)->gen >= 9)
14149 return false;
14150
Damien Lespiaucf404ce2014-10-01 20:04:15 +010014151 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014152 return false;
14153
14154 if (IS_CHERRYVIEW(dev))
14155 return false;
14156
14157 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
14158 return false;
14159
14160 return true;
14161}
14162
Jesse Barnes79e53942008-11-07 14:24:08 -080014163static void intel_setup_outputs(struct drm_device *dev)
14164{
Eric Anholt725e30a2009-01-22 13:01:02 -080014165 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010014166 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014167 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080014168
Daniel Vetterc9093352013-06-06 22:22:47 +020014169 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014170
Jesse Barnes84b4e042014-06-25 08:24:29 -070014171 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020014172 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014173
Vandana Kannanc776eb22014-08-19 12:05:01 +053014174 if (IS_BROXTON(dev)) {
14175 /*
14176 * FIXME: Broxton doesn't support port detection via the
14177 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14178 * detect the ports.
14179 */
14180 intel_ddi_init(dev, PORT_A);
14181 intel_ddi_init(dev, PORT_B);
14182 intel_ddi_init(dev, PORT_C);
14183 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014184 int found;
14185
Jesse Barnesde31fac2015-03-06 15:53:32 -080014186 /*
14187 * Haswell uses DDI functions to detect digital outputs.
14188 * On SKL pre-D0 the strap isn't connected, so we assume
14189 * it's there.
14190 */
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014191 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014192 /* WaIgnoreDDIAStrap: skl */
14193 if (found ||
14194 (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014195 intel_ddi_init(dev, PORT_A);
14196
14197 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14198 * register */
14199 found = I915_READ(SFUSE_STRAP);
14200
14201 if (found & SFUSE_STRAP_DDIB_DETECTED)
14202 intel_ddi_init(dev, PORT_B);
14203 if (found & SFUSE_STRAP_DDIC_DETECTED)
14204 intel_ddi_init(dev, PORT_C);
14205 if (found & SFUSE_STRAP_DDID_DETECTED)
14206 intel_ddi_init(dev, PORT_D);
14207 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014208 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020014209 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014210
14211 if (has_edp_a(dev))
14212 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014213
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014214 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014215 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010014216 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014217 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014218 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014219 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014220 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014221 }
14222
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014223 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014224 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014225
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014226 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014227 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014228
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014229 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014230 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014231
Daniel Vetter270b3042012-10-27 15:52:05 +020014232 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014233 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070014234 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014235 /*
14236 * The DP_DETECTED bit is the latched state of the DDC
14237 * SDA pin at boot. However since eDP doesn't require DDC
14238 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14239 * eDP ports may have been muxed to an alternate function.
14240 * Thus we can't rely on the DP_DETECTED bit alone to detect
14241 * eDP ports. Consult the VBT as well as DP_DETECTED to
14242 * detect eDP ports.
14243 */
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014244 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
14245 !intel_dp_is_edp(dev, PORT_B))
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014246 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
14247 PORT_B);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014248 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
14249 intel_dp_is_edp(dev, PORT_B))
14250 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014251
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014252 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
14253 !intel_dp_is_edp(dev, PORT_C))
Jesse Barnes6f6005a2013-08-09 09:34:35 -070014254 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
14255 PORT_C);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014256 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
14257 intel_dp_is_edp(dev, PORT_C))
14258 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014259
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014260 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014261 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014262 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
14263 PORT_D);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014264 /* eDP not supported on port D, so don't check VBT */
14265 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
14266 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014267 }
14268
Jani Nikula3cfca972013-08-27 15:12:26 +030014269 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +080014270 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014271 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014272
Paulo Zanonie2debe92013-02-18 19:00:27 -030014273 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014274 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014275 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014276 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
14277 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014278 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014279 }
Ma Ling27185ae2009-08-24 13:50:23 +080014280
Imre Deake7281ea2013-05-08 13:14:08 +030014281 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014282 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014283 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014284
14285 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014286
Paulo Zanonie2debe92013-02-18 19:00:27 -030014287 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014288 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014289 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014290 }
Ma Ling27185ae2009-08-24 13:50:23 +080014291
Paulo Zanonie2debe92013-02-18 19:00:27 -030014292 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014293
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014294 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
14295 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014296 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014297 }
Imre Deake7281ea2013-05-08 13:14:08 +030014298 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014299 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014300 }
Ma Ling27185ae2009-08-24 13:50:23 +080014301
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014302 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014303 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014304 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014305 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014306 intel_dvo_init(dev);
14307
Zhenyu Wang103a1962009-11-27 11:44:36 +080014308 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014309 intel_tv_init(dev);
14310
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014311 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014312
Damien Lespiaub2784e12014-08-05 11:29:37 +010014313 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014314 encoder->base.possible_crtcs = encoder->crtc_mask;
14315 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014316 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014317 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014318
Paulo Zanonidde86e22012-12-01 12:04:25 -020014319 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014320
14321 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014322}
14323
14324static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14325{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014326 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014327 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014328
Daniel Vetteref2d6332014-02-10 18:00:38 +010014329 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014330 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014331 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014332 drm_gem_object_unreference(&intel_fb->obj->base);
14333 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014334 kfree(intel_fb);
14335}
14336
14337static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014338 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014339 unsigned int *handle)
14340{
14341 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014342 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014343
Chris Wilson05394f32010-11-08 19:18:58 +000014344 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014345}
14346
14347static const struct drm_framebuffer_funcs intel_fb_funcs = {
14348 .destroy = intel_user_framebuffer_destroy,
14349 .create_handle = intel_user_framebuffer_create_handle,
14350};
14351
Damien Lespiaub3218032015-02-27 11:15:18 +000014352static
14353u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14354 uint32_t pixel_format)
14355{
14356 u32 gen = INTEL_INFO(dev)->gen;
14357
14358 if (gen >= 9) {
14359 /* "The stride in bytes must not exceed the of the size of 8K
14360 * pixels and 32K bytes."
14361 */
14362 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
14363 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
14364 return 32*1024;
14365 } else if (gen >= 4) {
14366 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14367 return 16*1024;
14368 else
14369 return 32*1024;
14370 } else if (gen >= 3) {
14371 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14372 return 8*1024;
14373 else
14374 return 16*1024;
14375 } else {
14376 /* XXX DSPC is limited to 4k tiled */
14377 return 8*1024;
14378 }
14379}
14380
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014381static int intel_framebuffer_init(struct drm_device *dev,
14382 struct intel_framebuffer *intel_fb,
14383 struct drm_mode_fb_cmd2 *mode_cmd,
14384 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014385{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014386 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014387 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014388 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014389
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014390 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14391
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014392 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14393 /* Enforce that fb modifier and tiling mode match, but only for
14394 * X-tiled. This is needed for FBC. */
14395 if (!!(obj->tiling_mode == I915_TILING_X) !=
14396 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14397 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14398 return -EINVAL;
14399 }
14400 } else {
14401 if (obj->tiling_mode == I915_TILING_X)
14402 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14403 else if (obj->tiling_mode == I915_TILING_Y) {
14404 DRM_DEBUG("No Y tiling for legacy addfb\n");
14405 return -EINVAL;
14406 }
14407 }
14408
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014409 /* Passed in modifier sanity checking. */
14410 switch (mode_cmd->modifier[0]) {
14411 case I915_FORMAT_MOD_Y_TILED:
14412 case I915_FORMAT_MOD_Yf_TILED:
14413 if (INTEL_INFO(dev)->gen < 9) {
14414 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14415 mode_cmd->modifier[0]);
14416 return -EINVAL;
14417 }
14418 case DRM_FORMAT_MOD_NONE:
14419 case I915_FORMAT_MOD_X_TILED:
14420 break;
14421 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014422 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14423 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014424 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014425 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014426
Damien Lespiaub3218032015-02-27 11:15:18 +000014427 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
14428 mode_cmd->pixel_format);
14429 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14430 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14431 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014432 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014433 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014434
Damien Lespiaub3218032015-02-27 11:15:18 +000014435 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14436 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014437 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014438 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14439 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014440 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014441 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014442 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014443 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014444
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014445 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014446 mode_cmd->pitches[0] != obj->stride) {
14447 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14448 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014449 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014450 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014451
Ville Syrjälä57779d02012-10-31 17:50:14 +020014452 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014453 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014454 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014455 case DRM_FORMAT_RGB565:
14456 case DRM_FORMAT_XRGB8888:
14457 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014458 break;
14459 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014460 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014461 DRM_DEBUG("unsupported pixel format: %s\n",
14462 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014463 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014464 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014465 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014466 case DRM_FORMAT_ABGR8888:
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014467 if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
14468 DRM_DEBUG("unsupported pixel format: %s\n",
14469 drm_get_format_name(mode_cmd->pixel_format));
14470 return -EINVAL;
14471 }
14472 break;
14473 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014474 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014475 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014476 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014477 DRM_DEBUG("unsupported pixel format: %s\n",
14478 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014479 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014480 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014481 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014482 case DRM_FORMAT_ABGR2101010:
14483 if (!IS_VALLEYVIEW(dev)) {
14484 DRM_DEBUG("unsupported pixel format: %s\n",
14485 drm_get_format_name(mode_cmd->pixel_format));
14486 return -EINVAL;
14487 }
14488 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014489 case DRM_FORMAT_YUYV:
14490 case DRM_FORMAT_UYVY:
14491 case DRM_FORMAT_YVYU:
14492 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014493 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014494 DRM_DEBUG("unsupported pixel format: %s\n",
14495 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014496 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014497 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014498 break;
14499 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014500 DRM_DEBUG("unsupported pixel format: %s\n",
14501 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014502 return -EINVAL;
14503 }
14504
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014505 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14506 if (mode_cmd->offsets[0] != 0)
14507 return -EINVAL;
14508
Damien Lespiauec2c9812015-01-20 12:51:45 +000014509 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014510 mode_cmd->pixel_format,
14511 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014512 /* FIXME drm helper for size checks (especially planar formats)? */
14513 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14514 return -EINVAL;
14515
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014516 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14517 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020014518 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014519
Jesse Barnes79e53942008-11-07 14:24:08 -080014520 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14521 if (ret) {
14522 DRM_ERROR("framebuffer init failed %d\n", ret);
14523 return ret;
14524 }
14525
Jesse Barnes79e53942008-11-07 14:24:08 -080014526 return 0;
14527}
14528
Jesse Barnes79e53942008-11-07 14:24:08 -080014529static struct drm_framebuffer *
14530intel_user_framebuffer_create(struct drm_device *dev,
14531 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014532 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014533{
Chris Wilson05394f32010-11-08 19:18:58 +000014534 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014535
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014536 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
14537 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014538 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014539 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014540
Chris Wilsond2dff872011-04-19 08:36:26 +010014541 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080014542}
14543
Daniel Vetter4520f532013-10-09 09:18:51 +020014544#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020014545static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014546{
14547}
14548#endif
14549
Jesse Barnes79e53942008-11-07 14:24:08 -080014550static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014551 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014552 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014553 .atomic_check = intel_atomic_check,
14554 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020014555 .atomic_state_alloc = intel_atomic_state_alloc,
14556 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080014557};
14558
Jesse Barnese70236a2009-09-21 10:42:27 -070014559/* Set up chip specific display functions */
14560static void intel_init_display(struct drm_device *dev)
14561{
14562 struct drm_i915_private *dev_priv = dev->dev_private;
14563
Daniel Vetteree9300b2013-06-03 22:40:22 +020014564 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14565 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030014566 else if (IS_CHERRYVIEW(dev))
14567 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020014568 else if (IS_VALLEYVIEW(dev))
14569 dev_priv->display.find_dpll = vlv_find_best_dpll;
14570 else if (IS_PINEVIEW(dev))
14571 dev_priv->display.find_dpll = pnv_find_best_dpll;
14572 else
14573 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14574
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014575 if (INTEL_INFO(dev)->gen >= 9) {
14576 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014577 dev_priv->display.get_initial_plane_config =
14578 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014579 dev_priv->display.crtc_compute_clock =
14580 haswell_crtc_compute_clock;
14581 dev_priv->display.crtc_enable = haswell_crtc_enable;
14582 dev_priv->display.crtc_disable = haswell_crtc_disable;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014583 dev_priv->display.update_primary_plane =
14584 skylake_update_primary_plane;
14585 } else if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014586 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014587 dev_priv->display.get_initial_plane_config =
14588 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014589 dev_priv->display.crtc_compute_clock =
14590 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014591 dev_priv->display.crtc_enable = haswell_crtc_enable;
14592 dev_priv->display.crtc_disable = haswell_crtc_disable;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014593 dev_priv->display.update_primary_plane =
14594 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030014595 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014596 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014597 dev_priv->display.get_initial_plane_config =
14598 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014599 dev_priv->display.crtc_compute_clock =
14600 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014601 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14602 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014603 dev_priv->display.update_primary_plane =
14604 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014605 } else if (IS_VALLEYVIEW(dev)) {
14606 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014607 dev_priv->display.get_initial_plane_config =
14608 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014609 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014610 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14611 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014612 dev_priv->display.update_primary_plane =
14613 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014614 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014615 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014616 dev_priv->display.get_initial_plane_config =
14617 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014618 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014619 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14620 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014621 dev_priv->display.update_primary_plane =
14622 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014623 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014624
Jesse Barnese70236a2009-09-21 10:42:27 -070014625 /* Returns the core display clock speed */
Ville Syrjälä1652d192015-03-31 14:12:01 +030014626 if (IS_SKYLAKE(dev))
14627 dev_priv->display.get_display_clock_speed =
14628 skylake_get_display_clock_speed;
Bob Paauweacd3f3d2015-06-23 14:14:26 -070014629 else if (IS_BROXTON(dev))
14630 dev_priv->display.get_display_clock_speed =
14631 broxton_get_display_clock_speed;
Ville Syrjälä1652d192015-03-31 14:12:01 +030014632 else if (IS_BROADWELL(dev))
14633 dev_priv->display.get_display_clock_speed =
14634 broadwell_get_display_clock_speed;
14635 else if (IS_HASWELL(dev))
14636 dev_priv->display.get_display_clock_speed =
14637 haswell_get_display_clock_speed;
14638 else if (IS_VALLEYVIEW(dev))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070014639 dev_priv->display.get_display_clock_speed =
14640 valleyview_get_display_clock_speed;
Ville Syrjäläb37a6432015-03-31 14:11:54 +030014641 else if (IS_GEN5(dev))
14642 dev_priv->display.get_display_clock_speed =
14643 ilk_get_display_clock_speed;
Ville Syrjäläa7c66cd2015-03-31 14:11:56 +030014644 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
Ville Syrjälä34edce22015-05-22 11:22:33 +030014645 IS_GEN6(dev) || IS_IVYBRIDGE(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014646 dev_priv->display.get_display_clock_speed =
14647 i945_get_display_clock_speed;
Ville Syrjälä34edce22015-05-22 11:22:33 +030014648 else if (IS_GM45(dev))
14649 dev_priv->display.get_display_clock_speed =
14650 gm45_get_display_clock_speed;
14651 else if (IS_CRESTLINE(dev))
14652 dev_priv->display.get_display_clock_speed =
14653 i965gm_get_display_clock_speed;
14654 else if (IS_PINEVIEW(dev))
14655 dev_priv->display.get_display_clock_speed =
14656 pnv_get_display_clock_speed;
14657 else if (IS_G33(dev) || IS_G4X(dev))
14658 dev_priv->display.get_display_clock_speed =
14659 g33_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014660 else if (IS_I915G(dev))
14661 dev_priv->display.get_display_clock_speed =
14662 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014663 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014664 dev_priv->display.get_display_clock_speed =
14665 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014666 else if (IS_PINEVIEW(dev))
14667 dev_priv->display.get_display_clock_speed =
14668 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014669 else if (IS_I915GM(dev))
14670 dev_priv->display.get_display_clock_speed =
14671 i915gm_get_display_clock_speed;
14672 else if (IS_I865G(dev))
14673 dev_priv->display.get_display_clock_speed =
14674 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020014675 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014676 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030014677 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014678 else { /* 830 */
14679 WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070014680 dev_priv->display.get_display_clock_speed =
14681 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014682 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014683
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014684 if (IS_GEN5(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014685 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014686 } else if (IS_GEN6(dev)) {
14687 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014688 } else if (IS_IVYBRIDGE(dev)) {
14689 /* FIXME: detect B0+ stepping and use auto training */
14690 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Paulo Zanoni059b2fe2014-09-02 16:53:57 -030014691 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014692 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014693 if (IS_BROADWELL(dev)) {
14694 dev_priv->display.modeset_commit_cdclk =
14695 broadwell_modeset_commit_cdclk;
14696 dev_priv->display.modeset_calc_cdclk =
14697 broadwell_modeset_calc_cdclk;
14698 }
Jesse Barnes30a970c2013-11-04 13:48:12 -080014699 } else if (IS_VALLEYVIEW(dev)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014700 dev_priv->display.modeset_commit_cdclk =
14701 valleyview_modeset_commit_cdclk;
14702 dev_priv->display.modeset_calc_cdclk =
14703 valleyview_modeset_calc_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +053014704 } else if (IS_BROXTON(dev)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014705 dev_priv->display.modeset_commit_cdclk =
14706 broxton_modeset_commit_cdclk;
14707 dev_priv->display.modeset_calc_cdclk =
14708 broxton_modeset_calc_cdclk;
Jesse Barnese70236a2009-09-21 10:42:27 -070014709 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014710
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014711 switch (INTEL_INFO(dev)->gen) {
14712 case 2:
14713 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14714 break;
14715
14716 case 3:
14717 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14718 break;
14719
14720 case 4:
14721 case 5:
14722 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14723 break;
14724
14725 case 6:
14726 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14727 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014728 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070014729 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014730 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14731 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000014732 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000014733 /* Drop through - unsupported since execlist only. */
14734 default:
14735 /* Default just returns -ENODEV to indicate unsupported */
14736 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014737 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020014738
14739 intel_panel_init_backlight_funcs(dev);
Ville Syrjäläe39b9992014-09-04 14:53:14 +030014740
14741 mutex_init(&dev_priv->pps_mutex);
Jesse Barnese70236a2009-09-21 10:42:27 -070014742}
14743
Jesse Barnesb690e962010-07-19 13:53:12 -070014744/*
14745 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14746 * resume, or other times. This quirk makes sure that's the case for
14747 * affected systems.
14748 */
Akshay Joshi0206e352011-08-16 15:34:10 -040014749static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070014750{
14751 struct drm_i915_private *dev_priv = dev->dev_private;
14752
14753 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014754 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014755}
14756
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014757static void quirk_pipeb_force(struct drm_device *dev)
14758{
14759 struct drm_i915_private *dev_priv = dev->dev_private;
14760
14761 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14762 DRM_INFO("applying pipe b force quirk\n");
14763}
14764
Keith Packard435793d2011-07-12 14:56:22 -070014765/*
14766 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14767 */
14768static void quirk_ssc_force_disable(struct drm_device *dev)
14769{
14770 struct drm_i915_private *dev_priv = dev->dev_private;
14771 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014772 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070014773}
14774
Carsten Emde4dca20e2012-03-15 15:56:26 +010014775/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010014776 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14777 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010014778 */
14779static void quirk_invert_brightness(struct drm_device *dev)
14780{
14781 struct drm_i915_private *dev_priv = dev->dev_private;
14782 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014783 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014784}
14785
Scot Doyle9c72cc62014-07-03 23:27:50 +000014786/* Some VBT's incorrectly indicate no backlight is present */
14787static void quirk_backlight_present(struct drm_device *dev)
14788{
14789 struct drm_i915_private *dev_priv = dev->dev_private;
14790 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14791 DRM_INFO("applying backlight present quirk\n");
14792}
14793
Jesse Barnesb690e962010-07-19 13:53:12 -070014794struct intel_quirk {
14795 int device;
14796 int subsystem_vendor;
14797 int subsystem_device;
14798 void (*hook)(struct drm_device *dev);
14799};
14800
Egbert Eich5f85f172012-10-14 15:46:38 +020014801/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14802struct intel_dmi_quirk {
14803 void (*hook)(struct drm_device *dev);
14804 const struct dmi_system_id (*dmi_id_list)[];
14805};
14806
14807static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14808{
14809 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14810 return 1;
14811}
14812
14813static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14814 {
14815 .dmi_id_list = &(const struct dmi_system_id[]) {
14816 {
14817 .callback = intel_dmi_reverse_brightness,
14818 .ident = "NCR Corporation",
14819 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14820 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14821 },
14822 },
14823 { } /* terminating entry */
14824 },
14825 .hook = quirk_invert_brightness,
14826 },
14827};
14828
Ben Widawskyc43b5632012-04-16 14:07:40 -070014829static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070014830 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14831 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14832
Jesse Barnesb690e962010-07-19 13:53:12 -070014833 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14834 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14835
Ville Syrjälä5f080c02014-08-15 01:22:06 +030014836 /* 830 needs to leave pipe A & dpll A up */
14837 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14838
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014839 /* 830 needs to leave pipe B & dpll B up */
14840 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14841
Keith Packard435793d2011-07-12 14:56:22 -070014842 /* Lenovo U160 cannot use SSC on LVDS */
14843 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020014844
14845 /* Sony Vaio Y cannot use SSC on LVDS */
14846 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010014847
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010014848 /* Acer Aspire 5734Z must invert backlight brightness */
14849 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14850
14851 /* Acer/eMachines G725 */
14852 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14853
14854 /* Acer/eMachines e725 */
14855 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14856
14857 /* Acer/Packard Bell NCL20 */
14858 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14859
14860 /* Acer Aspire 4736Z */
14861 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020014862
14863 /* Acer Aspire 5336 */
14864 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000014865
14866 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14867 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000014868
Scot Doyledfb3d47b2014-08-21 16:08:02 +000014869 /* Acer C720 Chromebook (Core i3 4005U) */
14870 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14871
jens steinb2a96012014-10-28 20:25:53 +010014872 /* Apple Macbook 2,1 (Core 2 T7400) */
14873 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14874
Scot Doyled4967d82014-07-03 23:27:52 +000014875 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14876 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000014877
14878 /* HP Chromebook 14 (Celeron 2955U) */
14879 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020014880
14881 /* Dell Chromebook 11 */
14882 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070014883};
14884
14885static void intel_init_quirks(struct drm_device *dev)
14886{
14887 struct pci_dev *d = dev->pdev;
14888 int i;
14889
14890 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14891 struct intel_quirk *q = &intel_quirks[i];
14892
14893 if (d->device == q->device &&
14894 (d->subsystem_vendor == q->subsystem_vendor ||
14895 q->subsystem_vendor == PCI_ANY_ID) &&
14896 (d->subsystem_device == q->subsystem_device ||
14897 q->subsystem_device == PCI_ANY_ID))
14898 q->hook(dev);
14899 }
Egbert Eich5f85f172012-10-14 15:46:38 +020014900 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14901 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14902 intel_dmi_quirks[i].hook(dev);
14903 }
Jesse Barnesb690e962010-07-19 13:53:12 -070014904}
14905
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014906/* Disable the VGA plane that we never use */
14907static void i915_disable_vga(struct drm_device *dev)
14908{
14909 struct drm_i915_private *dev_priv = dev->dev_private;
14910 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020014911 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014912
Ville Syrjälä2b37c612014-01-22 21:32:38 +020014913 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014914 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070014915 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014916 sr1 = inb(VGA_SR_DATA);
14917 outb(sr1 | 1<<5, VGA_SR_DATA);
14918 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
14919 udelay(300);
14920
Ville Syrjälä01f5a622014-12-16 18:38:37 +020014921 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014922 POSTING_READ(vga_reg);
14923}
14924
Daniel Vetterf8175862012-04-10 15:50:11 +020014925void intel_modeset_init_hw(struct drm_device *dev)
14926{
Ville Syrjäläb6283052015-06-03 15:45:07 +030014927 intel_update_cdclk(dev);
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030014928 intel_prepare_ddi(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020014929 intel_init_clock_gating(dev);
Daniel Vetter8090c6b2012-06-24 16:42:32 +020014930 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020014931}
14932
Jesse Barnes79e53942008-11-07 14:24:08 -080014933void intel_modeset_init(struct drm_device *dev)
14934{
Jesse Barnes652c3932009-08-17 13:31:43 -070014935 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000014936 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014937 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080014938 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080014939
14940 drm_mode_config_init(dev);
14941
14942 dev->mode_config.min_width = 0;
14943 dev->mode_config.min_height = 0;
14944
Dave Airlie019d96c2011-09-29 16:20:42 +010014945 dev->mode_config.preferred_depth = 24;
14946 dev->mode_config.prefer_shadow = 1;
14947
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000014948 dev->mode_config.allow_fb_modifiers = true;
14949
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020014950 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080014951
Jesse Barnesb690e962010-07-19 13:53:12 -070014952 intel_init_quirks(dev);
14953
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030014954 intel_init_pm(dev);
14955
Ben Widawskye3c74752013-04-05 13:12:39 -070014956 if (INTEL_INFO(dev)->num_pipes == 0)
14957 return;
14958
Jesse Barnese70236a2009-09-21 10:42:27 -070014959 intel_init_display(dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014960 intel_init_audio(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070014961
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014962 if (IS_GEN2(dev)) {
14963 dev->mode_config.max_width = 2048;
14964 dev->mode_config.max_height = 2048;
14965 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070014966 dev->mode_config.max_width = 4096;
14967 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080014968 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014969 dev->mode_config.max_width = 8192;
14970 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080014971 }
Damien Lespiau068be562014-03-28 14:17:49 +000014972
Ville Syrjälädc41c152014-08-13 11:57:05 +030014973 if (IS_845G(dev) || IS_I865G(dev)) {
14974 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
14975 dev->mode_config.cursor_height = 1023;
14976 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000014977 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
14978 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
14979 } else {
14980 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
14981 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
14982 }
14983
Ben Widawsky5d4545a2013-01-17 12:45:15 -080014984 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080014985
Zhao Yakui28c97732009-10-09 11:39:41 +080014986 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014987 INTEL_INFO(dev)->num_pipes,
14988 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080014989
Damien Lespiau055e3932014-08-18 13:49:10 +010014990 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014991 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000014992 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000014993 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070014994 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030014995 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000014996 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070014997 }
Jesse Barnes79e53942008-11-07 14:24:08 -080014998 }
14999
Jesse Barnesf42bb702013-12-16 16:34:23 -080015000 intel_init_dpio(dev);
15001
Daniel Vettere72f9fb2013-06-05 13:34:06 +020015002 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010015003
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015004 /* Just disable it once at startup */
15005 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015006 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000015007
15008 /* Just in case the BIOS is doing something questionable. */
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020015009 intel_fbc_disable(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080015010
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015011 drm_modeset_lock_all(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080015012 intel_modeset_setup_hw_state(dev, false);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015013 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015014
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015015 for_each_intel_crtc(dev, crtc) {
Jesse Barnes46f297f2014-03-07 08:57:48 -080015016 if (!crtc->active)
15017 continue;
15018
Jesse Barnes46f297f2014-03-07 08:57:48 -080015019 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080015020 * Note that reserving the BIOS fb up front prevents us
15021 * from stuffing other stolen allocations like the ring
15022 * on top. This prevents some ugliness at boot time, and
15023 * can even allow for smooth boot transitions if the BIOS
15024 * fb is large enough for the active pipe configuration.
15025 */
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015026 if (dev_priv->display.get_initial_plane_config) {
15027 dev_priv->display.get_initial_plane_config(crtc,
Jesse Barnes46f297f2014-03-07 08:57:48 -080015028 &crtc->plane_config);
15029 /*
15030 * If the fb is shared between multiple heads, we'll
15031 * just get the first one.
15032 */
Daniel Vetterf6936e22015-03-26 12:17:05 +010015033 intel_find_initial_plane_obj(crtc, &crtc->plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015034 }
Jesse Barnes46f297f2014-03-07 08:57:48 -080015035 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010015036}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015037
Daniel Vetter7fad7982012-07-04 17:51:47 +020015038static void intel_enable_pipe_a(struct drm_device *dev)
15039{
15040 struct intel_connector *connector;
15041 struct drm_connector *crt = NULL;
15042 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015043 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020015044
15045 /* We can't just switch on the pipe A, we need to set things up with a
15046 * proper mode and output configuration. As a gross hack, enable pipe A
15047 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015048 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020015049 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15050 crt = &connector->base;
15051 break;
15052 }
15053 }
15054
15055 if (!crt)
15056 return;
15057
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015058 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020015059 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020015060}
15061
Daniel Vetterfa555832012-10-10 23:14:00 +020015062static bool
15063intel_check_plane_mapping(struct intel_crtc *crtc)
15064{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015065 struct drm_device *dev = crtc->base.dev;
15066 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020015067 u32 reg, val;
15068
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015069 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020015070 return true;
15071
15072 reg = DSPCNTR(!crtc->plane);
15073 val = I915_READ(reg);
15074
15075 if ((val & DISPLAY_PLANE_ENABLE) &&
15076 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15077 return false;
15078
15079 return true;
15080}
15081
Daniel Vetter24929352012-07-02 20:28:59 +020015082static void intel_sanitize_crtc(struct intel_crtc *crtc)
15083{
15084 struct drm_device *dev = crtc->base.dev;
15085 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015086 struct intel_encoder *encoder;
Daniel Vetterfa555832012-10-10 23:14:00 +020015087 u32 reg;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015088 bool enable;
Daniel Vetter24929352012-07-02 20:28:59 +020015089
Daniel Vetter24929352012-07-02 20:28:59 +020015090 /* Clear any frame start delays used for debugging left by the BIOS */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015091 reg = PIPECONF(crtc->config->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015092 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15093
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015094 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010015095 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015096 if (crtc->active) {
15097 update_scanline_offset(crtc);
Daniel Vetter96256042015-02-13 21:03:42 +010015098 drm_crtc_vblank_on(&crtc->base);
15099 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015100
Daniel Vetter24929352012-07-02 20:28:59 +020015101 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020015102 * disable the crtc (and hence change the state) if it is wrong. Note
15103 * that gen4+ has a fixed plane -> pipe mapping. */
15104 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020015105 bool plane;
15106
Daniel Vetter24929352012-07-02 20:28:59 +020015107 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15108 crtc->base.base.id);
15109
15110 /* Pipe has the wrong plane attached and the plane is active.
15111 * Temporarily change the plane mapping and disable everything
15112 * ... */
15113 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015114 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020015115 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015116 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015117 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020015118 }
Daniel Vetter24929352012-07-02 20:28:59 +020015119
Daniel Vetter7fad7982012-07-04 17:51:47 +020015120 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15121 crtc->pipe == PIPE_A && !crtc->active) {
15122 /* BIOS forgot to enable pipe A, this mostly happens after
15123 * resume. Force-enable the pipe to fix this, the update_dpms
15124 * call below we restore the pipe to the right state, but leave
15125 * the required bits on. */
15126 intel_enable_pipe_a(dev);
15127 }
15128
Daniel Vetter24929352012-07-02 20:28:59 +020015129 /* Adjust the state of the output pipe according to whether we
15130 * have active connectors/encoders. */
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015131 enable = false;
15132 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15133 enable |= encoder->connectors_active;
Daniel Vetter24929352012-07-02 20:28:59 +020015134
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015135 if (!enable)
15136 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015137
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020015138 if (crtc->active != crtc->base.state->active) {
Daniel Vetter24929352012-07-02 20:28:59 +020015139
15140 /* This can happen either due to bugs in the get_hw_state
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015141 * functions or because of calls to intel_crtc_disable_noatomic,
15142 * or because the pipe is force-enabled due to the
Daniel Vetter24929352012-07-02 20:28:59 +020015143 * pipe A quirk. */
15144 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
15145 crtc->base.base.id,
Matt Roper83d65732015-02-25 13:12:16 -080015146 crtc->base.state->enable ? "enabled" : "disabled",
Daniel Vetter24929352012-07-02 20:28:59 +020015147 crtc->active ? "enabled" : "disabled");
15148
Matt Roper83d65732015-02-25 13:12:16 -080015149 crtc->base.state->enable = crtc->active;
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020015150 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020015151 crtc->base.enabled = crtc->active;
15152
15153 /* Because we only establish the connector -> encoder ->
15154 * crtc links if something is active, this means the
15155 * crtc is now deactivated. Break the links. connector
15156 * -> encoder links are only establish when things are
15157 * actually up, hence no need to break them. */
15158 WARN_ON(crtc->active);
15159
15160 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
15161 WARN_ON(encoder->connectors_active);
15162 encoder->base.crtc = NULL;
15163 }
15164 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015165
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030015166 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015167 /*
15168 * We start out with underrun reporting disabled to avoid races.
15169 * For correct bookkeeping mark this on active crtcs.
15170 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015171 * Also on gmch platforms we dont have any hardware bits to
15172 * disable the underrun reporting. Which means we need to start
15173 * out with underrun reporting disabled also on inactive pipes,
15174 * since otherwise we'll complain about the garbage we read when
15175 * e.g. coming up after runtime pm.
15176 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015177 * No protection against concurrent access is required - at
15178 * worst a fifo underrun happens which also sets this to false.
15179 */
15180 crtc->cpu_fifo_underrun_disabled = true;
15181 crtc->pch_fifo_underrun_disabled = true;
15182 }
Daniel Vetter24929352012-07-02 20:28:59 +020015183}
15184
15185static void intel_sanitize_encoder(struct intel_encoder *encoder)
15186{
15187 struct intel_connector *connector;
15188 struct drm_device *dev = encoder->base.dev;
15189
15190 /* We need to check both for a crtc link (meaning that the
15191 * encoder is active and trying to read from a pipe) and the
15192 * pipe itself being active. */
15193 bool has_active_crtc = encoder->base.crtc &&
15194 to_intel_crtc(encoder->base.crtc)->active;
15195
15196 if (encoder->connectors_active && !has_active_crtc) {
15197 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15198 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015199 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015200
15201 /* Connector is active, but has no active pipe. This is
15202 * fallout from our resume register restoring. Disable
15203 * the encoder manually again. */
15204 if (encoder->base.crtc) {
15205 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15206 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015207 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015208 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015209 if (encoder->post_disable)
15210 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015211 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015212 encoder->base.crtc = NULL;
15213 encoder->connectors_active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020015214
15215 /* Inconsistent output/port/pipe state happens presumably due to
15216 * a bug in one of the get_hw_state functions. Or someplace else
15217 * in our code, like the register restore mess on resume. Clamp
15218 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015219 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015220 if (connector->encoder != encoder)
15221 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020015222 connector->base.dpms = DRM_MODE_DPMS_OFF;
15223 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015224 }
15225 }
15226 /* Enabled encoders without active connectors will be fixed in
15227 * the crtc fixup. */
15228}
15229
Imre Deak04098752014-02-18 00:02:16 +020015230void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015231{
15232 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020015233 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015234
Imre Deak04098752014-02-18 00:02:16 +020015235 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15236 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15237 i915_disable_vga(dev);
15238 }
15239}
15240
15241void i915_redisable_vga(struct drm_device *dev)
15242{
15243 struct drm_i915_private *dev_priv = dev->dev_private;
15244
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015245 /* This function can be called both from intel_modeset_setup_hw_state or
15246 * at a very early point in our resume sequence, where the power well
15247 * structures are not yet restored. Since this function is at a very
15248 * paranoid "someone might have enabled VGA while we were not looking"
15249 * level, just check if the power well is enabled instead of trying to
15250 * follow the "don't touch the power well if we don't need it" policy
15251 * the rest of the driver uses. */
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015252 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015253 return;
15254
Imre Deak04098752014-02-18 00:02:16 +020015255 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015256}
15257
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015258static bool primary_get_hw_state(struct intel_crtc *crtc)
15259{
15260 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
15261
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015262 return !!(I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE);
15263}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015264
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015265static void readout_plane_state(struct intel_crtc *crtc,
15266 struct intel_crtc_state *crtc_state)
15267{
15268 struct intel_plane *p;
15269 struct drm_plane_state *drm_plane_state;
15270 bool active = crtc_state->base.active;
15271
15272 if (active) {
15273 crtc_state->quirks |= PIPE_CONFIG_QUIRK_INITIAL_PLANES;
15274
15275 /* apply to previous sw state too */
15276 to_intel_crtc_state(crtc->base.state)->quirks |=
15277 PIPE_CONFIG_QUIRK_INITIAL_PLANES;
15278 }
15279
15280 for_each_intel_plane(crtc->base.dev, p) {
15281 bool visible = active;
15282
15283 if (crtc->pipe != p->pipe)
15284 continue;
15285
15286 drm_plane_state = p->base.state;
15287 if (active && p->base.type == DRM_PLANE_TYPE_PRIMARY) {
15288 visible = primary_get_hw_state(crtc);
15289 to_intel_plane_state(drm_plane_state)->visible = visible;
15290 } else {
15291 /*
15292 * unknown state, assume it's off to force a transition
15293 * to on when calculating state changes.
15294 */
15295 to_intel_plane_state(drm_plane_state)->visible = false;
15296 }
15297
15298 if (visible) {
15299 crtc_state->base.plane_mask |=
15300 1 << drm_plane_index(&p->base);
15301 } else if (crtc_state->base.state) {
15302 /* Make this unconditional for atomic hw readout. */
15303 crtc_state->base.plane_mask &=
15304 ~(1 << drm_plane_index(&p->base));
15305 }
15306 }
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015307}
15308
Daniel Vetter30e984d2013-06-05 13:34:17 +020015309static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015310{
15311 struct drm_i915_private *dev_priv = dev->dev_private;
15312 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015313 struct intel_crtc *crtc;
15314 struct intel_encoder *encoder;
15315 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020015316 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015317
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015318 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015319 memset(crtc->config, 0, sizeof(*crtc->config));
Maarten Lankhorstf7217902015-06-10 10:24:20 +020015320 crtc->config->base.crtc = &crtc->base;
Daniel Vetter3b117c82013-04-17 20:15:07 +020015321
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015322 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
Daniel Vetter99535992014-04-13 12:00:33 +020015323
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015324 crtc->active = dev_priv->display.get_pipe_config(crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015325 crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015326
Matt Roper83d65732015-02-25 13:12:16 -080015327 crtc->base.state->enable = crtc->active;
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020015328 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020015329 crtc->base.enabled = crtc->active;
Maarten Lankhorstb8b7fad2015-06-12 11:15:41 +020015330 crtc->base.hwmode = crtc->config->base.adjusted_mode;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015331
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015332 readout_plane_state(crtc, to_intel_crtc_state(crtc->base.state));
Daniel Vetter24929352012-07-02 20:28:59 +020015333
15334 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15335 crtc->base.base.id,
15336 crtc->active ? "enabled" : "disabled");
15337 }
15338
Daniel Vetter53589012013-06-05 13:34:16 +020015339 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15340 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15341
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015342 pll->on = pll->get_hw_state(dev_priv, pll,
15343 &pll->config.hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020015344 pll->active = 0;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015345 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015346 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015347 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
Daniel Vetter53589012013-06-05 13:34:16 +020015348 pll->active++;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015349 pll->config.crtc_mask |= 1 << crtc->pipe;
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015350 }
Daniel Vetter53589012013-06-05 13:34:16 +020015351 }
Daniel Vetter53589012013-06-05 13:34:16 +020015352
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015353 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015354 pll->name, pll->config.crtc_mask, pll->on);
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015355
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015356 if (pll->config.crtc_mask)
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015357 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
Daniel Vetter53589012013-06-05 13:34:16 +020015358 }
15359
Damien Lespiaub2784e12014-08-05 11:29:37 +010015360 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015361 pipe = 0;
15362
15363 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015364 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15365 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015366 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015367 } else {
15368 encoder->base.crtc = NULL;
15369 }
15370
15371 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015372 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020015373 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015374 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015375 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015376 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015377 }
15378
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015379 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015380 if (connector->get_hw_state(connector)) {
15381 connector->base.dpms = DRM_MODE_DPMS_ON;
15382 connector->encoder->connectors_active = true;
15383 connector->base.encoder = &connector->encoder->base;
15384 } else {
15385 connector->base.dpms = DRM_MODE_DPMS_OFF;
15386 connector->base.encoder = NULL;
15387 }
15388 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15389 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030015390 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015391 connector->base.encoder ? "enabled" : "disabled");
15392 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015393}
15394
15395/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
15396 * and i915 state tracking structures. */
15397void intel_modeset_setup_hw_state(struct drm_device *dev,
15398 bool force_restore)
15399{
15400 struct drm_i915_private *dev_priv = dev->dev_private;
15401 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015402 struct intel_crtc *crtc;
15403 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015404 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015405
15406 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015407
Jesse Barnesbabea612013-06-26 18:57:38 +030015408 /*
15409 * Now that we have the config, copy it to each CRTC struct
15410 * Note that this could go away if we move to using crtc_config
15411 * checking everywhere.
15412 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015413 for_each_intel_crtc(dev, crtc) {
Jani Nikulad330a952014-01-21 11:24:25 +020015414 if (crtc->active && i915.fastboot) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015415 intel_mode_from_pipe_config(&crtc->base.mode,
15416 crtc->config);
Jesse Barnesbabea612013-06-26 18:57:38 +030015417 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
15418 crtc->base.base.id);
15419 drm_mode_debug_printmodeline(&crtc->base.mode);
15420 }
15421 }
15422
Daniel Vetter24929352012-07-02 20:28:59 +020015423 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010015424 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015425 intel_sanitize_encoder(encoder);
15426 }
15427
Damien Lespiau055e3932014-08-18 13:49:10 +010015428 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020015429 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15430 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015431 intel_dump_pipe_config(crtc, crtc->config,
15432 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015433 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015434
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015435 intel_modeset_update_connector_atomic_state(dev);
15436
Daniel Vetter35c95372013-07-17 06:55:04 +020015437 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15438 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15439
15440 if (!pll->on || pll->active)
15441 continue;
15442
15443 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15444
15445 pll->disable(dev_priv, pll);
15446 pll->on = false;
15447 }
15448
Pradeep Bhat30789992014-11-04 17:06:45 +000015449 if (IS_GEN9(dev))
15450 skl_wm_get_hw_state(dev);
15451 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015452 ilk_wm_get_hw_state(dev);
15453
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015454 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015455 i915_redisable_vga(dev);
15456
Daniel Vetterf30da182013-04-11 20:22:50 +020015457 /*
15458 * We need to use raw interfaces for restoring state to avoid
15459 * checking (bogus) intermediate states.
15460 */
Damien Lespiau055e3932014-08-18 13:49:10 +010015461 for_each_pipe(dev_priv, pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070015462 struct drm_crtc *crtc =
15463 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020015464
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020015465 intel_crtc_restore_mode(crtc);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015466 }
15467 } else {
15468 intel_modeset_update_staged_output_state(dev);
15469 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020015470
15471 intel_modeset_check_state(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015472}
15473
15474void intel_modeset_gem_init(struct drm_device *dev)
15475{
Jesse Barnes92122782014-10-09 12:57:42 -070015476 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015477 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070015478 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015479 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015480
Imre Deakae484342014-03-31 15:10:44 +030015481 mutex_lock(&dev->struct_mutex);
15482 intel_init_gt_powersave(dev);
15483 mutex_unlock(&dev->struct_mutex);
15484
Jesse Barnes92122782014-10-09 12:57:42 -070015485 /*
15486 * There may be no VBT; and if the BIOS enabled SSC we can
15487 * just keep using it to avoid unnecessary flicker. Whereas if the
15488 * BIOS isn't using it, don't assume it will work even if the VBT
15489 * indicates as much.
15490 */
15491 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
15492 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15493 DREF_SSC1_ENABLE);
15494
Chris Wilson1833b132012-05-09 11:56:28 +010015495 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020015496
15497 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015498
15499 /*
15500 * Make sure any fbs we allocated at startup are properly
15501 * pinned & fenced. When we do the allocation it's too early
15502 * for this.
15503 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010015504 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070015505 obj = intel_fb_obj(c->primary->fb);
15506 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080015507 continue;
15508
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015509 mutex_lock(&dev->struct_mutex);
15510 ret = intel_pin_and_fence_fb_obj(c->primary,
15511 c->primary->fb,
15512 c->primary->state,
John Harrison91af1272015-06-18 13:14:56 +010015513 NULL, NULL);
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015514 mutex_unlock(&dev->struct_mutex);
15515 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080015516 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15517 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100015518 drm_framebuffer_unreference(c->primary->fb);
15519 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015520 c->primary->crtc = c->primary->state->crtc = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080015521 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015522 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080015523 }
15524 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015525
15526 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015527}
15528
Imre Deak4932e2c2014-02-11 17:12:48 +020015529void intel_connector_unregister(struct intel_connector *intel_connector)
15530{
15531 struct drm_connector *connector = &intel_connector->base;
15532
15533 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010015534 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015535}
15536
Jesse Barnes79e53942008-11-07 14:24:08 -080015537void intel_modeset_cleanup(struct drm_device *dev)
15538{
Jesse Barnes652c3932009-08-17 13:31:43 -070015539 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid9255d52013-09-26 20:05:59 -030015540 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070015541
Imre Deak2eb52522014-11-19 15:30:05 +020015542 intel_disable_gt_powersave(dev);
15543
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015544 intel_backlight_unregister(dev);
15545
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015546 /*
15547 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020015548 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015549 * experience fancy races otherwise.
15550 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020015551 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070015552
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015553 /*
15554 * Due to the hpd irq storm handling the hotplug work can re-arm the
15555 * poll handlers. Hence disable polling after hpd handling is shut down.
15556 */
Keith Packardf87ea762010-10-03 19:36:26 -070015557 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015558
Jesse Barnes652c3932009-08-17 13:31:43 -070015559 mutex_lock(&dev->struct_mutex);
15560
Jesse Barnes723bfd72010-10-07 16:01:13 -070015561 intel_unregister_dsm_handler();
15562
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020015563 intel_fbc_disable(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070015564
Kristian Høgsberg69341a52009-11-11 12:19:17 -050015565 mutex_unlock(&dev->struct_mutex);
15566
Chris Wilson1630fe72011-07-08 12:22:42 +010015567 /* flush any delayed tasks or pending work */
15568 flush_scheduled_work();
15569
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015570 /* destroy the backlight and sysfs files before encoders/connectors */
15571 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020015572 struct intel_connector *intel_connector;
15573
15574 intel_connector = to_intel_connector(connector);
15575 intel_connector->unregister(intel_connector);
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015576 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030015577
Jesse Barnes79e53942008-11-07 14:24:08 -080015578 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010015579
15580 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030015581
15582 mutex_lock(&dev->struct_mutex);
15583 intel_cleanup_gt_powersave(dev);
15584 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080015585}
15586
Dave Airlie28d52042009-09-21 14:33:58 +100015587/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080015588 * Return which encoder is currently attached for connector.
15589 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010015590struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080015591{
Chris Wilsondf0e9242010-09-09 16:20:55 +010015592 return &intel_attached_encoder(connector)->base;
15593}
Jesse Barnes79e53942008-11-07 14:24:08 -080015594
Chris Wilsondf0e9242010-09-09 16:20:55 +010015595void intel_connector_attach_encoder(struct intel_connector *connector,
15596 struct intel_encoder *encoder)
15597{
15598 connector->encoder = encoder;
15599 drm_mode_connector_attach_encoder(&connector->base,
15600 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080015601}
Dave Airlie28d52042009-09-21 14:33:58 +100015602
15603/*
15604 * set vga decode state - true == enable VGA decode
15605 */
15606int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15607{
15608 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000015609 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100015610 u16 gmch_ctrl;
15611
Chris Wilson75fa0412014-02-07 18:37:02 -020015612 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15613 DRM_ERROR("failed to read control word\n");
15614 return -EIO;
15615 }
15616
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020015617 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15618 return 0;
15619
Dave Airlie28d52042009-09-21 14:33:58 +100015620 if (state)
15621 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15622 else
15623 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020015624
15625 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15626 DRM_ERROR("failed to write control word\n");
15627 return -EIO;
15628 }
15629
Dave Airlie28d52042009-09-21 14:33:58 +100015630 return 0;
15631}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015632
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015633struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015634
15635 u32 power_well_driver;
15636
Chris Wilson63b66e52013-08-08 15:12:06 +020015637 int num_transcoders;
15638
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015639 struct intel_cursor_error_state {
15640 u32 control;
15641 u32 position;
15642 u32 base;
15643 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010015644 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015645
15646 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015647 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015648 u32 source;
Imre Deakf301b1e12014-04-18 15:55:04 +030015649 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010015650 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015651
15652 struct intel_plane_error_state {
15653 u32 control;
15654 u32 stride;
15655 u32 size;
15656 u32 pos;
15657 u32 addr;
15658 u32 surface;
15659 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010015660 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020015661
15662 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015663 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020015664 enum transcoder cpu_transcoder;
15665
15666 u32 conf;
15667
15668 u32 htotal;
15669 u32 hblank;
15670 u32 hsync;
15671 u32 vtotal;
15672 u32 vblank;
15673 u32 vsync;
15674 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015675};
15676
15677struct intel_display_error_state *
15678intel_display_capture_error_state(struct drm_device *dev)
15679{
Jani Nikulafbee40d2014-03-31 14:27:18 +030015680 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015681 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020015682 int transcoders[] = {
15683 TRANSCODER_A,
15684 TRANSCODER_B,
15685 TRANSCODER_C,
15686 TRANSCODER_EDP,
15687 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015688 int i;
15689
Chris Wilson63b66e52013-08-08 15:12:06 +020015690 if (INTEL_INFO(dev)->num_pipes == 0)
15691 return NULL;
15692
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015693 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015694 if (error == NULL)
15695 return NULL;
15696
Imre Deak190be112013-11-25 17:15:31 +020015697 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015698 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15699
Damien Lespiau055e3932014-08-18 13:49:10 +010015700 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020015701 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015702 __intel_display_power_is_enabled(dev_priv,
15703 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020015704 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015705 continue;
15706
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030015707 error->cursor[i].control = I915_READ(CURCNTR(i));
15708 error->cursor[i].position = I915_READ(CURPOS(i));
15709 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015710
15711 error->plane[i].control = I915_READ(DSPCNTR(i));
15712 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015713 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030015714 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015715 error->plane[i].pos = I915_READ(DSPPOS(i));
15716 }
Paulo Zanonica291362013-03-06 20:03:14 -030015717 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15718 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015719 if (INTEL_INFO(dev)->gen >= 4) {
15720 error->plane[i].surface = I915_READ(DSPSURF(i));
15721 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15722 }
15723
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015724 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e12014-04-18 15:55:04 +030015725
Sonika Jindal3abfce72014-07-21 15:23:43 +053015726 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e12014-04-18 15:55:04 +030015727 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020015728 }
15729
15730 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
15731 if (HAS_DDI(dev_priv->dev))
15732 error->num_transcoders++; /* Account for eDP. */
15733
15734 for (i = 0; i < error->num_transcoders; i++) {
15735 enum transcoder cpu_transcoder = transcoders[i];
15736
Imre Deakddf9c532013-11-27 22:02:02 +020015737 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015738 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020015739 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015740 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015741 continue;
15742
Chris Wilson63b66e52013-08-08 15:12:06 +020015743 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15744
15745 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15746 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15747 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15748 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15749 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15750 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15751 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015752 }
15753
15754 return error;
15755}
15756
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015757#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15758
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015759void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015760intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015761 struct drm_device *dev,
15762 struct intel_display_error_state *error)
15763{
Damien Lespiau055e3932014-08-18 13:49:10 +010015764 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015765 int i;
15766
Chris Wilson63b66e52013-08-08 15:12:06 +020015767 if (!error)
15768 return;
15769
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015770 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020015771 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015772 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015773 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010015774 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015775 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020015776 err_printf(m, " Power: %s\n",
15777 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015778 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e12014-04-18 15:55:04 +030015779 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015780
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015781 err_printf(m, "Plane [%d]:\n", i);
15782 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15783 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015784 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015785 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15786 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015787 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030015788 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015789 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015790 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015791 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15792 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015793 }
15794
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015795 err_printf(m, "Cursor [%d]:\n", i);
15796 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15797 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15798 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015799 }
Chris Wilson63b66e52013-08-08 15:12:06 +020015800
15801 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010015802 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020015803 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015804 err_printf(m, " Power: %s\n",
15805 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020015806 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15807 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15808 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15809 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15810 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15811 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15812 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15813 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015814}
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015815
15816void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
15817{
15818 struct intel_crtc *crtc;
15819
15820 for_each_intel_crtc(dev, crtc) {
15821 struct intel_unpin_work *work;
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015822
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015823 spin_lock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015824
15825 work = crtc->unpin_work;
15826
15827 if (work && work->event &&
15828 work->event->base.file_priv == file) {
15829 kfree(work->event);
15830 work->event = NULL;
15831 }
15832
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015833 spin_unlock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015834 }
15835}