blob: 8aa803848f3597a658437cf88d5d81c89fa7fe0a [file] [log] [blame]
Jesse Barnes79e53942008-11-07 14:24:08 -08001/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
Daniel Vetter618563e2012-04-01 13:38:50 +020027#include <linux/dmi.h>
Jesse Barnesc1c7af62009-09-10 15:28:03 -070028#include <linux/module.h>
29#include <linux/input.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080030#include <linux/i2c.h>
Shaohua Li7662c8b2009-06-26 11:23:55 +080031#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Jesse Barnes9cce37f2010-08-13 15:11:26 -070033#include <linux/vgaarb.h>
Wu Fengguange0dac652011-09-05 14:25:34 +080034#include <drm/drm_edid.h>
David Howells760285e2012-10-02 18:01:07 +010035#include <drm/drmP.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include "intel_drv.h"
David Howells760285e2012-10-02 18:01:07 +010037#include <drm/i915_drm.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080038#include "i915_drv.h"
Jesse Barnese5510fa2010-07-01 16:48:37 -070039#include "i915_trace.h"
Xi Ruoyao319c1d42015-03-12 20:16:32 +080040#include <drm/drm_atomic.h>
Matt Roperc196e1d2015-01-21 16:35:48 -080041#include <drm/drm_atomic_helper.h>
David Howells760285e2012-10-02 18:01:07 +010042#include <drm/drm_dp_helper.h>
43#include <drm/drm_crtc_helper.h>
Matt Roper465c1202014-05-29 08:06:54 -070044#include <drm/drm_plane_helper.h>
45#include <drm/drm_rect.h>
Keith Packardc0f372b32011-11-16 22:24:52 -080046#include <linux/dma_remapping.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080047
Matt Roper465c1202014-05-29 08:06:54 -070048/* Primary plane formats for gen <= 3 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010049static const uint32_t i8xx_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010050 DRM_FORMAT_C8,
51 DRM_FORMAT_RGB565,
Matt Roper465c1202014-05-29 08:06:54 -070052 DRM_FORMAT_XRGB1555,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010053 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070054};
55
56/* Primary plane formats for gen >= 4 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010057static const uint32_t i965_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010058 DRM_FORMAT_C8,
59 DRM_FORMAT_RGB565,
60 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070061 DRM_FORMAT_XBGR8888,
Damien Lespiau6c0fd452015-05-19 12:29:16 +010062 DRM_FORMAT_XRGB2101010,
63 DRM_FORMAT_XBGR2101010,
64};
65
66static const uint32_t skl_primary_formats[] = {
67 DRM_FORMAT_C8,
68 DRM_FORMAT_RGB565,
69 DRM_FORMAT_XRGB8888,
70 DRM_FORMAT_XBGR8888,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010071 DRM_FORMAT_ARGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070072 DRM_FORMAT_ABGR8888,
73 DRM_FORMAT_XRGB2101010,
Matt Roper465c1202014-05-29 08:06:54 -070074 DRM_FORMAT_XBGR2101010,
Matt Roper465c1202014-05-29 08:06:54 -070075};
76
Matt Roper3d7d6512014-06-10 08:28:13 -070077/* Cursor formats */
78static const uint32_t intel_cursor_formats[] = {
79 DRM_FORMAT_ARGB8888,
80};
81
Chris Wilson6b383a72010-09-13 13:54:26 +010082static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
Jesse Barnes79e53942008-11-07 14:24:08 -080083
Jesse Barnesf1f644d2013-06-27 00:39:25 +030084static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020085 struct intel_crtc_state *pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +030086static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020087 struct intel_crtc_state *pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030088
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030089static int intel_set_mode(struct drm_crtc *crtc,
Ander Conselvan de Oliveirae62d8dc2015-06-16 11:49:43 +030090 struct drm_atomic_state *state,
91 bool force_restore);
Jesse Barneseb1bfe82014-02-12 12:26:25 -080092static int intel_framebuffer_init(struct drm_device *dev,
93 struct intel_framebuffer *ifb,
94 struct drm_mode_fb_cmd2 *mode_cmd,
95 struct drm_i915_gem_object *obj);
Daniel Vetter5b18e572014-04-24 23:55:06 +020096static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
97static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +020098static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -070099 struct intel_link_m_n *m_n,
100 struct intel_link_m_n *m2_n2);
Daniel Vetter29407aa2014-04-24 23:55:08 +0200101static void ironlake_set_pipeconf(struct drm_crtc *crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +0200102static void haswell_set_pipeconf(struct drm_crtc *crtc);
103static void intel_set_pipe_csc(struct drm_crtc *crtc);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200104static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200105 const struct intel_crtc_state *pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200106static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200107 const struct intel_crtc_state *pipe_config);
Matt Roperea2c67b2014-12-23 10:41:52 -0800108static void intel_begin_crtc_commit(struct drm_crtc *crtc);
109static void intel_finish_crtc_commit(struct drm_crtc *crtc);
Chandra Konduru549e2bf2015-04-07 15:28:38 -0700110static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
111 struct intel_crtc_state *crtc_state);
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200112static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
113 int num_connectors);
Maarten Lankhorstce22dba2015-04-21 17:12:56 +0300114static void intel_crtc_enable_planes(struct drm_crtc *crtc);
115static void intel_crtc_disable_planes(struct drm_crtc *crtc);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100116
Dave Airlie0e32b392014-05-02 14:02:48 +1000117static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
118{
119 if (!connector->mst_port)
120 return connector->encoder;
121 else
122 return &connector->mst_port->mst_encoders[pipe]->base;
123}
124
Jesse Barnes79e53942008-11-07 14:24:08 -0800125typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400126 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -0800127} intel_range_t;
128
129typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400130 int dot_limit;
131 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800132} intel_p2_t;
133
Ma Lingd4906092009-03-18 20:13:27 +0800134typedef struct intel_limit intel_limit_t;
135struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -0400136 intel_range_t dot, vco, n, m, m1, m2, p, p1;
137 intel_p2_t p2;
Ma Lingd4906092009-03-18 20:13:27 +0800138};
Jesse Barnes79e53942008-11-07 14:24:08 -0800139
Daniel Vetterd2acd212012-10-20 20:57:43 +0200140int
141intel_pch_rawclk(struct drm_device *dev)
142{
143 struct drm_i915_private *dev_priv = dev->dev_private;
144
145 WARN_ON(!HAS_PCH_SPLIT(dev));
146
147 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
148}
149
Chris Wilson021357a2010-09-07 20:54:59 +0100150static inline u32 /* units of 100MHz */
151intel_fdi_link_freq(struct drm_device *dev)
152{
Chris Wilson8b99e682010-10-13 09:59:17 +0100153 if (IS_GEN5(dev)) {
154 struct drm_i915_private *dev_priv = dev->dev_private;
155 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
156 } else
157 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +0100158}
159
Daniel Vetter5d536e22013-07-06 12:52:06 +0200160static const intel_limit_t intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400161 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200162 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200163 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400164 .m = { .min = 96, .max = 140 },
165 .m1 = { .min = 18, .max = 26 },
166 .m2 = { .min = 6, .max = 16 },
167 .p = { .min = 4, .max = 128 },
168 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700169 .p2 = { .dot_limit = 165000,
170 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700171};
172
Daniel Vetter5d536e22013-07-06 12:52:06 +0200173static const intel_limit_t intel_limits_i8xx_dvo = {
174 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200175 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200176 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200177 .m = { .min = 96, .max = 140 },
178 .m1 = { .min = 18, .max = 26 },
179 .m2 = { .min = 6, .max = 16 },
180 .p = { .min = 4, .max = 128 },
181 .p1 = { .min = 2, .max = 33 },
182 .p2 = { .dot_limit = 165000,
183 .p2_slow = 4, .p2_fast = 4 },
184};
185
Keith Packarde4b36692009-06-05 19:22:17 -0700186static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400187 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200188 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200189 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400190 .m = { .min = 96, .max = 140 },
191 .m1 = { .min = 18, .max = 26 },
192 .m2 = { .min = 6, .max = 16 },
193 .p = { .min = 4, .max = 128 },
194 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700195 .p2 = { .dot_limit = 165000,
196 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700197};
Eric Anholt273e27c2011-03-30 13:01:10 -0700198
Keith Packarde4b36692009-06-05 19:22:17 -0700199static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400200 .dot = { .min = 20000, .max = 400000 },
201 .vco = { .min = 1400000, .max = 2800000 },
202 .n = { .min = 1, .max = 6 },
203 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100204 .m1 = { .min = 8, .max = 18 },
205 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400206 .p = { .min = 5, .max = 80 },
207 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700208 .p2 = { .dot_limit = 200000,
209 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700210};
211
212static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400213 .dot = { .min = 20000, .max = 400000 },
214 .vco = { .min = 1400000, .max = 2800000 },
215 .n = { .min = 1, .max = 6 },
216 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100217 .m1 = { .min = 8, .max = 18 },
218 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400219 .p = { .min = 7, .max = 98 },
220 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700221 .p2 = { .dot_limit = 112000,
222 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700223};
224
Eric Anholt273e27c2011-03-30 13:01:10 -0700225
Keith Packarde4b36692009-06-05 19:22:17 -0700226static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700227 .dot = { .min = 25000, .max = 270000 },
228 .vco = { .min = 1750000, .max = 3500000},
229 .n = { .min = 1, .max = 4 },
230 .m = { .min = 104, .max = 138 },
231 .m1 = { .min = 17, .max = 23 },
232 .m2 = { .min = 5, .max = 11 },
233 .p = { .min = 10, .max = 30 },
234 .p1 = { .min = 1, .max = 3},
235 .p2 = { .dot_limit = 270000,
236 .p2_slow = 10,
237 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800238 },
Keith Packarde4b36692009-06-05 19:22:17 -0700239};
240
241static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700242 .dot = { .min = 22000, .max = 400000 },
243 .vco = { .min = 1750000, .max = 3500000},
244 .n = { .min = 1, .max = 4 },
245 .m = { .min = 104, .max = 138 },
246 .m1 = { .min = 16, .max = 23 },
247 .m2 = { .min = 5, .max = 11 },
248 .p = { .min = 5, .max = 80 },
249 .p1 = { .min = 1, .max = 8},
250 .p2 = { .dot_limit = 165000,
251 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700252};
253
254static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700255 .dot = { .min = 20000, .max = 115000 },
256 .vco = { .min = 1750000, .max = 3500000 },
257 .n = { .min = 1, .max = 3 },
258 .m = { .min = 104, .max = 138 },
259 .m1 = { .min = 17, .max = 23 },
260 .m2 = { .min = 5, .max = 11 },
261 .p = { .min = 28, .max = 112 },
262 .p1 = { .min = 2, .max = 8 },
263 .p2 = { .dot_limit = 0,
264 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800265 },
Keith Packarde4b36692009-06-05 19:22:17 -0700266};
267
268static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700269 .dot = { .min = 80000, .max = 224000 },
270 .vco = { .min = 1750000, .max = 3500000 },
271 .n = { .min = 1, .max = 3 },
272 .m = { .min = 104, .max = 138 },
273 .m1 = { .min = 17, .max = 23 },
274 .m2 = { .min = 5, .max = 11 },
275 .p = { .min = 14, .max = 42 },
276 .p1 = { .min = 2, .max = 6 },
277 .p2 = { .dot_limit = 0,
278 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800279 },
Keith Packarde4b36692009-06-05 19:22:17 -0700280};
281
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500282static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400283 .dot = { .min = 20000, .max = 400000},
284 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700285 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400286 .n = { .min = 3, .max = 6 },
287 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700288 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400289 .m1 = { .min = 0, .max = 0 },
290 .m2 = { .min = 0, .max = 254 },
291 .p = { .min = 5, .max = 80 },
292 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700293 .p2 = { .dot_limit = 200000,
294 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700295};
296
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500297static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400298 .dot = { .min = 20000, .max = 400000 },
299 .vco = { .min = 1700000, .max = 3500000 },
300 .n = { .min = 3, .max = 6 },
301 .m = { .min = 2, .max = 256 },
302 .m1 = { .min = 0, .max = 0 },
303 .m2 = { .min = 0, .max = 254 },
304 .p = { .min = 7, .max = 112 },
305 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700306 .p2 = { .dot_limit = 112000,
307 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700308};
309
Eric Anholt273e27c2011-03-30 13:01:10 -0700310/* Ironlake / Sandybridge
311 *
312 * We calculate clock using (register_value + 2) for N/M1/M2, so here
313 * the range value for them is (actual_value - 2).
314 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800315static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700316 .dot = { .min = 25000, .max = 350000 },
317 .vco = { .min = 1760000, .max = 3510000 },
318 .n = { .min = 1, .max = 5 },
319 .m = { .min = 79, .max = 127 },
320 .m1 = { .min = 12, .max = 22 },
321 .m2 = { .min = 5, .max = 9 },
322 .p = { .min = 5, .max = 80 },
323 .p1 = { .min = 1, .max = 8 },
324 .p2 = { .dot_limit = 225000,
325 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700326};
327
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800328static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700329 .dot = { .min = 25000, .max = 350000 },
330 .vco = { .min = 1760000, .max = 3510000 },
331 .n = { .min = 1, .max = 3 },
332 .m = { .min = 79, .max = 118 },
333 .m1 = { .min = 12, .max = 22 },
334 .m2 = { .min = 5, .max = 9 },
335 .p = { .min = 28, .max = 112 },
336 .p1 = { .min = 2, .max = 8 },
337 .p2 = { .dot_limit = 225000,
338 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800339};
340
341static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700342 .dot = { .min = 25000, .max = 350000 },
343 .vco = { .min = 1760000, .max = 3510000 },
344 .n = { .min = 1, .max = 3 },
345 .m = { .min = 79, .max = 127 },
346 .m1 = { .min = 12, .max = 22 },
347 .m2 = { .min = 5, .max = 9 },
348 .p = { .min = 14, .max = 56 },
349 .p1 = { .min = 2, .max = 8 },
350 .p2 = { .dot_limit = 225000,
351 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800352};
353
Eric Anholt273e27c2011-03-30 13:01:10 -0700354/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800355static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700356 .dot = { .min = 25000, .max = 350000 },
357 .vco = { .min = 1760000, .max = 3510000 },
358 .n = { .min = 1, .max = 2 },
359 .m = { .min = 79, .max = 126 },
360 .m1 = { .min = 12, .max = 22 },
361 .m2 = { .min = 5, .max = 9 },
362 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400363 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700364 .p2 = { .dot_limit = 225000,
365 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800366};
367
368static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700369 .dot = { .min = 25000, .max = 350000 },
370 .vco = { .min = 1760000, .max = 3510000 },
371 .n = { .min = 1, .max = 3 },
372 .m = { .min = 79, .max = 126 },
373 .m1 = { .min = 12, .max = 22 },
374 .m2 = { .min = 5, .max = 9 },
375 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400376 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700377 .p2 = { .dot_limit = 225000,
378 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800379};
380
Ville Syrjälädc730512013-09-24 21:26:30 +0300381static const intel_limit_t intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300382 /*
383 * These are the data rate limits (measured in fast clocks)
384 * since those are the strictest limits we have. The fast
385 * clock and actual rate limits are more relaxed, so checking
386 * them would make no difference.
387 */
388 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200389 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700390 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700391 .m1 = { .min = 2, .max = 3 },
392 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300393 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300394 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700395};
396
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300397static const intel_limit_t intel_limits_chv = {
398 /*
399 * These are the data rate limits (measured in fast clocks)
400 * since those are the strictest limits we have. The fast
401 * clock and actual rate limits are more relaxed, so checking
402 * them would make no difference.
403 */
404 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200405 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300406 .n = { .min = 1, .max = 1 },
407 .m1 = { .min = 2, .max = 2 },
408 .m2 = { .min = 24 << 22, .max = 175 << 22 },
409 .p1 = { .min = 2, .max = 4 },
410 .p2 = { .p2_slow = 1, .p2_fast = 14 },
411};
412
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200413static const intel_limit_t intel_limits_bxt = {
414 /* FIXME: find real dot limits */
415 .dot = { .min = 0, .max = INT_MAX },
416 .vco = { .min = 4800000, .max = 6480000 },
417 .n = { .min = 1, .max = 1 },
418 .m1 = { .min = 2, .max = 2 },
419 /* FIXME: find real m2 limits */
420 .m2 = { .min = 2 << 22, .max = 255 << 22 },
421 .p1 = { .min = 2, .max = 4 },
422 .p2 = { .p2_slow = 1, .p2_fast = 20 },
423};
424
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300425static void vlv_clock(int refclk, intel_clock_t *clock)
426{
427 clock->m = clock->m1 * clock->m2;
428 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200429 if (WARN_ON(clock->n == 0 || clock->p == 0))
430 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300431 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
432 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300433}
434
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300435/**
436 * Returns whether any output on the specified pipe is of the specified type
437 */
Damien Lespiau40935612014-10-29 11:16:59 +0000438bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300439{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300440 struct drm_device *dev = crtc->base.dev;
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300441 struct intel_encoder *encoder;
442
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300443 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300444 if (encoder->type == type)
445 return true;
446
447 return false;
448}
449
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200450/**
451 * Returns whether any output on the specified pipe will have the specified
452 * type after a staged modeset is complete, i.e., the same as
453 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
454 * encoder->crtc.
455 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200456static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
457 int type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200458{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200459 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300460 struct drm_connector *connector;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200461 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200462 struct intel_encoder *encoder;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200463 int i, num_connectors = 0;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200464
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300465 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200466 if (connector_state->crtc != crtc_state->base.crtc)
467 continue;
468
469 num_connectors++;
470
471 encoder = to_intel_encoder(connector_state->best_encoder);
472 if (encoder->type == type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200473 return true;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200474 }
475
476 WARN_ON(num_connectors == 0);
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200477
478 return false;
479}
480
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200481static const intel_limit_t *
482intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800483{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200484 struct drm_device *dev = crtc_state->base.crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800485 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800486
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200487 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100488 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000489 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800490 limit = &intel_limits_ironlake_dual_lvds_100m;
491 else
492 limit = &intel_limits_ironlake_dual_lvds;
493 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000494 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800495 limit = &intel_limits_ironlake_single_lvds_100m;
496 else
497 limit = &intel_limits_ironlake_single_lvds;
498 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200499 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800500 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800501
502 return limit;
503}
504
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200505static const intel_limit_t *
506intel_g4x_limit(struct intel_crtc_state *crtc_state)
Ma Ling044c7c42009-03-18 20:13:23 +0800507{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200508 struct drm_device *dev = crtc_state->base.crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800509 const intel_limit_t *limit;
510
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200511 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100512 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700513 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800514 else
Keith Packarde4b36692009-06-05 19:22:17 -0700515 limit = &intel_limits_g4x_single_channel_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200516 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
517 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700518 limit = &intel_limits_g4x_hdmi;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200519 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700520 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800521 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700522 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800523
524 return limit;
525}
526
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200527static const intel_limit_t *
528intel_limit(struct intel_crtc_state *crtc_state, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800529{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200530 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800531 const intel_limit_t *limit;
532
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200533 if (IS_BROXTON(dev))
534 limit = &intel_limits_bxt;
535 else if (HAS_PCH_SPLIT(dev))
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200536 limit = intel_ironlake_limit(crtc_state, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800537 else if (IS_G4X(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200538 limit = intel_g4x_limit(crtc_state);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500539 } else if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200540 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500541 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800542 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500543 limit = &intel_limits_pineview_sdvo;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300544 } else if (IS_CHERRYVIEW(dev)) {
545 limit = &intel_limits_chv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700546 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjälädc730512013-09-24 21:26:30 +0300547 limit = &intel_limits_vlv;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100548 } else if (!IS_GEN2(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200549 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100550 limit = &intel_limits_i9xx_lvds;
551 else
552 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800553 } else {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200554 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700555 limit = &intel_limits_i8xx_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200556 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Keith Packarde4b36692009-06-05 19:22:17 -0700557 limit = &intel_limits_i8xx_dvo;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200558 else
559 limit = &intel_limits_i8xx_dac;
Jesse Barnes79e53942008-11-07 14:24:08 -0800560 }
561 return limit;
562}
563
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500564/* m1 is reserved as 0 in Pineview, n is a ring counter */
565static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800566{
Shaohua Li21778322009-02-23 15:19:16 +0800567 clock->m = clock->m2 + 2;
568 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200569 if (WARN_ON(clock->n == 0 || clock->p == 0))
570 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300571 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
572 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Shaohua Li21778322009-02-23 15:19:16 +0800573}
574
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200575static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
576{
577 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
578}
579
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200580static void i9xx_clock(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800581{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200582 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800583 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200584 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
585 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300586 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
587 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Jesse Barnes79e53942008-11-07 14:24:08 -0800588}
589
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300590static void chv_clock(int refclk, intel_clock_t *clock)
591{
592 clock->m = clock->m1 * clock->m2;
593 clock->p = clock->p1 * clock->p2;
594 if (WARN_ON(clock->n == 0 || clock->p == 0))
595 return;
596 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
597 clock->n << 22);
598 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
599}
600
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800601#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800602/**
603 * Returns whether the given set of divisors are valid for a given refclk with
604 * the given connectors.
605 */
606
Chris Wilson1b894b52010-12-14 20:04:54 +0000607static bool intel_PLL_is_valid(struct drm_device *dev,
608 const intel_limit_t *limit,
609 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800610{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300611 if (clock->n < limit->n.min || limit->n.max < clock->n)
612 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800613 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400614 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800615 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400616 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800617 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400618 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300619
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200620 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300621 if (clock->m1 <= clock->m2)
622 INTELPllInvalid("m1 <= m2\n");
623
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200624 if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300625 if (clock->p < limit->p.min || limit->p.max < clock->p)
626 INTELPllInvalid("p out of range\n");
627 if (clock->m < limit->m.min || limit->m.max < clock->m)
628 INTELPllInvalid("m out of range\n");
629 }
630
Jesse Barnes79e53942008-11-07 14:24:08 -0800631 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400632 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800633 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
634 * connector, etc., rather than just a single range.
635 */
636 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400637 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800638
639 return true;
640}
641
Ma Lingd4906092009-03-18 20:13:27 +0800642static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200643i9xx_find_best_dpll(const intel_limit_t *limit,
644 struct intel_crtc_state *crtc_state,
Sean Paulcec2f352012-01-10 15:09:36 -0800645 int target, int refclk, intel_clock_t *match_clock,
646 intel_clock_t *best_clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800647{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200648 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300649 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800650 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800651 int err = target;
652
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200653 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800654 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100655 * For LVDS just rely on its current settings for dual-channel.
656 * We haven't figured out how to reliably set up different
657 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800658 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100659 if (intel_is_dual_link_lvds(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800660 clock.p2 = limit->p2.p2_fast;
661 else
662 clock.p2 = limit->p2.p2_slow;
663 } else {
664 if (target < limit->p2.dot_limit)
665 clock.p2 = limit->p2.p2_slow;
666 else
667 clock.p2 = limit->p2.p2_fast;
668 }
669
Akshay Joshi0206e352011-08-16 15:34:10 -0400670 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800671
Zhao Yakui42158662009-11-20 11:24:18 +0800672 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
673 clock.m1++) {
674 for (clock.m2 = limit->m2.min;
675 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200676 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800677 break;
678 for (clock.n = limit->n.min;
679 clock.n <= limit->n.max; clock.n++) {
680 for (clock.p1 = limit->p1.min;
681 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800682 int this_err;
683
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200684 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000685 if (!intel_PLL_is_valid(dev, limit,
686 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800687 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800688 if (match_clock &&
689 clock.p != match_clock->p)
690 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800691
692 this_err = abs(clock.dot - target);
693 if (this_err < err) {
694 *best_clock = clock;
695 err = this_err;
696 }
697 }
698 }
699 }
700 }
701
702 return (err != target);
703}
704
Ma Lingd4906092009-03-18 20:13:27 +0800705static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200706pnv_find_best_dpll(const intel_limit_t *limit,
707 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200708 int target, int refclk, intel_clock_t *match_clock,
709 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200710{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200711 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300712 struct drm_device *dev = crtc->base.dev;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200713 intel_clock_t clock;
714 int err = target;
715
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200716 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200717 /*
718 * For LVDS just rely on its current settings for dual-channel.
719 * We haven't figured out how to reliably set up different
720 * single/dual channel state, if we even can.
721 */
722 if (intel_is_dual_link_lvds(dev))
723 clock.p2 = limit->p2.p2_fast;
724 else
725 clock.p2 = limit->p2.p2_slow;
726 } else {
727 if (target < limit->p2.dot_limit)
728 clock.p2 = limit->p2.p2_slow;
729 else
730 clock.p2 = limit->p2.p2_fast;
731 }
732
733 memset(best_clock, 0, sizeof(*best_clock));
734
735 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
736 clock.m1++) {
737 for (clock.m2 = limit->m2.min;
738 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200739 for (clock.n = limit->n.min;
740 clock.n <= limit->n.max; clock.n++) {
741 for (clock.p1 = limit->p1.min;
742 clock.p1 <= limit->p1.max; clock.p1++) {
743 int this_err;
744
745 pineview_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800746 if (!intel_PLL_is_valid(dev, limit,
747 &clock))
748 continue;
749 if (match_clock &&
750 clock.p != match_clock->p)
751 continue;
752
753 this_err = abs(clock.dot - target);
754 if (this_err < err) {
755 *best_clock = clock;
756 err = this_err;
757 }
758 }
759 }
760 }
761 }
762
763 return (err != target);
764}
765
Ma Lingd4906092009-03-18 20:13:27 +0800766static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200767g4x_find_best_dpll(const intel_limit_t *limit,
768 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200769 int target, int refclk, intel_clock_t *match_clock,
770 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800771{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200772 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300773 struct drm_device *dev = crtc->base.dev;
Ma Lingd4906092009-03-18 20:13:27 +0800774 intel_clock_t clock;
775 int max_n;
776 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400777 /* approximately equals target * 0.00585 */
778 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800779 found = false;
780
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200781 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100782 if (intel_is_dual_link_lvds(dev))
Ma Lingd4906092009-03-18 20:13:27 +0800783 clock.p2 = limit->p2.p2_fast;
784 else
785 clock.p2 = limit->p2.p2_slow;
786 } else {
787 if (target < limit->p2.dot_limit)
788 clock.p2 = limit->p2.p2_slow;
789 else
790 clock.p2 = limit->p2.p2_fast;
791 }
792
793 memset(best_clock, 0, sizeof(*best_clock));
794 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200795 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800796 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200797 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800798 for (clock.m1 = limit->m1.max;
799 clock.m1 >= limit->m1.min; clock.m1--) {
800 for (clock.m2 = limit->m2.max;
801 clock.m2 >= limit->m2.min; clock.m2--) {
802 for (clock.p1 = limit->p1.max;
803 clock.p1 >= limit->p1.min; clock.p1--) {
804 int this_err;
805
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200806 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000807 if (!intel_PLL_is_valid(dev, limit,
808 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800809 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000810
811 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800812 if (this_err < err_most) {
813 *best_clock = clock;
814 err_most = this_err;
815 max_n = clock.n;
816 found = true;
817 }
818 }
819 }
820 }
821 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800822 return found;
823}
Ma Lingd4906092009-03-18 20:13:27 +0800824
Imre Deakd5dd62b2015-03-17 11:40:03 +0200825/*
826 * Check if the calculated PLL configuration is more optimal compared to the
827 * best configuration and error found so far. Return the calculated error.
828 */
829static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
830 const intel_clock_t *calculated_clock,
831 const intel_clock_t *best_clock,
832 unsigned int best_error_ppm,
833 unsigned int *error_ppm)
834{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200835 /*
836 * For CHV ignore the error and consider only the P value.
837 * Prefer a bigger P value based on HW requirements.
838 */
839 if (IS_CHERRYVIEW(dev)) {
840 *error_ppm = 0;
841
842 return calculated_clock->p > best_clock->p;
843 }
844
Imre Deak24be4e42015-03-17 11:40:04 +0200845 if (WARN_ON_ONCE(!target_freq))
846 return false;
847
Imre Deakd5dd62b2015-03-17 11:40:03 +0200848 *error_ppm = div_u64(1000000ULL *
849 abs(target_freq - calculated_clock->dot),
850 target_freq);
851 /*
852 * Prefer a better P value over a better (smaller) error if the error
853 * is small. Ensure this preference for future configurations too by
854 * setting the error to 0.
855 */
856 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
857 *error_ppm = 0;
858
859 return true;
860 }
861
862 return *error_ppm + 10 < best_error_ppm;
863}
864
Zhenyu Wang2c072452009-06-05 15:38:42 +0800865static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200866vlv_find_best_dpll(const intel_limit_t *limit,
867 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200868 int target, int refclk, intel_clock_t *match_clock,
869 intel_clock_t *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700870{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200871 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300872 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300873 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300874 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300875 /* min update 19.2 MHz */
876 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300877 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700878
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300879 target *= 5; /* fast clock */
880
881 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700882
883 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300884 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300885 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300886 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300887 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300888 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700889 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300890 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200891 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300892
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300893 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
894 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300895
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300896 vlv_clock(refclk, &clock);
897
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300898 if (!intel_PLL_is_valid(dev, limit,
899 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300900 continue;
901
Imre Deakd5dd62b2015-03-17 11:40:03 +0200902 if (!vlv_PLL_is_optimal(dev, target,
903 &clock,
904 best_clock,
905 bestppm, &ppm))
906 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300907
Imre Deakd5dd62b2015-03-17 11:40:03 +0200908 *best_clock = clock;
909 bestppm = ppm;
910 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700911 }
912 }
913 }
914 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700915
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300916 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700917}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700918
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300919static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200920chv_find_best_dpll(const intel_limit_t *limit,
921 struct intel_crtc_state *crtc_state,
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300922 int target, int refclk, intel_clock_t *match_clock,
923 intel_clock_t *best_clock)
924{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200925 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300926 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200927 unsigned int best_error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300928 intel_clock_t clock;
929 uint64_t m2;
930 int found = false;
931
932 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200933 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300934
935 /*
936 * Based on hardware doc, the n always set to 1, and m1 always
937 * set to 2. If requires to support 200Mhz refclk, we need to
938 * revisit this because n may not 1 anymore.
939 */
940 clock.n = 1, clock.m1 = 2;
941 target *= 5; /* fast clock */
942
943 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
944 for (clock.p2 = limit->p2.p2_fast;
945 clock.p2 >= limit->p2.p2_slow;
946 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +0200947 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300948
949 clock.p = clock.p1 * clock.p2;
950
951 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
952 clock.n) << 22, refclk * clock.m1);
953
954 if (m2 > INT_MAX/clock.m1)
955 continue;
956
957 clock.m2 = m2;
958
959 chv_clock(refclk, &clock);
960
961 if (!intel_PLL_is_valid(dev, limit, &clock))
962 continue;
963
Imre Deak9ca3ba02015-03-17 11:40:05 +0200964 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
965 best_error_ppm, &error_ppm))
966 continue;
967
968 *best_clock = clock;
969 best_error_ppm = error_ppm;
970 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300971 }
972 }
973
974 return found;
975}
976
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200977bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
978 intel_clock_t *best_clock)
979{
980 int refclk = i9xx_get_refclk(crtc_state, 0);
981
982 return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
983 target_clock, refclk, NULL, best_clock);
984}
985
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300986bool intel_crtc_active(struct drm_crtc *crtc)
987{
988 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
989
990 /* Be paranoid as we can arrive here with only partial
991 * state retrieved from the hardware during setup.
992 *
Damien Lespiau241bfc32013-09-25 16:45:37 +0100993 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300994 * as Haswell has gained clock readout/fastboot support.
995 *
Dave Airlie66e514c2014-04-03 07:51:54 +1000996 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300997 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -0700998 *
999 * FIXME: The intel_crtc->active here should be switched to
1000 * crtc->state->active once we have proper CRTC states wired up
1001 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001002 */
Matt Roperc3d1f432015-03-09 10:19:23 -07001003 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001004 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001005}
1006
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001007enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1008 enum pipe pipe)
1009{
1010 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1011 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1012
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001013 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001014}
1015
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001016static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1017{
1018 struct drm_i915_private *dev_priv = dev->dev_private;
1019 u32 reg = PIPEDSL(pipe);
1020 u32 line1, line2;
1021 u32 line_mask;
1022
1023 if (IS_GEN2(dev))
1024 line_mask = DSL_LINEMASK_GEN2;
1025 else
1026 line_mask = DSL_LINEMASK_GEN3;
1027
1028 line1 = I915_READ(reg) & line_mask;
1029 mdelay(5);
1030 line2 = I915_READ(reg) & line_mask;
1031
1032 return line1 == line2;
1033}
1034
Keith Packardab7ad7f2010-10-03 00:33:06 -07001035/*
1036 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001037 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001038 *
1039 * After disabling a pipe, we can't wait for vblank in the usual way,
1040 * spinning on the vblank interrupt status bit, since we won't actually
1041 * see an interrupt when the pipe is disabled.
1042 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001043 * On Gen4 and above:
1044 * wait for the pipe register state bit to turn off
1045 *
1046 * Otherwise:
1047 * wait for the display line value to settle (it usually
1048 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001049 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001050 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001051static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001052{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001053 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001054 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001055 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001056 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001057
Keith Packardab7ad7f2010-10-03 00:33:06 -07001058 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001059 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001060
Keith Packardab7ad7f2010-10-03 00:33:06 -07001061 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +01001062 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1063 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001064 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001065 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001066 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001067 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001068 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001069 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001070}
1071
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001072/*
1073 * ibx_digital_port_connected - is the specified port connected?
1074 * @dev_priv: i915 private structure
1075 * @port: the port to test
1076 *
1077 * Returns true if @port is connected, false otherwise.
1078 */
1079bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
1080 struct intel_digital_port *port)
1081{
1082 u32 bit;
1083
Damien Lespiauc36346e2012-12-13 16:09:03 +00001084 if (HAS_PCH_IBX(dev_priv->dev)) {
Robin Schroereba905b2014-05-18 02:24:50 +02001085 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +00001086 case PORT_B:
1087 bit = SDE_PORTB_HOTPLUG;
1088 break;
1089 case PORT_C:
1090 bit = SDE_PORTC_HOTPLUG;
1091 break;
1092 case PORT_D:
1093 bit = SDE_PORTD_HOTPLUG;
1094 break;
1095 default:
1096 return true;
1097 }
1098 } else {
Robin Schroereba905b2014-05-18 02:24:50 +02001099 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +00001100 case PORT_B:
1101 bit = SDE_PORTB_HOTPLUG_CPT;
1102 break;
1103 case PORT_C:
1104 bit = SDE_PORTC_HOTPLUG_CPT;
1105 break;
1106 case PORT_D:
1107 bit = SDE_PORTD_HOTPLUG_CPT;
1108 break;
1109 default:
1110 return true;
1111 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001112 }
1113
1114 return I915_READ(SDEISR) & bit;
1115}
1116
Jesse Barnesb24e7172011-01-04 15:09:30 -08001117static const char *state_string(bool enabled)
1118{
1119 return enabled ? "on" : "off";
1120}
1121
1122/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001123void assert_pll(struct drm_i915_private *dev_priv,
1124 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001125{
1126 int reg;
1127 u32 val;
1128 bool cur_state;
1129
1130 reg = DPLL(pipe);
1131 val = I915_READ(reg);
1132 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001133 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001134 "PLL state assertion failure (expected %s, current %s)\n",
1135 state_string(state), state_string(cur_state));
1136}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001137
Jani Nikula23538ef2013-08-27 15:12:22 +03001138/* XXX: the dsi pll is shared between MIPI DSI ports */
1139static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1140{
1141 u32 val;
1142 bool cur_state;
1143
Ville Syrjäläa5805162015-05-26 20:42:30 +03001144 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001145 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001146 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001147
1148 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001149 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001150 "DSI PLL state assertion failure (expected %s, current %s)\n",
1151 state_string(state), state_string(cur_state));
1152}
1153#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1154#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1155
Daniel Vetter55607e82013-06-16 21:42:39 +02001156struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +02001157intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -08001158{
Daniel Vettere2b78262013-06-07 23:10:03 +02001159 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1160
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001161 if (crtc->config->shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +02001162 return NULL;
1163
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001164 return &dev_priv->shared_dplls[crtc->config->shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +02001165}
1166
Jesse Barnesb24e7172011-01-04 15:09:30 -08001167/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +02001168void assert_shared_dpll(struct drm_i915_private *dev_priv,
1169 struct intel_shared_dpll *pll,
1170 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001171{
Jesse Barnes040484a2011-01-03 12:14:26 -08001172 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +02001173 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001174
Chris Wilson92b27b02012-05-20 18:10:50 +01001175 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +02001176 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001177 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001178
Daniel Vetter53589012013-06-05 13:34:16 +02001179 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Rob Clarke2c719b2014-12-15 13:56:32 -05001180 I915_STATE_WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +02001181 "%s assertion failure (expected %s, current %s)\n",
1182 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001183}
Jesse Barnes040484a2011-01-03 12:14:26 -08001184
1185static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1186 enum pipe pipe, bool state)
1187{
1188 int reg;
1189 u32 val;
1190 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001191 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1192 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001193
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001194 if (HAS_DDI(dev_priv->dev)) {
1195 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -02001196 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001197 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -02001198 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001199 } else {
1200 reg = FDI_TX_CTL(pipe);
1201 val = I915_READ(reg);
1202 cur_state = !!(val & FDI_TX_ENABLE);
1203 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001204 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001205 "FDI TX state assertion failure (expected %s, current %s)\n",
1206 state_string(state), state_string(cur_state));
1207}
1208#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1209#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1210
1211static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1212 enum pipe pipe, bool state)
1213{
1214 int reg;
1215 u32 val;
1216 bool cur_state;
1217
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001218 reg = FDI_RX_CTL(pipe);
1219 val = I915_READ(reg);
1220 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001221 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001222 "FDI RX state assertion failure (expected %s, current %s)\n",
1223 state_string(state), state_string(cur_state));
1224}
1225#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1226#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1227
1228static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1229 enum pipe pipe)
1230{
1231 int reg;
1232 u32 val;
1233
1234 /* ILK FDI PLL is always enabled */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001235 if (INTEL_INFO(dev_priv->dev)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001236 return;
1237
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001238 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001239 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001240 return;
1241
Jesse Barnes040484a2011-01-03 12:14:26 -08001242 reg = FDI_TX_CTL(pipe);
1243 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001244 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 -08001245}
1246
Daniel Vetter55607e82013-06-16 21:42:39 +02001247void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1248 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001249{
1250 int reg;
1251 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001252 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001253
1254 reg = FDI_RX_CTL(pipe);
1255 val = I915_READ(reg);
Daniel Vetter55607e82013-06-16 21:42:39 +02001256 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001257 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001258 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1259 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001260}
1261
Daniel Vetterb680c372014-09-19 18:27:27 +02001262void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1263 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001264{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001265 struct drm_device *dev = dev_priv->dev;
1266 int pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001267 u32 val;
1268 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001269 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001270
Jani Nikulabedd4db2014-08-22 15:04:13 +03001271 if (WARN_ON(HAS_DDI(dev)))
1272 return;
1273
1274 if (HAS_PCH_SPLIT(dev)) {
1275 u32 port_sel;
1276
Jesse Barnesea0760c2011-01-04 15:09:32 -08001277 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001278 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1279
1280 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1281 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1282 panel_pipe = PIPE_B;
1283 /* XXX: else fix for eDP */
1284 } else if (IS_VALLEYVIEW(dev)) {
1285 /* presumably write lock depends on pipe, not port select */
1286 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1287 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001288 } else {
1289 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001290 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1291 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001292 }
1293
1294 val = I915_READ(pp_reg);
1295 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001296 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001297 locked = false;
1298
Rob Clarke2c719b2014-12-15 13:56:32 -05001299 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001300 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001301 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001302}
1303
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001304static void assert_cursor(struct drm_i915_private *dev_priv,
1305 enum pipe pipe, bool state)
1306{
1307 struct drm_device *dev = dev_priv->dev;
1308 bool cur_state;
1309
Paulo Zanonid9d82082014-02-27 16:30:56 -03001310 if (IS_845G(dev) || IS_I865G(dev))
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001311 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001312 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001313 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001314
Rob Clarke2c719b2014-12-15 13:56:32 -05001315 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001316 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1317 pipe_name(pipe), state_string(state), state_string(cur_state));
1318}
1319#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1320#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1321
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001322void assert_pipe(struct drm_i915_private *dev_priv,
1323 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001324{
1325 int reg;
1326 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001327 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001328 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1329 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001330
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001331 /* if we need the pipe quirk it must be always on */
1332 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1333 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001334 state = true;
1335
Daniel Vetterf458ebb2014-09-30 10:56:39 +02001336 if (!intel_display_power_is_enabled(dev_priv,
Paulo Zanonib97186f2013-05-03 12:15:36 -03001337 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001338 cur_state = false;
1339 } else {
1340 reg = PIPECONF(cpu_transcoder);
1341 val = I915_READ(reg);
1342 cur_state = !!(val & PIPECONF_ENABLE);
1343 }
1344
Rob Clarke2c719b2014-12-15 13:56:32 -05001345 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001346 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001347 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001348}
1349
Chris Wilson931872f2012-01-16 23:01:13 +00001350static void assert_plane(struct drm_i915_private *dev_priv,
1351 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001352{
1353 int reg;
1354 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001355 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001356
1357 reg = DSPCNTR(plane);
1358 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001359 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001360 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001361 "plane %c assertion failure (expected %s, current %s)\n",
1362 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001363}
1364
Chris Wilson931872f2012-01-16 23:01:13 +00001365#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1366#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1367
Jesse Barnesb24e7172011-01-04 15:09:30 -08001368static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1369 enum pipe pipe)
1370{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001371 struct drm_device *dev = dev_priv->dev;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001372 int reg, i;
1373 u32 val;
1374 int cur_pipe;
1375
Ville Syrjälä653e1022013-06-04 13:49:05 +03001376 /* Primary planes are fixed to pipes on gen4+ */
1377 if (INTEL_INFO(dev)->gen >= 4) {
Adam Jackson28c057942011-10-07 14:38:42 -04001378 reg = DSPCNTR(pipe);
1379 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001380 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001381 "plane %c assertion failure, should be disabled but not\n",
1382 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001383 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001384 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001385
Jesse Barnesb24e7172011-01-04 15:09:30 -08001386 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001387 for_each_pipe(dev_priv, i) {
Jesse Barnesb24e7172011-01-04 15:09:30 -08001388 reg = DSPCNTR(i);
1389 val = I915_READ(reg);
1390 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1391 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001392 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001393 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1394 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001395 }
1396}
1397
Jesse Barnes19332d72013-03-28 09:55:38 -07001398static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1399 enum pipe pipe)
1400{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001401 struct drm_device *dev = dev_priv->dev;
Damien Lespiau1fe47782014-03-03 17:31:47 +00001402 int reg, sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001403 u32 val;
1404
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001405 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001406 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001407 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001408 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001409 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1410 sprite, pipe_name(pipe));
1411 }
1412 } else if (IS_VALLEYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001413 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +00001414 reg = SPCNTR(pipe, sprite);
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001415 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001416 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001417 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001418 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001419 }
1420 } else if (INTEL_INFO(dev)->gen >= 7) {
1421 reg = SPRCTL(pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001422 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001423 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001424 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001425 plane_name(pipe), pipe_name(pipe));
1426 } else if (INTEL_INFO(dev)->gen >= 5) {
1427 reg = DVSCNTR(pipe);
1428 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001429 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001430 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1431 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001432 }
1433}
1434
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001435static void assert_vblank_disabled(struct drm_crtc *crtc)
1436{
Rob Clarke2c719b2014-12-15 13:56:32 -05001437 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001438 drm_crtc_vblank_put(crtc);
1439}
1440
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001441static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
Jesse Barnes92f25842011-01-04 15:09:34 -08001442{
1443 u32 val;
1444 bool enabled;
1445
Rob Clarke2c719b2014-12-15 13:56:32 -05001446 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001447
Jesse Barnes92f25842011-01-04 15:09:34 -08001448 val = I915_READ(PCH_DREF_CONTROL);
1449 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1450 DREF_SUPERSPREAD_SOURCE_MASK));
Rob Clarke2c719b2014-12-15 13:56:32 -05001451 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
Jesse Barnes92f25842011-01-04 15:09:34 -08001452}
1453
Daniel Vetterab9412b2013-05-03 11:49:46 +02001454static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1455 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001456{
1457 int reg;
1458 u32 val;
1459 bool enabled;
1460
Daniel Vetterab9412b2013-05-03 11:49:46 +02001461 reg = PCH_TRANSCONF(pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001462 val = I915_READ(reg);
1463 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001464 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001465 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1466 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001467}
1468
Keith Packard4e634382011-08-06 10:39:45 -07001469static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1470 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001471{
1472 if ((val & DP_PORT_EN) == 0)
1473 return false;
1474
1475 if (HAS_PCH_CPT(dev_priv->dev)) {
1476 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1477 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1478 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1479 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001480 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1481 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1482 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001483 } else {
1484 if ((val & DP_PIPE_MASK) != (pipe << 30))
1485 return false;
1486 }
1487 return true;
1488}
1489
Keith Packard1519b992011-08-06 10:35:34 -07001490static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1491 enum pipe pipe, u32 val)
1492{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001493 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001494 return false;
1495
1496 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001497 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001498 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001499 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1500 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1501 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001502 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001503 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001504 return false;
1505 }
1506 return true;
1507}
1508
1509static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1510 enum pipe pipe, u32 val)
1511{
1512 if ((val & LVDS_PORT_EN) == 0)
1513 return false;
1514
1515 if (HAS_PCH_CPT(dev_priv->dev)) {
1516 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1517 return false;
1518 } else {
1519 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1520 return false;
1521 }
1522 return true;
1523}
1524
1525static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1526 enum pipe pipe, u32 val)
1527{
1528 if ((val & ADPA_DAC_ENABLE) == 0)
1529 return false;
1530 if (HAS_PCH_CPT(dev_priv->dev)) {
1531 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1532 return false;
1533 } else {
1534 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1535 return false;
1536 }
1537 return true;
1538}
1539
Jesse Barnes291906f2011-02-02 12:28:03 -08001540static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001541 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001542{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001543 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001544 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001545 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001546 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001547
Rob Clarke2c719b2014-12-15 13:56:32 -05001548 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001549 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001550 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001551}
1552
1553static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1554 enum pipe pipe, int reg)
1555{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001556 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001557 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001558 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001559 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001560
Rob Clarke2c719b2014-12-15 13:56:32 -05001561 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001562 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001563 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001564}
1565
1566static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1567 enum pipe pipe)
1568{
1569 int reg;
1570 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001571
Keith Packardf0575e92011-07-25 22:12:43 -07001572 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1573 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1574 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001575
1576 reg = PCH_ADPA;
1577 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001578 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001579 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001580 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001581
1582 reg = PCH_LVDS;
1583 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001584 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001585 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001586 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001587
Paulo Zanonie2debe92013-02-18 19:00:27 -03001588 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1589 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1590 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001591}
1592
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001593static void intel_init_dpio(struct drm_device *dev)
1594{
1595 struct drm_i915_private *dev_priv = dev->dev_private;
1596
1597 if (!IS_VALLEYVIEW(dev))
1598 return;
1599
Chon Ming Leea09cadd2014-04-09 13:28:14 +03001600 /*
1601 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1602 * CHV x1 PHY (DP/HDMI D)
1603 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1604 */
1605 if (IS_CHERRYVIEW(dev)) {
1606 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1607 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1608 } else {
1609 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1610 }
Jesse Barnes5382f5f352013-12-16 16:34:24 -08001611}
1612
Ville Syrjäläd288f652014-10-28 13:20:22 +02001613static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001614 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001615{
Daniel Vetter426115c2013-07-11 22:13:42 +02001616 struct drm_device *dev = crtc->base.dev;
1617 struct drm_i915_private *dev_priv = dev->dev_private;
1618 int reg = DPLL(crtc->pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02001619 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001620
Daniel Vetter426115c2013-07-11 22:13:42 +02001621 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001622
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001623 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001624 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1625
1626 /* PLL is protected by panel, make sure we can write it */
Jani Nikula6a9e7362014-08-22 15:06:35 +03001627 if (IS_MOBILE(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001628 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001629
Daniel Vetter426115c2013-07-11 22:13:42 +02001630 I915_WRITE(reg, dpll);
1631 POSTING_READ(reg);
1632 udelay(150);
1633
1634 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1635 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1636
Ville Syrjäläd288f652014-10-28 13:20:22 +02001637 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
Daniel Vetter426115c2013-07-11 22:13:42 +02001638 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001639
1640 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001641 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001642 POSTING_READ(reg);
1643 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001644 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001645 POSTING_READ(reg);
1646 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001647 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001648 POSTING_READ(reg);
1649 udelay(150); /* wait for warmup */
1650}
1651
Ville Syrjäläd288f652014-10-28 13:20:22 +02001652static void chv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001653 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001654{
1655 struct drm_device *dev = crtc->base.dev;
1656 struct drm_i915_private *dev_priv = dev->dev_private;
1657 int pipe = crtc->pipe;
1658 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001659 u32 tmp;
1660
1661 assert_pipe_disabled(dev_priv, crtc->pipe);
1662
1663 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1664
Ville Syrjäläa5805162015-05-26 20:42:30 +03001665 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001666
1667 /* Enable back the 10bit clock to display controller */
1668 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1669 tmp |= DPIO_DCLKP_EN;
1670 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1671
Ville Syrjälä54433e92015-05-26 20:42:31 +03001672 mutex_unlock(&dev_priv->sb_lock);
1673
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001674 /*
1675 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1676 */
1677 udelay(1);
1678
1679 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001680 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001681
1682 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001683 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001684 DRM_ERROR("PLL %d failed to lock\n", pipe);
1685
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001686 /* not sure when this should be written */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001687 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001688 POSTING_READ(DPLL_MD(pipe));
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001689}
1690
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001691static int intel_num_dvo_pipes(struct drm_device *dev)
1692{
1693 struct intel_crtc *crtc;
1694 int count = 0;
1695
1696 for_each_intel_crtc(dev, crtc)
1697 count += crtc->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001698 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001699
1700 return count;
1701}
1702
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001703static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001704{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001705 struct drm_device *dev = crtc->base.dev;
1706 struct drm_i915_private *dev_priv = dev->dev_private;
1707 int reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001708 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001709
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001710 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001711
1712 /* No really, not for ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001713 BUG_ON(INTEL_INFO(dev)->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001714
1715 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001716 if (IS_MOBILE(dev) && !IS_I830(dev))
1717 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001718
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001719 /* Enable DVO 2x clock on both PLLs if necessary */
1720 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1721 /*
1722 * It appears to be important that we don't enable this
1723 * for the current pipe before otherwise configuring the
1724 * PLL. No idea how this should be handled if multiple
1725 * DVO outputs are enabled simultaneosly.
1726 */
1727 dpll |= DPLL_DVO_2X_MODE;
1728 I915_WRITE(DPLL(!crtc->pipe),
1729 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1730 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001731
1732 /* Wait for the clocks to stabilize. */
1733 POSTING_READ(reg);
1734 udelay(150);
1735
1736 if (INTEL_INFO(dev)->gen >= 4) {
1737 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001738 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001739 } else {
1740 /* The pixel multiplier can only be updated once the
1741 * DPLL is enabled and the clocks are stable.
1742 *
1743 * So write it again.
1744 */
1745 I915_WRITE(reg, dpll);
1746 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001747
1748 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001749 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001750 POSTING_READ(reg);
1751 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001752 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001753 POSTING_READ(reg);
1754 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001755 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001756 POSTING_READ(reg);
1757 udelay(150); /* wait for warmup */
1758}
1759
1760/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001761 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001762 * @dev_priv: i915 private structure
1763 * @pipe: pipe PLL to disable
1764 *
1765 * Disable the PLL for @pipe, making sure the pipe is off first.
1766 *
1767 * Note! This is for pre-ILK only.
1768 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001769static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001770{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001771 struct drm_device *dev = crtc->base.dev;
1772 struct drm_i915_private *dev_priv = dev->dev_private;
1773 enum pipe pipe = crtc->pipe;
1774
1775 /* Disable DVO 2x clock on both PLLs if necessary */
1776 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001777 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001778 intel_num_dvo_pipes(dev) == 1) {
1779 I915_WRITE(DPLL(PIPE_B),
1780 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1781 I915_WRITE(DPLL(PIPE_A),
1782 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1783 }
1784
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001785 /* Don't disable pipe or pipe PLLs if needed */
1786 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1787 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001788 return;
1789
1790 /* Make sure the pipe isn't still relying on us */
1791 assert_pipe_disabled(dev_priv, pipe);
1792
Daniel Vetter50b44a42013-06-05 13:34:33 +02001793 I915_WRITE(DPLL(pipe), 0);
1794 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001795}
1796
Jesse Barnesf6071162013-10-01 10:41:38 -07001797static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1798{
1799 u32 val = 0;
1800
1801 /* Make sure the pipe isn't still relying on us */
1802 assert_pipe_disabled(dev_priv, pipe);
1803
Imre Deake5cbfbf2014-01-09 17:08:16 +02001804 /*
1805 * Leave integrated clock source and reference clock enabled for pipe B.
1806 * The latter is needed for VGA hotplug / manual detection.
1807 */
Jesse Barnesf6071162013-10-01 10:41:38 -07001808 if (pipe == PIPE_B)
Imre Deake5cbfbf2014-01-09 17:08:16 +02001809 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07001810 I915_WRITE(DPLL(pipe), val);
1811 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001812
1813}
1814
1815static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1816{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001817 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001818 u32 val;
1819
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001820 /* Make sure the pipe isn't still relying on us */
1821 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001822
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001823 /* Set PLL en = 0 */
Ville Syrjäläd17ec4c2014-06-28 02:03:59 +03001824 val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001825 if (pipe != PIPE_A)
1826 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1827 I915_WRITE(DPLL(pipe), val);
1828 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001829
Ville Syrjäläa5805162015-05-26 20:42:30 +03001830 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001831
1832 /* Disable 10bit clock to display controller */
1833 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1834 val &= ~DPIO_DCLKP_EN;
1835 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1836
Ville Syrjälä61407f62014-05-27 16:32:55 +03001837 /* disable left/right clock distribution */
1838 if (pipe != PIPE_B) {
1839 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1840 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1841 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1842 } else {
1843 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1844 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1845 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1846 }
1847
Ville Syrjäläa5805162015-05-26 20:42:30 +03001848 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001849}
1850
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001851void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001852 struct intel_digital_port *dport,
1853 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001854{
1855 u32 port_mask;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001856 int dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001857
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001858 switch (dport->port) {
1859 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001860 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001861 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001862 break;
1863 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001864 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001865 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001866 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001867 break;
1868 case PORT_D:
1869 port_mask = DPLL_PORTD_READY_MASK;
1870 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001871 break;
1872 default:
1873 BUG();
1874 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001875
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001876 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1877 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1878 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001879}
1880
Daniel Vetterb14b1052014-04-24 23:55:13 +02001881static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1882{
1883 struct drm_device *dev = crtc->base.dev;
1884 struct drm_i915_private *dev_priv = dev->dev_private;
1885 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1886
Chris Wilsonbe19f0f2014-05-28 16:16:42 +01001887 if (WARN_ON(pll == NULL))
1888 return;
1889
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001890 WARN_ON(!pll->config.crtc_mask);
Daniel Vetterb14b1052014-04-24 23:55:13 +02001891 if (pll->active == 0) {
1892 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1893 WARN_ON(pll->on);
1894 assert_shared_dpll_disabled(dev_priv, pll);
1895
1896 pll->mode_set(dev_priv, pll);
1897 }
1898}
1899
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001900/**
Daniel Vetter85b38942014-04-24 23:55:14 +02001901 * intel_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001902 * @dev_priv: i915 private structure
1903 * @pipe: pipe PLL to enable
1904 *
1905 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1906 * drives the transcoder clock.
1907 */
Daniel Vetter85b38942014-04-24 23:55:14 +02001908static void intel_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001909{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001910 struct drm_device *dev = crtc->base.dev;
1911 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001912 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001913
Daniel Vetter87a875b2013-06-05 13:34:19 +02001914 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001915 return;
1916
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001917 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001918 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001919
Damien Lespiau74dd6922014-07-29 18:06:17 +01001920 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
Daniel Vetter46edb022013-06-05 13:34:12 +02001921 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001922 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001923
Daniel Vettercdbd2312013-06-05 13:34:03 +02001924 if (pll->active++) {
1925 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001926 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001927 return;
1928 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001929 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001930
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001931 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1932
Daniel Vetter46edb022013-06-05 13:34:12 +02001933 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001934 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001935 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001936}
1937
Damien Lespiauf6daaec2014-08-09 23:00:56 +01001938static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001939{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001940 struct drm_device *dev = crtc->base.dev;
1941 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001942 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001943
Jesse Barnes92f25842011-01-04 15:09:34 -08001944 /* PCH only available on ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001945 BUG_ON(INTEL_INFO(dev)->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001946 if (WARN_ON(pll == NULL))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001947 return;
1948
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001949 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001950 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001951
Daniel Vetter46edb022013-06-05 13:34:12 +02001952 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1953 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001954 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001955
Chris Wilson48da64a2012-05-13 20:16:12 +01001956 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001957 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001958 return;
1959 }
1960
Daniel Vettere9d69442013-06-05 13:34:15 +02001961 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001962 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001963 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001964 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001965
Daniel Vetter46edb022013-06-05 13:34:12 +02001966 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001967 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001968 pll->on = false;
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001969
1970 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
Jesse Barnes92f25842011-01-04 15:09:34 -08001971}
1972
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001973static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1974 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001975{
Daniel Vetter23670b322012-11-01 09:15:30 +01001976 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001977 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001978 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001979 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001980
1981 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001982 BUG_ON(!HAS_PCH_SPLIT(dev));
Jesse Barnes040484a2011-01-03 12:14:26 -08001983
1984 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001985 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001986 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001987
1988 /* FDI must be feeding us bits for PCH ports */
1989 assert_fdi_tx_enabled(dev_priv, pipe);
1990 assert_fdi_rx_enabled(dev_priv, pipe);
1991
Daniel Vetter23670b322012-11-01 09:15:30 +01001992 if (HAS_PCH_CPT(dev)) {
1993 /* Workaround: Set the timing override bit before enabling the
1994 * pch transcoder. */
1995 reg = TRANS_CHICKEN2(pipe);
1996 val = I915_READ(reg);
1997 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1998 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001999 }
Daniel Vetter23670b322012-11-01 09:15:30 +01002000
Daniel Vetterab9412b2013-05-03 11:49:46 +02002001 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002002 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002003 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07002004
2005 if (HAS_PCH_IBX(dev_priv->dev)) {
2006 /*
2007 * make the BPC in transcoder be consistent with
2008 * that in pipeconf reg.
2009 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002010 val &= ~PIPECONF_BPC_MASK;
2011 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07002012 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002013
2014 val &= ~TRANS_INTERLACE_MASK;
2015 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002016 if (HAS_PCH_IBX(dev_priv->dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002017 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002018 val |= TRANS_LEGACY_INTERLACED_ILK;
2019 else
2020 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002021 else
2022 val |= TRANS_PROGRESSIVE;
2023
Jesse Barnes040484a2011-01-03 12:14:26 -08002024 I915_WRITE(reg, val | TRANS_ENABLE);
2025 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002026 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08002027}
2028
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002029static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02002030 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08002031{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002032 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002033
2034 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03002035 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002036
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002037 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01002038 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02002039 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002040
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002041 /* Workaround: set timing override bit. */
2042 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002043 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002044 I915_WRITE(_TRANSA_CHICKEN2, val);
2045
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02002046 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02002047 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002048
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02002049 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
2050 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02002051 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002052 else
2053 val |= TRANS_PROGRESSIVE;
2054
Daniel Vetterab9412b2013-05-03 11:49:46 +02002055 I915_WRITE(LPT_TRANSCONF, val);
2056 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02002057 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002058}
2059
Paulo Zanonib8a4f402012-10-31 18:12:42 -02002060static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
2061 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08002062{
Daniel Vetter23670b322012-11-01 09:15:30 +01002063 struct drm_device *dev = dev_priv->dev;
2064 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08002065
2066 /* FDI relies on the transcoder */
2067 assert_fdi_tx_disabled(dev_priv, pipe);
2068 assert_fdi_rx_disabled(dev_priv, pipe);
2069
Jesse Barnes291906f2011-02-02 12:28:03 -08002070 /* Ports must be off as well */
2071 assert_pch_ports_disabled(dev_priv, pipe);
2072
Daniel Vetterab9412b2013-05-03 11:49:46 +02002073 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002074 val = I915_READ(reg);
2075 val &= ~TRANS_ENABLE;
2076 I915_WRITE(reg, val);
2077 /* wait for PCH transcoder off, transcoder state */
2078 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002079 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01002080
2081 if (!HAS_PCH_IBX(dev)) {
2082 /* Workaround: Clear the timing override chicken bit again. */
2083 reg = TRANS_CHICKEN2(pipe);
2084 val = I915_READ(reg);
2085 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2086 I915_WRITE(reg, val);
2087 }
Jesse Barnes040484a2011-01-03 12:14:26 -08002088}
2089
Paulo Zanoniab4d9662012-10-31 18:12:55 -02002090static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002091{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002092 u32 val;
2093
Daniel Vetterab9412b2013-05-03 11:49:46 +02002094 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002095 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02002096 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002097 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02002098 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02002099 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002100
2101 /* Workaround: clear timing override bit. */
2102 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002103 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002104 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08002105}
2106
2107/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002108 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02002109 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08002110 *
Paulo Zanoni03722642014-01-17 13:51:09 -02002111 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08002112 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002113 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02002114static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002115{
Paulo Zanoni03722642014-01-17 13:51:09 -02002116 struct drm_device *dev = crtc->base.dev;
2117 struct drm_i915_private *dev_priv = dev->dev_private;
2118 enum pipe pipe = crtc->pipe;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002119 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2120 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002121 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002122 int reg;
2123 u32 val;
2124
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002125 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002126 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002127 assert_sprites_disabled(dev_priv, pipe);
2128
Paulo Zanoni681e5812012-12-06 11:12:38 -02002129 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002130 pch_transcoder = TRANSCODER_A;
2131 else
2132 pch_transcoder = pipe;
2133
Jesse Barnesb24e7172011-01-04 15:09:30 -08002134 /*
2135 * A pipe without a PLL won't actually be able to drive bits from
2136 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2137 * need the check.
2138 */
Imre Deak50360402015-01-16 00:55:16 -08002139 if (HAS_GMCH_DISPLAY(dev_priv->dev))
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002140 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03002141 assert_dsi_pll_enabled(dev_priv);
2142 else
2143 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002144 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002145 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002146 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002147 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002148 assert_fdi_tx_pll_enabled(dev_priv,
2149 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08002150 }
2151 /* FIXME: assert CPU port conditions for SNB+ */
2152 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08002153
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002154 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002155 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002156 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002157 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2158 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002159 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002160 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002161
2162 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002163 POSTING_READ(reg);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002164}
2165
2166/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002167 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002168 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002169 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002170 * Disable the pipe of @crtc, making sure that various hardware
2171 * specific requirements are met, if applicable, e.g. plane
2172 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002173 *
2174 * Will wait until the pipe has shut down before returning.
2175 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002176static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002177{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002178 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002179 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002180 enum pipe pipe = crtc->pipe;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002181 int reg;
2182 u32 val;
2183
2184 /*
2185 * Make sure planes won't keep trying to pump pixels to us,
2186 * or we might hang the display.
2187 */
2188 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002189 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002190 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002191
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002192 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002193 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002194 if ((val & PIPECONF_ENABLE) == 0)
2195 return;
2196
Ville Syrjälä67adc642014-08-15 01:21:57 +03002197 /*
2198 * Double wide has implications for planes
2199 * so best keep it disabled when not needed.
2200 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002201 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002202 val &= ~PIPECONF_DOUBLE_WIDE;
2203
2204 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002205 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2206 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002207 val &= ~PIPECONF_ENABLE;
2208
2209 I915_WRITE(reg, val);
2210 if ((val & PIPECONF_ENABLE) == 0)
2211 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002212}
2213
2214/**
Matt Roper262ca2b2014-03-18 17:22:55 -07002215 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002216 * @plane: plane to be enabled
2217 * @crtc: crtc for the plane
Jesse Barnesb24e7172011-01-04 15:09:30 -08002218 *
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002219 * Enable @plane on @crtc, making sure that the pipe is running first.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002220 */
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002221static void intel_enable_primary_hw_plane(struct drm_plane *plane,
2222 struct drm_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002223{
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002224 struct drm_device *dev = plane->dev;
2225 struct drm_i915_private *dev_priv = dev->dev_private;
2226 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002227
2228 /* If the pipe isn't enabled, we can't pump pixels and may hang */
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002229 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002230 to_intel_plane_state(plane->state)->visible = true;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002231
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002232 dev_priv->display.update_primary_plane(crtc, plane->fb,
2233 crtc->x, crtc->y);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002234}
2235
Chris Wilson693db182013-03-05 14:52:39 +00002236static bool need_vtd_wa(struct drm_device *dev)
2237{
2238#ifdef CONFIG_INTEL_IOMMU
2239 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2240 return true;
2241#endif
2242 return false;
2243}
2244
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002245unsigned int
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002246intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
2247 uint64_t fb_format_modifier)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002248{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002249 unsigned int tile_height;
2250 uint32_t pixel_bytes;
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002251
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002252 switch (fb_format_modifier) {
2253 case DRM_FORMAT_MOD_NONE:
2254 tile_height = 1;
2255 break;
2256 case I915_FORMAT_MOD_X_TILED:
2257 tile_height = IS_GEN2(dev) ? 16 : 8;
2258 break;
2259 case I915_FORMAT_MOD_Y_TILED:
2260 tile_height = 32;
2261 break;
2262 case I915_FORMAT_MOD_Yf_TILED:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002263 pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
2264 switch (pixel_bytes) {
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002265 default:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002266 case 1:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002267 tile_height = 64;
2268 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002269 case 2:
2270 case 4:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002271 tile_height = 32;
2272 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002273 case 8:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002274 tile_height = 16;
2275 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002276 case 16:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002277 WARN_ONCE(1,
2278 "128-bit pixels are not supported for display!");
2279 tile_height = 16;
2280 break;
2281 }
2282 break;
2283 default:
2284 MISSING_CASE(fb_format_modifier);
2285 tile_height = 1;
2286 break;
2287 }
Daniel Vetter091df6c2015-02-10 17:16:10 +00002288
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002289 return tile_height;
2290}
2291
2292unsigned int
2293intel_fb_align_height(struct drm_device *dev, unsigned int height,
2294 uint32_t pixel_format, uint64_t fb_format_modifier)
2295{
2296 return ALIGN(height, intel_tile_height(dev, pixel_format,
2297 fb_format_modifier));
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002298}
2299
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002300static int
2301intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
2302 const struct drm_plane_state *plane_state)
2303{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002304 struct intel_rotation_info *info = &view->rotation_info;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002305
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002306 *view = i915_ggtt_view_normal;
2307
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002308 if (!plane_state)
2309 return 0;
2310
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002311 if (!intel_rotation_90_or_270(plane_state->rotation))
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002312 return 0;
2313
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002314 *view = i915_ggtt_view_rotated;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002315
2316 info->height = fb->height;
2317 info->pixel_format = fb->pixel_format;
2318 info->pitch = fb->pitches[0];
2319 info->fb_modifier = fb->modifier[0];
2320
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002321 return 0;
2322}
2323
Chris Wilson127bd2a2010-07-23 23:32:05 +01002324int
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002325intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2326 struct drm_framebuffer *fb,
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002327 const struct drm_plane_state *plane_state,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002328 struct intel_engine_cs *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002329{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002330 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002331 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002332 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002333 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002334 u32 alignment;
2335 int ret;
2336
Matt Roperebcdd392014-07-09 16:22:11 -07002337 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2338
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002339 switch (fb->modifier[0]) {
2340 case DRM_FORMAT_MOD_NONE:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002341 if (INTEL_INFO(dev)->gen >= 9)
2342 alignment = 256 * 1024;
2343 else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
Chris Wilson534843d2010-07-05 18:01:46 +01002344 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002345 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01002346 alignment = 4 * 1024;
2347 else
2348 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002349 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002350 case I915_FORMAT_MOD_X_TILED:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002351 if (INTEL_INFO(dev)->gen >= 9)
2352 alignment = 256 * 1024;
2353 else {
2354 /* pin() will align the object as required by fence */
2355 alignment = 0;
2356 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002357 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002358 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiau1327b9a2015-02-27 11:15:20 +00002359 case I915_FORMAT_MOD_Yf_TILED:
2360 if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2361 "Y tiling bo slipped through, driver bug!\n"))
2362 return -EINVAL;
2363 alignment = 1 * 1024 * 1024;
2364 break;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002365 default:
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002366 MISSING_CASE(fb->modifier[0]);
2367 return -EINVAL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002368 }
2369
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002370 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2371 if (ret)
2372 return ret;
2373
Chris Wilson693db182013-03-05 14:52:39 +00002374 /* Note that the w/a also requires 64 PTE of padding following the
2375 * bo. We currently fill all unused PTE with the shadow page and so
2376 * we should always have valid PTE following the scanout preventing
2377 * the VT-d warning.
2378 */
2379 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2380 alignment = 256 * 1024;
2381
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002382 /*
2383 * Global gtt pte registers are special registers which actually forward
2384 * writes to a chunk of system memory. Which means that there is no risk
2385 * that the register values disappear as soon as we call
2386 * intel_runtime_pm_put(), so it is correct to wrap only the
2387 * pin/unpin/fence and not more.
2388 */
2389 intel_runtime_pm_get(dev_priv);
2390
Chris Wilsonce453d82011-02-21 14:43:56 +00002391 dev_priv->mm.interruptible = false;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00002392 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002393 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002394 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00002395 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002396
2397 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2398 * fence, whereas 965+ only requires a fence if using
2399 * framebuffer compression. For simplicity, we always install
2400 * a fence as the cost is not that onerous.
2401 */
Chris Wilson06d98132012-04-17 15:31:24 +01002402 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002403 if (ret)
2404 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002405
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002406 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002407
Chris Wilsonce453d82011-02-21 14:43:56 +00002408 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002409 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002410 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002411
2412err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002413 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilsonce453d82011-02-21 14:43:56 +00002414err_interruptible:
2415 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002416 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002417 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002418}
2419
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002420static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
2421 const struct drm_plane_state *plane_state)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002422{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002423 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002424 struct i915_ggtt_view view;
2425 int ret;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002426
Matt Roperebcdd392014-07-09 16:22:11 -07002427 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2428
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002429 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2430 WARN_ONCE(ret, "Couldn't get view from plane state!");
2431
Chris Wilson1690e1e2011-12-14 13:57:08 +01002432 i915_gem_object_unpin_fence(obj);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002433 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002434}
2435
Daniel Vetterc2c75132012-07-05 12:17:30 +02002436/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2437 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00002438unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2439 unsigned int tiling_mode,
2440 unsigned int cpp,
2441 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002442{
Chris Wilsonbc752862013-02-21 20:04:31 +00002443 if (tiling_mode != I915_TILING_NONE) {
2444 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002445
Chris Wilsonbc752862013-02-21 20:04:31 +00002446 tile_rows = *y / 8;
2447 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002448
Chris Wilsonbc752862013-02-21 20:04:31 +00002449 tiles = *x / (512/cpp);
2450 *x %= 512/cpp;
2451
2452 return tile_rows * pitch * 8 + tiles * 4096;
2453 } else {
2454 unsigned int offset;
2455
2456 offset = *y * pitch + *x * cpp;
2457 *y = 0;
2458 *x = (offset & 4095) / cpp;
2459 return offset & -4096;
2460 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002461}
2462
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002463static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002464{
2465 switch (format) {
2466 case DISPPLANE_8BPP:
2467 return DRM_FORMAT_C8;
2468 case DISPPLANE_BGRX555:
2469 return DRM_FORMAT_XRGB1555;
2470 case DISPPLANE_BGRX565:
2471 return DRM_FORMAT_RGB565;
2472 default:
2473 case DISPPLANE_BGRX888:
2474 return DRM_FORMAT_XRGB8888;
2475 case DISPPLANE_RGBX888:
2476 return DRM_FORMAT_XBGR8888;
2477 case DISPPLANE_BGRX101010:
2478 return DRM_FORMAT_XRGB2101010;
2479 case DISPPLANE_RGBX101010:
2480 return DRM_FORMAT_XBGR2101010;
2481 }
2482}
2483
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002484static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2485{
2486 switch (format) {
2487 case PLANE_CTL_FORMAT_RGB_565:
2488 return DRM_FORMAT_RGB565;
2489 default:
2490 case PLANE_CTL_FORMAT_XRGB_8888:
2491 if (rgb_order) {
2492 if (alpha)
2493 return DRM_FORMAT_ABGR8888;
2494 else
2495 return DRM_FORMAT_XBGR8888;
2496 } else {
2497 if (alpha)
2498 return DRM_FORMAT_ARGB8888;
2499 else
2500 return DRM_FORMAT_XRGB8888;
2501 }
2502 case PLANE_CTL_FORMAT_XRGB_2101010:
2503 if (rgb_order)
2504 return DRM_FORMAT_XBGR2101010;
2505 else
2506 return DRM_FORMAT_XRGB2101010;
2507 }
2508}
2509
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002510static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002511intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2512 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002513{
2514 struct drm_device *dev = crtc->base.dev;
2515 struct drm_i915_gem_object *obj = NULL;
2516 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002517 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002518 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2519 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2520 PAGE_SIZE);
2521
2522 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002523
Chris Wilsonff2652e2014-03-10 08:07:02 +00002524 if (plane_config->size == 0)
2525 return false;
2526
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002527 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2528 base_aligned,
2529 base_aligned,
2530 size_aligned);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002531 if (!obj)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002532 return false;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002533
Damien Lespiau49af4492015-01-20 12:51:44 +00002534 obj->tiling_mode = plane_config->tiling;
2535 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002536 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002537
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002538 mode_cmd.pixel_format = fb->pixel_format;
2539 mode_cmd.width = fb->width;
2540 mode_cmd.height = fb->height;
2541 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002542 mode_cmd.modifier[0] = fb->modifier[0];
2543 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002544
2545 mutex_lock(&dev->struct_mutex);
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002546 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002547 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002548 DRM_DEBUG_KMS("intel fb init failed\n");
2549 goto out_unref_obj;
2550 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002551 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002552
Daniel Vetterf6936e22015-03-26 12:17:05 +01002553 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002554 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002555
2556out_unref_obj:
2557 drm_gem_object_unreference(&obj->base);
2558 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002559 return false;
2560}
2561
Matt Roperafd65eb2015-02-03 13:10:04 -08002562/* Update plane->state->fb to match plane->fb after driver-internal updates */
2563static void
2564update_state_fb(struct drm_plane *plane)
2565{
2566 if (plane->fb == plane->state->fb)
2567 return;
2568
2569 if (plane->state->fb)
2570 drm_framebuffer_unreference(plane->state->fb);
2571 plane->state->fb = plane->fb;
2572 if (plane->state->fb)
2573 drm_framebuffer_reference(plane->state->fb);
2574}
2575
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002576static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002577intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2578 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002579{
2580 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002581 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002582 struct drm_crtc *c;
2583 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002584 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002585 struct drm_plane *primary = intel_crtc->base.primary;
2586 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002587
Damien Lespiau2d140302015-02-05 17:22:18 +00002588 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002589 return;
2590
Daniel Vetterf6936e22015-03-26 12:17:05 +01002591 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002592 fb = &plane_config->fb->base;
2593 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002594 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002595
Damien Lespiau2d140302015-02-05 17:22:18 +00002596 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002597
2598 /*
2599 * Failed to alloc the obj, check to see if we should share
2600 * an fb with another CRTC instead
2601 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002602 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002603 i = to_intel_crtc(c);
2604
2605 if (c == &intel_crtc->base)
2606 continue;
2607
Matt Roper2ff8fde2014-07-08 07:50:07 -07002608 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002609 continue;
2610
Daniel Vetter88595ac2015-03-26 12:42:24 +01002611 fb = c->primary->fb;
2612 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002613 continue;
2614
Daniel Vetter88595ac2015-03-26 12:42:24 +01002615 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002616 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002617 drm_framebuffer_reference(fb);
2618 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002619 }
2620 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002621
2622 return;
2623
2624valid_fb:
2625 obj = intel_fb_obj(fb);
2626 if (obj->tiling_mode != I915_TILING_NONE)
2627 dev_priv->preserve_bios_swizzle = true;
2628
2629 primary->fb = fb;
2630 primary->state->crtc = &intel_crtc->base;
2631 primary->crtc = &intel_crtc->base;
2632 update_state_fb(primary);
2633 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002634}
2635
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002636static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2637 struct drm_framebuffer *fb,
2638 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002639{
2640 struct drm_device *dev = crtc->dev;
2641 struct drm_i915_private *dev_priv = dev->dev_private;
2642 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002643 struct drm_plane *primary = crtc->primary;
2644 bool visible = to_intel_plane_state(primary->state)->visible;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002645 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002646 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002647 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002648 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002649 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302650 int pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002651
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002652 if (!visible || !fb) {
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002653 I915_WRITE(reg, 0);
2654 if (INTEL_INFO(dev)->gen >= 4)
2655 I915_WRITE(DSPSURF(plane), 0);
2656 else
2657 I915_WRITE(DSPADDR(plane), 0);
2658 POSTING_READ(reg);
2659 return;
2660 }
2661
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002662 obj = intel_fb_obj(fb);
2663 if (WARN_ON(obj == NULL))
2664 return;
2665
2666 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2667
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002668 dspcntr = DISPPLANE_GAMMA_ENABLE;
2669
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002670 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002671
2672 if (INTEL_INFO(dev)->gen < 4) {
2673 if (intel_crtc->pipe == PIPE_B)
2674 dspcntr |= DISPPLANE_SEL_PIPE_B;
2675
2676 /* pipesrc and dspsize control the size that is scaled from,
2677 * which should always be the user's requested size.
2678 */
2679 I915_WRITE(DSPSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002680 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2681 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002682 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002683 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2684 I915_WRITE(PRIMSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002685 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2686 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002687 I915_WRITE(PRIMPOS(plane), 0);
2688 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002689 }
2690
Ville Syrjälä57779d02012-10-31 17:50:14 +02002691 switch (fb->pixel_format) {
2692 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002693 dspcntr |= DISPPLANE_8BPP;
2694 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002695 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002696 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002697 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002698 case DRM_FORMAT_RGB565:
2699 dspcntr |= DISPPLANE_BGRX565;
2700 break;
2701 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002702 dspcntr |= DISPPLANE_BGRX888;
2703 break;
2704 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002705 dspcntr |= DISPPLANE_RGBX888;
2706 break;
2707 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002708 dspcntr |= DISPPLANE_BGRX101010;
2709 break;
2710 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002711 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002712 break;
2713 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002714 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002715 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002716
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002717 if (INTEL_INFO(dev)->gen >= 4 &&
2718 obj->tiling_mode != I915_TILING_NONE)
2719 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002720
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002721 if (IS_G4X(dev))
2722 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2723
Ville Syrjäläb98971272014-08-27 16:51:22 +03002724 linear_offset = y * fb->pitches[0] + x * pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002725
Daniel Vetterc2c75132012-07-05 12:17:30 +02002726 if (INTEL_INFO(dev)->gen >= 4) {
2727 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002728 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002729 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002730 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002731 linear_offset -= intel_crtc->dspaddr_offset;
2732 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002733 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002734 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002735
Matt Roper8e7d6882015-01-21 16:35:41 -08002736 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302737 dspcntr |= DISPPLANE_ROTATE_180;
2738
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002739 x += (intel_crtc->config->pipe_src_w - 1);
2740 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302741
2742 /* Finding the last pixel of the last line of the display
2743 data and adding to linear_offset*/
2744 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002745 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2746 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302747 }
2748
2749 I915_WRITE(reg, dspcntr);
2750
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002751 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002752 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002753 I915_WRITE(DSPSURF(plane),
2754 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002755 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002756 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002757 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002758 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002759 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002760}
2761
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002762static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2763 struct drm_framebuffer *fb,
2764 int x, int y)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002765{
2766 struct drm_device *dev = crtc->dev;
2767 struct drm_i915_private *dev_priv = dev->dev_private;
2768 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002769 struct drm_plane *primary = crtc->primary;
2770 bool visible = to_intel_plane_state(primary->state)->visible;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002771 struct drm_i915_gem_object *obj;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002772 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002773 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002774 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002775 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302776 int pixel_size;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002777
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002778 if (!visible || !fb) {
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002779 I915_WRITE(reg, 0);
2780 I915_WRITE(DSPSURF(plane), 0);
2781 POSTING_READ(reg);
2782 return;
2783 }
2784
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002785 obj = intel_fb_obj(fb);
2786 if (WARN_ON(obj == NULL))
2787 return;
2788
2789 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2790
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002791 dspcntr = DISPPLANE_GAMMA_ENABLE;
2792
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002793 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002794
2795 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2796 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2797
Ville Syrjälä57779d02012-10-31 17:50:14 +02002798 switch (fb->pixel_format) {
2799 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002800 dspcntr |= DISPPLANE_8BPP;
2801 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002802 case DRM_FORMAT_RGB565:
2803 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002804 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002805 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002806 dspcntr |= DISPPLANE_BGRX888;
2807 break;
2808 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002809 dspcntr |= DISPPLANE_RGBX888;
2810 break;
2811 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002812 dspcntr |= DISPPLANE_BGRX101010;
2813 break;
2814 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002815 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002816 break;
2817 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002818 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002819 }
2820
2821 if (obj->tiling_mode != I915_TILING_NONE)
2822 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002823
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002824 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002825 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002826
Ville Syrjäläb98971272014-08-27 16:51:22 +03002827 linear_offset = y * fb->pitches[0] + x * pixel_size;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002828 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002829 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002830 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002831 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002832 linear_offset -= intel_crtc->dspaddr_offset;
Matt Roper8e7d6882015-01-21 16:35:41 -08002833 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302834 dspcntr |= DISPPLANE_ROTATE_180;
2835
2836 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002837 x += (intel_crtc->config->pipe_src_w - 1);
2838 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302839
2840 /* Finding the last pixel of the last line of the display
2841 data and adding to linear_offset*/
2842 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002843 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2844 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302845 }
2846 }
2847
2848 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002849
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002850 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002851 I915_WRITE(DSPSURF(plane),
2852 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002853 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002854 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2855 } else {
2856 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2857 I915_WRITE(DSPLINOFF(plane), linear_offset);
2858 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002859 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002860}
2861
Damien Lespiaub3218032015-02-27 11:15:18 +00002862u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2863 uint32_t pixel_format)
2864{
2865 u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2866
2867 /*
2868 * The stride is either expressed as a multiple of 64 bytes
2869 * chunks for linear buffers or in number of tiles for tiled
2870 * buffers.
2871 */
2872 switch (fb_modifier) {
2873 case DRM_FORMAT_MOD_NONE:
2874 return 64;
2875 case I915_FORMAT_MOD_X_TILED:
2876 if (INTEL_INFO(dev)->gen == 2)
2877 return 128;
2878 return 512;
2879 case I915_FORMAT_MOD_Y_TILED:
2880 /* No need to check for old gens and Y tiling since this is
2881 * about the display engine and those will be blocked before
2882 * we get here.
2883 */
2884 return 128;
2885 case I915_FORMAT_MOD_Yf_TILED:
2886 if (bits_per_pixel == 8)
2887 return 64;
2888 else
2889 return 128;
2890 default:
2891 MISSING_CASE(fb_modifier);
2892 return 64;
2893 }
2894}
2895
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002896unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
2897 struct drm_i915_gem_object *obj)
2898{
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002899 const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002900
2901 if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002902 view = &i915_ggtt_view_rotated;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002903
2904 return i915_gem_obj_ggtt_offset_view(obj, view);
2905}
2906
Chandra Kondurua1b22782015-04-07 15:28:45 -07002907/*
2908 * This function detaches (aka. unbinds) unused scalers in hardware
2909 */
2910void skl_detach_scalers(struct intel_crtc *intel_crtc)
2911{
2912 struct drm_device *dev;
2913 struct drm_i915_private *dev_priv;
2914 struct intel_crtc_scaler_state *scaler_state;
2915 int i;
2916
2917 if (!intel_crtc || !intel_crtc->config)
2918 return;
2919
2920 dev = intel_crtc->base.dev;
2921 dev_priv = dev->dev_private;
2922 scaler_state = &intel_crtc->config->scaler_state;
2923
2924 /* loop through and disable scalers that aren't in use */
2925 for (i = 0; i < intel_crtc->num_scalers; i++) {
2926 if (!scaler_state->scalers[i].in_use) {
2927 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, i), 0);
2928 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, i), 0);
2929 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, i), 0);
2930 DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
2931 intel_crtc->base.base.id, intel_crtc->pipe, i);
2932 }
2933 }
2934}
2935
Chandra Konduru6156a452015-04-27 13:48:39 -07002936u32 skl_plane_ctl_format(uint32_t pixel_format)
2937{
Chandra Konduru6156a452015-04-27 13:48:39 -07002938 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01002939 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002940 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07002941 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002942 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07002943 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002944 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07002945 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002946 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07002947 /*
2948 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2949 * to be already pre-multiplied. We need to add a knob (or a different
2950 * DRM_FORMAT) for user-space to configure that.
2951 */
2952 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002953 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07002954 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002955 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002956 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07002957 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002958 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002959 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002960 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002961 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002962 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002963 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07002964 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002965 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07002966 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002967 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002968 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002969 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002970 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01002971 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07002972 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002973
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002974 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002975}
2976
2977u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2978{
Chandra Konduru6156a452015-04-27 13:48:39 -07002979 switch (fb_modifier) {
2980 case DRM_FORMAT_MOD_NONE:
2981 break;
2982 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002983 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07002984 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002985 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07002986 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002987 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07002988 default:
2989 MISSING_CASE(fb_modifier);
2990 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002991
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002992 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002993}
2994
2995u32 skl_plane_ctl_rotation(unsigned int rotation)
2996{
Chandra Konduru6156a452015-04-27 13:48:39 -07002997 switch (rotation) {
2998 case BIT(DRM_ROTATE_0):
2999 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05303000 /*
3001 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3002 * while i915 HW rotation is clockwise, thats why this swapping.
3003 */
Chandra Konduru6156a452015-04-27 13:48:39 -07003004 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303005 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07003006 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003007 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07003008 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303009 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07003010 default:
3011 MISSING_CASE(rotation);
3012 }
3013
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003014 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003015}
3016
Damien Lespiau70d21f02013-07-03 21:06:04 +01003017static void skylake_update_primary_plane(struct drm_crtc *crtc,
3018 struct drm_framebuffer *fb,
3019 int x, int y)
3020{
3021 struct drm_device *dev = crtc->dev;
3022 struct drm_i915_private *dev_priv = dev->dev_private;
3023 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03003024 struct drm_plane *plane = crtc->primary;
3025 bool visible = to_intel_plane_state(plane->state)->visible;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003026 struct drm_i915_gem_object *obj;
3027 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303028 u32 plane_ctl, stride_div, stride;
3029 u32 tile_height, plane_offset, plane_size;
3030 unsigned int rotation;
3031 int x_offset, y_offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003032 unsigned long surf_addr;
Chandra Konduru6156a452015-04-27 13:48:39 -07003033 struct intel_crtc_state *crtc_state = intel_crtc->config;
3034 struct intel_plane_state *plane_state;
3035 int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
3036 int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
3037 int scaler_id = -1;
3038
Chandra Konduru6156a452015-04-27 13:48:39 -07003039 plane_state = to_intel_plane_state(plane->state);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003040
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03003041 if (!visible || !fb) {
Damien Lespiau70d21f02013-07-03 21:06:04 +01003042 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3043 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3044 POSTING_READ(PLANE_CTL(pipe, 0));
3045 return;
3046 }
3047
3048 plane_ctl = PLANE_CTL_ENABLE |
3049 PLANE_CTL_PIPE_GAMMA_ENABLE |
3050 PLANE_CTL_PIPE_CSC_ENABLE;
3051
Chandra Konduru6156a452015-04-27 13:48:39 -07003052 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3053 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003054 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303055
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303056 rotation = plane->state->rotation;
Chandra Konduru6156a452015-04-27 13:48:39 -07003057 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003058
Damien Lespiaub3218032015-02-27 11:15:18 +00003059 obj = intel_fb_obj(fb);
3060 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3061 fb->pixel_format);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303062 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
3063
Chandra Konduru6156a452015-04-27 13:48:39 -07003064 /*
3065 * FIXME: intel_plane_state->src, dst aren't set when transitional
3066 * update_plane helpers are called from legacy paths.
3067 * Once full atomic crtc is available, below check can be avoided.
3068 */
3069 if (drm_rect_width(&plane_state->src)) {
3070 scaler_id = plane_state->scaler_id;
3071 src_x = plane_state->src.x1 >> 16;
3072 src_y = plane_state->src.y1 >> 16;
3073 src_w = drm_rect_width(&plane_state->src) >> 16;
3074 src_h = drm_rect_height(&plane_state->src) >> 16;
3075 dst_x = plane_state->dst.x1;
3076 dst_y = plane_state->dst.y1;
3077 dst_w = drm_rect_width(&plane_state->dst);
3078 dst_h = drm_rect_height(&plane_state->dst);
3079
3080 WARN_ON(x != src_x || y != src_y);
3081 } else {
3082 src_w = intel_crtc->config->pipe_src_w;
3083 src_h = intel_crtc->config->pipe_src_h;
3084 }
3085
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303086 if (intel_rotation_90_or_270(rotation)) {
3087 /* stride = Surface height in tiles */
Chandra Konduru2614f172015-05-08 20:22:46 -07003088 tile_height = intel_tile_height(dev, fb->pixel_format,
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303089 fb->modifier[0]);
3090 stride = DIV_ROUND_UP(fb->height, tile_height);
Chandra Konduru6156a452015-04-27 13:48:39 -07003091 x_offset = stride * tile_height - y - src_h;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303092 y_offset = x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003093 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303094 } else {
3095 stride = fb->pitches[0] / stride_div;
3096 x_offset = x;
3097 y_offset = y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003098 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303099 }
3100 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003101
Damien Lespiau70d21f02013-07-03 21:06:04 +01003102 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303103 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3104 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3105 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003106
3107 if (scaler_id >= 0) {
3108 uint32_t ps_ctrl = 0;
3109
3110 WARN_ON(!dst_w || !dst_h);
3111 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3112 crtc_state->scaler_state.scalers[scaler_id].mode;
3113 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3114 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3115 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3116 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3117 I915_WRITE(PLANE_POS(pipe, 0), 0);
3118 } else {
3119 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3120 }
3121
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003122 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003123
3124 POSTING_READ(PLANE_SURF(pipe, 0));
3125}
3126
Jesse Barnes17638cd2011-06-24 12:19:23 -07003127/* Assume fb object is pinned & idle & fenced and just update base pointers */
3128static int
3129intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3130 int x, int y, enum mode_set_atomic state)
3131{
3132 struct drm_device *dev = crtc->dev;
3133 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003134
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01003135 if (dev_priv->display.disable_fbc)
3136 dev_priv->display.disable_fbc(dev);
Jesse Barnes81255562010-08-02 12:07:50 -07003137
Daniel Vetter29b9bde2014-04-24 23:55:01 +02003138 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3139
3140 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07003141}
3142
Ville Syrjälä75147472014-11-24 18:28:11 +02003143static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003144{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003145 struct drm_crtc *crtc;
3146
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 enum plane plane = intel_crtc->plane;
3150
3151 intel_prepare_page_flip(dev, plane);
3152 intel_finish_page_flip_plane(dev, plane);
3153 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003154}
3155
3156static void intel_update_primary_planes(struct drm_device *dev)
3157{
3158 struct drm_i915_private *dev_priv = dev->dev_private;
3159 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003160
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003161 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003162 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3163
Rob Clark51fd3712013-11-19 12:10:12 -05003164 drm_modeset_lock(&crtc->mutex, NULL);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003165 /*
3166 * FIXME: Once we have proper support for primary planes (and
3167 * disabling them without disabling the entire crtc) allow again
Dave Airlie66e514c2014-04-03 07:51:54 +10003168 * a NULL crtc->primary->fb.
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003169 */
Matt Roperf4510a22014-04-01 15:22:40 -07003170 if (intel_crtc->active && crtc->primary->fb)
Matt Roper262ca2b2014-03-18 17:22:55 -07003171 dev_priv->display.update_primary_plane(crtc,
Dave Airlie66e514c2014-04-03 07:51:54 +10003172 crtc->primary->fb,
Matt Roper262ca2b2014-03-18 17:22:55 -07003173 crtc->x,
3174 crtc->y);
Rob Clark51fd3712013-11-19 12:10:12 -05003175 drm_modeset_unlock(&crtc->mutex);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003176 }
3177}
3178
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03003179void intel_crtc_reset(struct intel_crtc *crtc)
3180{
3181 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3182
3183 if (!crtc->active)
3184 return;
3185
3186 intel_crtc_disable_planes(&crtc->base);
3187 dev_priv->display.crtc_disable(&crtc->base);
3188 dev_priv->display.crtc_enable(&crtc->base);
3189 intel_crtc_enable_planes(&crtc->base);
3190}
3191
Ville Syrjälä75147472014-11-24 18:28:11 +02003192void intel_prepare_reset(struct drm_device *dev)
3193{
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003194 struct drm_i915_private *dev_priv = to_i915(dev);
3195 struct intel_crtc *crtc;
3196
Ville Syrjälä75147472014-11-24 18:28:11 +02003197 /* no reset support for gen2 */
3198 if (IS_GEN2(dev))
3199 return;
3200
3201 /* reset doesn't touch the display */
3202 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3203 return;
3204
3205 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003206
3207 /*
3208 * Disabling the crtcs gracefully seems nicer. Also the
3209 * g33 docs say we should at least disable all the planes.
3210 */
3211 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03003212 if (!crtc->active)
3213 continue;
3214
3215 intel_crtc_disable_planes(&crtc->base);
3216 dev_priv->display.crtc_disable(&crtc->base);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003217 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003218}
3219
3220void intel_finish_reset(struct drm_device *dev)
3221{
3222 struct drm_i915_private *dev_priv = to_i915(dev);
3223
3224 /*
3225 * Flips in the rings will be nuked by the reset,
3226 * so complete all pending flips so that user space
3227 * will get its events and not get stuck.
3228 */
3229 intel_complete_page_flips(dev);
3230
3231 /* no reset support for gen2 */
3232 if (IS_GEN2(dev))
3233 return;
3234
3235 /* reset doesn't touch the display */
3236 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3237 /*
3238 * Flips in the rings have been nuked by the reset,
3239 * so update the base address of all primary
3240 * planes to the the last fb to make sure we're
3241 * showing the correct fb after a reset.
3242 */
3243 intel_update_primary_planes(dev);
3244 return;
3245 }
3246
3247 /*
3248 * The display has been reset as well,
3249 * so need a full re-initialization.
3250 */
3251 intel_runtime_pm_disable_interrupts(dev_priv);
3252 intel_runtime_pm_enable_interrupts(dev_priv);
3253
3254 intel_modeset_init_hw(dev);
3255
3256 spin_lock_irq(&dev_priv->irq_lock);
3257 if (dev_priv->display.hpd_irq_setup)
3258 dev_priv->display.hpd_irq_setup(dev);
3259 spin_unlock_irq(&dev_priv->irq_lock);
3260
3261 intel_modeset_setup_hw_state(dev, true);
3262
3263 intel_hpd_init(dev_priv);
3264
3265 drm_modeset_unlock_all(dev);
3266}
3267
Chris Wilson2e2f3512015-04-27 13:41:14 +01003268static void
Chris Wilson14667a42012-04-03 17:58:35 +01003269intel_finish_fb(struct drm_framebuffer *old_fb)
3270{
Matt Roper2ff8fde2014-07-08 07:50:07 -07003271 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
Chris Wilson2e2f3512015-04-27 13:41:14 +01003272 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilson14667a42012-04-03 17:58:35 +01003273 bool was_interruptible = dev_priv->mm.interruptible;
3274 int ret;
3275
Chris Wilson14667a42012-04-03 17:58:35 +01003276 /* Big Hammer, we also need to ensure that any pending
3277 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3278 * current scanout is retired before unpinning the old
Chris Wilson2e2f3512015-04-27 13:41:14 +01003279 * framebuffer. Note that we rely on userspace rendering
3280 * into the buffer attached to the pipe they are waiting
3281 * on. If not, userspace generates a GPU hang with IPEHR
3282 * point to the MI_WAIT_FOR_EVENT.
Chris Wilson14667a42012-04-03 17:58:35 +01003283 *
3284 * This should only fail upon a hung GPU, in which case we
3285 * can safely continue.
3286 */
3287 dev_priv->mm.interruptible = false;
Chris Wilson2e2f3512015-04-27 13:41:14 +01003288 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson14667a42012-04-03 17:58:35 +01003289 dev_priv->mm.interruptible = was_interruptible;
3290
Chris Wilson2e2f3512015-04-27 13:41:14 +01003291 WARN_ON(ret);
Chris Wilson14667a42012-04-03 17:58:35 +01003292}
3293
Chris Wilson7d5e3792014-03-04 13:15:08 +00003294static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3295{
3296 struct drm_device *dev = crtc->dev;
3297 struct drm_i915_private *dev_priv = dev->dev_private;
3298 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003299 bool pending;
3300
3301 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3302 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3303 return false;
3304
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003305 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003306 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003307 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003308
3309 return pending;
3310}
3311
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003312static void intel_update_pipe_size(struct intel_crtc *crtc)
3313{
3314 struct drm_device *dev = crtc->base.dev;
3315 struct drm_i915_private *dev_priv = dev->dev_private;
3316 const struct drm_display_mode *adjusted_mode;
3317
3318 if (!i915.fastboot)
3319 return;
3320
3321 /*
3322 * Update pipe size and adjust fitter if needed: the reason for this is
3323 * that in compute_mode_changes we check the native mode (not the pfit
3324 * mode) to see if we can flip rather than do a full mode set. In the
3325 * fastboot case, we'll flip, but if we don't update the pipesrc and
3326 * pfit state, we'll end up with a big fb scanned out into the wrong
3327 * sized surface.
3328 *
3329 * To fix this properly, we need to hoist the checks up into
3330 * compute_mode_changes (or above), check the actual pfit state and
3331 * whether the platform allows pfit disable with pipe active, and only
3332 * then update the pipesrc and pfit state, even on the flip path.
3333 */
3334
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003335 adjusted_mode = &crtc->config->base.adjusted_mode;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003336
3337 I915_WRITE(PIPESRC(crtc->pipe),
3338 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3339 (adjusted_mode->crtc_vdisplay - 1));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003340 if (!crtc->config->pch_pfit.enabled &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03003341 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3342 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003343 I915_WRITE(PF_CTL(crtc->pipe), 0);
3344 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3345 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3346 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003347 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3348 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003349}
3350
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003351static void intel_fdi_normal_train(struct drm_crtc *crtc)
3352{
3353 struct drm_device *dev = crtc->dev;
3354 struct drm_i915_private *dev_priv = dev->dev_private;
3355 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3356 int pipe = intel_crtc->pipe;
3357 u32 reg, temp;
3358
3359 /* enable normal train */
3360 reg = FDI_TX_CTL(pipe);
3361 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003362 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003363 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3364 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003365 } else {
3366 temp &= ~FDI_LINK_TRAIN_NONE;
3367 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003368 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003369 I915_WRITE(reg, temp);
3370
3371 reg = FDI_RX_CTL(pipe);
3372 temp = I915_READ(reg);
3373 if (HAS_PCH_CPT(dev)) {
3374 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3375 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3376 } else {
3377 temp &= ~FDI_LINK_TRAIN_NONE;
3378 temp |= FDI_LINK_TRAIN_NONE;
3379 }
3380 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3381
3382 /* wait one idle pattern time */
3383 POSTING_READ(reg);
3384 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003385
3386 /* IVB wants error correction enabled */
3387 if (IS_IVYBRIDGE(dev))
3388 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3389 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003390}
3391
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003392/* The FDI link training functions for ILK/Ibexpeak. */
3393static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3394{
3395 struct drm_device *dev = crtc->dev;
3396 struct drm_i915_private *dev_priv = dev->dev_private;
3397 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3398 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003399 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003400
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003401 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003402 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003403
Adam Jacksone1a44742010-06-25 15:32:14 -04003404 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3405 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003406 reg = FDI_RX_IMR(pipe);
3407 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003408 temp &= ~FDI_RX_SYMBOL_LOCK;
3409 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003410 I915_WRITE(reg, temp);
3411 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003412 udelay(150);
3413
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003414 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003415 reg = FDI_TX_CTL(pipe);
3416 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003417 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003418 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003419 temp &= ~FDI_LINK_TRAIN_NONE;
3420 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003421 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003422
Chris Wilson5eddb702010-09-11 13:48:45 +01003423 reg = FDI_RX_CTL(pipe);
3424 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003425 temp &= ~FDI_LINK_TRAIN_NONE;
3426 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003427 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3428
3429 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003430 udelay(150);
3431
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003432 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003433 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3434 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3435 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003436
Chris Wilson5eddb702010-09-11 13:48:45 +01003437 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003438 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003439 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003440 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3441
3442 if ((temp & FDI_RX_BIT_LOCK)) {
3443 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003444 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003445 break;
3446 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003447 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003448 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003449 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003450
3451 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003452 reg = FDI_TX_CTL(pipe);
3453 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003454 temp &= ~FDI_LINK_TRAIN_NONE;
3455 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003456 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003457
Chris Wilson5eddb702010-09-11 13:48:45 +01003458 reg = FDI_RX_CTL(pipe);
3459 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003460 temp &= ~FDI_LINK_TRAIN_NONE;
3461 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003462 I915_WRITE(reg, temp);
3463
3464 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003465 udelay(150);
3466
Chris Wilson5eddb702010-09-11 13:48:45 +01003467 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003468 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003469 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003470 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3471
3472 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003473 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003474 DRM_DEBUG_KMS("FDI train 2 done.\n");
3475 break;
3476 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003477 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003478 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003479 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003480
3481 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003482
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003483}
3484
Akshay Joshi0206e352011-08-16 15:34:10 -04003485static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003486 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3487 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3488 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3489 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3490};
3491
3492/* The FDI link training functions for SNB/Cougarpoint. */
3493static void gen6_fdi_link_train(struct drm_crtc *crtc)
3494{
3495 struct drm_device *dev = crtc->dev;
3496 struct drm_i915_private *dev_priv = dev->dev_private;
3497 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3498 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05003499 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003500
Adam Jacksone1a44742010-06-25 15:32:14 -04003501 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3502 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003503 reg = FDI_RX_IMR(pipe);
3504 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003505 temp &= ~FDI_RX_SYMBOL_LOCK;
3506 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003507 I915_WRITE(reg, temp);
3508
3509 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003510 udelay(150);
3511
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003512 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003513 reg = FDI_TX_CTL(pipe);
3514 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003515 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003516 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003517 temp &= ~FDI_LINK_TRAIN_NONE;
3518 temp |= FDI_LINK_TRAIN_PATTERN_1;
3519 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3520 /* SNB-B */
3521 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003522 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003523
Daniel Vetterd74cf322012-10-26 10:58:13 +02003524 I915_WRITE(FDI_RX_MISC(pipe),
3525 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3526
Chris Wilson5eddb702010-09-11 13:48:45 +01003527 reg = FDI_RX_CTL(pipe);
3528 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003529 if (HAS_PCH_CPT(dev)) {
3530 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3531 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3532 } else {
3533 temp &= ~FDI_LINK_TRAIN_NONE;
3534 temp |= FDI_LINK_TRAIN_PATTERN_1;
3535 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003536 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3537
3538 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003539 udelay(150);
3540
Akshay Joshi0206e352011-08-16 15:34:10 -04003541 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003542 reg = FDI_TX_CTL(pipe);
3543 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003544 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3545 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003546 I915_WRITE(reg, temp);
3547
3548 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003549 udelay(500);
3550
Sean Paulfa37d392012-03-02 12:53:39 -05003551 for (retry = 0; retry < 5; retry++) {
3552 reg = FDI_RX_IIR(pipe);
3553 temp = I915_READ(reg);
3554 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3555 if (temp & FDI_RX_BIT_LOCK) {
3556 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3557 DRM_DEBUG_KMS("FDI train 1 done.\n");
3558 break;
3559 }
3560 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003561 }
Sean Paulfa37d392012-03-02 12:53:39 -05003562 if (retry < 5)
3563 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003564 }
3565 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003566 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003567
3568 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003569 reg = FDI_TX_CTL(pipe);
3570 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003571 temp &= ~FDI_LINK_TRAIN_NONE;
3572 temp |= FDI_LINK_TRAIN_PATTERN_2;
3573 if (IS_GEN6(dev)) {
3574 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3575 /* SNB-B */
3576 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3577 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003578 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003579
Chris Wilson5eddb702010-09-11 13:48:45 +01003580 reg = FDI_RX_CTL(pipe);
3581 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003582 if (HAS_PCH_CPT(dev)) {
3583 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3584 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3585 } else {
3586 temp &= ~FDI_LINK_TRAIN_NONE;
3587 temp |= FDI_LINK_TRAIN_PATTERN_2;
3588 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003589 I915_WRITE(reg, temp);
3590
3591 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003592 udelay(150);
3593
Akshay Joshi0206e352011-08-16 15:34:10 -04003594 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003595 reg = FDI_TX_CTL(pipe);
3596 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003597 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3598 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003599 I915_WRITE(reg, temp);
3600
3601 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003602 udelay(500);
3603
Sean Paulfa37d392012-03-02 12:53:39 -05003604 for (retry = 0; retry < 5; retry++) {
3605 reg = FDI_RX_IIR(pipe);
3606 temp = I915_READ(reg);
3607 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3608 if (temp & FDI_RX_SYMBOL_LOCK) {
3609 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3610 DRM_DEBUG_KMS("FDI train 2 done.\n");
3611 break;
3612 }
3613 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003614 }
Sean Paulfa37d392012-03-02 12:53:39 -05003615 if (retry < 5)
3616 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003617 }
3618 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003619 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003620
3621 DRM_DEBUG_KMS("FDI train done.\n");
3622}
3623
Jesse Barnes357555c2011-04-28 15:09:55 -07003624/* Manual link training for Ivy Bridge A0 parts */
3625static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3626{
3627 struct drm_device *dev = crtc->dev;
3628 struct drm_i915_private *dev_priv = dev->dev_private;
3629 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3630 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003631 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003632
3633 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3634 for train result */
3635 reg = FDI_RX_IMR(pipe);
3636 temp = I915_READ(reg);
3637 temp &= ~FDI_RX_SYMBOL_LOCK;
3638 temp &= ~FDI_RX_BIT_LOCK;
3639 I915_WRITE(reg, temp);
3640
3641 POSTING_READ(reg);
3642 udelay(150);
3643
Daniel Vetter01a415f2012-10-27 15:58:40 +02003644 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3645 I915_READ(FDI_RX_IIR(pipe)));
3646
Jesse Barnes139ccd32013-08-19 11:04:55 -07003647 /* Try each vswing and preemphasis setting twice before moving on */
3648 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3649 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003650 reg = FDI_TX_CTL(pipe);
3651 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003652 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3653 temp &= ~FDI_TX_ENABLE;
3654 I915_WRITE(reg, temp);
3655
3656 reg = FDI_RX_CTL(pipe);
3657 temp = I915_READ(reg);
3658 temp &= ~FDI_LINK_TRAIN_AUTO;
3659 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3660 temp &= ~FDI_RX_ENABLE;
3661 I915_WRITE(reg, temp);
3662
3663 /* enable CPU FDI TX and PCH FDI RX */
3664 reg = FDI_TX_CTL(pipe);
3665 temp = I915_READ(reg);
3666 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003667 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003668 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003669 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003670 temp |= snb_b_fdi_train_param[j/2];
3671 temp |= FDI_COMPOSITE_SYNC;
3672 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3673
3674 I915_WRITE(FDI_RX_MISC(pipe),
3675 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3676
3677 reg = FDI_RX_CTL(pipe);
3678 temp = I915_READ(reg);
3679 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3680 temp |= FDI_COMPOSITE_SYNC;
3681 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3682
3683 POSTING_READ(reg);
3684 udelay(1); /* should be 0.5us */
3685
3686 for (i = 0; i < 4; i++) {
3687 reg = FDI_RX_IIR(pipe);
3688 temp = I915_READ(reg);
3689 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3690
3691 if (temp & FDI_RX_BIT_LOCK ||
3692 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3693 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3694 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3695 i);
3696 break;
3697 }
3698 udelay(1); /* should be 0.5us */
3699 }
3700 if (i == 4) {
3701 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3702 continue;
3703 }
3704
3705 /* Train 2 */
3706 reg = FDI_TX_CTL(pipe);
3707 temp = I915_READ(reg);
3708 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3709 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3710 I915_WRITE(reg, temp);
3711
3712 reg = FDI_RX_CTL(pipe);
3713 temp = I915_READ(reg);
3714 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3715 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003716 I915_WRITE(reg, temp);
3717
3718 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003719 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003720
Jesse Barnes139ccd32013-08-19 11:04:55 -07003721 for (i = 0; i < 4; i++) {
3722 reg = FDI_RX_IIR(pipe);
3723 temp = I915_READ(reg);
3724 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003725
Jesse Barnes139ccd32013-08-19 11:04:55 -07003726 if (temp & FDI_RX_SYMBOL_LOCK ||
3727 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3728 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3729 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3730 i);
3731 goto train_done;
3732 }
3733 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003734 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003735 if (i == 4)
3736 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003737 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003738
Jesse Barnes139ccd32013-08-19 11:04:55 -07003739train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003740 DRM_DEBUG_KMS("FDI train done.\n");
3741}
3742
Daniel Vetter88cefb62012-08-12 19:27:14 +02003743static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003744{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003745 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003746 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003747 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003748 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003749
Jesse Barnesc64e3112010-09-10 11:27:03 -07003750
Jesse Barnes0e23b992010-09-10 11:10:00 -07003751 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003752 reg = FDI_RX_CTL(pipe);
3753 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003754 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003755 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003756 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003757 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3758
3759 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003760 udelay(200);
3761
3762 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003763 temp = I915_READ(reg);
3764 I915_WRITE(reg, temp | FDI_PCDCLK);
3765
3766 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003767 udelay(200);
3768
Paulo Zanoni20749732012-11-23 15:30:38 -02003769 /* Enable CPU FDI TX PLL, always on for Ironlake */
3770 reg = FDI_TX_CTL(pipe);
3771 temp = I915_READ(reg);
3772 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3773 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003774
Paulo Zanoni20749732012-11-23 15:30:38 -02003775 POSTING_READ(reg);
3776 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003777 }
3778}
3779
Daniel Vetter88cefb62012-08-12 19:27:14 +02003780static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3781{
3782 struct drm_device *dev = intel_crtc->base.dev;
3783 struct drm_i915_private *dev_priv = dev->dev_private;
3784 int pipe = intel_crtc->pipe;
3785 u32 reg, temp;
3786
3787 /* Switch from PCDclk to Rawclk */
3788 reg = FDI_RX_CTL(pipe);
3789 temp = I915_READ(reg);
3790 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3791
3792 /* Disable CPU FDI TX PLL */
3793 reg = FDI_TX_CTL(pipe);
3794 temp = I915_READ(reg);
3795 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3796
3797 POSTING_READ(reg);
3798 udelay(100);
3799
3800 reg = FDI_RX_CTL(pipe);
3801 temp = I915_READ(reg);
3802 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3803
3804 /* Wait for the clocks to turn off. */
3805 POSTING_READ(reg);
3806 udelay(100);
3807}
3808
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003809static void ironlake_fdi_disable(struct drm_crtc *crtc)
3810{
3811 struct drm_device *dev = crtc->dev;
3812 struct drm_i915_private *dev_priv = dev->dev_private;
3813 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3814 int pipe = intel_crtc->pipe;
3815 u32 reg, temp;
3816
3817 /* disable CPU FDI tx and PCH FDI rx */
3818 reg = FDI_TX_CTL(pipe);
3819 temp = I915_READ(reg);
3820 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3821 POSTING_READ(reg);
3822
3823 reg = FDI_RX_CTL(pipe);
3824 temp = I915_READ(reg);
3825 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003826 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003827 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3828
3829 POSTING_READ(reg);
3830 udelay(100);
3831
3832 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003833 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003834 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003835
3836 /* still set train pattern 1 */
3837 reg = FDI_TX_CTL(pipe);
3838 temp = I915_READ(reg);
3839 temp &= ~FDI_LINK_TRAIN_NONE;
3840 temp |= FDI_LINK_TRAIN_PATTERN_1;
3841 I915_WRITE(reg, temp);
3842
3843 reg = FDI_RX_CTL(pipe);
3844 temp = I915_READ(reg);
3845 if (HAS_PCH_CPT(dev)) {
3846 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3847 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3848 } else {
3849 temp &= ~FDI_LINK_TRAIN_NONE;
3850 temp |= FDI_LINK_TRAIN_PATTERN_1;
3851 }
3852 /* BPC in FDI rx is consistent with that in PIPECONF */
3853 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003854 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003855 I915_WRITE(reg, temp);
3856
3857 POSTING_READ(reg);
3858 udelay(100);
3859}
3860
Chris Wilson5dce5b932014-01-20 10:17:36 +00003861bool intel_has_pending_fb_unpin(struct drm_device *dev)
3862{
3863 struct intel_crtc *crtc;
3864
3865 /* Note that we don't need to be called with mode_config.lock here
3866 * as our list of CRTC objects is static for the lifetime of the
3867 * device and so cannot disappear as we iterate. Similarly, we can
3868 * happily treat the predicates as racy, atomic checks as userspace
3869 * cannot claim and pin a new fb without at least acquring the
3870 * struct_mutex and so serialising with us.
3871 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003872 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003873 if (atomic_read(&crtc->unpin_work_count) == 0)
3874 continue;
3875
3876 if (crtc->unpin_work)
3877 intel_wait_for_vblank(dev, crtc->pipe);
3878
3879 return true;
3880 }
3881
3882 return false;
3883}
3884
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003885static void page_flip_completed(struct intel_crtc *intel_crtc)
3886{
3887 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3888 struct intel_unpin_work *work = intel_crtc->unpin_work;
3889
3890 /* ensure that the unpin work is consistent wrt ->pending. */
3891 smp_rmb();
3892 intel_crtc->unpin_work = NULL;
3893
3894 if (work->event)
3895 drm_send_vblank_event(intel_crtc->base.dev,
3896 intel_crtc->pipe,
3897 work->event);
3898
3899 drm_crtc_vblank_put(&intel_crtc->base);
3900
3901 wake_up_all(&dev_priv->pending_flip_queue);
3902 queue_work(dev_priv->wq, &work->work);
3903
3904 trace_i915_flip_complete(intel_crtc->plane,
3905 work->pending_flip_obj);
3906}
3907
Ville Syrjälä46a55d32014-05-21 14:04:46 +03003908void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003909{
Chris Wilson0f911282012-04-17 10:05:38 +01003910 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003911 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003912
Daniel Vetter2c10d572012-12-20 21:24:07 +01003913 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Chris Wilson9c787942014-09-05 07:13:25 +01003914 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3915 !intel_crtc_has_pending_flip(crtc),
3916 60*HZ) == 0)) {
3917 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003918
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003919 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003920 if (intel_crtc->unpin_work) {
3921 WARN_ONCE(1, "Removing stuck page flip\n");
3922 page_flip_completed(intel_crtc);
3923 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003924 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003925 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003926
Chris Wilson975d5682014-08-20 13:13:34 +01003927 if (crtc->primary->fb) {
3928 mutex_lock(&dev->struct_mutex);
3929 intel_finish_fb(crtc->primary->fb);
3930 mutex_unlock(&dev->struct_mutex);
3931 }
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003932}
3933
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003934/* Program iCLKIP clock to the desired frequency */
3935static void lpt_program_iclkip(struct drm_crtc *crtc)
3936{
3937 struct drm_device *dev = crtc->dev;
3938 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003939 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003940 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3941 u32 temp;
3942
Ville Syrjäläa5805162015-05-26 20:42:30 +03003943 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01003944
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003945 /* It is necessary to ungate the pixclk gate prior to programming
3946 * the divisors, and gate it back when it is done.
3947 */
3948 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3949
3950 /* Disable SSCCTL */
3951 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003952 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3953 SBI_SSCCTL_DISABLE,
3954 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003955
3956 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003957 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003958 auxdiv = 1;
3959 divsel = 0x41;
3960 phaseinc = 0x20;
3961 } else {
3962 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003963 * but the adjusted_mode->crtc_clock in in KHz. To get the
3964 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003965 * convert the virtual clock precision to KHz here for higher
3966 * precision.
3967 */
3968 u32 iclk_virtual_root_freq = 172800 * 1000;
3969 u32 iclk_pi_range = 64;
3970 u32 desired_divisor, msb_divisor_value, pi_value;
3971
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003972 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003973 msb_divisor_value = desired_divisor / iclk_pi_range;
3974 pi_value = desired_divisor % iclk_pi_range;
3975
3976 auxdiv = 0;
3977 divsel = msb_divisor_value - 2;
3978 phaseinc = pi_value;
3979 }
3980
3981 /* This should not happen with any sane values */
3982 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3983 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3984 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3985 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3986
3987 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 +03003988 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003989 auxdiv,
3990 divsel,
3991 phasedir,
3992 phaseinc);
3993
3994 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003995 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003996 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3997 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3998 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3999 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4000 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4001 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004002 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004003
4004 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004005 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004006 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4007 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004008 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004009
4010 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004011 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004012 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004013 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004014
4015 /* Wait for initialization time */
4016 udelay(24);
4017
4018 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01004019
Ville Syrjäläa5805162015-05-26 20:42:30 +03004020 mutex_unlock(&dev_priv->sb_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004021}
4022
Daniel Vetter275f01b22013-05-03 11:49:47 +02004023static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4024 enum pipe pch_transcoder)
4025{
4026 struct drm_device *dev = crtc->base.dev;
4027 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004028 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02004029
4030 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4031 I915_READ(HTOTAL(cpu_transcoder)));
4032 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4033 I915_READ(HBLANK(cpu_transcoder)));
4034 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4035 I915_READ(HSYNC(cpu_transcoder)));
4036
4037 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4038 I915_READ(VTOTAL(cpu_transcoder)));
4039 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4040 I915_READ(VBLANK(cpu_transcoder)));
4041 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4042 I915_READ(VSYNC(cpu_transcoder)));
4043 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4044 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4045}
4046
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004047static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004048{
4049 struct drm_i915_private *dev_priv = dev->dev_private;
4050 uint32_t temp;
4051
4052 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004053 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004054 return;
4055
4056 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4057 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4058
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004059 temp &= ~FDI_BC_BIFURCATION_SELECT;
4060 if (enable)
4061 temp |= FDI_BC_BIFURCATION_SELECT;
4062
4063 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004064 I915_WRITE(SOUTH_CHICKEN1, temp);
4065 POSTING_READ(SOUTH_CHICKEN1);
4066}
4067
4068static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4069{
4070 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004071
4072 switch (intel_crtc->pipe) {
4073 case PIPE_A:
4074 break;
4075 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004076 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004077 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004078 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004079 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004080
4081 break;
4082 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004083 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004084
4085 break;
4086 default:
4087 BUG();
4088 }
4089}
4090
Jesse Barnesf67a5592011-01-05 10:31:48 -08004091/*
4092 * Enable PCH resources required for PCH ports:
4093 * - PCH PLLs
4094 * - FDI training & RX/TX
4095 * - update transcoder timings
4096 * - DP transcoding bits
4097 * - transcoder
4098 */
4099static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004100{
4101 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004102 struct drm_i915_private *dev_priv = dev->dev_private;
4103 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4104 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004105 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004106
Daniel Vetterab9412b2013-05-03 11:49:46 +02004107 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004108
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004109 if (IS_IVYBRIDGE(dev))
4110 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4111
Daniel Vettercd986ab2012-10-26 10:58:12 +02004112 /* Write the TU size bits before fdi link training, so that error
4113 * detection works. */
4114 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4115 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4116
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004117 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004118 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004119
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004120 /* We need to program the right clock selection before writing the pixel
4121 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004122 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004123 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004124
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004125 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004126 temp |= TRANS_DPLL_ENABLE(pipe);
4127 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004128 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004129 temp |= sel;
4130 else
4131 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004132 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004133 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004134
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004135 /* XXX: pch pll's can be enabled any time before we enable the PCH
4136 * transcoder, and we actually should do this to not upset any PCH
4137 * transcoder that already use the clock when we share it.
4138 *
4139 * Note that enable_shared_dpll tries to do the right thing, but
4140 * get_shared_dpll unconditionally resets the pll - we need that to have
4141 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004142 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004143
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004144 /* set transcoder timing, panel must allow it */
4145 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004146 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004147
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004148 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004149
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004150 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004151 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004152 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01004153 reg = TRANS_DP_CTL(pipe);
4154 temp = I915_READ(reg);
4155 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004156 TRANS_DP_SYNC_MASK |
4157 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004158 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004159 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004160
4161 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004162 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004163 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004164 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004165
4166 switch (intel_trans_dp_port_sel(crtc)) {
4167 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004168 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004169 break;
4170 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004171 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004172 break;
4173 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004174 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004175 break;
4176 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004177 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004178 }
4179
Chris Wilson5eddb702010-09-11 13:48:45 +01004180 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004181 }
4182
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004183 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004184}
4185
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004186static void lpt_pch_enable(struct drm_crtc *crtc)
4187{
4188 struct drm_device *dev = crtc->dev;
4189 struct drm_i915_private *dev_priv = dev->dev_private;
4190 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004191 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004192
Daniel Vetterab9412b2013-05-03 11:49:46 +02004193 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004194
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004195 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004196
Paulo Zanoni0540e482012-10-31 18:12:40 -02004197 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004198 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004199
Paulo Zanoni937bb612012-10-31 18:12:47 -02004200 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004201}
4202
Daniel Vetter716c2e52014-06-25 22:02:02 +03004203void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004204{
Daniel Vettere2b78262013-06-07 23:10:03 +02004205 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004206
4207 if (pll == NULL)
4208 return;
4209
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02004210 if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004211 WARN(1, "bad %s crtc mask\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004212 return;
4213 }
4214
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02004215 pll->config.crtc_mask &= ~(1 << crtc->pipe);
4216 if (pll->config.crtc_mask == 0) {
Daniel Vetterf4a091c2013-06-10 17:28:22 +02004217 WARN_ON(pll->on);
4218 WARN_ON(pll->active);
4219 }
4220
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004221 crtc->config->shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004222}
4223
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004224struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4225 struct intel_crtc_state *crtc_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004226{
Daniel Vettere2b78262013-06-07 23:10:03 +02004227 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004228 struct intel_shared_dpll *pll;
Daniel Vettere2b78262013-06-07 23:10:03 +02004229 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004230
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004231 if (HAS_PCH_IBX(dev_priv->dev)) {
4232 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02004233 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004234 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004235
Daniel Vetter46edb022013-06-05 13:34:12 +02004236 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4237 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004238
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004239 WARN_ON(pll->new_config->crtc_mask);
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004240
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004241 goto found;
4242 }
4243
Satheeshakrishna Mbcddf6102014-08-22 09:49:10 +05304244 if (IS_BROXTON(dev_priv->dev)) {
4245 /* PLL is attached to port in bxt */
4246 struct intel_encoder *encoder;
4247 struct intel_digital_port *intel_dig_port;
4248
4249 encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
4250 if (WARN_ON(!encoder))
4251 return NULL;
4252
4253 intel_dig_port = enc_to_dig_port(&encoder->base);
4254 /* 1:1 mapping between ports and PLLs */
4255 i = (enum intel_dpll_id)intel_dig_port->port;
4256 pll = &dev_priv->shared_dplls[i];
4257 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4258 crtc->base.base.id, pll->name);
4259 WARN_ON(pll->new_config->crtc_mask);
4260
4261 goto found;
4262 }
4263
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004264 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4265 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004266
4267 /* Only want to check enabled timings first */
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004268 if (pll->new_config->crtc_mask == 0)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004269 continue;
4270
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004271 if (memcmp(&crtc_state->dpll_hw_state,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004272 &pll->new_config->hw_state,
4273 sizeof(pll->new_config->hw_state)) == 0) {
4274 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004275 crtc->base.base.id, pll->name,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004276 pll->new_config->crtc_mask,
4277 pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004278 goto found;
4279 }
4280 }
4281
4282 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004283 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4284 pll = &dev_priv->shared_dplls[i];
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004285 if (pll->new_config->crtc_mask == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02004286 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4287 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004288 goto found;
4289 }
4290 }
4291
4292 return NULL;
4293
4294found:
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004295 if (pll->new_config->crtc_mask == 0)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004296 pll->new_config->hw_state = crtc_state->dpll_hw_state;
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004297
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004298 crtc_state->shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02004299 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4300 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02004301
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004302 pll->new_config->crtc_mask |= 1 << crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004303
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004304 return pll;
4305}
4306
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004307/**
4308 * intel_shared_dpll_start_config - start a new PLL staged config
4309 * @dev_priv: DRM device
4310 * @clear_pipes: mask of pipes that will have their PLLs freed
4311 *
4312 * Starts a new PLL staged config, copying the current config but
4313 * releasing the references of pipes specified in clear_pipes.
4314 */
4315static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
4316 unsigned clear_pipes)
4317{
4318 struct intel_shared_dpll *pll;
4319 enum intel_dpll_id i;
4320
4321 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4322 pll = &dev_priv->shared_dplls[i];
4323
4324 pll->new_config = kmemdup(&pll->config, sizeof pll->config,
4325 GFP_KERNEL);
4326 if (!pll->new_config)
4327 goto cleanup;
4328
4329 pll->new_config->crtc_mask &= ~clear_pipes;
4330 }
4331
4332 return 0;
4333
4334cleanup:
4335 while (--i >= 0) {
4336 pll = &dev_priv->shared_dplls[i];
Ander Conselvan de Oliveiraf354d732014-11-07 14:07:41 +02004337 kfree(pll->new_config);
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004338 pll->new_config = NULL;
4339 }
4340
4341 return -ENOMEM;
4342}
4343
4344static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
4345{
4346 struct intel_shared_dpll *pll;
4347 enum intel_dpll_id i;
4348
4349 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4350 pll = &dev_priv->shared_dplls[i];
4351
4352 WARN_ON(pll->new_config == &pll->config);
4353
4354 pll->config = *pll->new_config;
4355 kfree(pll->new_config);
4356 pll->new_config = NULL;
4357 }
4358}
4359
4360static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
4361{
4362 struct intel_shared_dpll *pll;
4363 enum intel_dpll_id i;
4364
4365 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4366 pll = &dev_priv->shared_dplls[i];
4367
4368 WARN_ON(pll->new_config == &pll->config);
4369
4370 kfree(pll->new_config);
4371 pll->new_config = NULL;
4372 }
4373}
4374
Daniel Vettera1520312013-05-03 11:49:50 +02004375static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004376{
4377 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01004378 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004379 u32 temp;
4380
4381 temp = I915_READ(dslreg);
4382 udelay(500);
4383 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004384 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004385 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004386 }
4387}
4388
Chandra Kondurua1b22782015-04-07 15:28:45 -07004389/**
4390 * skl_update_scaler_users - Stages update to crtc's scaler state
4391 * @intel_crtc: crtc
4392 * @crtc_state: crtc_state
4393 * @plane: plane (NULL indicates crtc is requesting update)
4394 * @plane_state: plane's state
4395 * @force_detach: request unconditional detachment of scaler
4396 *
4397 * This function updates scaler state for requested plane or crtc.
4398 * To request scaler usage update for a plane, caller shall pass plane pointer.
4399 * To request scaler usage update for crtc, caller shall pass plane pointer
4400 * as NULL.
4401 *
4402 * Return
4403 * 0 - scaler_usage updated successfully
4404 * error - requested scaling cannot be supported or other error condition
4405 */
4406int
4407skl_update_scaler_users(
4408 struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state,
4409 struct intel_plane *intel_plane, struct intel_plane_state *plane_state,
4410 int force_detach)
4411{
4412 int need_scaling;
4413 int idx;
4414 int src_w, src_h, dst_w, dst_h;
4415 int *scaler_id;
4416 struct drm_framebuffer *fb;
4417 struct intel_crtc_scaler_state *scaler_state;
Chandra Konduru6156a452015-04-27 13:48:39 -07004418 unsigned int rotation;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004419
4420 if (!intel_crtc || !crtc_state)
4421 return 0;
4422
4423 scaler_state = &crtc_state->scaler_state;
4424
4425 idx = intel_plane ? drm_plane_index(&intel_plane->base) : SKL_CRTC_INDEX;
4426 fb = intel_plane ? plane_state->base.fb : NULL;
4427
4428 if (intel_plane) {
4429 src_w = drm_rect_width(&plane_state->src) >> 16;
4430 src_h = drm_rect_height(&plane_state->src) >> 16;
4431 dst_w = drm_rect_width(&plane_state->dst);
4432 dst_h = drm_rect_height(&plane_state->dst);
4433 scaler_id = &plane_state->scaler_id;
Chandra Konduru6156a452015-04-27 13:48:39 -07004434 rotation = plane_state->base.rotation;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004435 } else {
4436 struct drm_display_mode *adjusted_mode =
4437 &crtc_state->base.adjusted_mode;
4438 src_w = crtc_state->pipe_src_w;
4439 src_h = crtc_state->pipe_src_h;
4440 dst_w = adjusted_mode->hdisplay;
4441 dst_h = adjusted_mode->vdisplay;
4442 scaler_id = &scaler_state->scaler_id;
Chandra Konduru6156a452015-04-27 13:48:39 -07004443 rotation = DRM_ROTATE_0;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004444 }
Chandra Konduru6156a452015-04-27 13:48:39 -07004445
4446 need_scaling = intel_rotation_90_or_270(rotation) ?
4447 (src_h != dst_w || src_w != dst_h):
4448 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004449
4450 /*
4451 * if plane is being disabled or scaler is no more required or force detach
4452 * - free scaler binded to this plane/crtc
4453 * - in order to do this, update crtc->scaler_usage
4454 *
4455 * Here scaler state in crtc_state is set free so that
4456 * scaler can be assigned to other user. Actual register
4457 * update to free the scaler is done in plane/panel-fit programming.
4458 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4459 */
4460 if (force_detach || !need_scaling || (intel_plane &&
4461 (!fb || !plane_state->visible))) {
4462 if (*scaler_id >= 0) {
4463 scaler_state->scaler_users &= ~(1 << idx);
4464 scaler_state->scalers[*scaler_id].in_use = 0;
4465
4466 DRM_DEBUG_KMS("Staged freeing scaler id %d.%d from %s:%d "
4467 "crtc_state = %p scaler_users = 0x%x\n",
4468 intel_crtc->pipe, *scaler_id, intel_plane ? "PLANE" : "CRTC",
4469 intel_plane ? intel_plane->base.base.id :
4470 intel_crtc->base.base.id, crtc_state,
4471 scaler_state->scaler_users);
4472 *scaler_id = -1;
4473 }
4474 return 0;
4475 }
4476
4477 /* range checks */
4478 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4479 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4480
4481 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4482 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
4483 DRM_DEBUG_KMS("%s:%d scaler_user index %u.%u: src %ux%u dst %ux%u "
4484 "size is out of scaler range\n",
4485 intel_plane ? "PLANE" : "CRTC",
4486 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4487 intel_crtc->pipe, idx, src_w, src_h, dst_w, dst_h);
4488 return -EINVAL;
4489 }
4490
4491 /* check colorkey */
Chandra Konduru225c2282015-05-18 16:18:44 -07004492 if (WARN_ON(intel_plane &&
4493 intel_plane->ckey.flags != I915_SET_COLORKEY_NONE)) {
4494 DRM_DEBUG_KMS("PLANE:%d scaling %ux%u->%ux%u not allowed with colorkey",
4495 intel_plane->base.base.id, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004496 return -EINVAL;
4497 }
4498
4499 /* Check src format */
4500 if (intel_plane) {
4501 switch (fb->pixel_format) {
4502 case DRM_FORMAT_RGB565:
4503 case DRM_FORMAT_XBGR8888:
4504 case DRM_FORMAT_XRGB8888:
4505 case DRM_FORMAT_ABGR8888:
4506 case DRM_FORMAT_ARGB8888:
4507 case DRM_FORMAT_XRGB2101010:
Chandra Kondurua1b22782015-04-07 15:28:45 -07004508 case DRM_FORMAT_XBGR2101010:
Chandra Kondurua1b22782015-04-07 15:28:45 -07004509 case DRM_FORMAT_YUYV:
4510 case DRM_FORMAT_YVYU:
4511 case DRM_FORMAT_UYVY:
4512 case DRM_FORMAT_VYUY:
4513 break;
4514 default:
4515 DRM_DEBUG_KMS("PLANE:%d FB:%d unsupported scaling format 0x%x\n",
4516 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4517 return -EINVAL;
4518 }
4519 }
4520
4521 /* mark this plane as a scaler user in crtc_state */
4522 scaler_state->scaler_users |= (1 << idx);
4523 DRM_DEBUG_KMS("%s:%d staged scaling request for %ux%u->%ux%u "
4524 "crtc_state = %p scaler_users = 0x%x\n",
4525 intel_plane ? "PLANE" : "CRTC",
4526 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4527 src_w, src_h, dst_w, dst_h, crtc_state, scaler_state->scaler_users);
4528 return 0;
4529}
4530
4531static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004532{
4533 struct drm_device *dev = crtc->base.dev;
4534 struct drm_i915_private *dev_priv = dev->dev_private;
4535 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004536 struct intel_crtc_scaler_state *scaler_state =
4537 &crtc->config->scaler_state;
4538
4539 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4540
4541 /* To update pfit, first update scaler state */
4542 skl_update_scaler_users(crtc, crtc->config, NULL, NULL, !enable);
4543 intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
4544 skl_detach_scalers(crtc);
4545 if (!enable)
4546 return;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004547
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004548 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004549 int id;
4550
4551 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4552 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4553 return;
4554 }
4555
4556 id = scaler_state->scaler_id;
4557 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4558 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4559 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4560 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4561
4562 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004563 }
4564}
4565
Jesse Barnesb074cec2013-04-25 12:55:02 -07004566static void ironlake_pfit_enable(struct intel_crtc *crtc)
4567{
4568 struct drm_device *dev = crtc->base.dev;
4569 struct drm_i915_private *dev_priv = dev->dev_private;
4570 int pipe = crtc->pipe;
4571
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004572 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004573 /* Force use of hard-coded filter coefficients
4574 * as some pre-programmed values are broken,
4575 * e.g. x201.
4576 */
4577 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4578 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4579 PF_PIPE_SEL_IVB(pipe));
4580 else
4581 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004582 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4583 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004584 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004585}
4586
Matt Roper4a3b8762014-12-23 10:41:51 -08004587static void intel_enable_sprite_planes(struct drm_crtc *crtc)
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004588{
4589 struct drm_device *dev = crtc->dev;
4590 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07004591 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004592 struct intel_plane *intel_plane;
4593
Matt Roperaf2b6532014-04-01 15:22:32 -07004594 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4595 intel_plane = to_intel_plane(plane);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004596 if (intel_plane->pipe == pipe)
4597 intel_plane_restore(&intel_plane->base);
Matt Roperaf2b6532014-04-01 15:22:32 -07004598 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004599}
4600
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004601void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004602{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004603 struct drm_device *dev = crtc->base.dev;
4604 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004605
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004606 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004607 return;
4608
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004609 /* We can only enable IPS after we enable a plane and wait for a vblank */
4610 intel_wait_for_vblank(dev, crtc->pipe);
4611
Paulo Zanonid77e4532013-09-24 13:52:55 -03004612 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004613 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004614 mutex_lock(&dev_priv->rps.hw_lock);
4615 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4616 mutex_unlock(&dev_priv->rps.hw_lock);
4617 /* Quoting Art Runyan: "its not safe to expect any particular
4618 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004619 * mailbox." Moreover, the mailbox may return a bogus state,
4620 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004621 */
4622 } else {
4623 I915_WRITE(IPS_CTL, IPS_ENABLE);
4624 /* The bit only becomes 1 in the next vblank, so this wait here
4625 * is essentially intel_wait_for_vblank. If we don't have this
4626 * and don't wait for vblanks until the end of crtc_enable, then
4627 * the HW state readout code will complain that the expected
4628 * IPS_CTL value is not the one we read. */
4629 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4630 DRM_ERROR("Timed out waiting for IPS enable\n");
4631 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004632}
4633
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004634void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004635{
4636 struct drm_device *dev = crtc->base.dev;
4637 struct drm_i915_private *dev_priv = dev->dev_private;
4638
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004639 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004640 return;
4641
4642 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004643 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004644 mutex_lock(&dev_priv->rps.hw_lock);
4645 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4646 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004647 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4648 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4649 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004650 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004651 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004652 POSTING_READ(IPS_CTL);
4653 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004654
4655 /* We need to wait for a vblank before we can disable the plane. */
4656 intel_wait_for_vblank(dev, crtc->pipe);
4657}
4658
4659/** Loads the palette/gamma unit for the CRTC with the prepared values */
4660static void intel_crtc_load_lut(struct drm_crtc *crtc)
4661{
4662 struct drm_device *dev = crtc->dev;
4663 struct drm_i915_private *dev_priv = dev->dev_private;
4664 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4665 enum pipe pipe = intel_crtc->pipe;
4666 int palreg = PALETTE(pipe);
4667 int i;
4668 bool reenable_ips = false;
4669
4670 /* The clocks have to be on to load the palette. */
Matt Roper83d65732015-02-25 13:12:16 -08004671 if (!crtc->state->enable || !intel_crtc->active)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004672 return;
4673
Imre Deak50360402015-01-16 00:55:16 -08004674 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03004675 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004676 assert_dsi_pll_enabled(dev_priv);
4677 else
4678 assert_pll_enabled(dev_priv, pipe);
4679 }
4680
4681 /* use legacy palette for Ironlake */
Sonika Jindal7a1db492014-07-22 11:18:27 +05304682 if (!HAS_GMCH_DISPLAY(dev))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004683 palreg = LGC_PALETTE(pipe);
4684
4685 /* Workaround : Do not read or write the pipe palette/gamma data while
4686 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4687 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004688 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03004689 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4690 GAMMA_MODE_MODE_SPLIT)) {
4691 hsw_disable_ips(intel_crtc);
4692 reenable_ips = true;
4693 }
4694
4695 for (i = 0; i < 256; i++) {
4696 I915_WRITE(palreg + 4 * i,
4697 (intel_crtc->lut_r[i] << 16) |
4698 (intel_crtc->lut_g[i] << 8) |
4699 intel_crtc->lut_b[i]);
4700 }
4701
4702 if (reenable_ips)
4703 hsw_enable_ips(intel_crtc);
4704}
4705
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004706static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004707{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004708 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004709 struct drm_device *dev = intel_crtc->base.dev;
4710 struct drm_i915_private *dev_priv = dev->dev_private;
4711
4712 mutex_lock(&dev->struct_mutex);
4713 dev_priv->mm.interruptible = false;
4714 (void) intel_overlay_switch_off(intel_crtc->overlay);
4715 dev_priv->mm.interruptible = true;
4716 mutex_unlock(&dev->struct_mutex);
4717 }
4718
4719 /* Let userspace switch the overlay on again. In most cases userspace
4720 * has to recompute where to put it anyway.
4721 */
4722}
4723
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004724/**
4725 * intel_post_enable_primary - Perform operations after enabling primary plane
4726 * @crtc: the CRTC whose primary plane was just enabled
4727 *
4728 * Performs potentially sleeping operations that must be done after the primary
4729 * plane is enabled, such as updating FBC and IPS. Note that this may be
4730 * called due to an explicit primary plane update, or due to an implicit
4731 * re-enable that is caused when a sprite plane is updated to no longer
4732 * completely hide the primary plane.
4733 */
4734static void
4735intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004736{
4737 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004738 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004739 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4740 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004741
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004742 /*
4743 * BDW signals flip done immediately if the plane
4744 * is disabled, even if the plane enable is already
4745 * armed to occur at the next vblank :(
4746 */
4747 if (IS_BROADWELL(dev))
4748 intel_wait_for_vblank(dev, pipe);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004749
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004750 /*
4751 * FIXME IPS should be fine as long as one plane is
4752 * enabled, but in practice it seems to have problems
4753 * when going from primary only to sprite only and vice
4754 * versa.
4755 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004756 hsw_enable_ips(intel_crtc);
4757
4758 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004759 intel_fbc_update(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004760 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf99d7062014-06-19 16:01:59 +02004761
4762 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004763 * Gen2 reports pipe underruns whenever all planes are disabled.
4764 * So don't enable underrun reporting before at least some planes
4765 * are enabled.
4766 * FIXME: Need to fix the logic to work when we turn off all planes
4767 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004768 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004769 if (IS_GEN2(dev))
4770 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4771
4772 /* Underruns don't raise interrupts, so check manually. */
4773 if (HAS_GMCH_DISPLAY(dev))
4774 i9xx_check_fifo_underruns(dev_priv);
4775}
4776
4777/**
4778 * intel_pre_disable_primary - Perform operations before disabling primary plane
4779 * @crtc: the CRTC whose primary plane is to be disabled
4780 *
4781 * Performs potentially sleeping operations that must be done before the
4782 * primary plane is disabled, such as updating FBC and IPS. Note that this may
4783 * be called due to an explicit primary plane update, or due to an implicit
4784 * disable that is caused when a sprite plane completely hides the primary
4785 * plane.
4786 */
4787static void
4788intel_pre_disable_primary(struct drm_crtc *crtc)
4789{
4790 struct drm_device *dev = crtc->dev;
4791 struct drm_i915_private *dev_priv = dev->dev_private;
4792 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4793 int pipe = intel_crtc->pipe;
4794
4795 /*
4796 * Gen2 reports pipe underruns whenever all planes are disabled.
4797 * So diasble underrun reporting before all the planes get disabled.
4798 * FIXME: Need to fix the logic to work when we turn off all planes
4799 * but leave the pipe running.
4800 */
4801 if (IS_GEN2(dev))
4802 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4803
4804 /*
4805 * Vblank time updates from the shadow to live plane control register
4806 * are blocked if the memory self-refresh mode is active at that
4807 * moment. So to make sure the plane gets truly disabled, disable
4808 * first the self-refresh mode. The self-refresh enable bit in turn
4809 * will be checked/applied by the HW only at the next frame start
4810 * event which is after the vblank start event, so we need to have a
4811 * wait-for-vblank between disabling the plane and the pipe.
4812 */
4813 if (HAS_GMCH_DISPLAY(dev))
4814 intel_set_memory_cxsr(dev_priv, false);
4815
4816 mutex_lock(&dev->struct_mutex);
4817 if (dev_priv->fbc.crtc == intel_crtc)
4818 intel_fbc_disable(dev);
4819 mutex_unlock(&dev->struct_mutex);
4820
4821 /*
4822 * FIXME IPS should be fine as long as one plane is
4823 * enabled, but in practice it seems to have problems
4824 * when going from primary only to sprite only and vice
4825 * versa.
4826 */
4827 hsw_disable_ips(intel_crtc);
4828}
4829
4830static void intel_crtc_enable_planes(struct drm_crtc *crtc)
4831{
Rodrigo Vivi2d847d42015-05-28 10:21:16 -07004832 struct drm_device *dev = crtc->dev;
4833 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4834 int pipe = intel_crtc->pipe;
4835
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004836 intel_enable_primary_hw_plane(crtc->primary, crtc);
4837 intel_enable_sprite_planes(crtc);
4838 intel_crtc_update_cursor(crtc, true);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004839
4840 intel_post_enable_primary(crtc);
Rodrigo Vivi2d847d42015-05-28 10:21:16 -07004841
4842 /*
4843 * FIXME: Once we grow proper nuclear flip support out of this we need
4844 * to compute the mask of flip planes precisely. For the time being
4845 * consider this a flip to a NULL plane.
4846 */
4847 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004848}
4849
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004850static void intel_crtc_disable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004851{
4852 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004853 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004854 struct intel_plane *intel_plane;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004855 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004856
4857 intel_crtc_wait_for_pending_flips(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004858
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004859 intel_pre_disable_primary(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004860
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004861 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004862 for_each_intel_plane(dev, intel_plane) {
4863 if (intel_plane->pipe == pipe) {
4864 struct drm_crtc *from = intel_plane->base.crtc;
4865
4866 intel_plane->disable_plane(&intel_plane->base,
4867 from ?: crtc, true);
4868 }
4869 }
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004870
Daniel Vetterf99d7062014-06-19 16:01:59 +02004871 /*
4872 * FIXME: Once we grow proper nuclear flip support out of this we need
4873 * to compute the mask of flip planes precisely. For the time being
4874 * consider this a flip to a NULL plane.
4875 */
4876 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004877}
4878
Jesse Barnesf67a5592011-01-05 10:31:48 -08004879static void ironlake_crtc_enable(struct drm_crtc *crtc)
4880{
4881 struct drm_device *dev = crtc->dev;
4882 struct drm_i915_private *dev_priv = dev->dev_private;
4883 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004884 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004885 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004886
Matt Roper83d65732015-02-25 13:12:16 -08004887 WARN_ON(!crtc->state->enable);
Daniel Vetter08a48462012-07-02 11:43:47 +02004888
Jesse Barnesf67a5592011-01-05 10:31:48 -08004889 if (intel_crtc->active)
4890 return;
4891
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004892 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004893 intel_prepare_shared_dpll(intel_crtc);
4894
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004895 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304896 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004897
4898 intel_set_pipe_timings(intel_crtc);
4899
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004900 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004901 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004902 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004903 }
4904
4905 ironlake_set_pipeconf(crtc);
4906
Jesse Barnesf67a5592011-01-05 10:31:48 -08004907 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004908
Daniel Vettera72e4c92014-09-30 10:56:47 +02004909 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4910 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004911
Daniel Vetterf6736a12013-06-05 13:34:30 +02004912 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004913 if (encoder->pre_enable)
4914 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004915
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004916 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004917 /* Note: FDI PLL enabling _must_ be done before we enable the
4918 * cpu pipes, hence this is separate from all the other fdi/pch
4919 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004920 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004921 } else {
4922 assert_fdi_tx_disabled(dev_priv, pipe);
4923 assert_fdi_rx_disabled(dev_priv, pipe);
4924 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004925
Jesse Barnesb074cec2013-04-25 12:55:02 -07004926 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004927
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004928 /*
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
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004934 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004935 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004936
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004937 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004938 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004939
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004940 assert_vblank_disabled(crtc);
4941 drm_crtc_vblank_on(crtc);
4942
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004943 for_each_encoder_on_crtc(dev, crtc, encoder)
4944 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004945
4946 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004947 cpt_verify_modeset(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004948}
4949
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004950/* IPS only exists on ULT machines and is tied to pipe A. */
4951static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4952{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004953 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004954}
4955
Paulo Zanonie4916942013-09-20 16:21:19 -03004956/*
4957 * This implements the workaround described in the "notes" section of the mode
4958 * set sequence documentation. When going from no pipes or single pipe to
4959 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4960 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4961 */
4962static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4963{
4964 struct drm_device *dev = crtc->base.dev;
4965 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4966
4967 /* We want to get the other_active_crtc only if there's only 1 other
4968 * active crtc. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004969 for_each_intel_crtc(dev, crtc_it) {
Paulo Zanonie4916942013-09-20 16:21:19 -03004970 if (!crtc_it->active || crtc_it == crtc)
4971 continue;
4972
4973 if (other_active_crtc)
4974 return;
4975
4976 other_active_crtc = crtc_it;
4977 }
4978 if (!other_active_crtc)
4979 return;
4980
4981 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4982 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4983}
4984
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004985static void haswell_crtc_enable(struct drm_crtc *crtc)
4986{
4987 struct drm_device *dev = crtc->dev;
4988 struct drm_i915_private *dev_priv = dev->dev_private;
4989 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4990 struct intel_encoder *encoder;
4991 int pipe = intel_crtc->pipe;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004992
Matt Roper83d65732015-02-25 13:12:16 -08004993 WARN_ON(!crtc->state->enable);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004994
4995 if (intel_crtc->active)
4996 return;
4997
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004998 if (intel_crtc_to_shared_dpll(intel_crtc))
4999 intel_enable_shared_dpll(intel_crtc);
5000
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005001 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05305002 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02005003
5004 intel_set_pipe_timings(intel_crtc);
5005
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005006 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
5007 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
5008 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07005009 }
5010
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005011 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02005012 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005013 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02005014 }
5015
5016 haswell_set_pipeconf(crtc);
5017
5018 intel_set_pipe_csc(crtc);
5019
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005020 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03005021
Daniel Vettera72e4c92014-09-30 10:56:47 +02005022 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005023 for_each_encoder_on_crtc(dev, crtc, encoder)
5024 if (encoder->pre_enable)
5025 encoder->pre_enable(encoder);
5026
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005027 if (intel_crtc->config->has_pch_encoder) {
Daniel Vettera72e4c92014-09-30 10:56:47 +02005028 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5029 true);
Imre Deak4fe94672014-06-25 22:01:49 +03005030 dev_priv->display.fdi_link_train(crtc);
5031 }
5032
Paulo Zanoni1f544382012-10-24 11:32:00 -02005033 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005034
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005035 if (INTEL_INFO(dev)->gen == 9)
Chandra Kondurua1b22782015-04-07 15:28:45 -07005036 skylake_pfit_update(intel_crtc, 1);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005037 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00005038 ironlake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005039 else
5040 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005041
5042 /*
5043 * On ILK+ LUT must be loaded before the pipe is running but with
5044 * clocks enabled
5045 */
5046 intel_crtc_load_lut(crtc);
5047
Paulo Zanoni1f544382012-10-24 11:32:00 -02005048 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00005049 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005050
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005051 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005052 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005053
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005054 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02005055 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005056
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005057 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10005058 intel_ddi_set_vc_payload_alloc(crtc, true);
5059
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005060 assert_vblank_disabled(crtc);
5061 drm_crtc_vblank_on(crtc);
5062
Jani Nikula8807e552013-08-30 19:40:32 +03005063 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005064 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005065 intel_opregion_notify_encoder(encoder, true);
5066 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005067
Paulo Zanonie4916942013-09-20 16:21:19 -03005068 /* If we change the relative order between pipe/planes enabling, we need
5069 * to change the workaround. */
5070 haswell_mode_set_planes_workaround(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005071}
5072
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005073static void ironlake_pfit_disable(struct intel_crtc *crtc)
5074{
5075 struct drm_device *dev = crtc->base.dev;
5076 struct drm_i915_private *dev_priv = dev->dev_private;
5077 int pipe = crtc->pipe;
5078
5079 /* To avoid upsetting the power well on haswell only disable the pfit if
5080 * it's in use. The hw state code will make sure we get this right. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005081 if (crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005082 I915_WRITE(PF_CTL(pipe), 0);
5083 I915_WRITE(PF_WIN_POS(pipe), 0);
5084 I915_WRITE(PF_WIN_SZ(pipe), 0);
5085 }
5086}
5087
Jesse Barnes6be4a602010-09-10 10:26:01 -07005088static void ironlake_crtc_disable(struct drm_crtc *crtc)
5089{
5090 struct drm_device *dev = crtc->dev;
5091 struct drm_i915_private *dev_priv = dev->dev_private;
5092 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005093 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005094 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01005095 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005096
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005097 if (!intel_crtc->active)
5098 return;
5099
Daniel Vetterea9d7582012-07-10 10:42:52 +02005100 for_each_encoder_on_crtc(dev, crtc, encoder)
5101 encoder->disable(encoder);
5102
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005103 drm_crtc_vblank_off(crtc);
5104 assert_vblank_disabled(crtc);
5105
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005106 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005107 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Daniel Vetterd925c592013-06-05 13:34:04 +02005108
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005109 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005110
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005111 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005112
Ville Syrjälä5a74f702015-05-05 17:17:38 +03005113 if (intel_crtc->config->has_pch_encoder)
5114 ironlake_fdi_disable(crtc);
5115
Daniel Vetterbf49ec82012-09-06 22:15:40 +02005116 for_each_encoder_on_crtc(dev, crtc, encoder)
5117 if (encoder->post_disable)
5118 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005119
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005120 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005121 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005122
Daniel Vetterd925c592013-06-05 13:34:04 +02005123 if (HAS_PCH_CPT(dev)) {
5124 /* disable TRANS_DP_CTL */
5125 reg = TRANS_DP_CTL(pipe);
5126 temp = I915_READ(reg);
5127 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5128 TRANS_DP_PORT_SEL_MASK);
5129 temp |= TRANS_DP_PORT_SEL_NONE;
5130 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005131
Daniel Vetterd925c592013-06-05 13:34:04 +02005132 /* disable DPLL_SEL */
5133 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005134 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005135 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005136 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005137
5138 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005139 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02005140
5141 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005142 }
5143
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005144 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005145 intel_update_watermarks(crtc);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01005146
5147 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005148 intel_fbc_update(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01005149 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005150}
5151
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005152static void haswell_crtc_disable(struct drm_crtc *crtc)
5153{
5154 struct drm_device *dev = crtc->dev;
5155 struct drm_i915_private *dev_priv = dev->dev_private;
5156 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5157 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005158 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005159
5160 if (!intel_crtc->active)
5161 return;
5162
Jani Nikula8807e552013-08-30 19:40:32 +03005163 for_each_encoder_on_crtc(dev, crtc, encoder) {
5164 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005165 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005166 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005167
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005168 drm_crtc_vblank_off(crtc);
5169 assert_vblank_disabled(crtc);
5170
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005171 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005172 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5173 false);
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005174 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005175
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005176 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005177 intel_ddi_set_vc_payload_alloc(crtc, false);
5178
Paulo Zanoniad80a812012-10-24 16:06:19 -02005179 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005180
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005181 if (INTEL_INFO(dev)->gen == 9)
Chandra Kondurua1b22782015-04-07 15:28:45 -07005182 skylake_pfit_update(intel_crtc, 0);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005183 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00005184 ironlake_pfit_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005185 else
5186 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005187
Paulo Zanoni1f544382012-10-24 11:32:00 -02005188 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005189
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005190 if (intel_crtc->config->has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02005191 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02005192 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02005193 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005194
Imre Deak97b040a2014-06-25 22:01:50 +03005195 for_each_encoder_on_crtc(dev, crtc, encoder)
5196 if (encoder->post_disable)
5197 encoder->post_disable(encoder);
5198
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005199 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005200 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005201
5202 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005203 intel_fbc_update(dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005204 mutex_unlock(&dev->struct_mutex);
Daniel Vetterdf8ad702014-06-25 22:02:03 +03005205
5206 if (intel_crtc_to_shared_dpll(intel_crtc))
5207 intel_disable_shared_dpll(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005208}
5209
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005210static void ironlake_crtc_off(struct drm_crtc *crtc)
5211{
5212 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005213 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005214}
5215
Paulo Zanoni6441ab52012-10-05 12:05:58 -03005216
Jesse Barnes2dd24552013-04-25 12:55:01 -07005217static void i9xx_pfit_enable(struct intel_crtc *crtc)
5218{
5219 struct drm_device *dev = crtc->base.dev;
5220 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005221 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005222
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005223 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005224 return;
5225
Daniel Vetterc0b03412013-05-28 12:05:54 +02005226 /*
5227 * The panel fitter should only be adjusted whilst the pipe is disabled,
5228 * according to register description and PRM.
5229 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005230 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5231 assert_pipe_disabled(dev_priv, crtc->pipe);
5232
Jesse Barnesb074cec2013-04-25 12:55:02 -07005233 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5234 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005235
5236 /* Border color in case we don't scale up to the full screen. Black by
5237 * default, change to something else for debugging. */
5238 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005239}
5240
Dave Airlied05410f2014-06-05 13:22:59 +10005241static enum intel_display_power_domain port_to_power_domain(enum port port)
5242{
5243 switch (port) {
5244 case PORT_A:
5245 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
5246 case PORT_B:
5247 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
5248 case PORT_C:
5249 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
5250 case PORT_D:
5251 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
5252 default:
5253 WARN_ON_ONCE(1);
5254 return POWER_DOMAIN_PORT_OTHER;
5255 }
5256}
5257
Imre Deak77d22dc2014-03-05 16:20:52 +02005258#define for_each_power_domain(domain, mask) \
5259 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
5260 if ((1 << (domain)) & (mask))
5261
Imre Deak319be8a2014-03-04 19:22:57 +02005262enum intel_display_power_domain
5263intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005264{
Imre Deak319be8a2014-03-04 19:22:57 +02005265 struct drm_device *dev = intel_encoder->base.dev;
5266 struct intel_digital_port *intel_dig_port;
5267
5268 switch (intel_encoder->type) {
5269 case INTEL_OUTPUT_UNKNOWN:
5270 /* Only DDI platforms should ever use this output type */
5271 WARN_ON_ONCE(!HAS_DDI(dev));
5272 case INTEL_OUTPUT_DISPLAYPORT:
5273 case INTEL_OUTPUT_HDMI:
5274 case INTEL_OUTPUT_EDP:
5275 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005276 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005277 case INTEL_OUTPUT_DP_MST:
5278 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5279 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005280 case INTEL_OUTPUT_ANALOG:
5281 return POWER_DOMAIN_PORT_CRT;
5282 case INTEL_OUTPUT_DSI:
5283 return POWER_DOMAIN_PORT_DSI;
5284 default:
5285 return POWER_DOMAIN_PORT_OTHER;
5286 }
5287}
5288
5289static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
5290{
5291 struct drm_device *dev = crtc->dev;
5292 struct intel_encoder *intel_encoder;
5293 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5294 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005295 unsigned long mask;
5296 enum transcoder transcoder;
5297
5298 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
5299
5300 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5301 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005302 if (intel_crtc->config->pch_pfit.enabled ||
5303 intel_crtc->config->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005304 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5305
Imre Deak319be8a2014-03-04 19:22:57 +02005306 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5307 mask |= BIT(intel_display_port_power_domain(intel_encoder));
5308
Imre Deak77d22dc2014-03-05 16:20:52 +02005309 return mask;
5310}
5311
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005312static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
Imre Deak77d22dc2014-03-05 16:20:52 +02005313{
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005314 struct drm_device *dev = state->dev;
Imre Deak77d22dc2014-03-05 16:20:52 +02005315 struct drm_i915_private *dev_priv = dev->dev_private;
5316 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
5317 struct intel_crtc *crtc;
5318
5319 /*
5320 * First get all needed power domains, then put all unneeded, to avoid
5321 * any unnecessary toggling of the power wells.
5322 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005323 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005324 enum intel_display_power_domain domain;
5325
Matt Roper83d65732015-02-25 13:12:16 -08005326 if (!crtc->base.state->enable)
Imre Deak77d22dc2014-03-05 16:20:52 +02005327 continue;
5328
Imre Deak319be8a2014-03-04 19:22:57 +02005329 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
Imre Deak77d22dc2014-03-05 16:20:52 +02005330
5331 for_each_power_domain(domain, pipe_domains[crtc->pipe])
5332 intel_display_power_get(dev_priv, domain);
5333 }
5334
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005335 if (dev_priv->display.modeset_global_resources)
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005336 dev_priv->display.modeset_global_resources(state);
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005337
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005338 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005339 enum intel_display_power_domain domain;
5340
5341 for_each_power_domain(domain, crtc->enabled_power_domains)
5342 intel_display_power_put(dev_priv, domain);
5343
5344 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
5345 }
5346
5347 intel_display_set_init_power(dev_priv, false);
5348}
5349
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305350void broxton_set_cdclk(struct drm_device *dev, int frequency)
5351{
5352 struct drm_i915_private *dev_priv = dev->dev_private;
5353 uint32_t divider;
5354 uint32_t ratio;
5355 uint32_t current_freq;
5356 int ret;
5357
5358 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5359 switch (frequency) {
5360 case 144000:
5361 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5362 ratio = BXT_DE_PLL_RATIO(60);
5363 break;
5364 case 288000:
5365 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5366 ratio = BXT_DE_PLL_RATIO(60);
5367 break;
5368 case 384000:
5369 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5370 ratio = BXT_DE_PLL_RATIO(60);
5371 break;
5372 case 576000:
5373 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5374 ratio = BXT_DE_PLL_RATIO(60);
5375 break;
5376 case 624000:
5377 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5378 ratio = BXT_DE_PLL_RATIO(65);
5379 break;
5380 case 19200:
5381 /*
5382 * Bypass frequency with DE PLL disabled. Init ratio, divider
5383 * to suppress GCC warning.
5384 */
5385 ratio = 0;
5386 divider = 0;
5387 break;
5388 default:
5389 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5390
5391 return;
5392 }
5393
5394 mutex_lock(&dev_priv->rps.hw_lock);
5395 /* Inform power controller of upcoming frequency change */
5396 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5397 0x80000000);
5398 mutex_unlock(&dev_priv->rps.hw_lock);
5399
5400 if (ret) {
5401 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5402 ret, frequency);
5403 return;
5404 }
5405
5406 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5407 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5408 current_freq = current_freq * 500 + 1000;
5409
5410 /*
5411 * DE PLL has to be disabled when
5412 * - setting to 19.2MHz (bypass, PLL isn't used)
5413 * - before setting to 624MHz (PLL needs toggling)
5414 * - before setting to any frequency from 624MHz (PLL needs toggling)
5415 */
5416 if (frequency == 19200 || frequency == 624000 ||
5417 current_freq == 624000) {
5418 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5419 /* Timeout 200us */
5420 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5421 1))
5422 DRM_ERROR("timout waiting for DE PLL unlock\n");
5423 }
5424
5425 if (frequency != 19200) {
5426 uint32_t val;
5427
5428 val = I915_READ(BXT_DE_PLL_CTL);
5429 val &= ~BXT_DE_PLL_RATIO_MASK;
5430 val |= ratio;
5431 I915_WRITE(BXT_DE_PLL_CTL, val);
5432
5433 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5434 /* Timeout 200us */
5435 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5436 DRM_ERROR("timeout waiting for DE PLL lock\n");
5437
5438 val = I915_READ(CDCLK_CTL);
5439 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5440 val |= divider;
5441 /*
5442 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5443 * enable otherwise.
5444 */
5445 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5446 if (frequency >= 500000)
5447 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5448
5449 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5450 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5451 val |= (frequency - 1000) / 500;
5452 I915_WRITE(CDCLK_CTL, val);
5453 }
5454
5455 mutex_lock(&dev_priv->rps.hw_lock);
5456 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5457 DIV_ROUND_UP(frequency, 25000));
5458 mutex_unlock(&dev_priv->rps.hw_lock);
5459
5460 if (ret) {
5461 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5462 ret, frequency);
5463 return;
5464 }
5465
5466 dev_priv->cdclk_freq = frequency;
5467}
5468
5469void broxton_init_cdclk(struct drm_device *dev)
5470{
5471 struct drm_i915_private *dev_priv = dev->dev_private;
5472 uint32_t val;
5473
5474 /*
5475 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5476 * or else the reset will hang because there is no PCH to respond.
5477 * Move the handshake programming to initialization sequence.
5478 * Previously was left up to BIOS.
5479 */
5480 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5481 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5482 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5483
5484 /* Enable PG1 for cdclk */
5485 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5486
5487 /* check if cd clock is enabled */
5488 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5489 DRM_DEBUG_KMS("Display already initialized\n");
5490 return;
5491 }
5492
5493 /*
5494 * FIXME:
5495 * - The initial CDCLK needs to be read from VBT.
5496 * Need to make this change after VBT has changes for BXT.
5497 * - check if setting the max (or any) cdclk freq is really necessary
5498 * here, it belongs to modeset time
5499 */
5500 broxton_set_cdclk(dev, 624000);
5501
5502 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005503 POSTING_READ(DBUF_CTL);
5504
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305505 udelay(10);
5506
5507 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5508 DRM_ERROR("DBuf power enable timeout!\n");
5509}
5510
5511void broxton_uninit_cdclk(struct drm_device *dev)
5512{
5513 struct drm_i915_private *dev_priv = dev->dev_private;
5514
5515 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005516 POSTING_READ(DBUF_CTL);
5517
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305518 udelay(10);
5519
5520 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5521 DRM_ERROR("DBuf power disable timeout!\n");
5522
5523 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5524 broxton_set_cdclk(dev, 19200);
5525
5526 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5527}
5528
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005529static const struct skl_cdclk_entry {
5530 unsigned int freq;
5531 unsigned int vco;
5532} skl_cdclk_frequencies[] = {
5533 { .freq = 308570, .vco = 8640 },
5534 { .freq = 337500, .vco = 8100 },
5535 { .freq = 432000, .vco = 8640 },
5536 { .freq = 450000, .vco = 8100 },
5537 { .freq = 540000, .vco = 8100 },
5538 { .freq = 617140, .vco = 8640 },
5539 { .freq = 675000, .vco = 8100 },
5540};
5541
5542static unsigned int skl_cdclk_decimal(unsigned int freq)
5543{
5544 return (freq - 1000) / 500;
5545}
5546
5547static unsigned int skl_cdclk_get_vco(unsigned int freq)
5548{
5549 unsigned int i;
5550
5551 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5552 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5553
5554 if (e->freq == freq)
5555 return e->vco;
5556 }
5557
5558 return 8100;
5559}
5560
5561static void
5562skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5563{
5564 unsigned int min_freq;
5565 u32 val;
5566
5567 /* select the minimum CDCLK before enabling DPLL 0 */
5568 val = I915_READ(CDCLK_CTL);
5569 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5570 val |= CDCLK_FREQ_337_308;
5571
5572 if (required_vco == 8640)
5573 min_freq = 308570;
5574 else
5575 min_freq = 337500;
5576
5577 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5578
5579 I915_WRITE(CDCLK_CTL, val);
5580 POSTING_READ(CDCLK_CTL);
5581
5582 /*
5583 * We always enable DPLL0 with the lowest link rate possible, but still
5584 * taking into account the VCO required to operate the eDP panel at the
5585 * desired frequency. The usual DP link rates operate with a VCO of
5586 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5587 * The modeset code is responsible for the selection of the exact link
5588 * rate later on, with the constraint of choosing a frequency that
5589 * works with required_vco.
5590 */
5591 val = I915_READ(DPLL_CTRL1);
5592
5593 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5594 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5595 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5596 if (required_vco == 8640)
5597 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5598 SKL_DPLL0);
5599 else
5600 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5601 SKL_DPLL0);
5602
5603 I915_WRITE(DPLL_CTRL1, val);
5604 POSTING_READ(DPLL_CTRL1);
5605
5606 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5607
5608 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5609 DRM_ERROR("DPLL0 not locked\n");
5610}
5611
5612static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5613{
5614 int ret;
5615 u32 val;
5616
5617 /* inform PCU we want to change CDCLK */
5618 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5619 mutex_lock(&dev_priv->rps.hw_lock);
5620 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5621 mutex_unlock(&dev_priv->rps.hw_lock);
5622
5623 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5624}
5625
5626static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5627{
5628 unsigned int i;
5629
5630 for (i = 0; i < 15; i++) {
5631 if (skl_cdclk_pcu_ready(dev_priv))
5632 return true;
5633 udelay(10);
5634 }
5635
5636 return false;
5637}
5638
5639static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5640{
5641 u32 freq_select, pcu_ack;
5642
5643 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5644
5645 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5646 DRM_ERROR("failed to inform PCU about cdclk change\n");
5647 return;
5648 }
5649
5650 /* set CDCLK_CTL */
5651 switch(freq) {
5652 case 450000:
5653 case 432000:
5654 freq_select = CDCLK_FREQ_450_432;
5655 pcu_ack = 1;
5656 break;
5657 case 540000:
5658 freq_select = CDCLK_FREQ_540;
5659 pcu_ack = 2;
5660 break;
5661 case 308570:
5662 case 337500:
5663 default:
5664 freq_select = CDCLK_FREQ_337_308;
5665 pcu_ack = 0;
5666 break;
5667 case 617140:
5668 case 675000:
5669 freq_select = CDCLK_FREQ_675_617;
5670 pcu_ack = 3;
5671 break;
5672 }
5673
5674 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5675 POSTING_READ(CDCLK_CTL);
5676
5677 /* inform PCU of the change */
5678 mutex_lock(&dev_priv->rps.hw_lock);
5679 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5680 mutex_unlock(&dev_priv->rps.hw_lock);
5681}
5682
5683void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5684{
5685 /* disable DBUF power */
5686 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5687 POSTING_READ(DBUF_CTL);
5688
5689 udelay(10);
5690
5691 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5692 DRM_ERROR("DBuf power disable timeout\n");
5693
5694 /* disable DPLL0 */
5695 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5696 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5697 DRM_ERROR("Couldn't disable DPLL0\n");
5698
5699 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5700}
5701
5702void skl_init_cdclk(struct drm_i915_private *dev_priv)
5703{
5704 u32 val;
5705 unsigned int required_vco;
5706
5707 /* enable PCH reset handshake */
5708 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5709 I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
5710
5711 /* enable PG1 and Misc I/O */
5712 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5713
5714 /* DPLL0 already enabed !? */
5715 if (I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE) {
5716 DRM_DEBUG_DRIVER("DPLL0 already running\n");
5717 return;
5718 }
5719
5720 /* enable DPLL0 */
5721 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5722 skl_dpll0_enable(dev_priv, required_vco);
5723
5724 /* set CDCLK to the frequency the BIOS chose */
5725 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5726
5727 /* enable DBUF power */
5728 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5729 POSTING_READ(DBUF_CTL);
5730
5731 udelay(10);
5732
5733 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5734 DRM_ERROR("DBuf power enable timeout\n");
5735}
5736
Ville Syrjälädfcab172014-06-13 13:37:47 +03005737/* returns HPLL frequency in kHz */
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005738static int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005739{
Jesse Barnes586f49d2013-11-04 16:06:59 -08005740 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08005741
Jesse Barnes586f49d2013-11-04 16:06:59 -08005742 /* Obtain SKU information */
Ville Syrjäläa5805162015-05-26 20:42:30 +03005743 mutex_lock(&dev_priv->sb_lock);
Jesse Barnes586f49d2013-11-04 16:06:59 -08005744 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
5745 CCK_FUSE_HPLL_FREQ_MASK;
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ädfcab172014-06-13 13:37:47 +03005748 return vco_freq[hpll_freq] * 1000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005749}
5750
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005751static void vlv_update_cdclk(struct drm_device *dev)
5752{
5753 struct drm_i915_private *dev_priv = dev->dev_private;
5754
Vandana Kannan164dfd22014-11-24 13:37:41 +05305755 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
Ville Syrjälä43dc52c2014-10-07 17:41:20 +03005756 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
Vandana Kannan164dfd22014-11-24 13:37:41 +05305757 dev_priv->cdclk_freq);
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005758
5759 /*
5760 * Program the gmbus_freq based on the cdclk frequency.
5761 * BSpec erroneously claims we should aim for 4MHz, but
5762 * in fact 1MHz is the correct frequency.
5763 */
Vandana Kannan164dfd22014-11-24 13:37:41 +05305764 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005765}
5766
Jesse Barnes30a970c2013-11-04 13:48:12 -08005767/* Adjust CDclk dividers to allow high res or save power if possible */
5768static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5769{
5770 struct drm_i915_private *dev_priv = dev->dev_private;
5771 u32 val, cmd;
5772
Vandana Kannan164dfd22014-11-24 13:37:41 +05305773 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5774 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005775
Ville Syrjälädfcab172014-06-13 13:37:47 +03005776 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005777 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005778 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005779 cmd = 1;
5780 else
5781 cmd = 0;
5782
5783 mutex_lock(&dev_priv->rps.hw_lock);
5784 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5785 val &= ~DSPFREQGUAR_MASK;
5786 val |= (cmd << DSPFREQGUAR_SHIFT);
5787 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5788 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5789 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5790 50)) {
5791 DRM_ERROR("timed out waiting for CDclk change\n");
5792 }
5793 mutex_unlock(&dev_priv->rps.hw_lock);
5794
Ville Syrjälä54433e92015-05-26 20:42:31 +03005795 mutex_lock(&dev_priv->sb_lock);
5796
Ville Syrjälädfcab172014-06-13 13:37:47 +03005797 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005798 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005799
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005800 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005801
Jesse Barnes30a970c2013-11-04 13:48:12 -08005802 /* adjust cdclk divider */
5803 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjälä9cf33db2014-06-13 13:37:48 +03005804 val &= ~DISPLAY_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005805 val |= divider;
5806 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005807
5808 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5809 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5810 50))
5811 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005812 }
5813
Jesse Barnes30a970c2013-11-04 13:48:12 -08005814 /* adjust self-refresh exit latency value */
5815 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5816 val &= ~0x7f;
5817
5818 /*
5819 * For high bandwidth configs, we set a higher latency in the bunit
5820 * so that the core display fetch happens in time to avoid underruns.
5821 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005822 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005823 val |= 4500 / 250; /* 4.5 usec */
5824 else
5825 val |= 3000 / 250; /* 3.0 usec */
5826 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03005827
Ville Syrjäläa5805162015-05-26 20:42:30 +03005828 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005829
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005830 vlv_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005831}
5832
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005833static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5834{
5835 struct drm_i915_private *dev_priv = dev->dev_private;
5836 u32 val, cmd;
5837
Vandana Kannan164dfd22014-11-24 13:37:41 +05305838 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5839 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005840
5841 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005842 case 333333:
5843 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005844 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005845 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005846 break;
5847 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005848 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005849 return;
5850 }
5851
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005852 /*
5853 * Specs are full of misinformation, but testing on actual
5854 * hardware has shown that we just need to write the desired
5855 * CCK divider into the Punit register.
5856 */
5857 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5858
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005859 mutex_lock(&dev_priv->rps.hw_lock);
5860 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5861 val &= ~DSPFREQGUAR_MASK_CHV;
5862 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5863 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5864 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5865 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5866 50)) {
5867 DRM_ERROR("timed out waiting for CDclk change\n");
5868 }
5869 mutex_unlock(&dev_priv->rps.hw_lock);
5870
5871 vlv_update_cdclk(dev);
5872}
5873
Jesse Barnes30a970c2013-11-04 13:48:12 -08005874static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5875 int max_pixclk)
5876{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005877 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005878 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005879
Jesse Barnes30a970c2013-11-04 13:48:12 -08005880 /*
5881 * Really only a few cases to deal with, as only 4 CDclks are supported:
5882 * 200MHz
5883 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005884 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005885 * 400MHz (VLV only)
5886 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5887 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005888 *
5889 * We seem to get an unstable or solid color picture at 200MHz.
5890 * Not sure what's wrong. For now use 200MHz only when all pipes
5891 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005892 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005893 if (!IS_CHERRYVIEW(dev_priv) &&
5894 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005895 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005896 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005897 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005898 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005899 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005900 else
5901 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005902}
5903
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305904static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5905 int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005906{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305907 /*
5908 * FIXME:
5909 * - remove the guardband, it's not needed on BXT
5910 * - set 19.2MHz bypass frequency if there are no active pipes
5911 */
5912 if (max_pixclk > 576000*9/10)
5913 return 624000;
5914 else if (max_pixclk > 384000*9/10)
5915 return 576000;
5916 else if (max_pixclk > 288000*9/10)
5917 return 384000;
5918 else if (max_pixclk > 144000*9/10)
5919 return 288000;
5920 else
5921 return 144000;
5922}
5923
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005924/* Compute the max pixel clock for new configuration. Uses atomic state if
5925 * that's non-NULL, look at current state otherwise. */
5926static int intel_mode_max_pixclk(struct drm_device *dev,
5927 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005928{
Jesse Barnes30a970c2013-11-04 13:48:12 -08005929 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005930 struct intel_crtc_state *crtc_state;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005931 int max_pixclk = 0;
5932
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005933 for_each_intel_crtc(dev, intel_crtc) {
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005934 if (state)
5935 crtc_state =
5936 intel_atomic_get_crtc_state(state, intel_crtc);
5937 else
5938 crtc_state = intel_crtc->config;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005939 if (IS_ERR(crtc_state))
5940 return PTR_ERR(crtc_state);
5941
5942 if (!crtc_state->base.enable)
5943 continue;
5944
5945 max_pixclk = max(max_pixclk,
5946 crtc_state->base.adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005947 }
5948
5949 return max_pixclk;
5950}
5951
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005952static int valleyview_modeset_global_pipes(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005953{
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005954 struct drm_i915_private *dev_priv = to_i915(state->dev);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005955 struct drm_crtc *crtc;
5956 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005957 int max_pixclk = intel_mode_max_pixclk(state->dev, state);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005958 int cdclk, i;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005959
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005960 if (max_pixclk < 0)
5961 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005962
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305963 if (IS_VALLEYVIEW(dev_priv))
5964 cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
5965 else
5966 cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
5967
5968 if (cdclk == dev_priv->cdclk_freq)
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005969 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005970
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005971 /* add all active pipes to the state */
5972 for_each_crtc(state->dev, crtc) {
5973 if (!crtc->state->enable)
5974 continue;
5975
5976 crtc_state = drm_atomic_get_crtc_state(state, crtc);
5977 if (IS_ERR(crtc_state))
5978 return PTR_ERR(crtc_state);
5979 }
5980
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005981 /* disable/enable all currently active pipes while we change cdclk */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005982 for_each_crtc_in_state(state, crtc, crtc_state, i)
5983 if (crtc_state->enable)
5984 crtc_state->mode_changed = true;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005985
5986 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005987}
5988
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005989static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5990{
5991 unsigned int credits, default_credits;
5992
5993 if (IS_CHERRYVIEW(dev_priv))
5994 default_credits = PFI_CREDIT(12);
5995 else
5996 default_credits = PFI_CREDIT(8);
5997
Vandana Kannan164dfd22014-11-24 13:37:41 +05305998 if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005999 /* CHV suggested value is 31 or 63 */
6000 if (IS_CHERRYVIEW(dev_priv))
6001 credits = PFI_CREDIT_31;
6002 else
6003 credits = PFI_CREDIT(15);
6004 } else {
6005 credits = default_credits;
6006 }
6007
6008 /*
6009 * WA - write default credits before re-programming
6010 * FIXME: should we also set the resend bit here?
6011 */
6012 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6013 default_credits);
6014
6015 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6016 credits | PFI_CREDIT_RESEND);
6017
6018 /*
6019 * FIXME is this guaranteed to clear
6020 * immediately or should we poll for it?
6021 */
6022 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6023}
6024
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006025static void valleyview_modeset_global_resources(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006026{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006027 struct drm_device *dev = old_state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006028 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006029 int max_pixclk = intel_mode_max_pixclk(dev, NULL);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006030 int req_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006031
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006032 /* The path in intel_mode_max_pixclk() with a NULL atomic state should
6033 * never fail. */
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006034 if (WARN_ON(max_pixclk < 0))
6035 return;
6036
6037 req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006038
Vandana Kannan164dfd22014-11-24 13:37:41 +05306039 if (req_cdclk != dev_priv->cdclk_freq) {
Imre Deak738c05c2014-11-19 16:25:37 +02006040 /*
6041 * FIXME: We can end up here with all power domains off, yet
6042 * with a CDCLK frequency other than the minimum. To account
6043 * for this take the PIPE-A power domain, which covers the HW
6044 * blocks needed for the following programming. This can be
6045 * removed once it's guaranteed that we get here either with
6046 * the minimum CDCLK set, or the required power domains
6047 * enabled.
6048 */
6049 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
6050
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006051 if (IS_CHERRYVIEW(dev))
6052 cherryview_set_cdclk(dev, req_cdclk);
6053 else
6054 valleyview_set_cdclk(dev, req_cdclk);
Imre Deak738c05c2014-11-19 16:25:37 +02006055
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006056 vlv_program_pfi_credits(dev_priv);
6057
Imre Deak738c05c2014-11-19 16:25:37 +02006058 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006059 }
Jesse Barnes30a970c2013-11-04 13:48:12 -08006060}
6061
Jesse Barnes89b667f2013-04-18 14:51:36 -07006062static void valleyview_crtc_enable(struct drm_crtc *crtc)
6063{
6064 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006065 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006066 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6067 struct intel_encoder *encoder;
6068 int pipe = intel_crtc->pipe;
Jani Nikula23538ef2013-08-27 15:12:22 +03006069 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006070
Matt Roper83d65732015-02-25 13:12:16 -08006071 WARN_ON(!crtc->state->enable);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006072
6073 if (intel_crtc->active)
6074 return;
6075
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006076 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
Shobhit Kumar8525a232014-06-25 12:20:39 +05306077
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006078 if (!is_dsi) {
6079 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006080 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006081 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006082 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006083 }
Daniel Vetter5b18e572014-04-24 23:55:06 +02006084
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006085 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306086 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006087
6088 intel_set_pipe_timings(intel_crtc);
6089
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006090 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6091 struct drm_i915_private *dev_priv = dev->dev_private;
6092
6093 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6094 I915_WRITE(CHV_CANVAS(pipe), 0);
6095 }
6096
Daniel Vetter5b18e572014-04-24 23:55:06 +02006097 i9xx_set_pipeconf(intel_crtc);
6098
Jesse Barnes89b667f2013-04-18 14:51:36 -07006099 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006100
Daniel Vettera72e4c92014-09-30 10:56:47 +02006101 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006102
Jesse Barnes89b667f2013-04-18 14:51:36 -07006103 for_each_encoder_on_crtc(dev, crtc, encoder)
6104 if (encoder->pre_pll_enable)
6105 encoder->pre_pll_enable(encoder);
6106
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006107 if (!is_dsi) {
6108 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006109 chv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006110 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006111 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006112 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006113
6114 for_each_encoder_on_crtc(dev, crtc, encoder)
6115 if (encoder->pre_enable)
6116 encoder->pre_enable(encoder);
6117
Jesse Barnes2dd24552013-04-25 12:55:01 -07006118 i9xx_pfit_enable(intel_crtc);
6119
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006120 intel_crtc_load_lut(crtc);
6121
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006122 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006123 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006124
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006125 assert_vblank_disabled(crtc);
6126 drm_crtc_vblank_on(crtc);
6127
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006128 for_each_encoder_on_crtc(dev, crtc, encoder)
6129 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006130}
6131
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006132static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6133{
6134 struct drm_device *dev = crtc->base.dev;
6135 struct drm_i915_private *dev_priv = dev->dev_private;
6136
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006137 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6138 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006139}
6140
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006141static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006142{
6143 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006144 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006145 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006146 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006147 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006148
Matt Roper83d65732015-02-25 13:12:16 -08006149 WARN_ON(!crtc->state->enable);
Daniel Vetter08a48462012-07-02 11:43:47 +02006150
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006151 if (intel_crtc->active)
6152 return;
6153
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006154 i9xx_set_pll_dividers(intel_crtc);
6155
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006156 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306157 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006158
6159 intel_set_pipe_timings(intel_crtc);
6160
Daniel Vetter5b18e572014-04-24 23:55:06 +02006161 i9xx_set_pipeconf(intel_crtc);
6162
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006163 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006164
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006165 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006166 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006167
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006168 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006169 if (encoder->pre_enable)
6170 encoder->pre_enable(encoder);
6171
Daniel Vetterf6736a12013-06-05 13:34:30 +02006172 i9xx_enable_pll(intel_crtc);
6173
Jesse Barnes2dd24552013-04-25 12:55:01 -07006174 i9xx_pfit_enable(intel_crtc);
6175
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006176 intel_crtc_load_lut(crtc);
6177
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006178 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006179 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006180
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006181 assert_vblank_disabled(crtc);
6182 drm_crtc_vblank_on(crtc);
6183
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006184 for_each_encoder_on_crtc(dev, crtc, encoder)
6185 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006186}
6187
Daniel Vetter87476d62013-04-11 16:29:06 +02006188static void i9xx_pfit_disable(struct intel_crtc *crtc)
6189{
6190 struct drm_device *dev = crtc->base.dev;
6191 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02006192
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006193 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006194 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006195
6196 assert_pipe_disabled(dev_priv, crtc->pipe);
6197
Daniel Vetter328d8e82013-05-08 10:36:31 +02006198 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6199 I915_READ(PFIT_CONTROL));
6200 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006201}
6202
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006203static void i9xx_crtc_disable(struct drm_crtc *crtc)
6204{
6205 struct drm_device *dev = crtc->dev;
6206 struct drm_i915_private *dev_priv = dev->dev_private;
6207 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006208 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006209 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006210
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006211 if (!intel_crtc->active)
6212 return;
6213
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006214 /*
6215 * On gen2 planes are double buffered but the pipe isn't, so we must
6216 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03006217 * We also need to wait on all gmch platforms because of the
6218 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006219 */
Imre Deak564ed192014-06-13 14:54:21 +03006220 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006221
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006222 for_each_encoder_on_crtc(dev, crtc, encoder)
6223 encoder->disable(encoder);
6224
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006225 drm_crtc_vblank_off(crtc);
6226 assert_vblank_disabled(crtc);
6227
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006228 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006229
Daniel Vetter87476d62013-04-11 16:29:06 +02006230 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006231
Jesse Barnes89b667f2013-04-18 14:51:36 -07006232 for_each_encoder_on_crtc(dev, crtc, encoder)
6233 if (encoder->post_disable)
6234 encoder->post_disable(encoder);
6235
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006236 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006237 if (IS_CHERRYVIEW(dev))
6238 chv_disable_pll(dev_priv, pipe);
6239 else if (IS_VALLEYVIEW(dev))
6240 vlv_disable_pll(dev_priv, pipe);
6241 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006242 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006243 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006244
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006245 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006246 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006247
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006248 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03006249 intel_update_watermarks(crtc);
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006250
Daniel Vetterefa96242014-04-24 23:55:02 +02006251 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02006252 intel_fbc_update(dev);
Daniel Vetterefa96242014-04-24 23:55:02 +02006253 mutex_unlock(&dev->struct_mutex);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006254}
6255
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006256static void i9xx_crtc_off(struct drm_crtc *crtc)
6257{
6258}
6259
Borun Fub04c5bd2014-07-12 10:02:27 +05306260/* Master function to enable/disable CRTC and corresponding power wells */
6261void intel_crtc_control(struct drm_crtc *crtc, bool enable)
Chris Wilsoncdd59982010-09-08 16:30:16 +01006262{
Chris Wilsoncdd59982010-09-08 16:30:16 +01006263 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006264 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006265 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006266 enum intel_display_power_domain domain;
6267 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006268
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006269 if (enable) {
6270 if (!intel_crtc->active) {
Daniel Vettere1e9fb82014-06-25 22:02:04 +03006271 domains = get_crtc_power_domains(crtc);
6272 for_each_power_domain(domain, domains)
6273 intel_display_power_get(dev_priv, domain);
6274 intel_crtc->enabled_power_domains = domains;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006275
6276 dev_priv->display.crtc_enable(crtc);
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03006277 intel_crtc_enable_planes(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006278 }
6279 } else {
6280 if (intel_crtc->active) {
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03006281 intel_crtc_disable_planes(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006282 dev_priv->display.crtc_disable(crtc);
6283
Daniel Vettere1e9fb82014-06-25 22:02:04 +03006284 domains = intel_crtc->enabled_power_domains;
6285 for_each_power_domain(domain, domains)
6286 intel_display_power_put(dev_priv, domain);
6287 intel_crtc->enabled_power_domains = 0;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006288 }
6289 }
Borun Fub04c5bd2014-07-12 10:02:27 +05306290}
6291
6292/**
6293 * Sets the power management mode of the pipe and plane.
6294 */
6295void intel_crtc_update_dpms(struct drm_crtc *crtc)
6296{
6297 struct drm_device *dev = crtc->dev;
6298 struct intel_encoder *intel_encoder;
6299 bool enable = false;
6300
6301 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
6302 enable |= intel_encoder->connectors_active;
6303
6304 intel_crtc_control(crtc, enable);
Ander Conselvan de Oliveira0f63cca2015-04-21 17:13:17 +03006305
6306 crtc->state->active = enable;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006307}
6308
Daniel Vetter976f8a22012-07-08 22:34:21 +02006309static void intel_crtc_disable(struct drm_crtc *crtc)
6310{
6311 struct drm_device *dev = crtc->dev;
6312 struct drm_connector *connector;
6313 struct drm_i915_private *dev_priv = dev->dev_private;
6314
6315 /* crtc should still be enabled when we disable it. */
Matt Roper83d65732015-02-25 13:12:16 -08006316 WARN_ON(!crtc->state->enable);
Daniel Vetter976f8a22012-07-08 22:34:21 +02006317
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03006318 intel_crtc_disable_planes(crtc);
Daniel Vetter976f8a22012-07-08 22:34:21 +02006319 dev_priv->display.crtc_disable(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006320 dev_priv->display.off(crtc);
6321
Matt Roper70a101f2015-04-08 18:56:53 -07006322 drm_plane_helper_disable(crtc->primary);
Daniel Vetter976f8a22012-07-08 22:34:21 +02006323
6324 /* Update computed state. */
6325 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
6326 if (!connector->encoder || !connector->encoder->crtc)
6327 continue;
6328
6329 if (connector->encoder->crtc != crtc)
6330 continue;
6331
6332 connector->dpms = DRM_MODE_DPMS_OFF;
6333 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01006334 }
6335}
6336
Chris Wilsonea5b2132010-08-04 13:50:23 +01006337void intel_encoder_destroy(struct drm_encoder *encoder)
6338{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006339 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006340
Chris Wilsonea5b2132010-08-04 13:50:23 +01006341 drm_encoder_cleanup(encoder);
6342 kfree(intel_encoder);
6343}
6344
Damien Lespiau92373292013-08-08 22:28:57 +01006345/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006346 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
6347 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01006348static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006349{
6350 if (mode == DRM_MODE_DPMS_ON) {
6351 encoder->connectors_active = true;
6352
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006353 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006354 } else {
6355 encoder->connectors_active = false;
6356
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006357 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006358 }
6359}
6360
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006361/* Cross check the actual hw state with our own modeset state tracking (and it's
6362 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02006363static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006364{
6365 if (connector->get_hw_state(connector)) {
6366 struct intel_encoder *encoder = connector->encoder;
6367 struct drm_crtc *crtc;
6368 bool encoder_enabled;
6369 enum pipe pipe;
6370
6371 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6372 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03006373 connector->base.name);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006374
Dave Airlie0e32b392014-05-02 14:02:48 +10006375 /* there is no real hw state for MST connectors */
6376 if (connector->mst_port)
6377 return;
6378
Rob Clarke2c719b2014-12-15 13:56:32 -05006379 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006380 "wrong connector dpms state\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006381 I915_STATE_WARN(connector->base.encoder != &encoder->base,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006382 "active connector not linked to encoder\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006383
Dave Airlie36cd7442014-05-02 13:44:18 +10006384 if (encoder) {
Rob Clarke2c719b2014-12-15 13:56:32 -05006385 I915_STATE_WARN(!encoder->connectors_active,
Dave Airlie36cd7442014-05-02 13:44:18 +10006386 "encoder->connectors_active not set\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006387
Dave Airlie36cd7442014-05-02 13:44:18 +10006388 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -05006389 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
6390 if (I915_STATE_WARN_ON(!encoder->base.crtc))
Dave Airlie36cd7442014-05-02 13:44:18 +10006391 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006392
Dave Airlie36cd7442014-05-02 13:44:18 +10006393 crtc = encoder->base.crtc;
6394
Matt Roper83d65732015-02-25 13:12:16 -08006395 I915_STATE_WARN(!crtc->state->enable,
6396 "crtc not enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006397 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
6398 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
Dave Airlie36cd7442014-05-02 13:44:18 +10006399 "encoder active on the wrong pipe\n");
6400 }
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006401 }
6402}
6403
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006404int intel_connector_init(struct intel_connector *connector)
6405{
6406 struct drm_connector_state *connector_state;
6407
6408 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6409 if (!connector_state)
6410 return -ENOMEM;
6411
6412 connector->base.state = connector_state;
6413 return 0;
6414}
6415
6416struct intel_connector *intel_connector_alloc(void)
6417{
6418 struct intel_connector *connector;
6419
6420 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6421 if (!connector)
6422 return NULL;
6423
6424 if (intel_connector_init(connector) < 0) {
6425 kfree(connector);
6426 return NULL;
6427 }
6428
6429 return connector;
6430}
6431
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006432/* Even simpler default implementation, if there's really no special case to
6433 * consider. */
6434void intel_connector_dpms(struct drm_connector *connector, int mode)
6435{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006436 /* All the simple cases only support two dpms states. */
6437 if (mode != DRM_MODE_DPMS_ON)
6438 mode = DRM_MODE_DPMS_OFF;
6439
6440 if (mode == connector->dpms)
6441 return;
6442
6443 connector->dpms = mode;
6444
6445 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dc2013-09-29 19:15:07 +01006446 if (connector->encoder)
6447 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006448
Daniel Vetterb9805142012-08-31 17:37:33 +02006449 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006450}
6451
Daniel Vetterf0947c32012-07-02 13:10:34 +02006452/* Simple connector->get_hw_state implementation for encoders that support only
6453 * one connector and no cloning and hence the encoder state determines the state
6454 * of the connector. */
6455bool intel_connector_get_hw_state(struct intel_connector *connector)
6456{
Daniel Vetter24929352012-07-02 20:28:59 +02006457 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006458 struct intel_encoder *encoder = connector->encoder;
6459
6460 return encoder->get_hw_state(encoder, &pipe);
6461}
6462
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006463static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006464{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006465 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6466 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006467
6468 return 0;
6469}
6470
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006471static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006472 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006473{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006474 struct drm_atomic_state *state = pipe_config->base.state;
6475 struct intel_crtc *other_crtc;
6476 struct intel_crtc_state *other_crtc_state;
6477
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006478 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6479 pipe_name(pipe), pipe_config->fdi_lanes);
6480 if (pipe_config->fdi_lanes > 4) {
6481 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6482 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006483 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006484 }
6485
Paulo Zanonibafb6552013-11-02 21:07:44 -07006486 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006487 if (pipe_config->fdi_lanes > 2) {
6488 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6489 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006490 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006491 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006492 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006493 }
6494 }
6495
6496 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006497 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006498
6499 /* Ivybridge 3 pipe is really complicated */
6500 switch (pipe) {
6501 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006502 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006503 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006504 if (pipe_config->fdi_lanes <= 2)
6505 return 0;
6506
6507 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6508 other_crtc_state =
6509 intel_atomic_get_crtc_state(state, other_crtc);
6510 if (IS_ERR(other_crtc_state))
6511 return PTR_ERR(other_crtc_state);
6512
6513 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006514 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6515 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006516 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006517 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006518 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006519 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006520 if (pipe_config->fdi_lanes > 2) {
6521 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6522 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006523 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006524 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006525
6526 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6527 other_crtc_state =
6528 intel_atomic_get_crtc_state(state, other_crtc);
6529 if (IS_ERR(other_crtc_state))
6530 return PTR_ERR(other_crtc_state);
6531
6532 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006533 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006534 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006535 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006536 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006537 default:
6538 BUG();
6539 }
6540}
6541
Daniel Vettere29c22c2013-02-21 00:00:16 +01006542#define RETRY 1
6543static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006544 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006545{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006546 struct drm_device *dev = intel_crtc->base.dev;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006547 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006548 int lane, link_bw, fdi_dotclock, ret;
6549 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006550
Daniel Vettere29c22c2013-02-21 00:00:16 +01006551retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006552 /* FDI is a binary signal running at ~2.7GHz, encoding
6553 * each output octet as 10 bits. The actual frequency
6554 * is stored as a divider into a 100MHz clock, and the
6555 * mode pixel clock is stored in units of 1KHz.
6556 * Hence the bw of each lane in terms of the mode signal
6557 * is:
6558 */
6559 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6560
Damien Lespiau241bfc32013-09-25 16:45:37 +01006561 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006562
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006563 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006564 pipe_config->pipe_bpp);
6565
6566 pipe_config->fdi_lanes = lane;
6567
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006568 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006569 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006570
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006571 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6572 intel_crtc->pipe, pipe_config);
6573 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006574 pipe_config->pipe_bpp -= 2*3;
6575 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6576 pipe_config->pipe_bpp);
6577 needs_recompute = true;
6578 pipe_config->bw_constrained = true;
6579
6580 goto retry;
6581 }
6582
6583 if (needs_recompute)
6584 return RETRY;
6585
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006586 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006587}
6588
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006589static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006590 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006591{
Jani Nikulad330a952014-01-21 11:24:25 +02006592 pipe_config->ips_enabled = i915.enable_ips &&
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03006593 hsw_crtc_supports_ips(crtc) &&
Jesse Barnesb6dfdc92013-07-25 10:06:50 -07006594 pipe_config->pipe_bpp <= 24;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006595}
6596
Daniel Vettera43f6e02013-06-07 23:10:32 +02006597static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006598 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006599{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006600 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006601 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006602 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chandra Kondurud03c93d2015-04-09 16:42:46 -07006603 int ret;
Chris Wilson89749352010-09-12 18:25:19 +01006604
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006605 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006606 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006607 int clock_limit =
6608 dev_priv->display.get_display_clock_speed(dev);
6609
6610 /*
6611 * Enable pixel doubling when the dot clock
6612 * is > 90% of the (display) core speed.
6613 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03006614 * GDG double wide on either pipe,
6615 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006616 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03006617 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01006618 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006619 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006620 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006621 }
6622
Damien Lespiau241bfc32013-09-25 16:45:37 +01006623 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006624 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006625 }
Chris Wilson89749352010-09-12 18:25:19 +01006626
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006627 /*
6628 * Pipe horizontal size must be even in:
6629 * - DVO ganged mode
6630 * - LVDS dual channel mode
6631 * - Double wide pipe
6632 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006633 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006634 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6635 pipe_config->pipe_src_w &= ~1;
6636
Damien Lespiau8693a822013-05-03 18:48:11 +01006637 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6638 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006639 */
6640 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
6641 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006642 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006643
Damien Lespiauf5adf942013-06-24 18:29:34 +01006644 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006645 hsw_compute_ips_config(crtc, pipe_config);
6646
Daniel Vetter877d48d2013-04-19 11:24:43 +02006647 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006648 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006649
Chandra Kondurud03c93d2015-04-09 16:42:46 -07006650 /* FIXME: remove below call once atomic mode set is place and all crtc
6651 * related checks called from atomic_crtc_check function */
6652 ret = 0;
6653 DRM_DEBUG_KMS("intel_crtc = %p drm_state (pipe_config->base.state) = %p\n",
6654 crtc, pipe_config->base.state);
6655 ret = intel_atomic_setup_scalers(dev, crtc, pipe_config);
6656
6657 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006658}
6659
Ville Syrjälä1652d192015-03-31 14:12:01 +03006660static int skylake_get_display_clock_speed(struct drm_device *dev)
6661{
6662 struct drm_i915_private *dev_priv = to_i915(dev);
6663 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6664 uint32_t cdctl = I915_READ(CDCLK_CTL);
6665 uint32_t linkrate;
6666
6667 if (!(lcpll1 & LCPLL_PLL_ENABLE)) {
6668 WARN(1, "LCPLL1 not enabled\n");
6669 return 24000; /* 24MHz is the cd freq with NSSC ref */
6670 }
6671
6672 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6673 return 540000;
6674
6675 linkrate = (I915_READ(DPLL_CTRL1) &
Damien Lespiau71cd8422015-04-30 16:39:17 +01006676 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006677
Damien Lespiau71cd8422015-04-30 16:39:17 +01006678 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6679 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006680 /* vco 8640 */
6681 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6682 case CDCLK_FREQ_450_432:
6683 return 432000;
6684 case CDCLK_FREQ_337_308:
6685 return 308570;
6686 case CDCLK_FREQ_675_617:
6687 return 617140;
6688 default:
6689 WARN(1, "Unknown cd freq selection\n");
6690 }
6691 } else {
6692 /* vco 8100 */
6693 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6694 case CDCLK_FREQ_450_432:
6695 return 450000;
6696 case CDCLK_FREQ_337_308:
6697 return 337500;
6698 case CDCLK_FREQ_675_617:
6699 return 675000;
6700 default:
6701 WARN(1, "Unknown cd freq selection\n");
6702 }
6703 }
6704
6705 /* error case, do as if DPLL0 isn't enabled */
6706 return 24000;
6707}
6708
6709static int broadwell_get_display_clock_speed(struct drm_device *dev)
6710{
6711 struct drm_i915_private *dev_priv = dev->dev_private;
6712 uint32_t lcpll = I915_READ(LCPLL_CTL);
6713 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6714
6715 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6716 return 800000;
6717 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6718 return 450000;
6719 else if (freq == LCPLL_CLK_FREQ_450)
6720 return 450000;
6721 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6722 return 540000;
6723 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6724 return 337500;
6725 else
6726 return 675000;
6727}
6728
6729static int haswell_get_display_clock_speed(struct drm_device *dev)
6730{
6731 struct drm_i915_private *dev_priv = dev->dev_private;
6732 uint32_t lcpll = I915_READ(LCPLL_CTL);
6733 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6734
6735 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6736 return 800000;
6737 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6738 return 450000;
6739 else if (freq == LCPLL_CLK_FREQ_450)
6740 return 450000;
6741 else if (IS_HSW_ULT(dev))
6742 return 337500;
6743 else
6744 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006745}
6746
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006747static int valleyview_get_display_clock_speed(struct drm_device *dev)
6748{
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006749 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006750 u32 val;
6751 int divider;
6752
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006753 if (dev_priv->hpll_freq == 0)
6754 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
6755
Ville Syrjäläa5805162015-05-26 20:42:30 +03006756 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006757 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03006758 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006759
6760 divider = val & DISPLAY_FREQUENCY_VALUES;
6761
Ville Syrjälä7d007f42014-06-13 13:37:53 +03006762 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
6763 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
6764 "cdclk change in progress\n");
6765
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006766 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006767}
6768
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006769static int ilk_get_display_clock_speed(struct drm_device *dev)
6770{
6771 return 450000;
6772}
6773
Jesse Barnese70236a2009-09-21 10:42:27 -07006774static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006775{
Jesse Barnese70236a2009-09-21 10:42:27 -07006776 return 400000;
6777}
Jesse Barnes79e53942008-11-07 14:24:08 -08006778
Jesse Barnese70236a2009-09-21 10:42:27 -07006779static int i915_get_display_clock_speed(struct drm_device *dev)
6780{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006781 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006782}
Jesse Barnes79e53942008-11-07 14:24:08 -08006783
Jesse Barnese70236a2009-09-21 10:42:27 -07006784static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6785{
6786 return 200000;
6787}
Jesse Barnes79e53942008-11-07 14:24:08 -08006788
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006789static int pnv_get_display_clock_speed(struct drm_device *dev)
6790{
6791 u16 gcfgc = 0;
6792
6793 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6794
6795 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6796 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006797 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006798 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006799 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006800 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006801 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006802 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6803 return 200000;
6804 default:
6805 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6806 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006807 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006808 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006809 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006810 }
6811}
6812
Jesse Barnese70236a2009-09-21 10:42:27 -07006813static int i915gm_get_display_clock_speed(struct drm_device *dev)
6814{
6815 u16 gcfgc = 0;
6816
6817 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6818
6819 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006820 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006821 else {
6822 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6823 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006824 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006825 default:
6826 case GC_DISPLAY_CLOCK_190_200_MHZ:
6827 return 190000;
6828 }
6829 }
6830}
Jesse Barnes79e53942008-11-07 14:24:08 -08006831
Jesse Barnese70236a2009-09-21 10:42:27 -07006832static int i865_get_display_clock_speed(struct drm_device *dev)
6833{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006834 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006835}
6836
6837static int i855_get_display_clock_speed(struct drm_device *dev)
6838{
6839 u16 hpllcc = 0;
6840 /* Assume that the hardware is in the high speed state. This
6841 * should be the default.
6842 */
6843 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6844 case GC_CLOCK_133_200:
6845 case GC_CLOCK_100_200:
6846 return 200000;
6847 case GC_CLOCK_166_250:
6848 return 250000;
6849 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006850 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006851 }
6852
6853 /* Shouldn't happen */
6854 return 0;
6855}
6856
6857static int i830_get_display_clock_speed(struct drm_device *dev)
6858{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006859 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006860}
6861
Zhenyu Wang2c072452009-06-05 15:38:42 +08006862static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006863intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006864{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006865 while (*num > DATA_LINK_M_N_MASK ||
6866 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08006867 *num >>= 1;
6868 *den >>= 1;
6869 }
6870}
6871
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006872static void compute_m_n(unsigned int m, unsigned int n,
6873 uint32_t *ret_m, uint32_t *ret_n)
6874{
6875 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
6876 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6877 intel_reduce_m_n_ratio(ret_m, ret_n);
6878}
6879
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006880void
6881intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6882 int pixel_clock, int link_clock,
6883 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006884{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006885 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006886
6887 compute_m_n(bits_per_pixel * pixel_clock,
6888 link_clock * nlanes * 8,
6889 &m_n->gmch_m, &m_n->gmch_n);
6890
6891 compute_m_n(pixel_clock, link_clock,
6892 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08006893}
6894
Chris Wilsona7615032011-01-12 17:04:08 +00006895static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
6896{
Jani Nikulad330a952014-01-21 11:24:25 +02006897 if (i915.panel_use_ssc >= 0)
6898 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006899 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07006900 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00006901}
6902
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006903static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
6904 int num_connectors)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006905{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006906 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006907 struct drm_i915_private *dev_priv = dev->dev_private;
6908 int refclk;
6909
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006910 WARN_ON(!crtc_state->base.state);
6911
Imre Deak5ab7b0b2015-03-06 03:29:25 +02006912 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02006913 refclk = 100000;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006914 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006915 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02006916 refclk = dev_priv->vbt.lvds_ssc_freq;
6917 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006918 } else if (!IS_GEN2(dev)) {
6919 refclk = 96000;
6920 } else {
6921 refclk = 48000;
6922 }
6923
6924 return refclk;
6925}
6926
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006927static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006928{
Daniel Vetter7df00d72013-05-21 21:54:55 +02006929 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006930}
Daniel Vetterf47709a2013-03-28 10:42:02 +01006931
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006932static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
6933{
6934 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006935}
6936
Daniel Vetterf47709a2013-03-28 10:42:02 +01006937static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006938 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08006939 intel_clock_t *reduced_clock)
6940{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006941 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006942 u32 fp, fp2 = 0;
6943
6944 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006945 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006946 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006947 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006948 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006949 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006950 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006951 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006952 }
6953
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006954 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006955
Daniel Vetterf47709a2013-03-28 10:42:02 +01006956 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006957 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07006958 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006959 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01006960 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006961 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006962 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006963 }
6964}
6965
Chon Ming Lee5e69f972013-09-05 20:41:49 +08006966static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
6967 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07006968{
6969 u32 reg_val;
6970
6971 /*
6972 * PLLB opamp always calibrates to max value of 0x3f, force enable it
6973 * and set it to a reasonable value instead.
6974 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006975 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006976 reg_val &= 0xffffff00;
6977 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006978 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006979
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006980 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006981 reg_val &= 0x8cffffff;
6982 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006983 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006984
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006985 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006986 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006987 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006988
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006989 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006990 reg_val &= 0x00ffffff;
6991 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006992 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006993}
6994
Daniel Vetterb5518422013-05-03 11:49:48 +02006995static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
6996 struct intel_link_m_n *m_n)
6997{
6998 struct drm_device *dev = crtc->base.dev;
6999 struct drm_i915_private *dev_priv = dev->dev_private;
7000 int pipe = crtc->pipe;
7001
Daniel Vettere3b95f12013-05-03 11:49:49 +02007002 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7003 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7004 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7005 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007006}
7007
7008static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007009 struct intel_link_m_n *m_n,
7010 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007011{
7012 struct drm_device *dev = crtc->base.dev;
7013 struct drm_i915_private *dev_priv = dev->dev_private;
7014 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007015 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007016
7017 if (INTEL_INFO(dev)->gen >= 5) {
7018 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7019 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7020 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7021 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007022 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7023 * for gen < 8) and if DRRS is supported (to make sure the
7024 * registers are not unnecessarily accessed).
7025 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307026 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007027 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007028 I915_WRITE(PIPE_DATA_M2(transcoder),
7029 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7030 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7031 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7032 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7033 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007034 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007035 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7036 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7037 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7038 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007039 }
7040}
7041
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307042void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007043{
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307044 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7045
7046 if (m_n == M1_N1) {
7047 dp_m_n = &crtc->config->dp_m_n;
7048 dp_m2_n2 = &crtc->config->dp_m2_n2;
7049 } else if (m_n == M2_N2) {
7050
7051 /*
7052 * M2_N2 registers are not supported. Hence m2_n2 divider value
7053 * needs to be programmed into M1_N1.
7054 */
7055 dp_m_n = &crtc->config->dp_m2_n2;
7056 } else {
7057 DRM_ERROR("Unsupported divider value\n");
7058 return;
7059 }
7060
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007061 if (crtc->config->has_pch_encoder)
7062 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007063 else
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307064 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007065}
7066
Ville Syrjäläd288f652014-10-28 13:20:22 +02007067static void vlv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007068 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007069{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007070 u32 dpll, dpll_md;
7071
7072 /*
7073 * Enable DPIO clock input. We should never disable the reference
7074 * clock for pipe B, since VGA hotplug / manual detection depends
7075 * on it.
7076 */
7077 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
7078 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
7079 /* We should never disable this, set it here for state tracking */
7080 if (crtc->pipe == PIPE_B)
7081 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7082 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007083 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007084
Ville Syrjäläd288f652014-10-28 13:20:22 +02007085 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007086 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007087 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007088}
7089
Ville Syrjäläd288f652014-10-28 13:20:22 +02007090static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007091 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007092{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007093 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007094 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007095 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007096 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007097 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007098 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007099
Ville Syrjäläa5805162015-05-26 20:42:30 +03007100 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007101
Ville Syrjäläd288f652014-10-28 13:20:22 +02007102 bestn = pipe_config->dpll.n;
7103 bestm1 = pipe_config->dpll.m1;
7104 bestm2 = pipe_config->dpll.m2;
7105 bestp1 = pipe_config->dpll.p1;
7106 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007107
Jesse Barnes89b667f2013-04-18 14:51:36 -07007108 /* See eDP HDMI DPIO driver vbios notes doc */
7109
7110 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007111 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007112 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007113
7114 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007115 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007116
7117 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007118 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007119 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007120 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007121
7122 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007123 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007124
7125 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007126 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7127 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7128 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007129 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007130
7131 /*
7132 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7133 * but we don't support that).
7134 * Note: don't use the DAC post divider as it seems unstable.
7135 */
7136 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007137 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007138
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007139 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007140 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007141
Jesse Barnes89b667f2013-04-18 14:51:36 -07007142 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007143 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007144 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7145 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007146 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03007147 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007148 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007149 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007150 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007151
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007152 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007153 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007154 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007155 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007156 0x0df40000);
7157 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007158 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007159 0x0df70000);
7160 } else { /* HDMI or VGA */
7161 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007162 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007163 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007164 0x0df70000);
7165 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007166 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007167 0x0df40000);
7168 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007169
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007170 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007171 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007172 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7173 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007174 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007175 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007176
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007177 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007178 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007179}
7180
Ville Syrjäläd288f652014-10-28 13:20:22 +02007181static void chv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007182 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007183{
Ville Syrjäläd288f652014-10-28 13:20:22 +02007184 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007185 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
7186 DPLL_VCO_ENABLE;
7187 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007188 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007189
Ville Syrjäläd288f652014-10-28 13:20:22 +02007190 pipe_config->dpll_hw_state.dpll_md =
7191 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007192}
7193
Ville Syrjäläd288f652014-10-28 13:20:22 +02007194static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007195 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007196{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007197 struct drm_device *dev = crtc->base.dev;
7198 struct drm_i915_private *dev_priv = dev->dev_private;
7199 int pipe = crtc->pipe;
7200 int dpll_reg = DPLL(crtc->pipe);
7201 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307202 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007203 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307204 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307205 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007206
Ville Syrjäläd288f652014-10-28 13:20:22 +02007207 bestn = pipe_config->dpll.n;
7208 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7209 bestm1 = pipe_config->dpll.m1;
7210 bestm2 = pipe_config->dpll.m2 >> 22;
7211 bestp1 = pipe_config->dpll.p1;
7212 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307213 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307214 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307215 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007216
7217 /*
7218 * Enable Refclk and SSC
7219 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007220 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007221 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007222
Ville Syrjäläa5805162015-05-26 20:42:30 +03007223 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007224
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007225 /* p1 and p2 divider */
7226 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7227 5 << DPIO_CHV_S1_DIV_SHIFT |
7228 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7229 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7230 1 << DPIO_CHV_K_DIV_SHIFT);
7231
7232 /* Feedback post-divider - m2 */
7233 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7234
7235 /* Feedback refclk divider - n and m1 */
7236 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7237 DPIO_CHV_M1_DIV_BY_2 |
7238 1 << DPIO_CHV_N_DIV_SHIFT);
7239
7240 /* M2 fraction division */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307241 if (bestm2_frac)
7242 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007243
7244 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307245 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7246 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7247 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7248 if (bestm2_frac)
7249 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7250 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007251
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307252 /* Program digital lock detect threshold */
7253 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7254 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7255 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7256 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7257 if (!bestm2_frac)
7258 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7259 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7260
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007261 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307262 if (vco == 5400000) {
7263 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7264 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7265 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7266 tribuf_calcntr = 0x9;
7267 } else if (vco <= 6200000) {
7268 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7269 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7270 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7271 tribuf_calcntr = 0x9;
7272 } else if (vco <= 6480000) {
7273 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7274 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7275 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7276 tribuf_calcntr = 0x8;
7277 } else {
7278 /* Not supported. Apply the same limits as in the max case */
7279 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7280 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7281 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7282 tribuf_calcntr = 0;
7283 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007284 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7285
Ville Syrjälä968040b2015-03-11 22:52:08 +02007286 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307287 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7288 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7289 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7290
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007291 /* AFC Recal */
7292 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7293 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7294 DPIO_AFC_RECAL);
7295
Ville Syrjäläa5805162015-05-26 20:42:30 +03007296 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007297}
7298
Ville Syrjäläd288f652014-10-28 13:20:22 +02007299/**
7300 * vlv_force_pll_on - forcibly enable just the PLL
7301 * @dev_priv: i915 private structure
7302 * @pipe: pipe PLL to enable
7303 * @dpll: PLL configuration
7304 *
7305 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7306 * in cases where we need the PLL enabled even when @pipe is not going to
7307 * be enabled.
7308 */
7309void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7310 const struct dpll *dpll)
7311{
7312 struct intel_crtc *crtc =
7313 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007314 struct intel_crtc_state pipe_config = {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007315 .base.crtc = &crtc->base,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007316 .pixel_multiplier = 1,
7317 .dpll = *dpll,
7318 };
7319
7320 if (IS_CHERRYVIEW(dev)) {
7321 chv_update_pll(crtc, &pipe_config);
7322 chv_prepare_pll(crtc, &pipe_config);
7323 chv_enable_pll(crtc, &pipe_config);
7324 } else {
7325 vlv_update_pll(crtc, &pipe_config);
7326 vlv_prepare_pll(crtc, &pipe_config);
7327 vlv_enable_pll(crtc, &pipe_config);
7328 }
7329}
7330
7331/**
7332 * vlv_force_pll_off - forcibly disable just the PLL
7333 * @dev_priv: i915 private structure
7334 * @pipe: pipe PLL to disable
7335 *
7336 * Disable the PLL for @pipe. To be used in cases where we need
7337 * the PLL enabled even when @pipe is not going to be enabled.
7338 */
7339void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7340{
7341 if (IS_CHERRYVIEW(dev))
7342 chv_disable_pll(to_i915(dev), pipe);
7343 else
7344 vlv_disable_pll(to_i915(dev), pipe);
7345}
7346
Daniel Vetterf47709a2013-03-28 10:42:02 +01007347static void i9xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007348 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01007349 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007350 int num_connectors)
7351{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007352 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007353 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007354 u32 dpll;
7355 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007356 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007357
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007358 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307359
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007360 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7361 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007362
7363 dpll = DPLL_VGA_MODE_DIS;
7364
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007365 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007366 dpll |= DPLLB_MODE_LVDS;
7367 else
7368 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007369
Daniel Vetteref1b4602013-06-01 17:17:04 +02007370 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007371 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007372 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007373 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007374
7375 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007376 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007377
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007378 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007379 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007380
7381 /* compute bitmask from p1 value */
7382 if (IS_PINEVIEW(dev))
7383 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7384 else {
7385 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7386 if (IS_G4X(dev) && reduced_clock)
7387 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7388 }
7389 switch (clock->p2) {
7390 case 5:
7391 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7392 break;
7393 case 7:
7394 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7395 break;
7396 case 10:
7397 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7398 break;
7399 case 14:
7400 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7401 break;
7402 }
7403 if (INTEL_INFO(dev)->gen >= 4)
7404 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7405
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007406 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007407 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007408 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007409 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7410 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7411 else
7412 dpll |= PLL_REF_INPUT_DREFCLK;
7413
7414 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007415 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007416
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007417 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007418 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007419 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007420 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007421 }
7422}
7423
Daniel Vetterf47709a2013-03-28 10:42:02 +01007424static void i8xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007425 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01007426 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007427 int num_connectors)
7428{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007429 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007430 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007431 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007432 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007433
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007434 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307435
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007436 dpll = DPLL_VGA_MODE_DIS;
7437
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007438 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007439 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7440 } else {
7441 if (clock->p1 == 2)
7442 dpll |= PLL_P1_DIVIDE_BY_TWO;
7443 else
7444 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7445 if (clock->p2 == 4)
7446 dpll |= PLL_P2_DIVIDE_BY_4;
7447 }
7448
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007449 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007450 dpll |= DPLL_DVO_2X_MODE;
7451
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007452 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007453 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7454 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7455 else
7456 dpll |= PLL_REF_INPUT_DREFCLK;
7457
7458 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007459 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007460}
7461
Daniel Vetter8a654f32013-06-01 17:16:22 +02007462static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007463{
7464 struct drm_device *dev = intel_crtc->base.dev;
7465 struct drm_i915_private *dev_priv = dev->dev_private;
7466 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007467 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02007468 struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007469 &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007470 uint32_t crtc_vtotal, crtc_vblank_end;
7471 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007472
7473 /* We need to be careful not to changed the adjusted mode, for otherwise
7474 * the hw state checker will get angry at the mismatch. */
7475 crtc_vtotal = adjusted_mode->crtc_vtotal;
7476 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007477
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007478 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007479 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007480 crtc_vtotal -= 1;
7481 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007482
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007483 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007484 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7485 else
7486 vsyncshift = adjusted_mode->crtc_hsync_start -
7487 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007488 if (vsyncshift < 0)
7489 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007490 }
7491
7492 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007493 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007494
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007495 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007496 (adjusted_mode->crtc_hdisplay - 1) |
7497 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007498 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007499 (adjusted_mode->crtc_hblank_start - 1) |
7500 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007501 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007502 (adjusted_mode->crtc_hsync_start - 1) |
7503 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7504
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007505 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007506 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007507 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007508 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007509 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007510 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007511 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007512 (adjusted_mode->crtc_vsync_start - 1) |
7513 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7514
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007515 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7516 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7517 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7518 * bits. */
7519 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7520 (pipe == PIPE_B || pipe == PIPE_C))
7521 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7522
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007523 /* pipesrc controls the size that is scaled from, which should
7524 * always be the user's requested size.
7525 */
7526 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007527 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7528 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007529}
7530
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007531static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007532 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007533{
7534 struct drm_device *dev = crtc->base.dev;
7535 struct drm_i915_private *dev_priv = dev->dev_private;
7536 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7537 uint32_t tmp;
7538
7539 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007540 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7541 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007542 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007543 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7544 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007545 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007546 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7547 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007548
7549 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007550 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7551 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007552 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007553 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7554 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007555 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007556 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7557 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007558
7559 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007560 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7561 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7562 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007563 }
7564
7565 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007566 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7567 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7568
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007569 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7570 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007571}
7572
Daniel Vetterf6a83282014-02-11 15:28:57 -08007573void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007574 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007575{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007576 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7577 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7578 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7579 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007580
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007581 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7582 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7583 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7584 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007585
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007586 mode->flags = pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007587
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007588 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7589 mode->flags |= pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007590}
7591
Daniel Vetter84b046f2013-02-19 18:48:54 +01007592static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7593{
7594 struct drm_device *dev = intel_crtc->base.dev;
7595 struct drm_i915_private *dev_priv = dev->dev_private;
7596 uint32_t pipeconf;
7597
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007598 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007599
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007600 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7601 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7602 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007603
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007604 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007605 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007606
Daniel Vetterff9ce462013-04-24 14:57:17 +02007607 /* only g4x and later have fancy bpc/dither controls */
7608 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007609 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007610 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007611 pipeconf |= PIPECONF_DITHER_EN |
7612 PIPECONF_DITHER_TYPE_SP;
7613
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007614 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007615 case 18:
7616 pipeconf |= PIPECONF_6BPC;
7617 break;
7618 case 24:
7619 pipeconf |= PIPECONF_8BPC;
7620 break;
7621 case 30:
7622 pipeconf |= PIPECONF_10BPC;
7623 break;
7624 default:
7625 /* Case prevented by intel_choose_pipe_bpp_dither. */
7626 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007627 }
7628 }
7629
7630 if (HAS_PIPE_CXSR(dev)) {
7631 if (intel_crtc->lowfreq_avail) {
7632 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7633 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7634 } else {
7635 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007636 }
7637 }
7638
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007639 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007640 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007641 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007642 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7643 else
7644 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7645 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007646 pipeconf |= PIPECONF_PROGRESSIVE;
7647
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007648 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007649 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007650
Daniel Vetter84b046f2013-02-19 18:48:54 +01007651 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7652 POSTING_READ(PIPECONF(intel_crtc->pipe));
7653}
7654
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007655static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7656 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007657{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007658 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007659 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc751ce42010-03-25 11:48:48 -07007660 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07007661 intel_clock_t clock, reduced_clock;
Daniel Vettera16af722013-04-30 14:01:44 +02007662 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007663 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01007664 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08007665 const intel_limit_t *limit;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007666 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007667 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007668 struct drm_connector_state *connector_state;
7669 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -08007670
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007671 memset(&crtc_state->dpll_hw_state, 0,
7672 sizeof(crtc_state->dpll_hw_state));
7673
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007674 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007675 if (connector_state->crtc != &crtc->base)
7676 continue;
7677
7678 encoder = to_intel_encoder(connector_state->best_encoder);
7679
Chris Wilson5eddb702010-09-11 13:48:45 +01007680 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007681 case INTEL_OUTPUT_LVDS:
7682 is_lvds = true;
7683 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007684 case INTEL_OUTPUT_DSI:
7685 is_dsi = true;
7686 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007687 default:
7688 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007689 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05007690
Eric Anholtc751ce42010-03-25 11:48:48 -07007691 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08007692 }
7693
Jani Nikulaf2335332013-09-13 11:03:09 +03007694 if (is_dsi)
Daniel Vetter5b18e572014-04-24 23:55:06 +02007695 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007696
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007697 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007698 refclk = i9xx_get_refclk(crtc_state, num_connectors);
Jani Nikulaf2335332013-09-13 11:03:09 +03007699
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007700 /*
7701 * Returns a set of divisors for the desired target clock with
7702 * the given refclk, or FALSE. The returned values represent
7703 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7704 * 2) / p1 / p2.
7705 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007706 limit = intel_limit(crtc_state, refclk);
7707 ok = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007708 crtc_state->port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007709 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03007710 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007711 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7712 return -EINVAL;
7713 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007714
Jani Nikulaf2335332013-09-13 11:03:09 +03007715 if (is_lvds && dev_priv->lvds_downclock_avail) {
7716 /*
7717 * Ensure we match the reduced clock's P to the target
7718 * clock. If the clocks don't match, we can't switch
7719 * the display clock by using the FP0/FP1. In such case
7720 * we will disable the LVDS downclock feature.
7721 */
7722 has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007723 dev_priv->display.find_dpll(limit, crtc_state,
Jani Nikulaf2335332013-09-13 11:03:09 +03007724 dev_priv->lvds_downclock,
7725 refclk, &clock,
7726 &reduced_clock);
7727 }
7728 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007729 crtc_state->dpll.n = clock.n;
7730 crtc_state->dpll.m1 = clock.m1;
7731 crtc_state->dpll.m2 = clock.m2;
7732 crtc_state->dpll.p1 = clock.p1;
7733 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007734 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007735
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007736 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007737 i8xx_update_pll(crtc, crtc_state,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307738 has_reduced_clock ? &reduced_clock : NULL,
7739 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007740 } else if (IS_CHERRYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007741 chv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007742 } else if (IS_VALLEYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007743 vlv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007744 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007745 i9xx_update_pll(crtc, crtc_state,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007746 has_reduced_clock ? &reduced_clock : NULL,
Robin Schroereba905b2014-05-18 02:24:50 +02007747 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007748 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007749
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007750 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007751}
7752
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007753static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007754 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007755{
7756 struct drm_device *dev = crtc->base.dev;
7757 struct drm_i915_private *dev_priv = dev->dev_private;
7758 uint32_t tmp;
7759
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007760 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7761 return;
7762
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007763 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007764 if (!(tmp & PFIT_ENABLE))
7765 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007766
Daniel Vetter06922822013-07-11 13:35:40 +02007767 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007768 if (INTEL_INFO(dev)->gen < 4) {
7769 if (crtc->pipe != PIPE_B)
7770 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007771 } else {
7772 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7773 return;
7774 }
7775
Daniel Vetter06922822013-07-11 13:35:40 +02007776 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007777 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7778 if (INTEL_INFO(dev)->gen < 5)
7779 pipe_config->gmch_pfit.lvds_border_bits =
7780 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7781}
7782
Jesse Barnesacbec812013-09-20 11:29:32 -07007783static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007784 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07007785{
7786 struct drm_device *dev = crtc->base.dev;
7787 struct drm_i915_private *dev_priv = dev->dev_private;
7788 int pipe = pipe_config->cpu_transcoder;
7789 intel_clock_t clock;
7790 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07007791 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07007792
Shobhit Kumarf573de52014-07-30 20:32:37 +05307793 /* In case of MIPI DPLL will not even be used */
7794 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7795 return;
7796
Ville Syrjäläa5805162015-05-26 20:42:30 +03007797 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007798 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03007799 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007800
7801 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7802 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7803 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7804 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7805 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7806
Ville Syrjäläf6466282013-10-14 14:50:31 +03007807 vlv_clock(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007808
Ville Syrjäläf6466282013-10-14 14:50:31 +03007809 /* clock.dot is the fast clock */
7810 pipe_config->port_clock = clock.dot / 5;
Jesse Barnesacbec812013-09-20 11:29:32 -07007811}
7812
Damien Lespiau5724dbd2015-01-20 12:51:52 +00007813static void
7814i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7815 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007816{
7817 struct drm_device *dev = crtc->base.dev;
7818 struct drm_i915_private *dev_priv = dev->dev_private;
7819 u32 val, base, offset;
7820 int pipe = crtc->pipe, plane = crtc->plane;
7821 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00007822 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007823 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00007824 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007825
Damien Lespiau42a7b082015-02-05 19:35:13 +00007826 val = I915_READ(DSPCNTR(plane));
7827 if (!(val & DISPLAY_PLANE_ENABLE))
7828 return;
7829
Damien Lespiaud9806c92015-01-21 14:07:19 +00007830 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00007831 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007832 DRM_DEBUG_KMS("failed to alloc fb\n");
7833 return;
7834 }
7835
Damien Lespiau1b842c82015-01-21 13:50:54 +00007836 fb = &intel_fb->base;
7837
Daniel Vetter18c52472015-02-10 17:16:09 +00007838 if (INTEL_INFO(dev)->gen >= 4) {
7839 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007840 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00007841 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
7842 }
7843 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007844
7845 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00007846 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007847 fb->pixel_format = fourcc;
7848 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007849
7850 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007851 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007852 offset = I915_READ(DSPTILEOFF(plane));
7853 else
7854 offset = I915_READ(DSPLINOFF(plane));
7855 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7856 } else {
7857 base = I915_READ(DSPADDR(plane));
7858 }
7859 plane_config->base = base;
7860
7861 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007862 fb->width = ((val >> 16) & 0xfff) + 1;
7863 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007864
7865 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007866 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007867
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007868 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00007869 fb->pixel_format,
7870 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007871
Daniel Vetterf37b5c22015-02-10 23:12:27 +01007872 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007873
Damien Lespiau2844a922015-01-20 12:51:48 +00007874 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7875 pipe_name(pipe), plane, fb->width, fb->height,
7876 fb->bits_per_pixel, base, fb->pitches[0],
7877 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007878
Damien Lespiau2d140302015-02-05 17:22:18 +00007879 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007880}
7881
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007882static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007883 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007884{
7885 struct drm_device *dev = crtc->base.dev;
7886 struct drm_i915_private *dev_priv = dev->dev_private;
7887 int pipe = pipe_config->cpu_transcoder;
7888 enum dpio_channel port = vlv_pipe_to_channel(pipe);
7889 intel_clock_t clock;
Imre Deak0d7b6b12015-07-02 14:29:58 +03007890 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007891 int refclk = 100000;
7892
Ville Syrjäläa5805162015-05-26 20:42:30 +03007893 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007894 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
7895 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
7896 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
7897 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Imre Deak0d7b6b12015-07-02 14:29:58 +03007898 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03007899 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007900
7901 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
Imre Deak0d7b6b12015-07-02 14:29:58 +03007902 clock.m2 = (pll_dw0 & 0xff) << 22;
7903 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
7904 clock.m2 |= pll_dw2 & 0x3fffff;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007905 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
7906 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
7907 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
7908
7909 chv_clock(refclk, &clock);
7910
7911 /* clock.dot is the fast clock */
7912 pipe_config->port_clock = clock.dot / 5;
7913}
7914
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007915static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007916 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007917{
7918 struct drm_device *dev = crtc->base.dev;
7919 struct drm_i915_private *dev_priv = dev->dev_private;
7920 uint32_t tmp;
7921
Daniel Vetterf458ebb2014-09-30 10:56:39 +02007922 if (!intel_display_power_is_enabled(dev_priv,
7923 POWER_DOMAIN_PIPE(crtc->pipe)))
Imre Deakb5482bd2014-03-05 16:20:55 +02007924 return false;
7925
Daniel Vettere143a212013-07-04 12:01:15 +02007926 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007927 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02007928
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007929 tmp = I915_READ(PIPECONF(crtc->pipe));
7930 if (!(tmp & PIPECONF_ENABLE))
7931 return false;
7932
Ville Syrjälä42571ae2013-09-06 23:29:00 +03007933 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
7934 switch (tmp & PIPECONF_BPC_MASK) {
7935 case PIPECONF_6BPC:
7936 pipe_config->pipe_bpp = 18;
7937 break;
7938 case PIPECONF_8BPC:
7939 pipe_config->pipe_bpp = 24;
7940 break;
7941 case PIPECONF_10BPC:
7942 pipe_config->pipe_bpp = 30;
7943 break;
7944 default:
7945 break;
7946 }
7947 }
7948
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02007949 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
7950 pipe_config->limited_color_range = true;
7951
Ville Syrjälä282740f2013-09-04 18:30:03 +03007952 if (INTEL_INFO(dev)->gen < 4)
7953 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
7954
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007955 intel_get_pipe_timings(crtc, pipe_config);
7956
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007957 i9xx_get_pfit_config(crtc, pipe_config);
7958
Daniel Vetter6c49f242013-06-06 12:45:25 +02007959 if (INTEL_INFO(dev)->gen >= 4) {
7960 tmp = I915_READ(DPLL_MD(crtc->pipe));
7961 pipe_config->pixel_multiplier =
7962 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
7963 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007964 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02007965 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
7966 tmp = I915_READ(DPLL(crtc->pipe));
7967 pipe_config->pixel_multiplier =
7968 ((tmp & SDVO_MULTIPLIER_MASK)
7969 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
7970 } else {
7971 /* Note that on i915G/GM the pixel multiplier is in the sdvo
7972 * port and will be fixed up in the encoder->get_config
7973 * function. */
7974 pipe_config->pixel_multiplier = 1;
7975 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007976 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
7977 if (!IS_VALLEYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03007978 /*
7979 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
7980 * on 830. Filter it out here so that we don't
7981 * report errors due to that.
7982 */
7983 if (IS_I830(dev))
7984 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
7985
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007986 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
7987 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03007988 } else {
7989 /* Mask out read-only status bits. */
7990 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
7991 DPLL_PORTC_READY_MASK |
7992 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007993 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02007994
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007995 if (IS_CHERRYVIEW(dev))
7996 chv_crtc_clock_get(crtc, pipe_config);
7997 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07007998 vlv_crtc_clock_get(crtc, pipe_config);
7999 else
8000 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008001
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008002 return true;
8003}
8004
Paulo Zanonidde86e22012-12-01 12:04:25 -02008005static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008006{
8007 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008008 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008009 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008010 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008011 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008012 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008013 bool has_ck505 = false;
8014 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008015
8016 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008017 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008018 switch (encoder->type) {
8019 case INTEL_OUTPUT_LVDS:
8020 has_panel = true;
8021 has_lvds = true;
8022 break;
8023 case INTEL_OUTPUT_EDP:
8024 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008025 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008026 has_cpu_edp = true;
8027 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008028 default:
8029 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008030 }
8031 }
8032
Keith Packard99eb6a02011-09-26 14:29:12 -07008033 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008034 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008035 can_ssc = has_ck505;
8036 } else {
8037 has_ck505 = false;
8038 can_ssc = true;
8039 }
8040
Imre Deak2de69052013-05-08 13:14:04 +03008041 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8042 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008043
8044 /* Ironlake: try to setup display ref clock before DPLL
8045 * enabling. This is only under driver's control after
8046 * PCH B stepping, previous chipset stepping should be
8047 * ignoring this setting.
8048 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008049 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008050
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008051 /* As we must carefully and slowly disable/enable each source in turn,
8052 * compute the final state we want first and check if we need to
8053 * make any changes at all.
8054 */
8055 final = val;
8056 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008057 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008058 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008059 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008060 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8061
8062 final &= ~DREF_SSC_SOURCE_MASK;
8063 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8064 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008065
Keith Packard199e5d72011-09-22 12:01:57 -07008066 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008067 final |= DREF_SSC_SOURCE_ENABLE;
8068
8069 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8070 final |= DREF_SSC1_ENABLE;
8071
8072 if (has_cpu_edp) {
8073 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8074 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8075 else
8076 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8077 } else
8078 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8079 } else {
8080 final |= DREF_SSC_SOURCE_DISABLE;
8081 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8082 }
8083
8084 if (final == val)
8085 return;
8086
8087 /* Always enable nonspread source */
8088 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8089
8090 if (has_ck505)
8091 val |= DREF_NONSPREAD_CK505_ENABLE;
8092 else
8093 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8094
8095 if (has_panel) {
8096 val &= ~DREF_SSC_SOURCE_MASK;
8097 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008098
Keith Packard199e5d72011-09-22 12:01:57 -07008099 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008100 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008101 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008102 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008103 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008104 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008105
8106 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008107 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008108 POSTING_READ(PCH_DREF_CONTROL);
8109 udelay(200);
8110
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008111 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008112
8113 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008114 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008115 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008116 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008117 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008118 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008119 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008120 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008121 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008122
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008123 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008124 POSTING_READ(PCH_DREF_CONTROL);
8125 udelay(200);
8126 } else {
8127 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8128
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008129 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008130
8131 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008132 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008133
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008134 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008135 POSTING_READ(PCH_DREF_CONTROL);
8136 udelay(200);
8137
8138 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008139 val &= ~DREF_SSC_SOURCE_MASK;
8140 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008141
8142 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008143 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008144
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008145 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008146 POSTING_READ(PCH_DREF_CONTROL);
8147 udelay(200);
8148 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008149
8150 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008151}
8152
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008153static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008154{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008155 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008156
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008157 tmp = I915_READ(SOUTH_CHICKEN2);
8158 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8159 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008160
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008161 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8162 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8163 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008164
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008165 tmp = I915_READ(SOUTH_CHICKEN2);
8166 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8167 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008168
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008169 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8170 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8171 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008172}
8173
8174/* WaMPhyProgramming:hsw */
8175static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8176{
8177 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008178
8179 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8180 tmp &= ~(0xFF << 24);
8181 tmp |= (0x12 << 24);
8182 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8183
Paulo Zanonidde86e22012-12-01 12:04:25 -02008184 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8185 tmp |= (1 << 11);
8186 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8187
8188 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8189 tmp |= (1 << 11);
8190 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8191
Paulo Zanonidde86e22012-12-01 12:04:25 -02008192 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8193 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8194 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8195
8196 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8197 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8198 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8199
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008200 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8201 tmp &= ~(7 << 13);
8202 tmp |= (5 << 13);
8203 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008204
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008205 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8206 tmp &= ~(7 << 13);
8207 tmp |= (5 << 13);
8208 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008209
8210 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8211 tmp &= ~0xFF;
8212 tmp |= 0x1C;
8213 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8214
8215 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8216 tmp &= ~0xFF;
8217 tmp |= 0x1C;
8218 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8219
8220 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8221 tmp &= ~(0xFF << 16);
8222 tmp |= (0x1C << 16);
8223 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8224
8225 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8226 tmp &= ~(0xFF << 16);
8227 tmp |= (0x1C << 16);
8228 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8229
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008230 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8231 tmp |= (1 << 27);
8232 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008233
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008234 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8235 tmp |= (1 << 27);
8236 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008237
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008238 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8239 tmp &= ~(0xF << 28);
8240 tmp |= (4 << 28);
8241 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008242
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008243 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8244 tmp &= ~(0xF << 28);
8245 tmp |= (4 << 28);
8246 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008247}
8248
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008249/* Implements 3 different sequences from BSpec chapter "Display iCLK
8250 * Programming" based on the parameters passed:
8251 * - Sequence to enable CLKOUT_DP
8252 * - Sequence to enable CLKOUT_DP without spread
8253 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8254 */
8255static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8256 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008257{
8258 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008259 uint32_t reg, tmp;
8260
8261 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8262 with_spread = true;
8263 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
8264 with_fdi, "LP PCH doesn't have FDI\n"))
8265 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008266
Ville Syrjäläa5805162015-05-26 20:42:30 +03008267 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008268
8269 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8270 tmp &= ~SBI_SSCCTL_DISABLE;
8271 tmp |= SBI_SSCCTL_PATHALT;
8272 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8273
8274 udelay(24);
8275
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008276 if (with_spread) {
8277 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8278 tmp &= ~SBI_SSCCTL_PATHALT;
8279 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008280
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008281 if (with_fdi) {
8282 lpt_reset_fdi_mphy(dev_priv);
8283 lpt_program_fdi_mphy(dev_priv);
8284 }
8285 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008286
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008287 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8288 SBI_GEN0 : SBI_DBUFF0;
8289 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8290 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8291 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008292
Ville Syrjäläa5805162015-05-26 20:42:30 +03008293 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008294}
8295
Paulo Zanoni47701c32013-07-23 11:19:25 -03008296/* Sequence to disable CLKOUT_DP */
8297static void lpt_disable_clkout_dp(struct drm_device *dev)
8298{
8299 struct drm_i915_private *dev_priv = dev->dev_private;
8300 uint32_t reg, tmp;
8301
Ville Syrjäläa5805162015-05-26 20:42:30 +03008302 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008303
8304 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8305 SBI_GEN0 : SBI_DBUFF0;
8306 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8307 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8308 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8309
8310 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8311 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8312 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8313 tmp |= SBI_SSCCTL_PATHALT;
8314 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8315 udelay(32);
8316 }
8317 tmp |= SBI_SSCCTL_DISABLE;
8318 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8319 }
8320
Ville Syrjäläa5805162015-05-26 20:42:30 +03008321 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008322}
8323
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008324static void lpt_init_pch_refclk(struct drm_device *dev)
8325{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008326 struct intel_encoder *encoder;
8327 bool has_vga = false;
8328
Damien Lespiaub2784e12014-08-05 11:29:37 +01008329 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008330 switch (encoder->type) {
8331 case INTEL_OUTPUT_ANALOG:
8332 has_vga = true;
8333 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008334 default:
8335 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008336 }
8337 }
8338
Paulo Zanoni47701c32013-07-23 11:19:25 -03008339 if (has_vga)
8340 lpt_enable_clkout_dp(dev, true, true);
8341 else
8342 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008343}
8344
Paulo Zanonidde86e22012-12-01 12:04:25 -02008345/*
8346 * Initialize reference clocks when the driver loads
8347 */
8348void intel_init_pch_refclk(struct drm_device *dev)
8349{
8350 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8351 ironlake_init_pch_refclk(dev);
8352 else if (HAS_PCH_LPT(dev))
8353 lpt_init_pch_refclk(dev);
8354}
8355
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008356static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008357{
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008358 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008359 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008360 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008361 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008362 struct drm_connector_state *connector_state;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008363 struct intel_encoder *encoder;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008364 int num_connectors = 0, i;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008365 bool is_lvds = false;
8366
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008367 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008368 if (connector_state->crtc != crtc_state->base.crtc)
8369 continue;
8370
8371 encoder = to_intel_encoder(connector_state->best_encoder);
8372
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008373 switch (encoder->type) {
8374 case INTEL_OUTPUT_LVDS:
8375 is_lvds = true;
8376 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008377 default:
8378 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008379 }
8380 num_connectors++;
8381 }
8382
8383 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008384 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008385 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008386 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008387 }
8388
8389 return 120000;
8390}
8391
Daniel Vetter6ff93602013-04-19 11:24:36 +02008392static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008393{
8394 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8395 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8396 int pipe = intel_crtc->pipe;
8397 uint32_t val;
8398
Daniel Vetter78114072013-06-13 00:54:57 +02008399 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008400
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008401 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008402 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008403 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008404 break;
8405 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008406 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008407 break;
8408 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008409 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008410 break;
8411 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008412 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008413 break;
8414 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008415 /* Case prevented by intel_choose_pipe_bpp_dither. */
8416 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008417 }
8418
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008419 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008420 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8421
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008422 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008423 val |= PIPECONF_INTERLACED_ILK;
8424 else
8425 val |= PIPECONF_PROGRESSIVE;
8426
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008427 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008428 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008429
Paulo Zanonic8203562012-09-12 10:06:29 -03008430 I915_WRITE(PIPECONF(pipe), val);
8431 POSTING_READ(PIPECONF(pipe));
8432}
8433
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008434/*
8435 * Set up the pipe CSC unit.
8436 *
8437 * Currently only full range RGB to limited range RGB conversion
8438 * is supported, but eventually this should handle various
8439 * RGB<->YCbCr scenarios as well.
8440 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01008441static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008442{
8443 struct drm_device *dev = crtc->dev;
8444 struct drm_i915_private *dev_priv = dev->dev_private;
8445 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8446 int pipe = intel_crtc->pipe;
8447 uint16_t coeff = 0x7800; /* 1.0 */
8448
8449 /*
8450 * TODO: Check what kind of values actually come out of the pipe
8451 * with these coeff/postoff values and adjust to get the best
8452 * accuracy. Perhaps we even need to take the bpc value into
8453 * consideration.
8454 */
8455
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008456 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008457 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8458
8459 /*
8460 * GY/GU and RY/RU should be the other way around according
8461 * to BSpec, but reality doesn't agree. Just set them up in
8462 * a way that results in the correct picture.
8463 */
8464 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8465 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8466
8467 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8468 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8469
8470 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8471 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8472
8473 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8474 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8475 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8476
8477 if (INTEL_INFO(dev)->gen > 6) {
8478 uint16_t postoff = 0;
8479
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008480 if (intel_crtc->config->limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02008481 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008482
8483 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8484 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8485 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8486
8487 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8488 } else {
8489 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8490
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008491 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008492 mode |= CSC_BLACK_SCREEN_OFFSET;
8493
8494 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8495 }
8496}
8497
Daniel Vetter6ff93602013-04-19 11:24:36 +02008498static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008499{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008500 struct drm_device *dev = crtc->dev;
8501 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008502 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008503 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008504 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008505 uint32_t val;
8506
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008507 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008508
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008509 if (IS_HASWELL(dev) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008510 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8511
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008512 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008513 val |= PIPECONF_INTERLACED_ILK;
8514 else
8515 val |= PIPECONF_PROGRESSIVE;
8516
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008517 I915_WRITE(PIPECONF(cpu_transcoder), val);
8518 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008519
8520 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8521 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008522
Satheeshakrishna M3cdf122c2014-04-08 15:46:53 +05308523 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008524 val = 0;
8525
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008526 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008527 case 18:
8528 val |= PIPEMISC_DITHER_6_BPC;
8529 break;
8530 case 24:
8531 val |= PIPEMISC_DITHER_8_BPC;
8532 break;
8533 case 30:
8534 val |= PIPEMISC_DITHER_10_BPC;
8535 break;
8536 case 36:
8537 val |= PIPEMISC_DITHER_12_BPC;
8538 break;
8539 default:
8540 /* Case prevented by pipe_config_set_bpp. */
8541 BUG();
8542 }
8543
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008544 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008545 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8546
8547 I915_WRITE(PIPEMISC(pipe), val);
8548 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008549}
8550
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008551static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008552 struct intel_crtc_state *crtc_state,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008553 intel_clock_t *clock,
8554 bool *has_reduced_clock,
8555 intel_clock_t *reduced_clock)
8556{
8557 struct drm_device *dev = crtc->dev;
8558 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008559 int refclk;
8560 const intel_limit_t *limit;
Daniel Vettera16af722013-04-30 14:01:44 +02008561 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008562
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008563 is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008564
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008565 refclk = ironlake_get_refclk(crtc_state);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008566
8567 /*
8568 * Returns a set of divisors for the desired target clock with the given
8569 * refclk, or FALSE. The returned values represent the clock equation:
8570 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8571 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008572 limit = intel_limit(crtc_state, refclk);
8573 ret = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008574 crtc_state->port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008575 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008576 if (!ret)
8577 return false;
8578
8579 if (is_lvds && dev_priv->lvds_downclock_avail) {
8580 /*
8581 * Ensure we match the reduced clock's P to the target clock.
8582 * If the clocks don't match, we can't switch the display clock
8583 * by using the FP0/FP1. In such case we will disable the LVDS
8584 * downclock feature.
8585 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02008586 *has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008587 dev_priv->display.find_dpll(limit, crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008588 dev_priv->lvds_downclock,
8589 refclk, clock,
8590 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008591 }
8592
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008593 return true;
8594}
8595
Paulo Zanonid4b19312012-11-29 11:29:32 -02008596int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8597{
8598 /*
8599 * Account for spread spectrum to avoid
8600 * oversubscribing the link. Max center spread
8601 * is 2.5%; use 5% for safety's sake.
8602 */
8603 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008604 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008605}
8606
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008607static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008608{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008609 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008610}
8611
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008612static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008613 struct intel_crtc_state *crtc_state,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008614 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008615 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008616{
8617 struct drm_crtc *crtc = &intel_crtc->base;
8618 struct drm_device *dev = crtc->dev;
8619 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008620 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008621 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008622 struct drm_connector_state *connector_state;
8623 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008624 uint32_t dpll;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008625 int factor, num_connectors = 0, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008626 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008627
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008628 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008629 if (connector_state->crtc != crtc_state->base.crtc)
8630 continue;
8631
8632 encoder = to_intel_encoder(connector_state->best_encoder);
8633
8634 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008635 case INTEL_OUTPUT_LVDS:
8636 is_lvds = true;
8637 break;
8638 case INTEL_OUTPUT_SDVO:
8639 case INTEL_OUTPUT_HDMI:
8640 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008641 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008642 default:
8643 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008644 }
8645
8646 num_connectors++;
8647 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008648
Chris Wilsonc1858122010-12-03 21:35:48 +00008649 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008650 factor = 21;
8651 if (is_lvds) {
8652 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008653 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008654 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008655 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008656 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008657 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008658
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008659 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02008660 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00008661
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008662 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8663 *fp2 |= FP_CB_TUNE;
8664
Chris Wilson5eddb702010-09-11 13:48:45 +01008665 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008666
Eric Anholta07d6782011-03-30 13:01:08 -07008667 if (is_lvds)
8668 dpll |= DPLLB_MODE_LVDS;
8669 else
8670 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008671
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008672 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008673 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008674
8675 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008676 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008677 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008678 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008679
Eric Anholta07d6782011-03-30 13:01:08 -07008680 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008681 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008682 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008683 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008684
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008685 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008686 case 5:
8687 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8688 break;
8689 case 7:
8690 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8691 break;
8692 case 10:
8693 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8694 break;
8695 case 14:
8696 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8697 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008698 }
8699
Daniel Vetterb4c09f32013-04-30 14:01:42 +02008700 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008701 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008702 else
8703 dpll |= PLL_REF_INPUT_DREFCLK;
8704
Daniel Vetter959e16d2013-06-05 13:34:21 +02008705 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008706}
8707
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008708static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8709 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008710{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008711 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008712 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008713 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03008714 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01008715 bool is_lvds = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008716 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008717
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008718 memset(&crtc_state->dpll_hw_state, 0,
8719 sizeof(crtc_state->dpll_hw_state));
8720
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03008721 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
Jesse Barnes79e53942008-11-07 14:24:08 -08008722
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008723 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8724 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
8725
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008726 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008727 &has_reduced_clock, &reduced_clock);
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008728 if (!ok && !crtc_state->clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008729 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8730 return -EINVAL;
8731 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01008732 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008733 if (!crtc_state->clock_set) {
8734 crtc_state->dpll.n = clock.n;
8735 crtc_state->dpll.m1 = clock.m1;
8736 crtc_state->dpll.m2 = clock.m2;
8737 crtc_state->dpll.p1 = clock.p1;
8738 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008739 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008740
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008741 /* 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 +02008742 if (crtc_state->has_pch_encoder) {
8743 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008744 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008745 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008746
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008747 dpll = ironlake_compute_dpll(crtc, crtc_state,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008748 &fp, &reduced_clock,
8749 has_reduced_clock ? &fp2 : NULL);
8750
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008751 crtc_state->dpll_hw_state.dpll = dpll;
8752 crtc_state->dpll_hw_state.fp0 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008753 if (has_reduced_clock)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008754 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008755 else
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008756 crtc_state->dpll_hw_state.fp1 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008757
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008758 pll = intel_get_shared_dpll(crtc, crtc_state);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008759 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03008760 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008761 pipe_name(crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07008762 return -EINVAL;
8763 }
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008764 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008765
Rodrigo Viviab585de2015-03-24 12:40:09 -07008766 if (is_lvds && has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008767 crtc->lowfreq_avail = true;
Daniel Vetterbcd644e2013-06-05 13:34:22 +02008768 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008769 crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008770
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008771 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008772}
8773
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008774static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8775 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008776{
8777 struct drm_device *dev = crtc->base.dev;
8778 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008779 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008780
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008781 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8782 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8783 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8784 & ~TU_SIZE_MASK;
8785 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8786 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8787 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8788}
8789
8790static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8791 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008792 struct intel_link_m_n *m_n,
8793 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008794{
8795 struct drm_device *dev = crtc->base.dev;
8796 struct drm_i915_private *dev_priv = dev->dev_private;
8797 enum pipe pipe = crtc->pipe;
8798
8799 if (INTEL_INFO(dev)->gen >= 5) {
8800 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8801 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8802 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8803 & ~TU_SIZE_MASK;
8804 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8805 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8806 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008807 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8808 * gen < 8) and if DRRS is supported (to make sure the
8809 * registers are not unnecessarily read).
8810 */
8811 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008812 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008813 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8814 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8815 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8816 & ~TU_SIZE_MASK;
8817 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8818 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8819 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8820 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008821 } else {
8822 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8823 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8824 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8825 & ~TU_SIZE_MASK;
8826 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8827 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8828 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8829 }
8830}
8831
8832void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008833 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008834{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02008835 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008836 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8837 else
8838 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008839 &pipe_config->dp_m_n,
8840 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008841}
8842
Daniel Vetter72419202013-04-04 13:28:53 +02008843static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008844 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02008845{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008846 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008847 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02008848}
8849
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008850static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008851 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008852{
8853 struct drm_device *dev = crtc->base.dev;
8854 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07008855 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8856 uint32_t ps_ctrl = 0;
8857 int id = -1;
8858 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008859
Chandra Kondurua1b22782015-04-07 15:28:45 -07008860 /* find scaler attached to this pipe */
8861 for (i = 0; i < crtc->num_scalers; i++) {
8862 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8863 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8864 id = i;
8865 pipe_config->pch_pfit.enabled = true;
8866 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8867 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8868 break;
8869 }
8870 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008871
Chandra Kondurua1b22782015-04-07 15:28:45 -07008872 scaler_state->scaler_id = id;
8873 if (id >= 0) {
8874 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8875 } else {
8876 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008877 }
8878}
8879
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008880static void
8881skylake_get_initial_plane_config(struct intel_crtc *crtc,
8882 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008883{
8884 struct drm_device *dev = crtc->base.dev;
8885 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00008886 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008887 int pipe = crtc->pipe;
8888 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008889 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008890 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008891 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008892
Damien Lespiaud9806c92015-01-21 14:07:19 +00008893 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008894 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008895 DRM_DEBUG_KMS("failed to alloc fb\n");
8896 return;
8897 }
8898
Damien Lespiau1b842c82015-01-21 13:50:54 +00008899 fb = &intel_fb->base;
8900
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008901 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00008902 if (!(val & PLANE_CTL_ENABLE))
8903 goto error;
8904
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008905 pixel_format = val & PLANE_CTL_FORMAT_MASK;
8906 fourcc = skl_format_to_fourcc(pixel_format,
8907 val & PLANE_CTL_ORDER_RGBX,
8908 val & PLANE_CTL_ALPHA_MASK);
8909 fb->pixel_format = fourcc;
8910 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
8911
Damien Lespiau40f46282015-02-27 11:15:21 +00008912 tiling = val & PLANE_CTL_TILED_MASK;
8913 switch (tiling) {
8914 case PLANE_CTL_TILED_LINEAR:
8915 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
8916 break;
8917 case PLANE_CTL_TILED_X:
8918 plane_config->tiling = I915_TILING_X;
8919 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8920 break;
8921 case PLANE_CTL_TILED_Y:
8922 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
8923 break;
8924 case PLANE_CTL_TILED_YF:
8925 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
8926 break;
8927 default:
8928 MISSING_CASE(tiling);
8929 goto error;
8930 }
8931
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008932 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
8933 plane_config->base = base;
8934
8935 offset = I915_READ(PLANE_OFFSET(pipe, 0));
8936
8937 val = I915_READ(PLANE_SIZE(pipe, 0));
8938 fb->height = ((val >> 16) & 0xfff) + 1;
8939 fb->width = ((val >> 0) & 0x1fff) + 1;
8940
8941 val = I915_READ(PLANE_STRIDE(pipe, 0));
Damien Lespiau40f46282015-02-27 11:15:21 +00008942 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
8943 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008944 fb->pitches[0] = (val & 0x3ff) * stride_mult;
8945
8946 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008947 fb->pixel_format,
8948 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008949
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008950 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008951
8952 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8953 pipe_name(pipe), fb->width, fb->height,
8954 fb->bits_per_pixel, base, fb->pitches[0],
8955 plane_config->size);
8956
Damien Lespiau2d140302015-02-05 17:22:18 +00008957 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008958 return;
8959
8960error:
8961 kfree(fb);
8962}
8963
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008964static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008965 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008966{
8967 struct drm_device *dev = crtc->base.dev;
8968 struct drm_i915_private *dev_priv = dev->dev_private;
8969 uint32_t tmp;
8970
8971 tmp = I915_READ(PF_CTL(crtc->pipe));
8972
8973 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008974 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008975 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
8976 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02008977
8978 /* We currently do not free assignements of panel fitters on
8979 * ivb/hsw (since we don't use the higher upscaling modes which
8980 * differentiates them) so just WARN about this case for now. */
8981 if (IS_GEN7(dev)) {
8982 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
8983 PF_PIPE_SEL_IVB(crtc->pipe));
8984 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008985 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008986}
8987
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008988static void
8989ironlake_get_initial_plane_config(struct intel_crtc *crtc,
8990 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008991{
8992 struct drm_device *dev = crtc->base.dev;
8993 struct drm_i915_private *dev_priv = dev->dev_private;
8994 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008995 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008996 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008997 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008998 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008999 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009000
Damien Lespiau42a7b082015-02-05 19:35:13 +00009001 val = I915_READ(DSPCNTR(pipe));
9002 if (!(val & DISPLAY_PLANE_ENABLE))
9003 return;
9004
Damien Lespiaud9806c92015-01-21 14:07:19 +00009005 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009006 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009007 DRM_DEBUG_KMS("failed to alloc fb\n");
9008 return;
9009 }
9010
Damien Lespiau1b842c82015-01-21 13:50:54 +00009011 fb = &intel_fb->base;
9012
Daniel Vetter18c52472015-02-10 17:16:09 +00009013 if (INTEL_INFO(dev)->gen >= 4) {
9014 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009015 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009016 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9017 }
9018 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009019
9020 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009021 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009022 fb->pixel_format = fourcc;
9023 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009024
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009025 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009026 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009027 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009028 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009029 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009030 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009031 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009032 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009033 }
9034 plane_config->base = base;
9035
9036 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009037 fb->width = ((val >> 16) & 0xfff) + 1;
9038 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009039
9040 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009041 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009042
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009043 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009044 fb->pixel_format,
9045 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009046
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009047 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009048
Damien Lespiau2844a922015-01-20 12:51:48 +00009049 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9050 pipe_name(pipe), fb->width, fb->height,
9051 fb->bits_per_pixel, base, fb->pitches[0],
9052 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009053
Damien Lespiau2d140302015-02-05 17:22:18 +00009054 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009055}
9056
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009057static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009058 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009059{
9060 struct drm_device *dev = crtc->base.dev;
9061 struct drm_i915_private *dev_priv = dev->dev_private;
9062 uint32_t tmp;
9063
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009064 if (!intel_display_power_is_enabled(dev_priv,
9065 POWER_DOMAIN_PIPE(crtc->pipe)))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009066 return false;
9067
Daniel Vettere143a212013-07-04 12:01:15 +02009068 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009069 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02009070
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009071 tmp = I915_READ(PIPECONF(crtc->pipe));
9072 if (!(tmp & PIPECONF_ENABLE))
9073 return false;
9074
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009075 switch (tmp & PIPECONF_BPC_MASK) {
9076 case PIPECONF_6BPC:
9077 pipe_config->pipe_bpp = 18;
9078 break;
9079 case PIPECONF_8BPC:
9080 pipe_config->pipe_bpp = 24;
9081 break;
9082 case PIPECONF_10BPC:
9083 pipe_config->pipe_bpp = 30;
9084 break;
9085 case PIPECONF_12BPC:
9086 pipe_config->pipe_bpp = 36;
9087 break;
9088 default:
9089 break;
9090 }
9091
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009092 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9093 pipe_config->limited_color_range = true;
9094
Daniel Vetterab9412b2013-05-03 11:49:46 +02009095 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009096 struct intel_shared_dpll *pll;
9097
Daniel Vetter88adfff2013-03-28 10:42:01 +01009098 pipe_config->has_pch_encoder = true;
9099
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009100 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9101 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9102 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009103
9104 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009105
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009106 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02009107 pipe_config->shared_dpll =
9108 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009109 } else {
9110 tmp = I915_READ(PCH_DPLL_SEL);
9111 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
9112 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
9113 else
9114 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
9115 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009116
9117 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9118
9119 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9120 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009121
9122 tmp = pipe_config->dpll_hw_state.dpll;
9123 pipe_config->pixel_multiplier =
9124 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9125 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009126
9127 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009128 } else {
9129 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009130 }
9131
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009132 intel_get_pipe_timings(crtc, pipe_config);
9133
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009134 ironlake_get_pfit_config(crtc, pipe_config);
9135
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009136 return true;
9137}
9138
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009139static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9140{
9141 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009142 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009143
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009144 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009145 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009146 pipe_name(crtc->pipe));
9147
Rob Clarke2c719b2014-12-15 13:56:32 -05009148 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9149 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
9150 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9151 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
9152 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9153 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009154 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009155 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009156 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009157 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009158 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009159 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009160 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009161 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009162 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009163
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009164 /*
9165 * In theory we can still leave IRQs enabled, as long as only the HPD
9166 * interrupts remain enabled. We used to check for that, but since it's
9167 * gen-specific and since we only disable LCPLL after we fully disable
9168 * the interrupts, the check below should be enough.
9169 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009170 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009171}
9172
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009173static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9174{
9175 struct drm_device *dev = dev_priv->dev;
9176
9177 if (IS_HASWELL(dev))
9178 return I915_READ(D_COMP_HSW);
9179 else
9180 return I915_READ(D_COMP_BDW);
9181}
9182
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009183static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9184{
9185 struct drm_device *dev = dev_priv->dev;
9186
9187 if (IS_HASWELL(dev)) {
9188 mutex_lock(&dev_priv->rps.hw_lock);
9189 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9190 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009191 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009192 mutex_unlock(&dev_priv->rps.hw_lock);
9193 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009194 I915_WRITE(D_COMP_BDW, val);
9195 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009196 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009197}
9198
9199/*
9200 * This function implements pieces of two sequences from BSpec:
9201 * - Sequence for display software to disable LCPLL
9202 * - Sequence for display software to allow package C8+
9203 * The steps implemented here are just the steps that actually touch the LCPLL
9204 * register. Callers should take care of disabling all the display engine
9205 * functions, doing the mode unset, fixing interrupts, etc.
9206 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009207static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9208 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009209{
9210 uint32_t val;
9211
9212 assert_can_disable_lcpll(dev_priv);
9213
9214 val = I915_READ(LCPLL_CTL);
9215
9216 if (switch_to_fclk) {
9217 val |= LCPLL_CD_SOURCE_FCLK;
9218 I915_WRITE(LCPLL_CTL, val);
9219
9220 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9221 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9222 DRM_ERROR("Switching to FCLK failed\n");
9223
9224 val = I915_READ(LCPLL_CTL);
9225 }
9226
9227 val |= LCPLL_PLL_DISABLE;
9228 I915_WRITE(LCPLL_CTL, val);
9229 POSTING_READ(LCPLL_CTL);
9230
9231 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9232 DRM_ERROR("LCPLL still locked\n");
9233
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009234 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009235 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009236 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009237 ndelay(100);
9238
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009239 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9240 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009241 DRM_ERROR("D_COMP RCOMP still in progress\n");
9242
9243 if (allow_power_down) {
9244 val = I915_READ(LCPLL_CTL);
9245 val |= LCPLL_POWER_DOWN_ALLOW;
9246 I915_WRITE(LCPLL_CTL, val);
9247 POSTING_READ(LCPLL_CTL);
9248 }
9249}
9250
9251/*
9252 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9253 * source.
9254 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009255static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009256{
9257 uint32_t val;
9258
9259 val = I915_READ(LCPLL_CTL);
9260
9261 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9262 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9263 return;
9264
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009265 /*
9266 * Make sure we're not on PC8 state before disabling PC8, otherwise
9267 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009268 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009269 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009270
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009271 if (val & LCPLL_POWER_DOWN_ALLOW) {
9272 val &= ~LCPLL_POWER_DOWN_ALLOW;
9273 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009274 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009275 }
9276
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009277 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009278 val |= D_COMP_COMP_FORCE;
9279 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009280 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009281
9282 val = I915_READ(LCPLL_CTL);
9283 val &= ~LCPLL_PLL_DISABLE;
9284 I915_WRITE(LCPLL_CTL, val);
9285
9286 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9287 DRM_ERROR("LCPLL not locked yet\n");
9288
9289 if (val & LCPLL_CD_SOURCE_FCLK) {
9290 val = I915_READ(LCPLL_CTL);
9291 val &= ~LCPLL_CD_SOURCE_FCLK;
9292 I915_WRITE(LCPLL_CTL, val);
9293
9294 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9295 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9296 DRM_ERROR("Switching back to LCPLL failed\n");
9297 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009298
Mika Kuoppala59bad942015-01-16 11:34:40 +02009299 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009300}
9301
Paulo Zanoni765dab672014-03-07 20:08:18 -03009302/*
9303 * Package states C8 and deeper are really deep PC states that can only be
9304 * reached when all the devices on the system allow it, so even if the graphics
9305 * device allows PC8+, it doesn't mean the system will actually get to these
9306 * states. Our driver only allows PC8+ when going into runtime PM.
9307 *
9308 * The requirements for PC8+ are that all the outputs are disabled, the power
9309 * well is disabled and most interrupts are disabled, and these are also
9310 * requirements for runtime PM. When these conditions are met, we manually do
9311 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9312 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9313 * hang the machine.
9314 *
9315 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9316 * the state of some registers, so when we come back from PC8+ we need to
9317 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9318 * need to take care of the registers kept by RC6. Notice that this happens even
9319 * if we don't put the device in PCI D3 state (which is what currently happens
9320 * because of the runtime PM support).
9321 *
9322 * For more, read "Display Sequences for Package C8" on the hardware
9323 * documentation.
9324 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009325void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009326{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009327 struct drm_device *dev = dev_priv->dev;
9328 uint32_t val;
9329
Paulo Zanonic67a4702013-08-19 13:18:09 -03009330 DRM_DEBUG_KMS("Enabling package C8+\n");
9331
Paulo Zanonic67a4702013-08-19 13:18:09 -03009332 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9333 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9334 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9335 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9336 }
9337
9338 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009339 hsw_disable_lcpll(dev_priv, true, true);
9340}
9341
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009342void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009343{
9344 struct drm_device *dev = dev_priv->dev;
9345 uint32_t val;
9346
Paulo Zanonic67a4702013-08-19 13:18:09 -03009347 DRM_DEBUG_KMS("Disabling package C8+\n");
9348
9349 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009350 lpt_init_pch_refclk(dev);
9351
9352 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9353 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9354 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9355 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9356 }
9357
9358 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009359}
9360
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009361static void broxton_modeset_global_resources(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309362{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009363 struct drm_device *dev = old_state->dev;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309364 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009365 int max_pixclk = intel_mode_max_pixclk(dev, NULL);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309366 int req_cdclk;
9367
9368 /* see the comment in valleyview_modeset_global_resources */
9369 if (WARN_ON(max_pixclk < 0))
9370 return;
9371
9372 req_cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
9373
9374 if (req_cdclk != dev_priv->cdclk_freq)
9375 broxton_set_cdclk(dev, req_cdclk);
9376}
9377
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009378static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9379 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009380{
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009381 if (!intel_ddi_pll_select(crtc, crtc_state))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009382 return -EINVAL;
Daniel Vetter716c2e52014-06-25 22:02:02 +03009383
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009384 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009385
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009386 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009387}
9388
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309389static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9390 enum port port,
9391 struct intel_crtc_state *pipe_config)
9392{
9393 switch (port) {
9394 case PORT_A:
9395 pipe_config->ddi_pll_sel = SKL_DPLL0;
9396 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9397 break;
9398 case PORT_B:
9399 pipe_config->ddi_pll_sel = SKL_DPLL1;
9400 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9401 break;
9402 case PORT_C:
9403 pipe_config->ddi_pll_sel = SKL_DPLL2;
9404 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9405 break;
9406 default:
9407 DRM_ERROR("Incorrect port type\n");
9408 }
9409}
9410
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009411static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9412 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009413 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009414{
Damien Lespiau3148ade2014-11-21 16:14:56 +00009415 u32 temp, dpll_ctl1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009416
9417 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9418 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9419
9420 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009421 case SKL_DPLL0:
9422 /*
9423 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9424 * of the shared DPLL framework and thus needs to be read out
9425 * separately
9426 */
9427 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9428 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9429 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009430 case SKL_DPLL1:
9431 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9432 break;
9433 case SKL_DPLL2:
9434 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9435 break;
9436 case SKL_DPLL3:
9437 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9438 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009439 }
9440}
9441
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009442static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9443 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009444 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009445{
9446 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9447
9448 switch (pipe_config->ddi_pll_sel) {
9449 case PORT_CLK_SEL_WRPLL1:
9450 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9451 break;
9452 case PORT_CLK_SEL_WRPLL2:
9453 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9454 break;
9455 }
9456}
9457
Daniel Vetter26804af2014-06-25 22:01:55 +03009458static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009459 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009460{
9461 struct drm_device *dev = crtc->base.dev;
9462 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009463 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009464 enum port port;
9465 uint32_t tmp;
9466
9467 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9468
9469 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9470
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009471 if (IS_SKYLAKE(dev))
9472 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309473 else if (IS_BROXTON(dev))
9474 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009475 else
9476 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009477
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009478 if (pipe_config->shared_dpll >= 0) {
9479 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9480
9481 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9482 &pipe_config->dpll_hw_state));
9483 }
9484
Daniel Vetter26804af2014-06-25 22:01:55 +03009485 /*
9486 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9487 * DDI E. So just check whether this pipe is wired to DDI E and whether
9488 * the PCH transcoder is on.
9489 */
Damien Lespiauca370452013-12-03 13:56:24 +00009490 if (INTEL_INFO(dev)->gen < 9 &&
9491 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009492 pipe_config->has_pch_encoder = true;
9493
9494 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9495 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9496 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9497
9498 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9499 }
9500}
9501
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009502static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009503 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009504{
9505 struct drm_device *dev = crtc->base.dev;
9506 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009507 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009508 uint32_t tmp;
9509
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009510 if (!intel_display_power_is_enabled(dev_priv,
Imre Deakb5482bd2014-03-05 16:20:55 +02009511 POWER_DOMAIN_PIPE(crtc->pipe)))
9512 return false;
9513
Daniel Vettere143a212013-07-04 12:01:15 +02009514 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009515 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9516
Daniel Vettereccb1402013-05-22 00:50:22 +02009517 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9518 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9519 enum pipe trans_edp_pipe;
9520 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9521 default:
9522 WARN(1, "unknown pipe linked to edp transcoder\n");
9523 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9524 case TRANS_DDI_EDP_INPUT_A_ON:
9525 trans_edp_pipe = PIPE_A;
9526 break;
9527 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9528 trans_edp_pipe = PIPE_B;
9529 break;
9530 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9531 trans_edp_pipe = PIPE_C;
9532 break;
9533 }
9534
9535 if (trans_edp_pipe == crtc->pipe)
9536 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9537 }
9538
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009539 if (!intel_display_power_is_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02009540 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03009541 return false;
9542
Daniel Vettereccb1402013-05-22 00:50:22 +02009543 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009544 if (!(tmp & PIPECONF_ENABLE))
9545 return false;
9546
Daniel Vetter26804af2014-06-25 22:01:55 +03009547 haswell_get_ddi_port_state(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009548
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009549 intel_get_pipe_timings(crtc, pipe_config);
9550
Chandra Kondurua1b22782015-04-07 15:28:45 -07009551 if (INTEL_INFO(dev)->gen >= 9) {
9552 skl_init_scalers(dev, crtc, pipe_config);
9553 }
9554
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009555 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Chandra Konduruaf99ced2015-05-11 14:35:47 -07009556
9557 if (INTEL_INFO(dev)->gen >= 9) {
9558 pipe_config->scaler_state.scaler_id = -1;
9559 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9560 }
9561
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009562 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009563 if (INTEL_INFO(dev)->gen == 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009564 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009565 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009566 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009567 else
9568 MISSING_CASE(INTEL_INFO(dev)->gen);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009569 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01009570
Jesse Barnese59150d2014-01-07 13:30:45 -08009571 if (IS_HASWELL(dev))
9572 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9573 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009574
Clint Taylorebb69c92014-09-30 10:30:22 -07009575 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
9576 pipe_config->pixel_multiplier =
9577 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9578 } else {
9579 pipe_config->pixel_multiplier = 1;
9580 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009581
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009582 return true;
9583}
9584
Chris Wilson560b85b2010-08-07 11:01:38 +01009585static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
9586{
9587 struct drm_device *dev = crtc->dev;
9588 struct drm_i915_private *dev_priv = dev->dev_private;
9589 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +03009590 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01009591
Ville Syrjälädc41c152014-08-13 11:57:05 +03009592 if (base) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009593 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
9594 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009595 unsigned int stride = roundup_pow_of_two(width) * 4;
9596
9597 switch (stride) {
9598 default:
9599 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9600 width, stride);
9601 stride = 256;
9602 /* fallthrough */
9603 case 256:
9604 case 512:
9605 case 1024:
9606 case 2048:
9607 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009608 }
9609
Ville Syrjälädc41c152014-08-13 11:57:05 +03009610 cntl |= CURSOR_ENABLE |
9611 CURSOR_GAMMA_ENABLE |
9612 CURSOR_FORMAT_ARGB |
9613 CURSOR_STRIDE(stride);
9614
9615 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009616 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009617
Ville Syrjälädc41c152014-08-13 11:57:05 +03009618 if (intel_crtc->cursor_cntl != 0 &&
9619 (intel_crtc->cursor_base != base ||
9620 intel_crtc->cursor_size != size ||
9621 intel_crtc->cursor_cntl != cntl)) {
9622 /* On these chipsets we can only modify the base/size/stride
9623 * whilst the cursor is disabled.
9624 */
9625 I915_WRITE(_CURACNTR, 0);
9626 POSTING_READ(_CURACNTR);
9627 intel_crtc->cursor_cntl = 0;
9628 }
9629
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009630 if (intel_crtc->cursor_base != base) {
Ville Syrjälädc41c152014-08-13 11:57:05 +03009631 I915_WRITE(_CURABASE, base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009632 intel_crtc->cursor_base = base;
9633 }
Ville Syrjälädc41c152014-08-13 11:57:05 +03009634
9635 if (intel_crtc->cursor_size != size) {
9636 I915_WRITE(CURSIZE, size);
9637 intel_crtc->cursor_size = size;
9638 }
9639
Chris Wilson4b0e3332014-05-30 16:35:26 +03009640 if (intel_crtc->cursor_cntl != cntl) {
9641 I915_WRITE(_CURACNTR, cntl);
9642 POSTING_READ(_CURACNTR);
9643 intel_crtc->cursor_cntl = cntl;
9644 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009645}
9646
9647static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
9648{
9649 struct drm_device *dev = crtc->dev;
9650 struct drm_i915_private *dev_priv = dev->dev_private;
9651 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9652 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009653 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +01009654
Chris Wilson4b0e3332014-05-30 16:35:26 +03009655 cntl = 0;
9656 if (base) {
9657 cntl = MCURSOR_GAMMA_ENABLE;
Matt Roper3dd512f2015-02-27 10:12:00 -08009658 switch (intel_crtc->base.cursor->state->crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309659 case 64:
9660 cntl |= CURSOR_MODE_64_ARGB_AX;
9661 break;
9662 case 128:
9663 cntl |= CURSOR_MODE_128_ARGB_AX;
9664 break;
9665 case 256:
9666 cntl |= CURSOR_MODE_256_ARGB_AX;
9667 break;
9668 default:
Matt Roper3dd512f2015-02-27 10:12:00 -08009669 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309670 return;
Chris Wilson560b85b2010-08-07 11:01:38 +01009671 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009672 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +03009673
9674 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
9675 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson560b85b2010-08-07 11:01:38 +01009676 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009677
Matt Roper8e7d6882015-01-21 16:35:41 -08009678 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009679 cntl |= CURSOR_ROTATE_180;
9680
Chris Wilson4b0e3332014-05-30 16:35:26 +03009681 if (intel_crtc->cursor_cntl != cntl) {
9682 I915_WRITE(CURCNTR(pipe), cntl);
9683 POSTING_READ(CURCNTR(pipe));
9684 intel_crtc->cursor_cntl = cntl;
9685 }
9686
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009687 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009688 I915_WRITE(CURBASE(pipe), base);
9689 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009690
9691 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009692}
9693
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009694/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01009695static void intel_crtc_update_cursor(struct drm_crtc *crtc,
9696 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009697{
9698 struct drm_device *dev = crtc->dev;
9699 struct drm_i915_private *dev_priv = dev->dev_private;
9700 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9701 int pipe = intel_crtc->pipe;
Matt Roper3d7d6512014-06-10 08:28:13 -07009702 int x = crtc->cursor_x;
9703 int y = crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009704 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009705
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009706 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009707 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009708
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009709 if (x >= intel_crtc->config->pipe_src_w)
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009710 base = 0;
9711
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009712 if (y >= intel_crtc->config->pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009713 base = 0;
9714
9715 if (x < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009716 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009717 base = 0;
9718
9719 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
9720 x = -x;
9721 }
9722 pos |= x << CURSOR_X_SHIFT;
9723
9724 if (y < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009725 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009726 base = 0;
9727
9728 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
9729 y = -y;
9730 }
9731 pos |= y << CURSOR_Y_SHIFT;
9732
Chris Wilson4b0e3332014-05-30 16:35:26 +03009733 if (base == 0 && intel_crtc->cursor_base == 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009734 return;
9735
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009736 I915_WRITE(CURPOS(pipe), pos);
9737
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009738 /* ILK+ do this automagically */
9739 if (HAS_GMCH_DISPLAY(dev) &&
Matt Roper8e7d6882015-01-21 16:35:41 -08009740 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009741 base += (intel_crtc->base.cursor->state->crtc_h *
9742 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009743 }
9744
Ville Syrjälä8ac54662014-08-12 19:39:54 +03009745 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009746 i845_update_cursor(crtc, base);
9747 else
9748 i9xx_update_cursor(crtc, base);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009749}
9750
Ville Syrjälädc41c152014-08-13 11:57:05 +03009751static bool cursor_size_ok(struct drm_device *dev,
9752 uint32_t width, uint32_t height)
9753{
9754 if (width == 0 || height == 0)
9755 return false;
9756
9757 /*
9758 * 845g/865g are special in that they are only limited by
9759 * the width of their cursors, the height is arbitrary up to
9760 * the precision of the register. Everything else requires
9761 * square cursors, limited to a few power-of-two sizes.
9762 */
9763 if (IS_845G(dev) || IS_I865G(dev)) {
9764 if ((width & 63) != 0)
9765 return false;
9766
9767 if (width > (IS_845G(dev) ? 64 : 512))
9768 return false;
9769
9770 if (height > 1023)
9771 return false;
9772 } else {
9773 switch (width | height) {
9774 case 256:
9775 case 128:
9776 if (IS_GEN2(dev))
9777 return false;
9778 case 64:
9779 break;
9780 default:
9781 return false;
9782 }
9783 }
9784
9785 return true;
9786}
9787
Jesse Barnes79e53942008-11-07 14:24:08 -08009788static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01009789 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08009790{
James Simmons72034252010-08-03 01:33:19 +01009791 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08009792 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08009793
James Simmons72034252010-08-03 01:33:19 +01009794 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009795 intel_crtc->lut_r[i] = red[i] >> 8;
9796 intel_crtc->lut_g[i] = green[i] >> 8;
9797 intel_crtc->lut_b[i] = blue[i] >> 8;
9798 }
9799
9800 intel_crtc_load_lut(crtc);
9801}
9802
Jesse Barnes79e53942008-11-07 14:24:08 -08009803/* VESA 640x480x72Hz mode to set on the pipe */
9804static struct drm_display_mode load_detect_mode = {
9805 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
9806 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
9807};
9808
Daniel Vettera8bb6812014-02-10 18:00:39 +01009809struct drm_framebuffer *
9810__intel_framebuffer_create(struct drm_device *dev,
9811 struct drm_mode_fb_cmd2 *mode_cmd,
9812 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +01009813{
9814 struct intel_framebuffer *intel_fb;
9815 int ret;
9816
9817 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
9818 if (!intel_fb) {
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +03009819 drm_gem_object_unreference(&obj->base);
Chris Wilsond2dff872011-04-19 08:36:26 +01009820 return ERR_PTR(-ENOMEM);
9821 }
9822
9823 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009824 if (ret)
9825 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +01009826
9827 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009828err:
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +03009829 drm_gem_object_unreference(&obj->base);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009830 kfree(intel_fb);
9831
9832 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +01009833}
9834
Daniel Vetterb5ea6422014-03-02 21:18:00 +01009835static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +01009836intel_framebuffer_create(struct drm_device *dev,
9837 struct drm_mode_fb_cmd2 *mode_cmd,
9838 struct drm_i915_gem_object *obj)
9839{
9840 struct drm_framebuffer *fb;
9841 int ret;
9842
9843 ret = i915_mutex_lock_interruptible(dev);
9844 if (ret)
9845 return ERR_PTR(ret);
9846 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
9847 mutex_unlock(&dev->struct_mutex);
9848
9849 return fb;
9850}
9851
Chris Wilsond2dff872011-04-19 08:36:26 +01009852static u32
9853intel_framebuffer_pitch_for_width(int width, int bpp)
9854{
9855 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
9856 return ALIGN(pitch, 64);
9857}
9858
9859static u32
9860intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
9861{
9862 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +02009863 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +01009864}
9865
9866static struct drm_framebuffer *
9867intel_framebuffer_create_for_mode(struct drm_device *dev,
9868 struct drm_display_mode *mode,
9869 int depth, int bpp)
9870{
9871 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00009872 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01009873
9874 obj = i915_gem_alloc_object(dev,
9875 intel_framebuffer_size_for_mode(mode, bpp));
9876 if (obj == NULL)
9877 return ERR_PTR(-ENOMEM);
9878
9879 mode_cmd.width = mode->hdisplay;
9880 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009881 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
9882 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00009883 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01009884
9885 return intel_framebuffer_create(dev, &mode_cmd, obj);
9886}
9887
9888static struct drm_framebuffer *
9889mode_fits_in_fbdev(struct drm_device *dev,
9890 struct drm_display_mode *mode)
9891{
Daniel Vetter4520f532013-10-09 09:18:51 +02009892#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +01009893 struct drm_i915_private *dev_priv = dev->dev_private;
9894 struct drm_i915_gem_object *obj;
9895 struct drm_framebuffer *fb;
9896
Daniel Vetter4c0e5522014-02-14 16:35:54 +01009897 if (!dev_priv->fbdev)
9898 return NULL;
9899
9900 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +01009901 return NULL;
9902
Jesse Barnes8bcd4552014-02-07 12:10:38 -08009903 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +01009904 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +01009905
Jesse Barnes8bcd4552014-02-07 12:10:38 -08009906 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009907 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
9908 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01009909 return NULL;
9910
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009911 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01009912 return NULL;
9913
9914 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +02009915#else
9916 return NULL;
9917#endif
Chris Wilsond2dff872011-04-19 08:36:26 +01009918}
9919
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +03009920static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
9921 struct drm_crtc *crtc,
9922 struct drm_display_mode *mode,
9923 struct drm_framebuffer *fb,
9924 int x, int y)
9925{
9926 struct drm_plane_state *plane_state;
9927 int hdisplay, vdisplay;
9928 int ret;
9929
9930 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
9931 if (IS_ERR(plane_state))
9932 return PTR_ERR(plane_state);
9933
9934 if (mode)
9935 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
9936 else
9937 hdisplay = vdisplay = 0;
9938
9939 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
9940 if (ret)
9941 return ret;
9942 drm_atomic_set_fb_for_plane(plane_state, fb);
9943 plane_state->crtc_x = 0;
9944 plane_state->crtc_y = 0;
9945 plane_state->crtc_w = hdisplay;
9946 plane_state->crtc_h = vdisplay;
9947 plane_state->src_x = x << 16;
9948 plane_state->src_y = y << 16;
9949 plane_state->src_w = hdisplay << 16;
9950 plane_state->src_h = vdisplay << 16;
9951
9952 return 0;
9953}
9954
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009955bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01009956 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -05009957 struct intel_load_detect_pipe *old,
9958 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08009959{
9960 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009961 struct intel_encoder *intel_encoder =
9962 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08009963 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01009964 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08009965 struct drm_crtc *crtc = NULL;
9966 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02009967 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -05009968 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009969 struct drm_atomic_state *state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009970 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +03009971 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -05009972 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -08009973
Chris Wilsond2dff872011-04-19 08:36:26 +01009974 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03009975 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03009976 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01009977
Rob Clark51fd3712013-11-19 12:10:12 -05009978retry:
9979 ret = drm_modeset_lock(&config->connection_mutex, ctx);
9980 if (ret)
9981 goto fail_unlock;
Daniel Vetter6e9f7982014-05-29 23:54:47 +02009982
Jesse Barnes79e53942008-11-07 14:24:08 -08009983 /*
9984 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01009985 *
Jesse Barnes79e53942008-11-07 14:24:08 -08009986 * - if the connector already has an assigned crtc, use it (but make
9987 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01009988 *
Jesse Barnes79e53942008-11-07 14:24:08 -08009989 * - try to find the first unused crtc that can drive this connector,
9990 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08009991 */
9992
9993 /* See if we already have a CRTC for this connector */
9994 if (encoder->crtc) {
9995 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01009996
Rob Clark51fd3712013-11-19 12:10:12 -05009997 ret = drm_modeset_lock(&crtc->mutex, ctx);
9998 if (ret)
9999 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010000 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10001 if (ret)
10002 goto fail_unlock;
Daniel Vetter7b240562012-12-12 00:35:33 +010010003
Daniel Vetter24218aa2012-08-12 19:27:11 +020010004 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +010010005 old->load_detect_temp = false;
10006
10007 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +020010008 if (connector->dpms != DRM_MODE_DPMS_ON)
10009 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +010010010
Chris Wilson71731882011-04-19 23:10:58 +010010011 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -080010012 }
10013
10014 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010015 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010016 i++;
10017 if (!(encoder->possible_crtcs & (1 << i)))
10018 continue;
Matt Roper83d65732015-02-25 13:12:16 -080010019 if (possible_crtc->state->enable)
Ville Syrjäläa4592492014-08-11 13:15:36 +030010020 continue;
10021 /* This can occur when applying the pipe A quirk on resume. */
10022 if (to_intel_crtc(possible_crtc)->new_enabled)
10023 continue;
10024
10025 crtc = possible_crtc;
10026 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010027 }
10028
10029 /*
10030 * If we didn't find an unused CRTC, don't use any.
10031 */
10032 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010033 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Rob Clark51fd3712013-11-19 12:10:12 -050010034 goto fail_unlock;
Jesse Barnes79e53942008-11-07 14:24:08 -080010035 }
10036
Rob Clark51fd3712013-11-19 12:10:12 -050010037 ret = drm_modeset_lock(&crtc->mutex, ctx);
10038 if (ret)
10039 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010040 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10041 if (ret)
10042 goto fail_unlock;
Daniel Vetterfc303102012-07-09 10:40:58 +020010043 intel_encoder->new_crtc = to_intel_crtc(crtc);
10044 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010045
10046 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010047 intel_crtc->new_enabled = true;
Daniel Vetter24218aa2012-08-12 19:27:11 +020010048 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +010010049 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +010010050 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -080010051
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010052 state = drm_atomic_state_alloc(dev);
10053 if (!state)
10054 return false;
10055
10056 state->acquire_ctx = ctx;
10057
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010058 connector_state = drm_atomic_get_connector_state(state, connector);
10059 if (IS_ERR(connector_state)) {
10060 ret = PTR_ERR(connector_state);
10061 goto fail;
10062 }
10063
10064 connector_state->crtc = crtc;
10065 connector_state->best_encoder = &intel_encoder->base;
10066
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010067 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10068 if (IS_ERR(crtc_state)) {
10069 ret = PTR_ERR(crtc_state);
10070 goto fail;
10071 }
10072
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010073 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010074
Chris Wilson64927112011-04-20 07:25:26 +010010075 if (!mode)
10076 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010077
Chris Wilsond2dff872011-04-19 08:36:26 +010010078 /* We need a framebuffer large enough to accommodate all accesses
10079 * that the plane may generate whilst we perform load detection.
10080 * We can not rely on the fbcon either being present (we get called
10081 * during its initialisation to detect all boot displays, or it may
10082 * not even exist) or that it is large enough to satisfy the
10083 * requested mode.
10084 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010085 fb = mode_fits_in_fbdev(dev, mode);
10086 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010087 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010088 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
10089 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010090 } else
10091 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010092 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010093 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010094 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010095 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010096
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010097 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10098 if (ret)
10099 goto fail;
10100
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010101 drm_mode_copy(&crtc_state->base.mode, mode);
10102
Ander Conselvan de Oliveirae62d8dc2015-06-16 11:49:43 +030010103 if (intel_set_mode(crtc, state, true)) {
Chris Wilson64927112011-04-20 07:25:26 +010010104 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +010010105 if (old->release_fb)
10106 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010107 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010108 }
Daniel Vetter9128b042015-03-03 17:31:21 +010010109 crtc->primary->crtc = crtc;
Chris Wilson71731882011-04-19 23:10:58 +010010110
Jesse Barnes79e53942008-11-07 14:24:08 -080010111 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010112 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010113 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010114
10115 fail:
Matt Roper83d65732015-02-25 13:12:16 -080010116 intel_crtc->new_enabled = crtc->state->enable;
Rob Clark51fd3712013-11-19 12:10:12 -050010117fail_unlock:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010118 drm_atomic_state_free(state);
10119 state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010120
Rob Clark51fd3712013-11-19 12:10:12 -050010121 if (ret == -EDEADLK) {
10122 drm_modeset_backoff(ctx);
10123 goto retry;
10124 }
10125
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010126 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010127}
10128
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010129void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010130 struct intel_load_detect_pipe *old,
10131 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010132{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010133 struct drm_device *dev = connector->dev;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010134 struct intel_encoder *intel_encoder =
10135 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010136 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +010010137 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010138 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010139 struct drm_atomic_state *state;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010140 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010141 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010142 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010143
Chris Wilsond2dff872011-04-19 08:36:26 +010010144 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010145 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010146 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010147
Chris Wilson8261b192011-04-19 23:18:09 +010010148 if (old->load_detect_temp) {
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010149 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010150 if (!state)
10151 goto fail;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010152
10153 state->acquire_ctx = ctx;
10154
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010155 connector_state = drm_atomic_get_connector_state(state, connector);
10156 if (IS_ERR(connector_state))
10157 goto fail;
10158
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010159 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10160 if (IS_ERR(crtc_state))
10161 goto fail;
10162
Daniel Vetterfc303102012-07-09 10:40:58 +020010163 to_intel_connector(connector)->new_encoder = NULL;
10164 intel_encoder->new_crtc = NULL;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010165 intel_crtc->new_enabled = false;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010166
10167 connector_state->best_encoder = NULL;
10168 connector_state->crtc = NULL;
10169
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010170 crtc_state->base.enable = crtc_state->base.active = false;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010171
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010172 ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
10173 0, 0);
10174 if (ret)
10175 goto fail;
10176
Ander Conselvan de Oliveirae62d8dc2015-06-16 11:49:43 +030010177 ret = intel_set_mode(crtc, state, true);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030010178 if (ret)
10179 goto fail;
Chris Wilsond2dff872011-04-19 08:36:26 +010010180
Daniel Vetter36206362012-12-10 20:42:17 +010010181 if (old->release_fb) {
10182 drm_framebuffer_unregister_private(old->release_fb);
10183 drm_framebuffer_unreference(old->release_fb);
10184 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010185
Chris Wilson0622a532011-04-21 09:32:11 +010010186 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010187 }
10188
Eric Anholtc751ce42010-03-25 11:48:48 -070010189 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +020010190 if (old->dpms_mode != DRM_MODE_DPMS_ON)
10191 connector->funcs->dpms(connector, old->dpms_mode);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010192
10193 return;
10194fail:
10195 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
10196 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010197}
10198
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010199static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010200 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010201{
10202 struct drm_i915_private *dev_priv = dev->dev_private;
10203 u32 dpll = pipe_config->dpll_hw_state.dpll;
10204
10205 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010206 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010207 else if (HAS_PCH_SPLIT(dev))
10208 return 120000;
10209 else if (!IS_GEN2(dev))
10210 return 96000;
10211 else
10212 return 48000;
10213}
10214
Jesse Barnes79e53942008-11-07 14:24:08 -080010215/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010216static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010217 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010218{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010219 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010220 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010221 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010222 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010223 u32 fp;
10224 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010225 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010226
10227 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010228 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010229 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010230 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010231
10232 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010233 if (IS_PINEVIEW(dev)) {
10234 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10235 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010236 } else {
10237 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10238 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10239 }
10240
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010241 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010242 if (IS_PINEVIEW(dev))
10243 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10244 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010245 else
10246 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010247 DPLL_FPA01_P1_POST_DIV_SHIFT);
10248
10249 switch (dpll & DPLL_MODE_MASK) {
10250 case DPLLB_MODE_DAC_SERIAL:
10251 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10252 5 : 10;
10253 break;
10254 case DPLLB_MODE_LVDS:
10255 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10256 7 : 14;
10257 break;
10258 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010259 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010260 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010261 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010262 }
10263
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010264 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010265 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010266 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010267 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010268 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010269 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010270 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010271
10272 if (is_lvds) {
10273 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10274 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010275
10276 if (lvds & LVDS_CLKB_POWER_UP)
10277 clock.p2 = 7;
10278 else
10279 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010280 } else {
10281 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10282 clock.p1 = 2;
10283 else {
10284 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10285 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10286 }
10287 if (dpll & PLL_P2_DIVIDE_BY_4)
10288 clock.p2 = 4;
10289 else
10290 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010291 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010292
10293 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010294 }
10295
Ville Syrjälä18442d02013-09-13 16:00:08 +030010296 /*
10297 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010298 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010299 * encoder's get_config() function.
10300 */
10301 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010302}
10303
Ville Syrjälä6878da02013-09-13 15:59:11 +030010304int intel_dotclock_calculate(int link_freq,
10305 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010306{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010307 /*
10308 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010309 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010310 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010311 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010312 *
10313 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010314 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010315 */
10316
Ville Syrjälä6878da02013-09-13 15:59:11 +030010317 if (!m_n->link_n)
10318 return 0;
10319
10320 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10321}
10322
Ville Syrjälä18442d02013-09-13 16:00:08 +030010323static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010324 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010325{
10326 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +030010327
10328 /* read out port_clock from the DPLL */
10329 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010330
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010331 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +030010332 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +010010333 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +030010334 * agree once we know their relationship in the encoder's
10335 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010336 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010337 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +030010338 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10339 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010340}
10341
10342/** Returns the currently programmed mode of the given pipe. */
10343struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10344 struct drm_crtc *crtc)
10345{
Jesse Barnes548f2452011-02-17 10:40:53 -080010346 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010347 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010348 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010349 struct drm_display_mode *mode;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010350 struct intel_crtc_state pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010351 int htot = I915_READ(HTOTAL(cpu_transcoder));
10352 int hsync = I915_READ(HSYNC(cpu_transcoder));
10353 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10354 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010355 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010356
10357 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10358 if (!mode)
10359 return NULL;
10360
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010361 /*
10362 * Construct a pipe_config sufficient for getting the clock info
10363 * back out of crtc_clock_get.
10364 *
10365 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10366 * to use a real value here instead.
10367 */
Ville Syrjälä293623f2013-09-13 16:18:46 +030010368 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010369 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010370 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10371 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10372 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010373 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10374
Ville Syrjälä773ae032013-09-23 17:48:20 +030010375 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010376 mode->hdisplay = (htot & 0xffff) + 1;
10377 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10378 mode->hsync_start = (hsync & 0xffff) + 1;
10379 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10380 mode->vdisplay = (vtot & 0xffff) + 1;
10381 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10382 mode->vsync_start = (vsync & 0xffff) + 1;
10383 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10384
10385 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010386
10387 return mode;
10388}
10389
Jesse Barnes652c3932009-08-17 13:31:43 -070010390static void intel_decrease_pllclock(struct drm_crtc *crtc)
10391{
10392 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030010393 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -070010394 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -070010395
Sonika Jindalbaff2962014-07-22 11:16:35 +053010396 if (!HAS_GMCH_DISPLAY(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -070010397 return;
10398
10399 if (!dev_priv->lvds_downclock_avail)
10400 return;
10401
10402 /*
10403 * Since this is called by a timer, we should never get here in
10404 * the manual case.
10405 */
10406 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +010010407 int pipe = intel_crtc->pipe;
10408 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +020010409 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +010010410
Zhao Yakui44d98a62009-10-09 11:39:40 +080010411 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -070010412
Sean Paul8ac5a6d2012-02-13 13:14:51 -050010413 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -070010414
Chris Wilson074b5e12012-05-02 12:07:06 +010010415 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -070010416 dpll |= DISPLAY_RATE_SELECT_FPA1;
10417 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010418 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -070010419 dpll = I915_READ(dpll_reg);
10420 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +080010421 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -070010422 }
10423
10424}
10425
Chris Wilsonf047e392012-07-21 12:31:41 +010010426void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010427{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010428 struct drm_i915_private *dev_priv = dev->dev_private;
10429
Chris Wilsonf62a0072014-02-21 17:55:39 +000010430 if (dev_priv->mm.busy)
10431 return;
10432
Paulo Zanoni43694d62014-03-07 20:08:08 -030010433 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010434 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010435 if (INTEL_INFO(dev)->gen >= 6)
10436 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010437 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010438}
10439
10440void intel_mark_idle(struct drm_device *dev)
10441{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010442 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010443 struct drm_crtc *crtc;
10444
Chris Wilsonf62a0072014-02-21 17:55:39 +000010445 if (!dev_priv->mm.busy)
10446 return;
10447
10448 dev_priv->mm.busy = false;
10449
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010450 for_each_crtc(dev, crtc) {
Matt Roperf4510a22014-04-01 15:22:40 -070010451 if (!crtc->primary->fb)
Chris Wilson725a5b52013-01-08 11:02:57 +000010452 continue;
10453
10454 intel_decrease_pllclock(crtc);
10455 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010456
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010457 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010458 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010459
Paulo Zanoni43694d62014-03-07 20:08:08 -030010460 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010461}
10462
Jesse Barnes79e53942008-11-07 14:24:08 -080010463static void intel_crtc_destroy(struct drm_crtc *crtc)
10464{
10465 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010466 struct drm_device *dev = crtc->dev;
10467 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010468
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010469 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010470 work = intel_crtc->unpin_work;
10471 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010472 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010473
10474 if (work) {
10475 cancel_work_sync(&work->work);
10476 kfree(work);
10477 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010478
10479 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010480
Jesse Barnes79e53942008-11-07 14:24:08 -080010481 kfree(intel_crtc);
10482}
10483
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010484static void intel_unpin_work_fn(struct work_struct *__work)
10485{
10486 struct intel_unpin_work *work =
10487 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010488 struct drm_device *dev = work->crtc->dev;
Daniel Vetterf99d7062014-06-19 16:01:59 +020010489 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010490
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010491 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000010492 intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
Chris Wilson05394f32010-11-08 19:18:58 +000010493 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010494
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020010495 intel_fbc_update(dev);
John Harrisonf06cc1b2014-11-24 18:49:37 +000010496
10497 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010498 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010499 mutex_unlock(&dev->struct_mutex);
10500
Daniel Vetterf99d7062014-06-19 16:01:59 +020010501 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Chris Wilson89ed88b2015-02-16 14:31:49 +000010502 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010503
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010504 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
10505 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
10506
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010507 kfree(work);
10508}
10509
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010510static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010010511 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010512{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010513 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10514 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010515 unsigned long flags;
10516
10517 /* Ignore early vblank irqs */
10518 if (intel_crtc == NULL)
10519 return;
10520
Daniel Vetterf3260382014-09-15 14:55:23 +020010521 /*
10522 * This is called both by irq handlers and the reset code (to complete
10523 * lost pageflips) so needs the full irqsave spinlocks.
10524 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010525 spin_lock_irqsave(&dev->event_lock, flags);
10526 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000010527
10528 /* Ensure we don't miss a work->pending update ... */
10529 smp_rmb();
10530
10531 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010532 spin_unlock_irqrestore(&dev->event_lock, flags);
10533 return;
10534 }
10535
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010536 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010010537
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010538 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010539}
10540
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010541void intel_finish_page_flip(struct drm_device *dev, int pipe)
10542{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010543 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010544 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10545
Mario Kleiner49b14a52010-12-09 07:00:07 +010010546 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010547}
10548
10549void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10550{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010551 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010552 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10553
Mario Kleiner49b14a52010-12-09 07:00:07 +010010554 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010555}
10556
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010557/* Is 'a' after or equal to 'b'? */
10558static bool g4x_flip_count_after_eq(u32 a, u32 b)
10559{
10560 return !((a - b) & 0x80000000);
10561}
10562
10563static bool page_flip_finished(struct intel_crtc *crtc)
10564{
10565 struct drm_device *dev = crtc->base.dev;
10566 struct drm_i915_private *dev_priv = dev->dev_private;
10567
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030010568 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10569 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10570 return true;
10571
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010572 /*
10573 * The relevant registers doen't exist on pre-ctg.
10574 * As the flip done interrupt doesn't trigger for mmio
10575 * flips on gmch platforms, a flip count check isn't
10576 * really needed there. But since ctg has the registers,
10577 * include it in the check anyway.
10578 */
10579 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10580 return true;
10581
10582 /*
10583 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10584 * used the same base address. In that case the mmio flip might
10585 * have completed, but the CS hasn't even executed the flip yet.
10586 *
10587 * A flip count check isn't enough as the CS might have updated
10588 * the base address just after start of vblank, but before we
10589 * managed to process the interrupt. This means we'd complete the
10590 * CS flip too soon.
10591 *
10592 * Combining both checks should get us a good enough result. It may
10593 * still happen that the CS flip has been executed, but has not
10594 * yet actually completed. But in case the base address is the same
10595 * anyway, we don't really care.
10596 */
10597 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10598 crtc->unpin_work->gtt_offset &&
10599 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
10600 crtc->unpin_work->flip_count);
10601}
10602
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010603void intel_prepare_page_flip(struct drm_device *dev, int plane)
10604{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010605 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010606 struct intel_crtc *intel_crtc =
10607 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10608 unsigned long flags;
10609
Daniel Vetterf3260382014-09-15 14:55:23 +020010610
10611 /*
10612 * This is called both by irq handlers and the reset code (to complete
10613 * lost pageflips) so needs the full irqsave spinlocks.
10614 *
10615 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000010616 * generate a page-flip completion irq, i.e. every modeset
10617 * is also accompanied by a spurious intel_prepare_page_flip().
10618 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010619 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010620 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000010621 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010622 spin_unlock_irqrestore(&dev->event_lock, flags);
10623}
10624
Robin Schroereba905b2014-05-18 02:24:50 +020010625static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
Chris Wilsone7d841c2012-12-03 11:36:30 +000010626{
10627 /* Ensure that the work item is consistent when activating it ... */
10628 smp_wmb();
10629 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
10630 /* and that it is marked active as soon as the irq could fire. */
10631 smp_wmb();
10632}
10633
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010634static int intel_gen2_queue_flip(struct drm_device *dev,
10635 struct drm_crtc *crtc,
10636 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010637 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010638 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010639 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010640{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010641 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010642 u32 flip_mask;
10643 int ret;
10644
Daniel Vetter6d90c952012-04-26 23:28:05 +020010645 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010646 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010647 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010648
10649 /* Can't queue multiple flips, so wait for the previous
10650 * one to finish before executing the next.
10651 */
10652 if (intel_crtc->plane)
10653 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10654 else
10655 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010656 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10657 intel_ring_emit(ring, MI_NOOP);
10658 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10659 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10660 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010661 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010662 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000010663
10664 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010665 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010666 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010667}
10668
10669static int intel_gen3_queue_flip(struct drm_device *dev,
10670 struct drm_crtc *crtc,
10671 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010672 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010673 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010674 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010675{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010676 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010677 u32 flip_mask;
10678 int ret;
10679
Daniel Vetter6d90c952012-04-26 23:28:05 +020010680 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010681 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010682 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010683
10684 if (intel_crtc->plane)
10685 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10686 else
10687 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010688 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10689 intel_ring_emit(ring, MI_NOOP);
10690 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
10691 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10692 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010693 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010694 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010695
Chris Wilsone7d841c2012-12-03 11:36:30 +000010696 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010697 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010698 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010699}
10700
10701static int intel_gen4_queue_flip(struct drm_device *dev,
10702 struct drm_crtc *crtc,
10703 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010704 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010705 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010706 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010707{
10708 struct drm_i915_private *dev_priv = dev->dev_private;
10709 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10710 uint32_t pf, pipesrc;
10711 int ret;
10712
Daniel Vetter6d90c952012-04-26 23:28:05 +020010713 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010714 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010715 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010716
10717 /* i965+ uses the linear or tiled offsets from the
10718 * Display Registers (which do not change across a page-flip)
10719 * so we need only reprogram the base address.
10720 */
Daniel Vetter6d90c952012-04-26 23:28:05 +020010721 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10722 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10723 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010724 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020010725 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010726
10727 /* XXX Enabling the panel-fitter across page-flip is so far
10728 * untested on non-native modes, so ignore it for now.
10729 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
10730 */
10731 pf = 0;
10732 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010733 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010734
10735 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010736 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010737 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010738}
10739
10740static int intel_gen6_queue_flip(struct drm_device *dev,
10741 struct drm_crtc *crtc,
10742 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010743 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010744 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010745 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010746{
10747 struct drm_i915_private *dev_priv = dev->dev_private;
10748 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10749 uint32_t pf, pipesrc;
10750 int ret;
10751
Daniel Vetter6d90c952012-04-26 23:28:05 +020010752 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010753 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010754 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010755
Daniel Vetter6d90c952012-04-26 23:28:05 +020010756 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10757 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10758 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010759 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010760
Chris Wilson99d9acd2012-04-17 20:37:00 +010010761 /* Contrary to the suggestions in the documentation,
10762 * "Enable Panel Fitter" does not seem to be required when page
10763 * flipping with a non-native mode, and worse causes a normal
10764 * modeset to fail.
10765 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
10766 */
10767 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010768 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010769 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010770
10771 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010772 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010773 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010774}
10775
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010776static int intel_gen7_queue_flip(struct drm_device *dev,
10777 struct drm_crtc *crtc,
10778 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010779 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010780 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010781 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010782{
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010783 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010784 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010010785 int len, ret;
10786
Robin Schroereba905b2014-05-18 02:24:50 +020010787 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010788 case PLANE_A:
10789 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
10790 break;
10791 case PLANE_B:
10792 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
10793 break;
10794 case PLANE_C:
10795 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
10796 break;
10797 default:
10798 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010799 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010800 }
10801
Chris Wilsonffe74d72013-08-26 20:58:12 +010010802 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +010010803 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010010804 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010010805 /*
10806 * On Gen 8, SRM is now taking an extra dword to accommodate
10807 * 48bits addresses, and we need a NOOP for the batch size to
10808 * stay even.
10809 */
10810 if (IS_GEN8(dev))
10811 len += 2;
10812 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010010813
Ville Syrjäläf66fab82014-02-11 19:52:06 +020010814 /*
10815 * BSpec MI_DISPLAY_FLIP for IVB:
10816 * "The full packet must be contained within the same cache line."
10817 *
10818 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
10819 * cacheline, if we ever start emitting more commands before
10820 * the MI_DISPLAY_FLIP we may need to first emit everything else,
10821 * then do the cacheline alignment, and finally emit the
10822 * MI_DISPLAY_FLIP.
10823 */
10824 ret = intel_ring_cacheline_align(ring);
10825 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010826 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020010827
Chris Wilsonffe74d72013-08-26 20:58:12 +010010828 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010829 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010830 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010831
Chris Wilsonffe74d72013-08-26 20:58:12 +010010832 /* Unmask the flip-done completion message. Note that the bspec says that
10833 * we should do this for both the BCS and RCS, and that we must not unmask
10834 * more than one flip event at any time (or ensure that one flip message
10835 * can be sent by waiting for flip-done prior to queueing new flips).
10836 * Experimentation says that BCS works despite DERRMR masking all
10837 * flip-done completion events and that unmasking all planes at once
10838 * for the RCS also doesn't appear to drop events. Setting the DERRMR
10839 * to zero does lead to lockups within MI_DISPLAY_FLIP.
10840 */
10841 if (ring->id == RCS) {
10842 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
10843 intel_ring_emit(ring, DERRMR);
10844 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
10845 DERRMR_PIPEB_PRI_FLIP_DONE |
10846 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010010847 if (IS_GEN8(dev))
10848 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
10849 MI_SRM_LRM_GLOBAL_GTT);
10850 else
10851 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
10852 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +010010853 intel_ring_emit(ring, DERRMR);
10854 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010010855 if (IS_GEN8(dev)) {
10856 intel_ring_emit(ring, 0);
10857 intel_ring_emit(ring, MI_NOOP);
10858 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010010859 }
10860
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010861 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010862 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010863 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010864 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000010865
10866 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010867 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010868 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010869}
10870
Sourab Gupta84c33a62014-06-02 16:47:17 +053010871static bool use_mmio_flip(struct intel_engine_cs *ring,
10872 struct drm_i915_gem_object *obj)
10873{
10874 /*
10875 * This is not being used for older platforms, because
10876 * non-availability of flip done interrupt forces us to use
10877 * CS flips. Older platforms derive flip done using some clever
10878 * tricks involving the flip_pending status bits and vblank irqs.
10879 * So using MMIO flips there would disrupt this mechanism.
10880 */
10881
Chris Wilson8e09bf82014-07-08 10:40:30 +010010882 if (ring == NULL)
10883 return true;
10884
Sourab Gupta84c33a62014-06-02 16:47:17 +053010885 if (INTEL_INFO(ring->dev)->gen < 5)
10886 return false;
10887
10888 if (i915.use_mmio_flip < 0)
10889 return false;
10890 else if (i915.use_mmio_flip > 0)
10891 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010010892 else if (i915.enable_execlists)
10893 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053010894 else
Chris Wilsonb4716182015-04-27 13:41:17 +010010895 return ring != i915_gem_request_get_ring(obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010896}
10897
Damien Lespiauff944562014-11-20 14:58:16 +000010898static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
10899{
10900 struct drm_device *dev = intel_crtc->base.dev;
10901 struct drm_i915_private *dev_priv = dev->dev_private;
10902 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000010903 const enum pipe pipe = intel_crtc->pipe;
10904 u32 ctl, stride;
10905
10906 ctl = I915_READ(PLANE_CTL(pipe, 0));
10907 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010010908 switch (fb->modifier[0]) {
10909 case DRM_FORMAT_MOD_NONE:
10910 break;
10911 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000010912 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010010913 break;
10914 case I915_FORMAT_MOD_Y_TILED:
10915 ctl |= PLANE_CTL_TILED_Y;
10916 break;
10917 case I915_FORMAT_MOD_Yf_TILED:
10918 ctl |= PLANE_CTL_TILED_YF;
10919 break;
10920 default:
10921 MISSING_CASE(fb->modifier[0]);
10922 }
Damien Lespiauff944562014-11-20 14:58:16 +000010923
10924 /*
10925 * The stride is either expressed as a multiple of 64 bytes chunks for
10926 * linear buffers or in number of tiles for tiled buffers.
10927 */
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010010928 stride = fb->pitches[0] /
10929 intel_fb_stride_alignment(dev, fb->modifier[0],
10930 fb->pixel_format);
Damien Lespiauff944562014-11-20 14:58:16 +000010931
10932 /*
10933 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
10934 * PLANE_SURF updates, the update is then guaranteed to be atomic.
10935 */
10936 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
10937 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
10938
10939 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
10940 POSTING_READ(PLANE_SURF(pipe, 0));
10941}
10942
10943static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
Sourab Gupta84c33a62014-06-02 16:47:17 +053010944{
10945 struct drm_device *dev = intel_crtc->base.dev;
10946 struct drm_i915_private *dev_priv = dev->dev_private;
10947 struct intel_framebuffer *intel_fb =
10948 to_intel_framebuffer(intel_crtc->base.primary->fb);
10949 struct drm_i915_gem_object *obj = intel_fb->obj;
10950 u32 dspcntr;
10951 u32 reg;
10952
Sourab Gupta84c33a62014-06-02 16:47:17 +053010953 reg = DSPCNTR(intel_crtc->plane);
10954 dspcntr = I915_READ(reg);
10955
Damien Lespiauc5d97472014-10-25 00:11:11 +010010956 if (obj->tiling_mode != I915_TILING_NONE)
10957 dspcntr |= DISPPLANE_TILED;
10958 else
10959 dspcntr &= ~DISPPLANE_TILED;
10960
Sourab Gupta84c33a62014-06-02 16:47:17 +053010961 I915_WRITE(reg, dspcntr);
10962
10963 I915_WRITE(DSPSURF(intel_crtc->plane),
10964 intel_crtc->unpin_work->gtt_offset);
10965 POSTING_READ(DSPSURF(intel_crtc->plane));
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010966
Damien Lespiauff944562014-11-20 14:58:16 +000010967}
10968
10969/*
10970 * XXX: This is the temporary way to update the plane registers until we get
10971 * around to using the usual plane update functions for MMIO flips
10972 */
10973static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
10974{
10975 struct drm_device *dev = intel_crtc->base.dev;
10976 bool atomic_update;
10977 u32 start_vbl_count;
10978
10979 intel_mark_page_flip_active(intel_crtc);
10980
10981 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
10982
10983 if (INTEL_INFO(dev)->gen >= 9)
10984 skl_do_mmio_flip(intel_crtc);
10985 else
10986 /* use_mmio_flip() retricts MMIO flips to ilk+ */
10987 ilk_do_mmio_flip(intel_crtc);
10988
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010989 if (atomic_update)
10990 intel_pipe_update_end(intel_crtc, start_vbl_count);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010991}
10992
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010993static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053010994{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010010995 struct intel_mmio_flip *mmio_flip =
10996 container_of(work, struct intel_mmio_flip, work);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010997
Daniel Vettereed29a52015-05-21 14:21:25 +020010998 if (mmio_flip->req)
10999 WARN_ON(__i915_wait_request(mmio_flip->req,
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011000 mmio_flip->crtc->reset_counter,
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011001 false, NULL,
11002 &mmio_flip->i915->rps.mmioflips));
Sourab Gupta84c33a62014-06-02 16:47:17 +053011003
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011004 intel_do_mmio_flip(mmio_flip->crtc);
11005
Daniel Vettereed29a52015-05-21 14:21:25 +020011006 i915_gem_request_unreference__unlocked(mmio_flip->req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011007 kfree(mmio_flip);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011008}
11009
11010static int intel_queue_mmio_flip(struct drm_device *dev,
11011 struct drm_crtc *crtc,
11012 struct drm_framebuffer *fb,
11013 struct drm_i915_gem_object *obj,
11014 struct intel_engine_cs *ring,
11015 uint32_t flags)
11016{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011017 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011018
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011019 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11020 if (mmio_flip == NULL)
11021 return -ENOMEM;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011022
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011023 mmio_flip->i915 = to_i915(dev);
Daniel Vettereed29a52015-05-21 14:21:25 +020011024 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011025 mmio_flip->crtc = to_intel_crtc(crtc);
11026
11027 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11028 schedule_work(&mmio_flip->work);
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020011029
Sourab Gupta84c33a62014-06-02 16:47:17 +053011030 return 0;
11031}
11032
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011033static int intel_default_queue_flip(struct drm_device *dev,
11034 struct drm_crtc *crtc,
11035 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011036 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011037 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070011038 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011039{
11040 return -ENODEV;
11041}
11042
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011043static bool __intel_pageflip_stall_check(struct drm_device *dev,
11044 struct drm_crtc *crtc)
11045{
11046 struct drm_i915_private *dev_priv = dev->dev_private;
11047 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11048 struct intel_unpin_work *work = intel_crtc->unpin_work;
11049 u32 addr;
11050
11051 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11052 return true;
11053
11054 if (!work->enable_stall_check)
11055 return false;
11056
11057 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010011058 if (work->flip_queued_req &&
11059 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011060 return false;
11061
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011062 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011063 }
11064
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011065 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011066 return false;
11067
11068 /* Potential stall - if we see that the flip has happened,
11069 * assume a missed interrupt. */
11070 if (INTEL_INFO(dev)->gen >= 4)
11071 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11072 else
11073 addr = I915_READ(DSPADDR(intel_crtc->plane));
11074
11075 /* There is a potential issue here with a false positive after a flip
11076 * to the same address. We could address this by checking for a
11077 * non-incrementing frame counter.
11078 */
11079 return addr == work->gtt_offset;
11080}
11081
11082void intel_check_page_flip(struct drm_device *dev, int pipe)
11083{
11084 struct drm_i915_private *dev_priv = dev->dev_private;
11085 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11086 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011087 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020011088
Dave Gordon6c51d462015-03-06 15:34:26 +000011089 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011090
11091 if (crtc == NULL)
11092 return;
11093
Daniel Vetterf3260382014-09-15 14:55:23 +020011094 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011095 work = intel_crtc->unpin_work;
11096 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011097 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010011098 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011099 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011100 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011101 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010011102 if (work != NULL &&
11103 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11104 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020011105 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011106}
11107
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011108static int intel_crtc_page_flip(struct drm_crtc *crtc,
11109 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011110 struct drm_pending_vblank_event *event,
11111 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011112{
11113 struct drm_device *dev = crtc->dev;
11114 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070011115 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070011116 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011117 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080011118 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020011119 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011120 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011121 struct intel_engine_cs *ring;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011122 bool mmio_flip;
Chris Wilson52e68632010-08-08 10:15:59 +010011123 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011124
Matt Roper2ff8fde2014-07-08 07:50:07 -070011125 /*
11126 * drm_mode_page_flip_ioctl() should already catch this, but double
11127 * check to be safe. In the future we may enable pageflipping from
11128 * a disabled primary plane.
11129 */
11130 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11131 return -EBUSY;
11132
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011133 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070011134 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011135 return -EINVAL;
11136
11137 /*
11138 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11139 * Note that pitch changes could also affect these register.
11140 */
11141 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070011142 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11143 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011144 return -EINVAL;
11145
Chris Wilsonf900db42014-02-20 09:26:13 +000011146 if (i915_terminally_wedged(&dev_priv->gpu_error))
11147 goto out_hang;
11148
Daniel Vetterb14c5672013-09-19 12:18:32 +020011149 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011150 if (work == NULL)
11151 return -ENOMEM;
11152
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011153 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011154 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011155 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011156 INIT_WORK(&work->work, intel_unpin_work_fn);
11157
Daniel Vetter87b6b102014-05-15 15:33:46 +020011158 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011159 if (ret)
11160 goto free_work;
11161
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011162 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011163 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011164 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011165 /* Before declaring the flip queue wedged, check if
11166 * the hardware completed the operation behind our backs.
11167 */
11168 if (__intel_pageflip_stall_check(dev, crtc)) {
11169 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11170 page_flip_completed(intel_crtc);
11171 } else {
11172 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011173 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010011174
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011175 drm_crtc_vblank_put(crtc);
11176 kfree(work);
11177 return -EBUSY;
11178 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011179 }
11180 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011181 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011182
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011183 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11184 flush_workqueue(dev_priv->wq);
11185
Jesse Barnes75dfca82010-02-10 15:09:44 -080011186 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011187 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000011188 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011189
Matt Roperf4510a22014-04-01 15:22:40 -070011190 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011191 update_state_fb(crtc->primary);
Matt Roper1ed1f962015-01-30 16:22:36 -080011192
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011193 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011194
Chris Wilson89ed88b2015-02-16 14:31:49 +000011195 ret = i915_mutex_lock_interruptible(dev);
11196 if (ret)
11197 goto cleanup;
11198
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011199 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020011200 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011201
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011202 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Daniel Vettera071fa02014-06-18 23:28:09 +020011203 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011204
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011205 if (IS_VALLEYVIEW(dev)) {
11206 ring = &dev_priv->ring[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011207 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011208 /* vlv: DISPLAY_FLIP fails to change tiling */
11209 ring = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000011210 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Chris Wilson2a92d5b2014-07-08 10:40:29 +010011211 ring = &dev_priv->ring[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011212 } else if (INTEL_INFO(dev)->gen >= 7) {
Chris Wilsonb4716182015-04-27 13:41:17 +010011213 ring = i915_gem_request_get_ring(obj->last_write_req);
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011214 if (ring == NULL || ring->id != RCS)
11215 ring = &dev_priv->ring[BCS];
11216 } else {
11217 ring = &dev_priv->ring[RCS];
11218 }
11219
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011220 mmio_flip = use_mmio_flip(ring, obj);
11221
11222 /* When using CS flips, we want to emit semaphores between rings.
11223 * However, when using mmio flips we will create a task to do the
11224 * synchronisation, so all we want here is to pin the framebuffer
11225 * into the display plane and skip any waits.
11226 */
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011227 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011228 crtc->primary->state,
Chris Wilsonb4716182015-04-27 13:41:17 +010011229 mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011230 if (ret)
11231 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011232
Tvrtko Ursulin121920f2015-03-23 11:10:37 +000011233 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
11234 + intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011235
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011236 if (mmio_flip) {
Sourab Gupta84c33a62014-06-02 16:47:17 +053011237 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
11238 page_flip_flags);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011239 if (ret)
11240 goto cleanup_unpin;
11241
John Harrisonf06cc1b2014-11-24 18:49:37 +000011242 i915_gem_request_assign(&work->flip_queued_req,
11243 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011244 } else {
Chris Wilsond94b5032015-04-27 13:41:15 +010011245 if (obj->last_write_req) {
11246 ret = i915_gem_check_olr(obj->last_write_req);
11247 if (ret)
11248 goto cleanup_unpin;
11249 }
11250
Sourab Gupta84c33a62014-06-02 16:47:17 +053011251 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011252 page_flip_flags);
11253 if (ret)
11254 goto cleanup_unpin;
11255
John Harrisonf06cc1b2014-11-24 18:49:37 +000011256 i915_gem_request_assign(&work->flip_queued_req,
11257 intel_ring_get_request(ring));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011258 }
11259
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011260 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011261 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011262
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011263 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Daniel Vettera071fa02014-06-18 23:28:09 +020011264 INTEL_FRONTBUFFER_PRIMARY(pipe));
11265
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020011266 intel_fbc_disable(dev);
Daniel Vetterf99d7062014-06-19 16:01:59 +020011267 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011268 mutex_unlock(&dev->struct_mutex);
11269
Jesse Barnese5510fa2010-07-01 16:48:37 -070011270 trace_i915_flip_request(intel_crtc->plane, obj);
11271
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011272 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010011273
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011274cleanup_unpin:
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011275 intel_unpin_fb_obj(fb, crtc->primary->state);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011276cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011277 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000011278 mutex_unlock(&dev->struct_mutex);
11279cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070011280 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011281 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010011282
Chris Wilson89ed88b2015-02-16 14:31:49 +000011283 drm_gem_object_unreference_unlocked(&obj->base);
11284 drm_framebuffer_unreference(work->old_fb);
11285
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011286 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011287 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011288 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011289
Daniel Vetter87b6b102014-05-15 15:33:46 +020011290 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011291free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010011292 kfree(work);
11293
Chris Wilsonf900db42014-02-20 09:26:13 +000011294 if (ret == -EIO) {
11295out_hang:
Matt Roper53a366b2014-12-23 10:41:53 -080011296 ret = intel_plane_restore(primary);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011297 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011298 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020011299 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011300 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011301 }
Chris Wilsonf900db42014-02-20 09:26:13 +000011302 }
Chris Wilson96b099f2010-06-07 14:03:04 +010011303 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011304}
11305
Jani Nikula65b38e02015-04-13 11:26:56 +030011306static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011307 .mode_set_base_atomic = intel_pipe_set_base_atomic,
11308 .load_lut = intel_crtc_load_lut,
Matt Roperea2c67b2014-12-23 10:41:52 -080011309 .atomic_begin = intel_begin_crtc_commit,
11310 .atomic_flush = intel_finish_crtc_commit,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011311};
11312
Daniel Vetter9a935852012-07-05 22:34:27 +020011313/**
11314 * intel_modeset_update_staged_output_state
11315 *
11316 * Updates the staged output configuration state, e.g. after we've read out the
11317 * current hw state.
11318 */
11319static void intel_modeset_update_staged_output_state(struct drm_device *dev)
11320{
Ville Syrjälä76688512014-01-10 11:28:06 +020011321 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011322 struct intel_encoder *encoder;
11323 struct intel_connector *connector;
11324
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011325 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011326 connector->new_encoder =
11327 to_intel_encoder(connector->base.encoder);
11328 }
11329
Damien Lespiaub2784e12014-08-05 11:29:37 +010011330 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011331 encoder->new_crtc =
11332 to_intel_crtc(encoder->base.crtc);
11333 }
Ville Syrjälä76688512014-01-10 11:28:06 +020011334
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011335 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011336 crtc->new_enabled = crtc->base.state->enable;
Ville Syrjälä76688512014-01-10 11:28:06 +020011337 }
Daniel Vetter9a935852012-07-05 22:34:27 +020011338}
11339
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011340/* Transitional helper to copy current connector/encoder state to
11341 * connector->state. This is needed so that code that is partially
11342 * converted to atomic does the right thing.
11343 */
11344static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11345{
11346 struct intel_connector *connector;
11347
11348 for_each_intel_connector(dev, connector) {
11349 if (connector->base.encoder) {
11350 connector->base.state->best_encoder =
11351 connector->base.encoder;
11352 connector->base.state->crtc =
11353 connector->base.encoder->crtc;
11354 } else {
11355 connector->base.state->best_encoder = NULL;
11356 connector->base.state->crtc = NULL;
11357 }
11358 }
11359}
11360
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011361/* Fixup legacy state after an atomic state swap.
Daniel Vetter9a935852012-07-05 22:34:27 +020011362 */
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011363static void intel_modeset_fixup_state(struct drm_atomic_state *state)
Daniel Vetter9a935852012-07-05 22:34:27 +020011364{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011365 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011366 struct intel_encoder *encoder;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011367 struct intel_connector *connector;
Daniel Vetter9a935852012-07-05 22:34:27 +020011368
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011369 for_each_intel_connector(state->dev, connector) {
11370 connector->base.encoder = connector->base.state->best_encoder;
11371 if (connector->base.encoder)
11372 connector->base.encoder->crtc =
11373 connector->base.state->crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011374 }
11375
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030011376 /* Update crtc of disabled encoders */
11377 for_each_intel_encoder(state->dev, encoder) {
11378 int num_connectors = 0;
11379
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011380 for_each_intel_connector(state->dev, connector)
11381 if (connector->base.encoder == &encoder->base)
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030011382 num_connectors++;
11383
11384 if (num_connectors == 0)
11385 encoder->base.crtc = NULL;
Daniel Vetter9a935852012-07-05 22:34:27 +020011386 }
Ville Syrjälä76688512014-01-10 11:28:06 +020011387
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011388 for_each_intel_crtc(state->dev, crtc) {
11389 crtc->base.enabled = crtc->base.state->enable;
11390 crtc->config = to_intel_crtc_state(crtc->base.state);
Ville Syrjälä76688512014-01-10 11:28:06 +020011391 }
Daniel Vetter9a935852012-07-05 22:34:27 +020011392}
11393
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011394static void
Robin Schroereba905b2014-05-18 02:24:50 +020011395connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011396 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011397{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011398 int bpp = pipe_config->pipe_bpp;
11399
11400 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11401 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011402 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011403
11404 /* Don't use an invalid EDID bpc value */
11405 if (connector->base.display_info.bpc &&
11406 connector->base.display_info.bpc * 3 < bpp) {
11407 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11408 bpp, connector->base.display_info.bpc*3);
11409 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11410 }
11411
11412 /* Clamp bpp to 8 on screens without EDID 1.4 */
11413 if (connector->base.display_info.bpc == 0 && bpp > 24) {
11414 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11415 bpp);
11416 pipe_config->pipe_bpp = 24;
11417 }
11418}
11419
11420static int
11421compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011422 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011423{
11424 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011425 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011426 struct drm_connector *connector;
11427 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011428 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011429
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011430 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011431 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011432 else if (INTEL_INFO(dev)->gen >= 5)
11433 bpp = 12*3;
11434 else
11435 bpp = 8*3;
11436
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011437
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011438 pipe_config->pipe_bpp = bpp;
11439
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011440 state = pipe_config->base.state;
11441
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011442 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011443 for_each_connector_in_state(state, connector, connector_state, i) {
11444 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011445 continue;
11446
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011447 connected_sink_compute_bpp(to_intel_connector(connector),
11448 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011449 }
11450
11451 return bpp;
11452}
11453
Daniel Vetter644db712013-09-19 14:53:58 +020011454static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11455{
11456 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11457 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010011458 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020011459 mode->crtc_hdisplay, mode->crtc_hsync_start,
11460 mode->crtc_hsync_end, mode->crtc_htotal,
11461 mode->crtc_vdisplay, mode->crtc_vsync_start,
11462 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11463}
11464
Daniel Vetterc0b03412013-05-28 12:05:54 +020011465static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011466 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020011467 const char *context)
11468{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011469 struct drm_device *dev = crtc->base.dev;
11470 struct drm_plane *plane;
11471 struct intel_plane *intel_plane;
11472 struct intel_plane_state *state;
11473 struct drm_framebuffer *fb;
11474
11475 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
11476 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020011477
11478 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
11479 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
11480 pipe_config->pipe_bpp, pipe_config->dither);
11481 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11482 pipe_config->has_pch_encoder,
11483 pipe_config->fdi_lanes,
11484 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
11485 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
11486 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011487 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11488 pipe_config->has_dp_encoder,
11489 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
11490 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
11491 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011492
11493 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
11494 pipe_config->has_dp_encoder,
11495 pipe_config->dp_m2_n2.gmch_m,
11496 pipe_config->dp_m2_n2.gmch_n,
11497 pipe_config->dp_m2_n2.link_m,
11498 pipe_config->dp_m2_n2.link_n,
11499 pipe_config->dp_m2_n2.tu);
11500
Daniel Vetter55072d12014-11-20 16:10:28 +010011501 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
11502 pipe_config->has_audio,
11503 pipe_config->has_infoframe);
11504
Daniel Vetterc0b03412013-05-28 12:05:54 +020011505 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011506 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011507 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011508 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11509 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030011510 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030011511 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
11512 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010011513 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
11514 crtc->num_scalers,
11515 pipe_config->scaler_state.scaler_users,
11516 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011517 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11518 pipe_config->gmch_pfit.control,
11519 pipe_config->gmch_pfit.pgm_ratios,
11520 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011521 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020011522 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011523 pipe_config->pch_pfit.size,
11524 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030011525 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030011526 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011527
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010011528 if (IS_BROXTON(dev)) {
11529 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, "
11530 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
11531 "pll6: 0x%x, pll8: 0x%x, pcsdw12: 0x%x\n",
11532 pipe_config->ddi_pll_sel,
11533 pipe_config->dpll_hw_state.ebb0,
11534 pipe_config->dpll_hw_state.pll0,
11535 pipe_config->dpll_hw_state.pll1,
11536 pipe_config->dpll_hw_state.pll2,
11537 pipe_config->dpll_hw_state.pll3,
11538 pipe_config->dpll_hw_state.pll6,
11539 pipe_config->dpll_hw_state.pll8,
11540 pipe_config->dpll_hw_state.pcsdw12);
11541 } else if (IS_SKYLAKE(dev)) {
11542 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
11543 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
11544 pipe_config->ddi_pll_sel,
11545 pipe_config->dpll_hw_state.ctrl1,
11546 pipe_config->dpll_hw_state.cfgcr1,
11547 pipe_config->dpll_hw_state.cfgcr2);
11548 } else if (HAS_DDI(dev)) {
11549 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
11550 pipe_config->ddi_pll_sel,
11551 pipe_config->dpll_hw_state.wrpll);
11552 } else {
11553 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
11554 "fp0: 0x%x, fp1: 0x%x\n",
11555 pipe_config->dpll_hw_state.dpll,
11556 pipe_config->dpll_hw_state.dpll_md,
11557 pipe_config->dpll_hw_state.fp0,
11558 pipe_config->dpll_hw_state.fp1);
11559 }
11560
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011561 DRM_DEBUG_KMS("planes on this crtc\n");
11562 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
11563 intel_plane = to_intel_plane(plane);
11564 if (intel_plane->pipe != crtc->pipe)
11565 continue;
11566
11567 state = to_intel_plane_state(plane->state);
11568 fb = state->base.fb;
11569 if (!fb) {
11570 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
11571 "disabled, scaler_id = %d\n",
11572 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11573 plane->base.id, intel_plane->pipe,
11574 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
11575 drm_plane_index(plane), state->scaler_id);
11576 continue;
11577 }
11578
11579 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
11580 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11581 plane->base.id, intel_plane->pipe,
11582 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
11583 drm_plane_index(plane));
11584 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
11585 fb->base.id, fb->width, fb->height, fb->pixel_format);
11586 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
11587 state->scaler_id,
11588 state->src.x1 >> 16, state->src.y1 >> 16,
11589 drm_rect_width(&state->src) >> 16,
11590 drm_rect_height(&state->src) >> 16,
11591 state->dst.x1, state->dst.y1,
11592 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
11593 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020011594}
11595
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011596static bool encoders_cloneable(const struct intel_encoder *a,
11597 const struct intel_encoder *b)
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011598{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011599 /* masks could be asymmetric, so check both ways */
11600 return a == b || (a->cloneable & (1 << b->type) &&
11601 b->cloneable & (1 << a->type));
11602}
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011603
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011604static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11605 struct intel_crtc *crtc,
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011606 struct intel_encoder *encoder)
11607{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011608 struct intel_encoder *source_encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011609 struct drm_connector *connector;
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011610 struct drm_connector_state *connector_state;
11611 int i;
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011612
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011613 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011614 if (connector_state->crtc != &crtc->base)
11615 continue;
11616
11617 source_encoder =
11618 to_intel_encoder(connector_state->best_encoder);
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011619 if (!encoders_cloneable(encoder, source_encoder))
11620 return false;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011621 }
11622
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011623 return true;
11624}
11625
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011626static bool check_encoder_cloning(struct drm_atomic_state *state,
11627 struct intel_crtc *crtc)
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011628{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011629 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011630 struct drm_connector *connector;
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011631 struct drm_connector_state *connector_state;
11632 int i;
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011633
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011634 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011635 if (connector_state->crtc != &crtc->base)
11636 continue;
11637
11638 encoder = to_intel_encoder(connector_state->best_encoder);
11639 if (!check_single_encoder_cloning(state, crtc, encoder))
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011640 return false;
11641 }
11642
11643 return true;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011644}
11645
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011646static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011647{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011648 struct drm_device *dev = state->dev;
11649 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011650 struct drm_connector *connector;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011651 struct drm_connector_state *connector_state;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011652 unsigned int used_ports = 0;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011653 int i;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011654
11655 /*
11656 * Walk the connector list instead of the encoder
11657 * list to detect the problem on ddi platforms
11658 * where there's just one encoder per digital port.
11659 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011660 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011661 if (!connector_state->best_encoder)
11662 continue;
11663
11664 encoder = to_intel_encoder(connector_state->best_encoder);
11665
11666 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011667
11668 switch (encoder->type) {
11669 unsigned int port_mask;
11670 case INTEL_OUTPUT_UNKNOWN:
11671 if (WARN_ON(!HAS_DDI(dev)))
11672 break;
11673 case INTEL_OUTPUT_DISPLAYPORT:
11674 case INTEL_OUTPUT_HDMI:
11675 case INTEL_OUTPUT_EDP:
11676 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
11677
11678 /* the same port mustn't appear more than once */
11679 if (used_ports & port_mask)
11680 return false;
11681
11682 used_ports |= port_mask;
11683 default:
11684 break;
11685 }
11686 }
11687
11688 return true;
11689}
11690
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011691static void
11692clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
11693{
11694 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070011695 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011696 struct intel_dpll_hw_state dpll_hw_state;
11697 enum intel_dpll_id shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030011698 uint32_t ddi_pll_sel;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011699
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030011700 /* FIXME: before the switch to atomic started, a new pipe_config was
11701 * kzalloc'd. Code that depends on any field being zero should be
11702 * fixed, so that the crtc_state can be safely duplicated. For now,
11703 * only fields that are know to not cause problems are preserved. */
11704
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011705 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070011706 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011707 shared_dpll = crtc_state->shared_dpll;
11708 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030011709 ddi_pll_sel = crtc_state->ddi_pll_sel;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011710
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011711 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011712
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011713 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070011714 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011715 crtc_state->shared_dpll = shared_dpll;
11716 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030011717 crtc_state->ddi_pll_sel = ddi_pll_sel;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011718}
11719
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011720static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011721intel_modeset_pipe_config(struct drm_crtc *crtc,
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011722 struct drm_atomic_state *state,
11723 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020011724{
Daniel Vetter7758a112012-07-08 19:40:39 +020011725 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011726 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011727 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011728 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011729 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010011730 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020011731
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011732 if (!check_encoder_cloning(state, to_intel_crtc(crtc))) {
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011733 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011734 return -EINVAL;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011735 }
11736
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011737 if (!check_digital_port_conflicts(state)) {
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011738 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011739 return -EINVAL;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011740 }
11741
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011742 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020011743
Daniel Vettere143a212013-07-04 12:01:15 +020011744 pipe_config->cpu_transcoder =
11745 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011746
Imre Deak2960bc92013-07-30 13:36:32 +030011747 /*
11748 * Sanitize sync polarity flags based on requested ones. If neither
11749 * positive or negative polarity is requested, treat this as meaning
11750 * negative polarity.
11751 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011752 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030011753 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011754 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030011755
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011756 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030011757 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011758 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030011759
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011760 /* Compute a starting value for pipe_config->pipe_bpp taking the source
11761 * plane pixel format and any sink constraints into account. Returns the
11762 * source plane bpp so that dithering can be selected on mismatches
11763 * after encoders and crtc also have had their say. */
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011764 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
11765 pipe_config);
11766 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011767 goto fail;
11768
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030011769 /*
11770 * Determine the real pipe dimensions. Note that stereo modes can
11771 * increase the actual pipe size due to the frame doubling and
11772 * insertion of additional space for blanks between the frame. This
11773 * is stored in the crtc timings. We use the requested mode to do this
11774 * computation to clearly distinguish it from the adjusted mode, which
11775 * can be changed by the connectors in the below retry loop.
11776 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011777 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080011778 &pipe_config->pipe_src_w,
11779 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030011780
Daniel Vettere29c22c2013-02-21 00:00:16 +010011781encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020011782 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020011783 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020011784 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020011785
Daniel Vetter135c81b2013-07-21 21:37:09 +020011786 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011787 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
11788 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020011789
Daniel Vetter7758a112012-07-08 19:40:39 +020011790 /* Pass our mode to the connectors and the CRTC to give them a chance to
11791 * adjust it according to limitations or connector properties, and also
11792 * a chance to reject the mode entirely.
11793 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011794 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011795 if (connector_state->crtc != crtc)
11796 continue;
11797
11798 encoder = to_intel_encoder(connector_state->best_encoder);
11799
Daniel Vetterefea6e82013-07-21 21:36:59 +020011800 if (!(encoder->compute_config(encoder, pipe_config))) {
11801 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020011802 goto fail;
11803 }
11804 }
11805
Daniel Vetterff9a6752013-06-01 17:16:21 +020011806 /* Set default port clock if not overwritten by the encoder. Needs to be
11807 * done afterwards in case the encoder adjusts the mode. */
11808 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011809 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010011810 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020011811
Daniel Vettera43f6e02013-06-07 23:10:32 +020011812 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010011813 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020011814 DRM_DEBUG_KMS("CRTC fixup failed\n");
11815 goto fail;
11816 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010011817
11818 if (ret == RETRY) {
11819 if (WARN(!retry, "loop in pipe configuration computation\n")) {
11820 ret = -EINVAL;
11821 goto fail;
11822 }
11823
11824 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
11825 retry = false;
11826 goto encoder_retry;
11827 }
11828
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011829 pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011830 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011831 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011832
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011833 return 0;
Daniel Vetter7758a112012-07-08 19:40:39 +020011834fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011835 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020011836}
11837
Daniel Vetterea9d7582012-07-10 10:42:52 +020011838static bool intel_crtc_in_use(struct drm_crtc *crtc)
11839{
11840 struct drm_encoder *encoder;
11841 struct drm_device *dev = crtc->dev;
11842
11843 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
11844 if (encoder->crtc == crtc)
11845 return true;
11846
11847 return false;
11848}
11849
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011850static bool
11851needs_modeset(struct drm_crtc_state *state)
Daniel Vetterea9d7582012-07-10 10:42:52 +020011852{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011853 return state->mode_changed || state->active_changed;
11854}
11855
11856static void
11857intel_modeset_update_state(struct drm_atomic_state *state)
11858{
11859 struct drm_device *dev = state->dev;
Daniel Vetterba41c0de2014-11-03 15:04:55 +010011860 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011861 struct intel_encoder *intel_encoder;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011862 struct drm_crtc *crtc;
11863 struct drm_crtc_state *crtc_state;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011864 struct drm_connector *connector;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011865 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011866
Daniel Vetterba41c0de2014-11-03 15:04:55 +010011867 intel_shared_dpll_commit(dev_priv);
11868
Damien Lespiaub2784e12014-08-05 11:29:37 +010011869 for_each_intel_encoder(dev, intel_encoder) {
Daniel Vetterea9d7582012-07-10 10:42:52 +020011870 if (!intel_encoder->base.crtc)
11871 continue;
11872
Ander Conselvan de Oliveira9044a812015-05-29 14:28:09 +030011873 for_each_crtc_in_state(state, crtc, crtc_state, i) {
11874 if (crtc != intel_encoder->base.crtc)
11875 continue;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011876
Ander Conselvan de Oliveira9044a812015-05-29 14:28:09 +030011877 if (crtc_state->enable && needs_modeset(crtc_state))
11878 intel_encoder->connectors_active = false;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011879
Ander Conselvan de Oliveira9044a812015-05-29 14:28:09 +030011880 break;
11881 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020011882 }
11883
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011884 drm_atomic_helper_swap_state(state->dev, state);
11885 intel_modeset_fixup_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020011886
Ville Syrjälä76688512014-01-10 11:28:06 +020011887 /* Double check state. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011888 for_each_crtc(dev, crtc) {
11889 WARN_ON(crtc->state->enable != intel_crtc_in_use(crtc));
Daniel Vetterea9d7582012-07-10 10:42:52 +020011890 }
11891
11892 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
11893 if (!connector->encoder || !connector->encoder->crtc)
11894 continue;
11895
Ander Conselvan de Oliveira9044a812015-05-29 14:28:09 +030011896 for_each_crtc_in_state(state, crtc, crtc_state, i) {
11897 if (crtc != connector->encoder->crtc)
11898 continue;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011899
Ander Conselvan de Oliveira9044a812015-05-29 14:28:09 +030011900 if (crtc->state->enable && needs_modeset(crtc->state)) {
11901 struct drm_property *dpms_property =
11902 dev->mode_config.dpms_property;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011903
Ander Conselvan de Oliveira9044a812015-05-29 14:28:09 +030011904 connector->dpms = DRM_MODE_DPMS_ON;
11905 drm_object_property_set_value(&connector->base,
11906 dpms_property,
11907 DRM_MODE_DPMS_ON);
Daniel Vetter68d34722012-09-06 22:08:35 +020011908
Ander Conselvan de Oliveira9044a812015-05-29 14:28:09 +030011909 intel_encoder = to_intel_encoder(connector->encoder);
11910 intel_encoder->connectors_active = true;
11911 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020011912
Ander Conselvan de Oliveira9044a812015-05-29 14:28:09 +030011913 break;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011914 }
11915 }
11916
11917}
11918
Ville Syrjälä3bd26262013-09-06 23:29:02 +030011919static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011920{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030011921 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011922
11923 if (clock1 == clock2)
11924 return true;
11925
11926 if (!clock1 || !clock2)
11927 return false;
11928
11929 diff = abs(clock1 - clock2);
11930
11931 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
11932 return true;
11933
11934 return false;
11935}
11936
Daniel Vetter25c5b262012-07-08 22:08:04 +020011937#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
11938 list_for_each_entry((intel_crtc), \
11939 &(dev)->mode_config.crtc_list, \
11940 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +020011941 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020011942
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011943static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020011944intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011945 struct intel_crtc_state *current_config,
11946 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011947{
Daniel Vetter66e985c2013-06-05 13:34:20 +020011948#define PIPE_CONF_CHECK_X(name) \
11949 if (current_config->name != pipe_config->name) { \
11950 DRM_ERROR("mismatch in " #name " " \
11951 "(expected 0x%08x, found 0x%08x)\n", \
11952 current_config->name, \
11953 pipe_config->name); \
11954 return false; \
11955 }
11956
Daniel Vetter08a24032013-04-19 11:25:34 +020011957#define PIPE_CONF_CHECK_I(name) \
11958 if (current_config->name != pipe_config->name) { \
11959 DRM_ERROR("mismatch in " #name " " \
11960 "(expected %i, found %i)\n", \
11961 current_config->name, \
11962 pipe_config->name); \
11963 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010011964 }
11965
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011966/* This is required for BDW+ where there is only one set of registers for
11967 * switching between high and low RR.
11968 * This macro can be used whenever a comparison has to be made between one
11969 * hw state and multiple sw state variables.
11970 */
11971#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
11972 if ((current_config->name != pipe_config->name) && \
11973 (current_config->alt_name != pipe_config->name)) { \
11974 DRM_ERROR("mismatch in " #name " " \
11975 "(expected %i or %i, found %i)\n", \
11976 current_config->name, \
11977 current_config->alt_name, \
11978 pipe_config->name); \
11979 return false; \
11980 }
11981
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011982#define PIPE_CONF_CHECK_FLAGS(name, mask) \
11983 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -070011984 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011985 "(expected %i, found %i)\n", \
11986 current_config->name & (mask), \
11987 pipe_config->name & (mask)); \
11988 return false; \
11989 }
11990
Ville Syrjälä5e550652013-09-06 23:29:07 +030011991#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
11992 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
11993 DRM_ERROR("mismatch in " #name " " \
11994 "(expected %i, found %i)\n", \
11995 current_config->name, \
11996 pipe_config->name); \
11997 return false; \
11998 }
11999
Daniel Vetterbb760062013-06-06 14:55:52 +020012000#define PIPE_CONF_QUIRK(quirk) \
12001 ((current_config->quirks | pipe_config->quirks) & (quirk))
12002
Daniel Vettereccb1402013-05-22 00:50:22 +020012003 PIPE_CONF_CHECK_I(cpu_transcoder);
12004
Daniel Vetter08a24032013-04-19 11:25:34 +020012005 PIPE_CONF_CHECK_I(has_pch_encoder);
12006 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +020012007 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
12008 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
12009 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
12010 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
12011 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +020012012
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012013 PIPE_CONF_CHECK_I(has_dp_encoder);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012014
12015 if (INTEL_INFO(dev)->gen < 8) {
12016 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
12017 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
12018 PIPE_CONF_CHECK_I(dp_m_n.link_m);
12019 PIPE_CONF_CHECK_I(dp_m_n.link_n);
12020 PIPE_CONF_CHECK_I(dp_m_n.tu);
12021
12022 if (current_config->has_drrs) {
12023 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
12024 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
12025 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
12026 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
12027 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
12028 }
12029 } else {
12030 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
12031 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
12032 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
12033 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
12034 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
12035 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012036
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012037 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12038 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12039 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12040 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12041 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12042 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012043
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012044 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12045 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12046 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12047 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12048 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12049 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012050
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012051 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b52014-04-24 23:54:47 +020012052 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012053 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
12054 IS_VALLEYVIEW(dev))
12055 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012056 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012057
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012058 PIPE_CONF_CHECK_I(has_audio);
12059
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012060 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012061 DRM_MODE_FLAG_INTERLACE);
12062
Daniel Vetterbb760062013-06-06 14:55:52 +020012063 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012064 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012065 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012066 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012067 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012068 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012069 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012070 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012071 DRM_MODE_FLAG_NVSYNC);
12072 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012073
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012074 PIPE_CONF_CHECK_I(pipe_src_w);
12075 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012076
Daniel Vetter99535992014-04-13 12:00:33 +020012077 /*
12078 * FIXME: BIOS likes to set up a cloned config with lvds+external
12079 * screen. Since we don't yet re-compute the pipe config when moving
12080 * just the lvds port away to another pipe the sw tracking won't match.
12081 *
12082 * Proper atomic modesets with recomputed global state will fix this.
12083 * Until then just don't check gmch state for inherited modes.
12084 */
12085 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
12086 PIPE_CONF_CHECK_I(gmch_pfit.control);
12087 /* pfit ratios are autocomputed by the hw on gen4+ */
12088 if (INTEL_INFO(dev)->gen < 4)
12089 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
12090 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
12091 }
12092
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012093 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12094 if (current_config->pch_pfit.enabled) {
12095 PIPE_CONF_CHECK_I(pch_pfit.pos);
12096 PIPE_CONF_CHECK_I(pch_pfit.size);
12097 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012098
Chandra Kondurua1b22782015-04-07 15:28:45 -070012099 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12100
Jesse Barnese59150d2014-01-07 13:30:45 -080012101 /* BDW+ don't expose a synchronous way to read the state */
12102 if (IS_HASWELL(dev))
12103 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012104
Ville Syrjälä282740f2013-09-04 18:30:03 +030012105 PIPE_CONF_CHECK_I(double_wide);
12106
Daniel Vetter26804af2014-06-25 22:01:55 +030012107 PIPE_CONF_CHECK_X(ddi_pll_sel);
12108
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012109 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012110 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012111 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012112 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12113 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012114 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012115 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12116 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12117 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012118
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012119 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12120 PIPE_CONF_CHECK_I(pipe_bpp);
12121
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012122 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012123 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012124
Daniel Vetter66e985c2013-06-05 13:34:20 +020012125#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012126#undef PIPE_CONF_CHECK_I
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012127#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012128#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012129#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012130#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012131
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012132 return true;
12133}
12134
Damien Lespiau08db6652014-11-04 17:06:52 +000012135static void check_wm_state(struct drm_device *dev)
12136{
12137 struct drm_i915_private *dev_priv = dev->dev_private;
12138 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12139 struct intel_crtc *intel_crtc;
12140 int plane;
12141
12142 if (INTEL_INFO(dev)->gen < 9)
12143 return;
12144
12145 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12146 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12147
12148 for_each_intel_crtc(dev, intel_crtc) {
12149 struct skl_ddb_entry *hw_entry, *sw_entry;
12150 const enum pipe pipe = intel_crtc->pipe;
12151
12152 if (!intel_crtc->active)
12153 continue;
12154
12155 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000012156 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012157 hw_entry = &hw_ddb.plane[pipe][plane];
12158 sw_entry = &sw_ddb->plane[pipe][plane];
12159
12160 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12161 continue;
12162
12163 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12164 "(expected (%u,%u), found (%u,%u))\n",
12165 pipe_name(pipe), plane + 1,
12166 sw_entry->start, sw_entry->end,
12167 hw_entry->start, hw_entry->end);
12168 }
12169
12170 /* cursor */
12171 hw_entry = &hw_ddb.cursor[pipe];
12172 sw_entry = &sw_ddb->cursor[pipe];
12173
12174 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12175 continue;
12176
12177 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12178 "(expected (%u,%u), found (%u,%u))\n",
12179 pipe_name(pipe),
12180 sw_entry->start, sw_entry->end,
12181 hw_entry->start, hw_entry->end);
12182 }
12183}
12184
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012185static void
12186check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012187{
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012188 struct intel_connector *connector;
12189
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012190 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012191 /* This also checks the encoder/connector hw state with the
12192 * ->get_hw_state callbacks. */
12193 intel_connector_check_state(connector);
12194
Rob Clarke2c719b2014-12-15 13:56:32 -050012195 I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012196 "connector's staged encoder doesn't match current encoder\n");
12197 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012198}
12199
12200static void
12201check_encoder_state(struct drm_device *dev)
12202{
12203 struct intel_encoder *encoder;
12204 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012205
Damien Lespiaub2784e12014-08-05 11:29:37 +010012206 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012207 bool enabled = false;
12208 bool active = false;
12209 enum pipe pipe, tracked_pipe;
12210
12211 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12212 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012213 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012214
Rob Clarke2c719b2014-12-15 13:56:32 -050012215 I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012216 "encoder's stage crtc doesn't match current crtc\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012217 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012218 "encoder's active_connectors set, but no crtc\n");
12219
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012220 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012221 if (connector->base.encoder != &encoder->base)
12222 continue;
12223 enabled = true;
12224 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
12225 active = true;
12226 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012227 /*
12228 * for MST connectors if we unplug the connector is gone
12229 * away but the encoder is still connected to a crtc
12230 * until a modeset happens in response to the hotplug.
12231 */
12232 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
12233 continue;
12234
Rob Clarke2c719b2014-12-15 13:56:32 -050012235 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012236 "encoder's enabled state mismatch "
12237 "(expected %i, found %i)\n",
12238 !!encoder->base.crtc, enabled);
Rob Clarke2c719b2014-12-15 13:56:32 -050012239 I915_STATE_WARN(active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012240 "active encoder with no crtc\n");
12241
Rob Clarke2c719b2014-12-15 13:56:32 -050012242 I915_STATE_WARN(encoder->connectors_active != active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012243 "encoder's computed active state doesn't match tracked active state "
12244 "(expected %i, found %i)\n", active, encoder->connectors_active);
12245
12246 active = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -050012247 I915_STATE_WARN(active != encoder->connectors_active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012248 "encoder's hw state doesn't match sw tracking "
12249 "(expected %i, found %i)\n",
12250 encoder->connectors_active, active);
12251
12252 if (!encoder->base.crtc)
12253 continue;
12254
12255 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
Rob Clarke2c719b2014-12-15 13:56:32 -050012256 I915_STATE_WARN(active && pipe != tracked_pipe,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012257 "active encoder's pipe doesn't match"
12258 "(expected %i, found %i)\n",
12259 tracked_pipe, pipe);
12260
12261 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012262}
12263
12264static void
12265check_crtc_state(struct drm_device *dev)
12266{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012267 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012268 struct intel_crtc *crtc;
12269 struct intel_encoder *encoder;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012270 struct intel_crtc_state pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012271
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012272 for_each_intel_crtc(dev, crtc) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012273 bool enabled = false;
12274 bool active = false;
12275
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012276 memset(&pipe_config, 0, sizeof(pipe_config));
12277
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012278 DRM_DEBUG_KMS("[CRTC:%d]\n",
12279 crtc->base.base.id);
12280
Matt Roper83d65732015-02-25 13:12:16 -080012281 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012282 "active crtc, but not enabled in sw tracking\n");
12283
Damien Lespiaub2784e12014-08-05 11:29:37 +010012284 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012285 if (encoder->base.crtc != &crtc->base)
12286 continue;
12287 enabled = true;
12288 if (encoder->connectors_active)
12289 active = true;
12290 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020012291
Rob Clarke2c719b2014-12-15 13:56:32 -050012292 I915_STATE_WARN(active != crtc->active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012293 "crtc's computed active state doesn't match tracked active state "
12294 "(expected %i, found %i)\n", active, crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080012295 I915_STATE_WARN(enabled != crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012296 "crtc's computed enabled state doesn't match tracked enabled state "
Matt Roper83d65732015-02-25 13:12:16 -080012297 "(expected %i, found %i)\n", enabled,
12298 crtc->base.state->enable);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012299
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012300 active = dev_priv->display.get_pipe_config(crtc,
12301 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +020012302
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030012303 /* hw state is inconsistent with the pipe quirk */
12304 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12305 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetterd62cf622013-05-29 10:41:29 +020012306 active = crtc->active;
12307
Damien Lespiaub2784e12014-08-05 11:29:37 +010012308 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +030012309 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +020012310 if (encoder->base.crtc != &crtc->base)
12311 continue;
Daniel Vetter1d37b682013-11-18 09:00:59 +010012312 if (encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +020012313 encoder->get_config(encoder, &pipe_config);
12314 }
12315
Rob Clarke2c719b2014-12-15 13:56:32 -050012316 I915_STATE_WARN(crtc->active != active,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012317 "crtc active state doesn't match with hw state "
12318 "(expected %i, found %i)\n", crtc->active, active);
12319
Daniel Vetterc0b03412013-05-28 12:05:54 +020012320 if (active &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012321 !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050012322 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Daniel Vetterc0b03412013-05-28 12:05:54 +020012323 intel_dump_pipe_config(crtc, &pipe_config,
12324 "[hw state]");
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012325 intel_dump_pipe_config(crtc, crtc->config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012326 "[sw state]");
12327 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012328 }
12329}
12330
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012331static void
12332check_shared_dpll_state(struct drm_device *dev)
12333{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012334 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012335 struct intel_crtc *crtc;
12336 struct intel_dpll_hw_state dpll_hw_state;
12337 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020012338
12339 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12340 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12341 int enabled_crtcs = 0, active_crtcs = 0;
12342 bool active;
12343
12344 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12345
12346 DRM_DEBUG_KMS("%s\n", pll->name);
12347
12348 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
12349
Rob Clarke2c719b2014-12-15 13:56:32 -050012350 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
Daniel Vetter53589012013-06-05 13:34:16 +020012351 "more active pll users than references: %i vs %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012352 pll->active, hweight32(pll->config.crtc_mask));
Rob Clarke2c719b2014-12-15 13:56:32 -050012353 I915_STATE_WARN(pll->active && !pll->on,
Daniel Vetter53589012013-06-05 13:34:16 +020012354 "pll in active use but not on in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012355 I915_STATE_WARN(pll->on && !pll->active,
Daniel Vetter35c95372013-07-17 06:55:04 +020012356 "pll in on but not on in use in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012357 I915_STATE_WARN(pll->on != active,
Daniel Vetter53589012013-06-05 13:34:16 +020012358 "pll on state mismatch (expected %i, found %i)\n",
12359 pll->on, active);
12360
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012361 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080012362 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
Daniel Vetter53589012013-06-05 13:34:16 +020012363 enabled_crtcs++;
12364 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12365 active_crtcs++;
12366 }
Rob Clarke2c719b2014-12-15 13:56:32 -050012367 I915_STATE_WARN(pll->active != active_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012368 "pll active crtcs mismatch (expected %i, found %i)\n",
12369 pll->active, active_crtcs);
Rob Clarke2c719b2014-12-15 13:56:32 -050012370 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012371 "pll enabled crtcs mismatch (expected %i, found %i)\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012372 hweight32(pll->config.crtc_mask), enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012373
Rob Clarke2c719b2014-12-15 13:56:32 -050012374 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020012375 sizeof(dpll_hw_state)),
12376 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020012377 }
Daniel Vettere2e1ed42012-07-08 21:14:38 +020012378}
12379
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012380void
12381intel_modeset_check_state(struct drm_device *dev)
12382{
Damien Lespiau08db6652014-11-04 17:06:52 +000012383 check_wm_state(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012384 check_connector_state(dev);
12385 check_encoder_state(dev);
12386 check_crtc_state(dev);
12387 check_shared_dpll_state(dev);
12388}
12389
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012390void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
Ville Syrjälä18442d02013-09-13 16:00:08 +030012391 int dotclock)
12392{
12393 /*
12394 * FDI already provided one idea for the dotclock.
12395 * Yell if the encoder disagrees.
12396 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012397 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030012398 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012399 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030012400}
12401
Ville Syrjälä80715b22014-05-15 20:23:23 +030012402static void update_scanline_offset(struct intel_crtc *crtc)
12403{
12404 struct drm_device *dev = crtc->base.dev;
12405
12406 /*
12407 * The scanline counter increments at the leading edge of hsync.
12408 *
12409 * On most platforms it starts counting from vtotal-1 on the
12410 * first active line. That means the scanline counter value is
12411 * always one less than what we would expect. Ie. just after
12412 * start of vblank, which also occurs at start of hsync (on the
12413 * last active line), the scanline counter will read vblank_start-1.
12414 *
12415 * On gen2 the scanline counter starts counting from 1 instead
12416 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12417 * to keep the value positive), instead of adding one.
12418 *
12419 * On HSW+ the behaviour of the scanline counter depends on the output
12420 * type. For DP ports it behaves like most other platforms, but on HDMI
12421 * there's an extra 1 line difference. So we need to add two instead of
12422 * one to the value.
12423 */
12424 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012425 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012426 int vtotal;
12427
12428 vtotal = mode->crtc_vtotal;
12429 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
12430 vtotal /= 2;
12431
12432 crtc->scanline_offset = vtotal - 1;
12433 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030012434 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030012435 crtc->scanline_offset = 2;
12436 } else
12437 crtc->scanline_offset = 1;
12438}
12439
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012440static struct intel_crtc_state *
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012441intel_modeset_compute_config(struct drm_crtc *crtc,
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012442 struct drm_atomic_state *state)
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012443{
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012444 struct intel_crtc_state *pipe_config;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012445 int ret = 0;
12446
12447 ret = drm_atomic_add_affected_connectors(state, crtc);
12448 if (ret)
12449 return ERR_PTR(ret);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012450
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012451 ret = drm_atomic_helper_check_modeset(state->dev, state);
12452 if (ret)
12453 return ERR_PTR(ret);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012454
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012455 /*
12456 * Note this needs changes when we start tracking multiple modes
12457 * and crtcs. At that point we'll need to compute the whole config
12458 * (i.e. one pipe_config for each crtc) rather than just the one
12459 * for this crtc.
12460 */
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012461 pipe_config = intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));
12462 if (IS_ERR(pipe_config))
12463 return pipe_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012464
Ander Conselvan de Oliveira4fed33f2015-04-21 17:13:03 +030012465 if (!pipe_config->base.enable)
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012466 return pipe_config;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012467
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012468 ret = intel_modeset_pipe_config(crtc, state, pipe_config);
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012469 if (ret)
12470 return ERR_PTR(ret);
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020012471
Ander Conselvan de Oliveira8d8c9b52015-04-21 17:13:11 +030012472 /* Check things that can only be changed through modeset */
12473 if (pipe_config->has_audio !=
12474 to_intel_crtc(crtc)->config->has_audio)
12475 pipe_config->base.mode_changed = true;
12476
12477 /*
12478 * Note we have an issue here with infoframes: current code
12479 * only updates them on the full mode set path per hw
12480 * requirements. So here we should be checking for any
12481 * required changes and forcing a mode set.
12482 */
12483
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012484 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,"[modeset]");
12485
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012486 ret = drm_atomic_helper_check_planes(state->dev, state);
12487 if (ret)
12488 return ERR_PTR(ret);
12489
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012490 return pipe_config;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012491}
12492
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012493static int __intel_set_mode_setup_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012494{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012495 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012496 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012497 unsigned clear_pipes = 0;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012498 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012499 struct intel_crtc_state *intel_crtc_state;
12500 struct drm_crtc *crtc;
12501 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012502 int ret = 0;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012503 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012504
12505 if (!dev_priv->display.crtc_compute_clock)
12506 return 0;
12507
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012508 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12509 intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012510 intel_crtc_state = to_intel_crtc_state(crtc_state);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012511
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012512 if (needs_modeset(crtc_state)) {
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012513 clear_pipes |= 1 << intel_crtc->pipe;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012514 intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012515 }
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012516 }
12517
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012518 ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
12519 if (ret)
12520 goto done;
12521
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012522 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12523 if (!needs_modeset(crtc_state) || !crtc_state->enable)
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012524 continue;
12525
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012526 intel_crtc = to_intel_crtc(crtc);
12527 intel_crtc_state = to_intel_crtc_state(crtc_state);
12528
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012529 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012530 intel_crtc_state);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012531 if (ret) {
12532 intel_shared_dpll_abort_config(dev_priv);
12533 goto done;
12534 }
12535 }
12536
12537done:
12538 return ret;
12539}
12540
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012541/* Code that should eventually be part of atomic_check() */
12542static int __intel_set_mode_checks(struct drm_atomic_state *state)
12543{
12544 struct drm_device *dev = state->dev;
12545 int ret;
12546
12547 /*
12548 * See if the config requires any additional preparation, e.g.
12549 * to adjust global state with pipes off. We need to do this
12550 * here so we can get the modeset_pipe updated config for the new
12551 * mode set on this crtc. For other crtcs we need to use the
12552 * adjusted_mode bits in the crtc directly.
12553 */
12554 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
12555 ret = valleyview_modeset_global_pipes(state);
12556 if (ret)
12557 return ret;
12558 }
12559
12560 ret = __intel_set_mode_setup_plls(state);
12561 if (ret)
12562 return ret;
12563
12564 return 0;
12565}
12566
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012567static int __intel_set_mode(struct drm_crtc *modeset_crtc,
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012568 struct intel_crtc_state *pipe_config)
Daniel Vettera6778b32012-07-02 09:56:42 +020012569{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012570 struct drm_device *dev = modeset_crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030012571 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030012572 struct drm_atomic_state *state = pipe_config->base.state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012573 struct drm_crtc *crtc;
12574 struct drm_crtc_state *crtc_state;
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012575 int ret = 0;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012576 int i;
Daniel Vettera6778b32012-07-02 09:56:42 +020012577
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012578 ret = __intel_set_mode_checks(state);
12579 if (ret < 0)
12580 return ret;
12581
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030012582 ret = drm_atomic_helper_prepare_planes(dev, state);
12583 if (ret)
12584 return ret;
12585
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012586 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12587 if (!needs_modeset(crtc_state))
12588 continue;
Daniel Vetter460da9162013-03-27 00:44:51 +010012589
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012590 if (!crtc_state->enable) {
12591 intel_crtc_disable(crtc);
12592 } else if (crtc->state->enable) {
12593 intel_crtc_disable_planes(crtc);
12594 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorstce22dba2015-04-21 17:12:56 +030012595 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012596 }
Daniel Vettera6778b32012-07-02 09:56:42 +020012597
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020012598 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
12599 * to set it here already despite that we pass it down the callchain.
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012600 *
12601 * Note we'll need to fix this up when we start tracking multiple
12602 * pipes; here we assume a single modeset_pipe and only track the
12603 * single crtc and mode.
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020012604 */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012605 if (pipe_config->base.enable && needs_modeset(&pipe_config->base)) {
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012606 modeset_crtc->mode = pipe_config->base.mode;
Ville Syrjäläc326c0a2013-10-28 12:53:41 +020012607
12608 /*
12609 * Calculate and store various constants which
12610 * are later needed by vblank and swap-completion
12611 * timestamping. They are derived from true hwmode.
12612 */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012613 drm_calc_timestamping_constants(modeset_crtc,
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012614 &pipe_config->base.adjusted_mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012615 }
Daniel Vetter7758a112012-07-08 19:40:39 +020012616
Daniel Vetterea9d7582012-07-10 10:42:52 +020012617 /* Only after disabling all output pipelines that will be changed can we
12618 * update the the output configuration. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012619 intel_modeset_update_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020012620
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030012621 /* The state has been swaped above, so state actually contains the
12622 * old state now. */
12623
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030012624 modeset_update_crtc_power_domains(state);
Daniel Vetter47fab732012-10-26 10:58:18 +020012625
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030012626 drm_atomic_helper_commit_planes(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020012627
12628 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012629 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030012630 if (!needs_modeset(crtc->state) || !crtc->state->enable)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012631 continue;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012632
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012633 update_scanline_offset(to_intel_crtc(crtc));
12634
12635 dev_priv->display.crtc_enable(crtc);
12636 intel_crtc_enable_planes(crtc);
Ville Syrjälä80715b22014-05-15 20:23:23 +030012637 }
Daniel Vettera6778b32012-07-02 09:56:42 +020012638
Daniel Vettera6778b32012-07-02 09:56:42 +020012639 /* FIXME: add subpixel order */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012640
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030012641 drm_atomic_helper_cleanup_planes(dev, state);
12642
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030012643 drm_atomic_state_free(state);
12644
Ander Conselvan de Oliveira9eb45f22015-04-21 17:13:07 +030012645 return 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020012646}
12647
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012648static int intel_set_mode_with_config(struct drm_crtc *crtc,
Ander Conselvan de Oliveirae62d8dc2015-06-16 11:49:43 +030012649 struct intel_crtc_state *pipe_config,
12650 bool force_restore)
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012651{
12652 int ret;
12653
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012654 ret = __intel_set_mode(crtc, pipe_config);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012655
Ander Conselvan de Oliveira0d26fb82015-06-16 11:49:44 +030012656 if (ret == 0 && force_restore) {
12657 intel_modeset_update_staged_output_state(crtc->dev);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012658 intel_modeset_check_state(crtc->dev);
Ander Conselvan de Oliveira0d26fb82015-06-16 11:49:44 +030012659 }
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012660
12661 return ret;
12662}
12663
Damien Lespiaue7457a92013-08-08 22:28:59 +010012664static int intel_set_mode(struct drm_crtc *crtc,
Ander Conselvan de Oliveirae62d8dc2015-06-16 11:49:43 +030012665 struct drm_atomic_state *state,
12666 bool force_restore)
Daniel Vetterf30da182013-04-11 20:22:50 +020012667{
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012668 struct intel_crtc_state *pipe_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012669 int ret = 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020012670
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012671 pipe_config = intel_modeset_compute_config(crtc, state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012672 if (IS_ERR(pipe_config)) {
12673 ret = PTR_ERR(pipe_config);
12674 goto out;
12675 }
Daniel Vetterf30da182013-04-11 20:22:50 +020012676
Ander Conselvan de Oliveirae62d8dc2015-06-16 11:49:43 +030012677 ret = intel_set_mode_with_config(crtc, pipe_config, force_restore);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012678 if (ret)
12679 goto out;
12680
12681out:
12682 return ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020012683}
12684
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012685void intel_crtc_restore_mode(struct drm_crtc *crtc)
12686{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012687 struct drm_device *dev = crtc->dev;
12688 struct drm_atomic_state *state;
12689 struct intel_encoder *encoder;
12690 struct intel_connector *connector;
12691 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030012692 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030012693 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012694
12695 state = drm_atomic_state_alloc(dev);
12696 if (!state) {
12697 DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
12698 crtc->base.id);
12699 return;
12700 }
12701
12702 state->acquire_ctx = dev->mode_config.acquire_ctx;
12703
12704 /* The force restore path in the HW readout code relies on the staged
12705 * config still keeping the user requested config while the actual
12706 * state has been overwritten by the configuration read from HW. We
12707 * need to copy the staged config to the atomic state, otherwise the
12708 * mode set will just reapply the state the HW is already in. */
12709 for_each_intel_encoder(dev, encoder) {
12710 if (&encoder->new_crtc->base != crtc)
12711 continue;
12712
12713 for_each_intel_connector(dev, connector) {
12714 if (connector->new_encoder != encoder)
12715 continue;
12716
12717 connector_state = drm_atomic_get_connector_state(state, &connector->base);
12718 if (IS_ERR(connector_state)) {
12719 DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
12720 connector->base.base.id,
12721 connector->base.name,
12722 PTR_ERR(connector_state));
12723 continue;
12724 }
12725
12726 connector_state->crtc = crtc;
12727 connector_state->best_encoder = &encoder->base;
12728 }
12729 }
12730
Ander Conselvan de Oliveira4ed9fb32015-06-16 11:49:45 +030012731 crtc_state = intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));
12732 if (IS_ERR(crtc_state)) {
12733 DRM_DEBUG_KMS("Failed to add [CRTC:%d] to state: %ld\n",
12734 crtc->base.id, PTR_ERR(crtc_state));
12735 drm_atomic_state_free(state);
12736 return;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030012737 }
12738
Ander Conselvan de Oliveira4ed9fb32015-06-16 11:49:45 +030012739 crtc_state->base.active = crtc_state->base.enable =
12740 to_intel_crtc(crtc)->new_enabled;
12741
12742 drm_mode_copy(&crtc_state->base.mode, &crtc->mode);
12743
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030012744 intel_modeset_setup_plane_state(state, crtc, &crtc->mode,
12745 crtc->primary->fb, crtc->x, crtc->y);
12746
Ander Conselvan de Oliveirae62d8dc2015-06-16 11:49:43 +030012747 ret = intel_set_mode(crtc, state, false);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030012748 if (ret)
12749 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012750}
12751
Daniel Vetter25c5b262012-07-08 22:08:04 +020012752#undef for_each_intel_crtc_masked
12753
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030012754static bool intel_connector_in_mode_set(struct intel_connector *connector,
12755 struct drm_mode_set *set)
12756{
12757 int ro;
12758
12759 for (ro = 0; ro < set->num_connectors; ro++)
12760 if (set->connectors[ro] == &connector->base)
12761 return true;
12762
12763 return false;
12764}
12765
Daniel Vetter2e431052012-07-04 22:42:15 +020012766static int
Daniel Vetter9a935852012-07-05 22:34:27 +020012767intel_modeset_stage_output_state(struct drm_device *dev,
12768 struct drm_mode_set *set,
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012769 struct drm_atomic_state *state)
Daniel Vetter50f56112012-07-02 09:35:43 +020012770{
Daniel Vetter9a935852012-07-05 22:34:27 +020012771 struct intel_connector *connector;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012772 struct drm_connector *drm_connector;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012773 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012774 struct drm_crtc *crtc;
12775 struct drm_crtc_state *crtc_state;
12776 int i, ret;
Daniel Vetter50f56112012-07-02 09:35:43 +020012777
Damien Lespiau9abdda72013-02-13 13:29:23 +000012778 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +020012779 * of connectors. For paranoia, double-check this. */
12780 WARN_ON(!set->fb && (set->num_connectors != 0));
12781 WARN_ON(set->fb && (set->num_connectors == 0));
12782
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012783 for_each_intel_connector(dev, connector) {
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030012784 bool in_mode_set = intel_connector_in_mode_set(connector, set);
12785
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012786 if (!in_mode_set && connector->base.state->crtc != set->crtc)
12787 continue;
12788
12789 connector_state =
12790 drm_atomic_get_connector_state(state, &connector->base);
12791 if (IS_ERR(connector_state))
12792 return PTR_ERR(connector_state);
12793
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030012794 if (in_mode_set) {
12795 int pipe = to_intel_crtc(set->crtc)->pipe;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012796 connector_state->best_encoder =
12797 &intel_find_encoder(connector, pipe)->base;
Daniel Vetter50f56112012-07-02 09:35:43 +020012798 }
12799
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012800 if (connector->base.state->crtc != set->crtc)
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030012801 continue;
12802
Daniel Vetter9a935852012-07-05 22:34:27 +020012803 /* If we disable the crtc, disable all its connectors. Also, if
12804 * the connector is on the changing crtc but not on the new
12805 * connector list, disable it. */
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030012806 if (!set->fb || !in_mode_set) {
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012807 connector_state->best_encoder = NULL;
Daniel Vetter9a935852012-07-05 22:34:27 +020012808
12809 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
12810 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012811 connector->base.name);
Daniel Vetter9a935852012-07-05 22:34:27 +020012812 }
Daniel Vetter9a935852012-07-05 22:34:27 +020012813 }
12814 /* connector->new_encoder is now updated for all connectors. */
12815
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012816 for_each_connector_in_state(state, drm_connector, connector_state, i) {
12817 connector = to_intel_connector(drm_connector);
Ville Syrjälä76688512014-01-10 11:28:06 +020012818
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012819 if (!connector_state->best_encoder) {
12820 ret = drm_atomic_set_crtc_for_connector(connector_state,
12821 NULL);
12822 if (ret)
12823 return ret;
12824
Daniel Vetter50f56112012-07-02 09:35:43 +020012825 continue;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012826 }
Daniel Vetter50f56112012-07-02 09:35:43 +020012827
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012828 if (intel_connector_in_mode_set(connector, set)) {
12829 struct drm_crtc *crtc = connector->base.state->crtc;
12830
12831 /* If this connector was in a previous crtc, add it
12832 * to the state. We might need to disable it. */
12833 if (crtc) {
12834 crtc_state =
12835 drm_atomic_get_crtc_state(state, crtc);
12836 if (IS_ERR(crtc_state))
12837 return PTR_ERR(crtc_state);
12838 }
12839
12840 ret = drm_atomic_set_crtc_for_connector(connector_state,
12841 set->crtc);
12842 if (ret)
12843 return ret;
12844 }
Daniel Vetter50f56112012-07-02 09:35:43 +020012845
12846 /* Make sure the new CRTC will work with the encoder */
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012847 if (!drm_encoder_crtc_ok(connector_state->best_encoder,
12848 connector_state->crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012849 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +020012850 }
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012851
Daniel Vetter9a935852012-07-05 22:34:27 +020012852 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
12853 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012854 connector->base.name,
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012855 connector_state->crtc->base.id);
12856
12857 if (connector_state->best_encoder != &connector->encoder->base)
12858 connector->encoder =
12859 to_intel_encoder(connector_state->best_encoder);
Daniel Vetter9a935852012-07-05 22:34:27 +020012860 }
12861
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012862 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020012863 bool has_connectors;
12864
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012865 ret = drm_atomic_add_affected_connectors(state, crtc);
12866 if (ret)
12867 return ret;
Paulo Zanoni5a65f352014-01-07 14:55:53 -020012868
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020012869 has_connectors = !!drm_atomic_connectors_for_crtc(state, crtc);
12870 if (has_connectors != crtc_state->enable)
12871 crtc_state->enable =
12872 crtc_state->active = has_connectors;
Ville Syrjälä76688512014-01-10 11:28:06 +020012873 }
12874
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012875 ret = intel_modeset_setup_plane_state(state, set->crtc, set->mode,
12876 set->fb, set->x, set->y);
12877 if (ret)
12878 return ret;
12879
12880 crtc_state = drm_atomic_get_crtc_state(state, set->crtc);
12881 if (IS_ERR(crtc_state))
12882 return PTR_ERR(crtc_state);
12883
12884 if (set->mode)
12885 drm_mode_copy(&crtc_state->mode, set->mode);
12886
12887 if (set->num_connectors)
12888 crtc_state->active = true;
12889
Daniel Vetter2e431052012-07-04 22:42:15 +020012890 return 0;
12891}
12892
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030012893static bool primary_plane_visible(struct drm_crtc *crtc)
12894{
12895 struct intel_plane_state *plane_state =
12896 to_intel_plane_state(crtc->primary->state);
12897
12898 return plane_state->visible;
12899}
12900
Daniel Vetter2e431052012-07-04 22:42:15 +020012901static int intel_crtc_set_config(struct drm_mode_set *set)
12902{
12903 struct drm_device *dev;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012904 struct drm_atomic_state *state = NULL;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012905 struct intel_crtc_state *pipe_config;
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030012906 bool primary_plane_was_visible;
Daniel Vetter2e431052012-07-04 22:42:15 +020012907 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +020012908
Daniel Vetter8d3e3752012-07-05 16:09:09 +020012909 BUG_ON(!set);
12910 BUG_ON(!set->crtc);
12911 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +020012912
Daniel Vetter7e53f3a2013-01-21 10:52:17 +010012913 /* Enforce sane interface api - has been abused by the fb helper. */
12914 BUG_ON(!set->mode && set->fb);
12915 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +020012916
Daniel Vetter2e431052012-07-04 22:42:15 +020012917 if (set->fb) {
12918 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
12919 set->crtc->base.id, set->fb->base.id,
12920 (int)set->num_connectors, set->x, set->y);
12921 } else {
12922 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +020012923 }
12924
12925 dev = set->crtc->dev;
12926
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012927 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030012928 if (!state)
12929 return -ENOMEM;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012930
12931 state->acquire_ctx = dev->mode_config.acquire_ctx;
12932
Ander Conselvan de Oliveira462a4252015-04-21 17:13:00 +030012933 ret = intel_modeset_stage_output_state(dev, set, state);
Daniel Vetter2e431052012-07-04 22:42:15 +020012934 if (ret)
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030012935 goto out;
Daniel Vetter2e431052012-07-04 22:42:15 +020012936
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012937 pipe_config = intel_modeset_compute_config(set->crtc, state);
Jesse Barnes20664592014-11-05 14:26:09 -080012938 if (IS_ERR(pipe_config)) {
Matt Roper6ac04832014-11-17 09:59:28 -080012939 ret = PTR_ERR(pipe_config);
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030012940 goto out;
Jesse Barnes20664592014-11-05 14:26:09 -080012941 }
Jesse Barnes50f52752014-11-07 13:11:00 -080012942
Jesse Barnes1f9954d2014-11-05 14:26:10 -080012943 intel_update_pipe_size(to_intel_crtc(set->crtc));
12944
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030012945 primary_plane_was_visible = primary_plane_visible(set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070012946
Ander Conselvan de Oliveirae62d8dc2015-06-16 11:49:43 +030012947 ret = intel_set_mode_with_config(set->crtc, pipe_config, true);
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030012948
12949 if (ret == 0 &&
12950 pipe_config->base.enable &&
12951 pipe_config->base.planes_changed &&
12952 !needs_modeset(&pipe_config->base)) {
12953 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070012954
12955 /*
12956 * We need to make sure the primary plane is re-enabled if it
12957 * has previously been turned off.
12958 */
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030012959 if (ret == 0 && !primary_plane_was_visible &&
12960 primary_plane_visible(set->crtc)) {
Matt Roper3b150f02014-05-29 08:06:53 -070012961 WARN_ON(!intel_crtc->active);
Maarten Lankhorst87d43002015-04-21 17:12:54 +030012962 intel_post_enable_primary(set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070012963 }
12964
Jesse Barnes7ca51a32014-01-07 13:50:49 -080012965 /*
12966 * In the fastboot case this may be our only check of the
12967 * state after boot. It would be better to only do it on
12968 * the first update, but we don't have a nice way of doing that
12969 * (and really, set_config isn't used much for high freq page
12970 * flipping, so increasing its cost here shouldn't be a big
12971 * deal).
12972 */
Jani Nikulad330a952014-01-21 11:24:25 +020012973 if (i915.fastboot && ret == 0)
Jesse Barnes7ca51a32014-01-07 13:50:49 -080012974 intel_modeset_check_state(set->crtc->dev);
Daniel Vetter50f56112012-07-02 09:35:43 +020012975 }
12976
Chris Wilson2d05eae2013-05-03 17:36:25 +010012977 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +020012978 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
12979 set->crtc->base.id, ret);
Chris Wilson2d05eae2013-05-03 17:36:25 +010012980 }
Daniel Vetter50f56112012-07-02 09:35:43 +020012981
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030012982out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030012983 if (ret)
12984 drm_atomic_state_free(state);
Daniel Vetter50f56112012-07-02 09:35:43 +020012985 return ret;
12986}
12987
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012988static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012989 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +020012990 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012991 .destroy = intel_crtc_destroy,
12992 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080012993 .atomic_duplicate_state = intel_crtc_duplicate_state,
12994 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012995};
12996
Daniel Vetter53589012013-06-05 13:34:16 +020012997static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
12998 struct intel_shared_dpll *pll,
12999 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013000{
Daniel Vetter53589012013-06-05 13:34:16 +020013001 uint32_t val;
13002
Daniel Vetterf458ebb2014-09-30 10:56:39 +020013003 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030013004 return false;
13005
Daniel Vetter53589012013-06-05 13:34:16 +020013006 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020013007 hw_state->dpll = val;
13008 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
13009 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020013010
13011 return val & DPLL_VCO_ENABLE;
13012}
13013
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013014static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
13015 struct intel_shared_dpll *pll)
13016{
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013017 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
13018 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013019}
13020
Daniel Vettere7b903d2013-06-05 13:34:14 +020013021static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
13022 struct intel_shared_dpll *pll)
13023{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013024 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020013025 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020013026
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013027 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013028
13029 /* Wait for the clocks to stabilize. */
13030 POSTING_READ(PCH_DPLL(pll->id));
13031 udelay(150);
13032
13033 /* The pixel multiplier can only be updated once the
13034 * DPLL is enabled and the clocks are stable.
13035 *
13036 * So write it again.
13037 */
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013038 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013039 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013040 udelay(200);
13041}
13042
13043static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
13044 struct intel_shared_dpll *pll)
13045{
13046 struct drm_device *dev = dev_priv->dev;
13047 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013048
13049 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013050 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020013051 if (intel_crtc_to_shared_dpll(crtc) == pll)
13052 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
13053 }
13054
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013055 I915_WRITE(PCH_DPLL(pll->id), 0);
13056 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013057 udelay(200);
13058}
13059
Daniel Vetter46edb022013-06-05 13:34:12 +020013060static char *ibx_pch_dpll_names[] = {
13061 "PCH DPLL A",
13062 "PCH DPLL B",
13063};
13064
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013065static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013066{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013067 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013068 int i;
13069
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013070 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013071
Daniel Vettere72f9fb2013-06-05 13:34:06 +020013072 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020013073 dev_priv->shared_dplls[i].id = i;
13074 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013075 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013076 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
13077 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020013078 dev_priv->shared_dplls[i].get_hw_state =
13079 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013080 }
13081}
13082
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013083static void intel_shared_dpll_init(struct drm_device *dev)
13084{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013085 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013086
Daniel Vetter9cd86932014-06-25 22:01:57 +030013087 if (HAS_DDI(dev))
13088 intel_ddi_pll_init(dev);
13089 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013090 ibx_pch_dpll_init(dev);
13091 else
13092 dev_priv->num_shared_dpll = 0;
13093
13094 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013095}
13096
Matt Roper6beb8c232014-12-01 15:40:14 -080013097/**
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +000013098 * intel_wm_need_update - Check whether watermarks need updating
13099 * @plane: drm plane
13100 * @state: new plane state
13101 *
13102 * Check current plane state versus the new one to determine whether
13103 * watermarks need to be recalculated.
13104 *
13105 * Returns true or false.
13106 */
13107bool intel_wm_need_update(struct drm_plane *plane,
13108 struct drm_plane_state *state)
13109{
13110 /* Update watermarks on tiling changes. */
13111 if (!plane->state->fb || !state->fb ||
13112 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
13113 plane->state->rotation != state->rotation)
13114 return true;
13115
13116 return false;
13117}
13118
13119/**
Matt Roper6beb8c232014-12-01 15:40:14 -080013120 * intel_prepare_plane_fb - Prepare fb for usage on plane
13121 * @plane: drm plane to prepare for
13122 * @fb: framebuffer to prepare for presentation
13123 *
13124 * Prepares a framebuffer for usage on a display plane. Generally this
13125 * involves pinning the underlying object and updating the frontbuffer tracking
13126 * bits. Some older platforms need special physical address handling for
13127 * cursor planes.
13128 *
13129 * Returns 0 on success, negative error code on failure.
13130 */
13131int
13132intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013133 struct drm_framebuffer *fb,
13134 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013135{
13136 struct drm_device *dev = plane->dev;
Matt Roper6beb8c232014-12-01 15:40:14 -080013137 struct intel_plane *intel_plane = to_intel_plane(plane);
13138 enum pipe pipe = intel_plane->pipe;
13139 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13140 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
13141 unsigned frontbuffer_bits = 0;
13142 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013143
Matt Roperea2c67b2014-12-23 10:41:52 -080013144 if (!obj)
Matt Roper465c1202014-05-29 08:06:54 -070013145 return 0;
13146
Matt Roper6beb8c232014-12-01 15:40:14 -080013147 switch (plane->type) {
13148 case DRM_PLANE_TYPE_PRIMARY:
13149 frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
13150 break;
13151 case DRM_PLANE_TYPE_CURSOR:
13152 frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
13153 break;
13154 case DRM_PLANE_TYPE_OVERLAY:
13155 frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
13156 break;
13157 }
Matt Roper465c1202014-05-29 08:06:54 -070013158
Matt Roper4c345742014-07-09 16:22:10 -070013159 mutex_lock(&dev->struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070013160
Matt Roper6beb8c232014-12-01 15:40:14 -080013161 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13162 INTEL_INFO(dev)->cursor_needs_physical) {
13163 int align = IS_I830(dev) ? 16 * 1024 : 256;
13164 ret = i915_gem_object_attach_phys(obj, align);
13165 if (ret)
13166 DRM_DEBUG_KMS("failed to attach phys object\n");
13167 } else {
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000013168 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
Matt Roper6beb8c232014-12-01 15:40:14 -080013169 }
13170
13171 if (ret == 0)
13172 i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
13173
13174 mutex_unlock(&dev->struct_mutex);
13175
13176 return ret;
13177}
13178
Matt Roper38f3ce32014-12-02 07:45:25 -080013179/**
13180 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13181 * @plane: drm plane to clean up for
13182 * @fb: old framebuffer that was on plane
13183 *
13184 * Cleans up a framebuffer that has just been removed from a plane.
13185 */
13186void
13187intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013188 struct drm_framebuffer *fb,
13189 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013190{
13191 struct drm_device *dev = plane->dev;
13192 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13193
13194 if (WARN_ON(!obj))
13195 return;
13196
13197 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
13198 !INTEL_INFO(dev)->cursor_needs_physical) {
13199 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000013200 intel_unpin_fb_obj(fb, old_state);
Matt Roper38f3ce32014-12-02 07:45:25 -080013201 mutex_unlock(&dev->struct_mutex);
13202 }
Matt Roper465c1202014-05-29 08:06:54 -070013203}
13204
Chandra Konduru6156a452015-04-27 13:48:39 -070013205int
13206skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13207{
13208 int max_scale;
13209 struct drm_device *dev;
13210 struct drm_i915_private *dev_priv;
13211 int crtc_clock, cdclk;
13212
13213 if (!intel_crtc || !crtc_state)
13214 return DRM_PLANE_HELPER_NO_SCALING;
13215
13216 dev = intel_crtc->base.dev;
13217 dev_priv = dev->dev_private;
13218 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
13219 cdclk = dev_priv->display.get_display_clock_speed(dev);
13220
13221 if (!crtc_clock || !cdclk)
13222 return DRM_PLANE_HELPER_NO_SCALING;
13223
13224 /*
13225 * skl max scale is lower of:
13226 * close to 3 but not 3, -1 is for that purpose
13227 * or
13228 * cdclk/crtc_clock
13229 */
13230 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13231
13232 return max_scale;
13233}
13234
Matt Roper465c1202014-05-29 08:06:54 -070013235static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013236intel_check_primary_plane(struct drm_plane *plane,
13237 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013238{
Matt Roper32b7eee2014-12-24 07:59:06 -080013239 struct drm_device *dev = plane->dev;
13240 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roper2b875c22014-12-01 15:40:13 -080013241 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013242 struct intel_crtc *intel_crtc;
Chandra Konduru6156a452015-04-27 13:48:39 -070013243 struct intel_crtc_state *crtc_state;
Matt Roper2b875c22014-12-01 15:40:13 -080013244 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013245 struct drm_rect *dest = &state->dst;
13246 struct drm_rect *src = &state->src;
13247 const struct drm_rect *clip = &state->clip;
Sonika Jindald8106362015-04-10 14:37:28 +053013248 bool can_position = false;
Chandra Konduru6156a452015-04-27 13:48:39 -070013249 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13250 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013251 int ret;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013252
Matt Roperea2c67b2014-12-23 10:41:52 -080013253 crtc = crtc ? crtc : plane->crtc;
13254 intel_crtc = to_intel_crtc(crtc);
Chandra Konduru6156a452015-04-27 13:48:39 -070013255 crtc_state = state->base.state ?
13256 intel_atomic_get_crtc_state(state->base.state, intel_crtc) : NULL;
Matt Roperea2c67b2014-12-23 10:41:52 -080013257
Chandra Konduru6156a452015-04-27 13:48:39 -070013258 if (INTEL_INFO(dev)->gen >= 9) {
Chandra Konduru225c2282015-05-18 16:18:44 -070013259 /* use scaler when colorkey is not required */
13260 if (to_intel_plane(plane)->ckey.flags == I915_SET_COLORKEY_NONE) {
13261 min_scale = 1;
13262 max_scale = skl_max_scale(intel_crtc, crtc_state);
13263 }
Sonika Jindald8106362015-04-10 14:37:28 +053013264 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070013265 }
Sonika Jindald8106362015-04-10 14:37:28 +053013266
Matt Roperc59cb172014-12-01 15:40:16 -080013267 ret = drm_plane_helper_check_update(plane, crtc, fb,
13268 src, dest, clip,
Chandra Konduru6156a452015-04-27 13:48:39 -070013269 min_scale,
13270 max_scale,
Sonika Jindald8106362015-04-10 14:37:28 +053013271 can_position, true,
13272 &state->visible);
Matt Roperc59cb172014-12-01 15:40:16 -080013273 if (ret)
13274 return ret;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013275
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013276 if (intel_crtc->active) {
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013277 struct intel_plane_state *old_state =
13278 to_intel_plane_state(plane->state);
13279
Matt Roper32b7eee2014-12-24 07:59:06 -080013280 intel_crtc->atomic.wait_for_flips = true;
13281
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013282 /*
13283 * FBC does not work on some platforms for rotated
13284 * planes, so disable it when rotation is not 0 and
13285 * update it when rotation is set back to 0.
13286 *
13287 * FIXME: This is redundant with the fbc update done in
13288 * the primary plane enable function except that that
13289 * one is done too late. We eventually need to unify
13290 * this.
13291 */
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013292 if (state->visible &&
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013293 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
Paulo Zanonie35fef22015-02-09 14:46:29 -020013294 dev_priv->fbc.crtc == intel_crtc &&
Matt Roper8e7d6882015-01-21 16:35:41 -080013295 state->base.rotation != BIT(DRM_ROTATE_0)) {
Matt Roper32b7eee2014-12-24 07:59:06 -080013296 intel_crtc->atomic.disable_fbc = true;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013297 }
13298
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013299 if (state->visible && !old_state->visible) {
Matt Roper32b7eee2014-12-24 07:59:06 -080013300 /*
13301 * BDW signals flip done immediately if the plane
13302 * is disabled, even if the plane enable is already
13303 * armed to occur at the next vblank :(
13304 */
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013305 if (IS_BROADWELL(dev))
Matt Roper32b7eee2014-12-24 07:59:06 -080013306 intel_crtc->atomic.wait_vblank = true;
13307 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013308
Rodrigo Viviac88cd72015-05-28 11:07:11 -070013309 /*
13310 * FIXME: Actually if we will still have any other plane enabled
13311 * on the pipe we could let IPS enabled still, but for
13312 * now lets consider that when we make primary invisible
13313 * by setting DSPCNTR to 0 on update_primary_plane function
13314 * IPS needs to be disable.
13315 */
13316 if (!state->visible || !fb)
13317 intel_crtc->atomic.disable_ips = true;
13318
Matt Roper32b7eee2014-12-24 07:59:06 -080013319 intel_crtc->atomic.fb_bits |=
13320 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
13321
13322 intel_crtc->atomic.update_fbc = true;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +000013323
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +000013324 if (intel_wm_need_update(plane, &state->base))
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +000013325 intel_crtc->atomic.update_wm = true;
Matt Roperc59cb172014-12-01 15:40:16 -080013326 }
13327
Chandra Konduru6156a452015-04-27 13:48:39 -070013328 if (INTEL_INFO(dev)->gen >= 9) {
13329 ret = skl_update_scaler_users(intel_crtc, crtc_state,
13330 to_intel_plane(plane), state, 0);
13331 if (ret)
13332 return ret;
13333 }
13334
Matt Roperc59cb172014-12-01 15:40:16 -080013335 return 0;
Matt Roper465c1202014-05-29 08:06:54 -070013336}
13337
Sonika Jindal48404c12014-08-22 14:06:04 +053013338static void
13339intel_commit_primary_plane(struct drm_plane *plane,
13340 struct intel_plane_state *state)
13341{
Matt Roper2b875c22014-12-01 15:40:13 -080013342 struct drm_crtc *crtc = state->base.crtc;
13343 struct drm_framebuffer *fb = state->base.fb;
13344 struct drm_device *dev = plane->dev;
Sonika Jindal48404c12014-08-22 14:06:04 +053013345 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperea2c67b2014-12-23 10:41:52 -080013346 struct intel_crtc *intel_crtc;
Sonika Jindalce54d852014-08-21 11:44:39 +053013347 struct drm_rect *src = &state->src;
Matt Ropercf4c7c12014-12-04 10:27:42 -080013348
Matt Roperea2c67b2014-12-23 10:41:52 -080013349 crtc = crtc ? crtc : plane->crtc;
13350 intel_crtc = to_intel_crtc(crtc);
13351
Matt Ropercf4c7c12014-12-04 10:27:42 -080013352 plane->fb = fb;
Sonika Jindalce54d852014-08-21 11:44:39 +053013353 crtc->x = src->x1 >> 16;
Matt Roper465c1202014-05-29 08:06:54 -070013354 crtc->y = src->y1 >> 16;
13355
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013356 if (intel_crtc->active) {
Maarten Lankhorst27321ae2015-04-21 17:12:52 +030013357 if (state->visible)
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013358 /* FIXME: kill this fastboot hack */
13359 intel_update_pipe_size(intel_crtc);
13360
Maarten Lankhorst27321ae2015-04-21 17:12:52 +030013361 dev_priv->display.update_primary_plane(crtc, plane->fb,
13362 crtc->x, crtc->y);
Matt Roper32b7eee2014-12-24 07:59:06 -080013363 }
13364}
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013365
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013366static void
13367intel_disable_primary_plane(struct drm_plane *plane,
13368 struct drm_crtc *crtc,
13369 bool force)
13370{
13371 struct drm_device *dev = plane->dev;
13372 struct drm_i915_private *dev_priv = dev->dev_private;
13373
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013374 dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
13375}
13376
Matt Roper32b7eee2014-12-24 07:59:06 -080013377static void intel_begin_crtc_commit(struct drm_crtc *crtc)
13378{
13379 struct drm_device *dev = crtc->dev;
13380 struct drm_i915_private *dev_priv = dev->dev_private;
13381 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080013382 struct intel_plane *intel_plane;
13383 struct drm_plane *p;
13384 unsigned fb_bits = 0;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013385
Matt Roperea2c67b2014-12-23 10:41:52 -080013386 /* Track fb's for any planes being disabled */
13387 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
13388 intel_plane = to_intel_plane(p);
13389
13390 if (intel_crtc->atomic.disabled_planes &
13391 (1 << drm_plane_index(p))) {
13392 switch (p->type) {
13393 case DRM_PLANE_TYPE_PRIMARY:
13394 fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
13395 break;
13396 case DRM_PLANE_TYPE_CURSOR:
13397 fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
13398 break;
13399 case DRM_PLANE_TYPE_OVERLAY:
13400 fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
13401 break;
13402 }
13403
13404 mutex_lock(&dev->struct_mutex);
13405 i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
13406 mutex_unlock(&dev->struct_mutex);
13407 }
13408 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013409
Matt Roper32b7eee2014-12-24 07:59:06 -080013410 if (intel_crtc->atomic.wait_for_flips)
13411 intel_crtc_wait_for_pending_flips(crtc);
13412
13413 if (intel_crtc->atomic.disable_fbc)
13414 intel_fbc_disable(dev);
13415
Rodrigo Viviac88cd72015-05-28 11:07:11 -070013416 if (intel_crtc->atomic.disable_ips)
13417 hsw_disable_ips(intel_crtc);
13418
Matt Roper32b7eee2014-12-24 07:59:06 -080013419 if (intel_crtc->atomic.pre_disable_primary)
13420 intel_pre_disable_primary(crtc);
13421
13422 if (intel_crtc->atomic.update_wm)
13423 intel_update_watermarks(crtc);
13424
13425 intel_runtime_pm_get(dev_priv);
Matt Roperc34c9ee2014-12-23 10:41:50 -080013426
13427 /* Perform vblank evasion around commit operation */
13428 if (intel_crtc->active)
13429 intel_crtc->atomic.evade =
13430 intel_pipe_update_start(intel_crtc,
13431 &intel_crtc->atomic.start_vbl_count);
Matt Roper32b7eee2014-12-24 07:59:06 -080013432}
13433
13434static void intel_finish_crtc_commit(struct drm_crtc *crtc)
13435{
13436 struct drm_device *dev = crtc->dev;
13437 struct drm_i915_private *dev_priv = dev->dev_private;
13438 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13439 struct drm_plane *p;
13440
Matt Roperc34c9ee2014-12-23 10:41:50 -080013441 if (intel_crtc->atomic.evade)
13442 intel_pipe_update_end(intel_crtc,
13443 intel_crtc->atomic.start_vbl_count);
13444
Matt Roper32b7eee2014-12-24 07:59:06 -080013445 intel_runtime_pm_put(dev_priv);
13446
13447 if (intel_crtc->atomic.wait_vblank)
13448 intel_wait_for_vblank(dev, intel_crtc->pipe);
13449
13450 intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
13451
13452 if (intel_crtc->atomic.update_fbc) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013453 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020013454 intel_fbc_update(dev);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013455 mutex_unlock(&dev->struct_mutex);
13456 }
Matt Roper465c1202014-05-29 08:06:54 -070013457
Matt Roper32b7eee2014-12-24 07:59:06 -080013458 if (intel_crtc->atomic.post_enable_primary)
13459 intel_post_enable_primary(crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013460
Matt Roper32b7eee2014-12-24 07:59:06 -080013461 drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
13462 if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
13463 intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
13464 false, false);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013465
Matt Roper32b7eee2014-12-24 07:59:06 -080013466 memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013467}
13468
Matt Ropercf4c7c12014-12-04 10:27:42 -080013469/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013470 * intel_plane_destroy - destroy a plane
13471 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013472 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013473 * Common destruction function for all types of planes (primary, cursor,
13474 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013475 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013476void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013477{
13478 struct intel_plane *intel_plane = to_intel_plane(plane);
13479 drm_plane_cleanup(plane);
13480 kfree(intel_plane);
13481}
13482
Matt Roper65a3fea2015-01-21 16:35:42 -080013483const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013484 .update_plane = drm_atomic_helper_update_plane,
13485 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013486 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080013487 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080013488 .atomic_get_property = intel_plane_atomic_get_property,
13489 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013490 .atomic_duplicate_state = intel_plane_duplicate_state,
13491 .atomic_destroy_state = intel_plane_destroy_state,
13492
Matt Roper465c1202014-05-29 08:06:54 -070013493};
13494
13495static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13496 int pipe)
13497{
13498 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080013499 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070013500 const uint32_t *intel_primary_formats;
13501 int num_formats;
13502
13503 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13504 if (primary == NULL)
13505 return NULL;
13506
Matt Roper8e7d6882015-01-21 16:35:41 -080013507 state = intel_create_plane_state(&primary->base);
13508 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013509 kfree(primary);
13510 return NULL;
13511 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013512 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013513
Matt Roper465c1202014-05-29 08:06:54 -070013514 primary->can_scale = false;
13515 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013516 if (INTEL_INFO(dev)->gen >= 9) {
13517 primary->can_scale = true;
Chandra Konduruaf99ced2015-05-11 14:35:47 -070013518 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013519 }
Matt Roper465c1202014-05-29 08:06:54 -070013520 primary->pipe = pipe;
13521 primary->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080013522 primary->check_plane = intel_check_primary_plane;
13523 primary->commit_plane = intel_commit_primary_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013524 primary->disable_plane = intel_disable_primary_plane;
Chandra Konduru08e221f2015-04-07 15:28:37 -070013525 primary->ckey.flags = I915_SET_COLORKEY_NONE;
Matt Roper465c1202014-05-29 08:06:54 -070013526 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13527 primary->plane = !pipe;
13528
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013529 if (INTEL_INFO(dev)->gen >= 9) {
13530 intel_primary_formats = skl_primary_formats;
13531 num_formats = ARRAY_SIZE(skl_primary_formats);
13532 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010013533 intel_primary_formats = i965_primary_formats;
13534 num_formats = ARRAY_SIZE(i965_primary_formats);
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013535 } else {
13536 intel_primary_formats = i8xx_primary_formats;
13537 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Matt Roper465c1202014-05-29 08:06:54 -070013538 }
13539
13540 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013541 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070013542 intel_primary_formats, num_formats,
13543 DRM_PLANE_TYPE_PRIMARY);
Sonika Jindal48404c12014-08-22 14:06:04 +053013544
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013545 if (INTEL_INFO(dev)->gen >= 4)
13546 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053013547
Matt Roperea2c67b2014-12-23 10:41:52 -080013548 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13549
Matt Roper465c1202014-05-29 08:06:54 -070013550 return &primary->base;
13551}
13552
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013553void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13554{
13555 if (!dev->mode_config.rotation_property) {
13556 unsigned long flags = BIT(DRM_ROTATE_0) |
13557 BIT(DRM_ROTATE_180);
13558
13559 if (INTEL_INFO(dev)->gen >= 9)
13560 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13561
13562 dev->mode_config.rotation_property =
13563 drm_mode_create_rotation_property(dev, flags);
13564 }
13565 if (dev->mode_config.rotation_property)
13566 drm_object_attach_property(&plane->base.base,
13567 dev->mode_config.rotation_property,
13568 plane->base.state->rotation);
13569}
13570
Matt Roper3d7d6512014-06-10 08:28:13 -070013571static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030013572intel_check_cursor_plane(struct drm_plane *plane,
13573 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070013574{
Matt Roper2b875c22014-12-01 15:40:13 -080013575 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013576 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080013577 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013578 struct drm_rect *dest = &state->dst;
13579 struct drm_rect *src = &state->src;
13580 const struct drm_rect *clip = &state->clip;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013581 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Matt Roperea2c67b2014-12-23 10:41:52 -080013582 struct intel_crtc *intel_crtc;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013583 unsigned stride;
13584 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013585
Matt Roperea2c67b2014-12-23 10:41:52 -080013586 crtc = crtc ? crtc : plane->crtc;
13587 intel_crtc = to_intel_crtc(crtc);
13588
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013589 ret = drm_plane_helper_check_update(plane, crtc, fb,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013590 src, dest, clip,
13591 DRM_PLANE_HELPER_NO_SCALING,
13592 DRM_PLANE_HELPER_NO_SCALING,
13593 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013594 if (ret)
13595 return ret;
13596
13597
13598 /* if we want to turn off the cursor ignore width and height */
13599 if (!obj)
Matt Roper32b7eee2014-12-24 07:59:06 -080013600 goto finish;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013601
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013602 /* Check for which cursor types we support */
Matt Roperea2c67b2014-12-23 10:41:52 -080013603 if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
13604 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13605 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013606 return -EINVAL;
13607 }
13608
Matt Roperea2c67b2014-12-23 10:41:52 -080013609 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13610 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013611 DRM_DEBUG_KMS("buffer is too small\n");
13612 return -ENOMEM;
13613 }
13614
Ville Syrjälä3a656b52015-03-09 21:08:37 +020013615 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013616 DRM_DEBUG_KMS("cursor cannot be tiled\n");
13617 ret = -EINVAL;
13618 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013619
Matt Roper32b7eee2014-12-24 07:59:06 -080013620finish:
13621 if (intel_crtc->active) {
Ville Syrjälä3749f462015-03-10 13:15:22 +020013622 if (plane->state->crtc_w != state->base.crtc_w)
Matt Roper32b7eee2014-12-24 07:59:06 -080013623 intel_crtc->atomic.update_wm = true;
13624
13625 intel_crtc->atomic.fb_bits |=
13626 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
13627 }
13628
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013629 return ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013630}
13631
Matt Roperf4a2cf22014-12-01 15:40:12 -080013632static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013633intel_disable_cursor_plane(struct drm_plane *plane,
13634 struct drm_crtc *crtc,
13635 bool force)
13636{
13637 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13638
13639 if (!force) {
13640 plane->fb = NULL;
13641 intel_crtc->cursor_bo = NULL;
13642 intel_crtc->cursor_addr = 0;
13643 }
13644
13645 intel_crtc_update_cursor(crtc, false);
13646}
13647
13648static void
Gustavo Padovan852e7872014-09-05 17:22:31 -030013649intel_commit_cursor_plane(struct drm_plane *plane,
13650 struct intel_plane_state *state)
13651{
Matt Roper2b875c22014-12-01 15:40:13 -080013652 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013653 struct drm_device *dev = plane->dev;
13654 struct intel_crtc *intel_crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080013655 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080013656 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070013657
Matt Roperea2c67b2014-12-23 10:41:52 -080013658 crtc = crtc ? crtc : plane->crtc;
13659 intel_crtc = to_intel_crtc(crtc);
Sonika Jindala919db92014-10-23 07:41:33 -070013660
Matt Roperea2c67b2014-12-23 10:41:52 -080013661 plane->fb = state->base.fb;
13662 crtc->cursor_x = state->base.crtc_x;
13663 crtc->cursor_y = state->base.crtc_y;
13664
Gustavo Padovana912f122014-12-01 15:40:10 -080013665 if (intel_crtc->cursor_bo == obj)
13666 goto update;
13667
Matt Roperf4a2cf22014-12-01 15:40:12 -080013668 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080013669 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080013670 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080013671 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080013672 else
Gustavo Padovana912f122014-12-01 15:40:10 -080013673 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080013674
Gustavo Padovana912f122014-12-01 15:40:10 -080013675 intel_crtc->cursor_addr = addr;
13676 intel_crtc->cursor_bo = obj;
13677update:
Gustavo Padovana912f122014-12-01 15:40:10 -080013678
Matt Roper32b7eee2014-12-24 07:59:06 -080013679 if (intel_crtc->active)
Gustavo Padovan852e7872014-09-05 17:22:31 -030013680 intel_crtc_update_cursor(crtc, state->visible);
Matt Roper3d7d6512014-06-10 08:28:13 -070013681}
Gustavo Padovan852e7872014-09-05 17:22:31 -030013682
Matt Roper3d7d6512014-06-10 08:28:13 -070013683static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
13684 int pipe)
13685{
13686 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080013687 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070013688
13689 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
13690 if (cursor == NULL)
13691 return NULL;
13692
Matt Roper8e7d6882015-01-21 16:35:41 -080013693 state = intel_create_plane_state(&cursor->base);
13694 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013695 kfree(cursor);
13696 return NULL;
13697 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013698 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013699
Matt Roper3d7d6512014-06-10 08:28:13 -070013700 cursor->can_scale = false;
13701 cursor->max_downscale = 1;
13702 cursor->pipe = pipe;
13703 cursor->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080013704 cursor->check_plane = intel_check_cursor_plane;
13705 cursor->commit_plane = intel_commit_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013706 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070013707
13708 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013709 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070013710 intel_cursor_formats,
13711 ARRAY_SIZE(intel_cursor_formats),
13712 DRM_PLANE_TYPE_CURSOR);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013713
13714 if (INTEL_INFO(dev)->gen >= 4) {
13715 if (!dev->mode_config.rotation_property)
13716 dev->mode_config.rotation_property =
13717 drm_mode_create_rotation_property(dev,
13718 BIT(DRM_ROTATE_0) |
13719 BIT(DRM_ROTATE_180));
13720 if (dev->mode_config.rotation_property)
13721 drm_object_attach_property(&cursor->base.base,
13722 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080013723 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013724 }
13725
Chandra Konduruaf99ced2015-05-11 14:35:47 -070013726 if (INTEL_INFO(dev)->gen >=9)
13727 state->scaler_id = -1;
13728
Matt Roperea2c67b2014-12-23 10:41:52 -080013729 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
13730
Matt Roper3d7d6512014-06-10 08:28:13 -070013731 return &cursor->base;
13732}
13733
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013734static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
13735 struct intel_crtc_state *crtc_state)
13736{
13737 int i;
13738 struct intel_scaler *intel_scaler;
13739 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
13740
13741 for (i = 0; i < intel_crtc->num_scalers; i++) {
13742 intel_scaler = &scaler_state->scalers[i];
13743 intel_scaler->in_use = 0;
13744 intel_scaler->id = i;
13745
13746 intel_scaler->mode = PS_SCALER_MODE_DYN;
13747 }
13748
13749 scaler_state->scaler_id = -1;
13750}
13751
Hannes Ederb358d0a2008-12-18 21:18:47 +010013752static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080013753{
Jani Nikulafbee40d2014-03-31 14:27:18 +030013754 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080013755 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013756 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070013757 struct drm_plane *primary = NULL;
13758 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070013759 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080013760
Daniel Vetter955382f2013-09-19 14:05:45 +020013761 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080013762 if (intel_crtc == NULL)
13763 return;
13764
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013765 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
13766 if (!crtc_state)
13767 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030013768 intel_crtc->config = crtc_state;
13769 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080013770 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013771
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013772 /* initialize shared scalers */
13773 if (INTEL_INFO(dev)->gen >= 9) {
13774 if (pipe == PIPE_C)
13775 intel_crtc->num_scalers = 1;
13776 else
13777 intel_crtc->num_scalers = SKL_NUM_SCALERS;
13778
13779 skl_init_scalers(dev, intel_crtc, crtc_state);
13780 }
13781
Matt Roper465c1202014-05-29 08:06:54 -070013782 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070013783 if (!primary)
13784 goto fail;
13785
13786 cursor = intel_cursor_plane_create(dev, pipe);
13787 if (!cursor)
13788 goto fail;
13789
Matt Roper465c1202014-05-29 08:06:54 -070013790 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Matt Roper3d7d6512014-06-10 08:28:13 -070013791 cursor, &intel_crtc_funcs);
13792 if (ret)
13793 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080013794
13795 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080013796 for (i = 0; i < 256; i++) {
13797 intel_crtc->lut_r[i] = i;
13798 intel_crtc->lut_g[i] = i;
13799 intel_crtc->lut_b[i] = i;
13800 }
13801
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020013802 /*
13803 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020013804 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020013805 */
Jesse Barnes80824002009-09-10 15:28:06 -070013806 intel_crtc->pipe = pipe;
13807 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010013808 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080013809 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010013810 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070013811 }
13812
Chris Wilson4b0e3332014-05-30 16:35:26 +030013813 intel_crtc->cursor_base = ~0;
13814 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030013815 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030013816
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080013817 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
13818 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
13819 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
13820 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
13821
Jesse Barnes79e53942008-11-07 14:24:08 -080013822 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020013823
13824 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070013825 return;
13826
13827fail:
13828 if (primary)
13829 drm_plane_cleanup(primary);
13830 if (cursor)
13831 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013832 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070013833 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080013834}
13835
Jesse Barnes752aa882013-10-31 18:55:49 +020013836enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
13837{
13838 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020013839 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020013840
Rob Clark51fd3712013-11-19 12:10:12 -050013841 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020013842
Ville Syrjäläd3babd32014-11-07 11:16:01 +020013843 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020013844 return INVALID_PIPE;
13845
13846 return to_intel_crtc(encoder->crtc)->pipe;
13847}
13848
Carl Worth08d7b3d2009-04-29 14:43:54 -070013849int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000013850 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070013851{
Carl Worth08d7b3d2009-04-29 14:43:54 -070013852 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040013853 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020013854 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013855
Rob Clark7707e652014-07-17 23:30:04 -040013856 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070013857
Rob Clark7707e652014-07-17 23:30:04 -040013858 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070013859 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030013860 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013861 }
13862
Rob Clark7707e652014-07-17 23:30:04 -040013863 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020013864 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013865
Daniel Vetterc05422d2009-08-11 16:05:30 +020013866 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013867}
13868
Daniel Vetter66a92782012-07-12 20:08:18 +020013869static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080013870{
Daniel Vetter66a92782012-07-12 20:08:18 +020013871 struct drm_device *dev = encoder->base.dev;
13872 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080013873 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080013874 int entry = 0;
13875
Damien Lespiaub2784e12014-08-05 11:29:37 +010013876 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020013877 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020013878 index_mask |= (1 << entry);
13879
Jesse Barnes79e53942008-11-07 14:24:08 -080013880 entry++;
13881 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010013882
Jesse Barnes79e53942008-11-07 14:24:08 -080013883 return index_mask;
13884}
13885
Chris Wilson4d302442010-12-14 19:21:29 +000013886static bool has_edp_a(struct drm_device *dev)
13887{
13888 struct drm_i915_private *dev_priv = dev->dev_private;
13889
13890 if (!IS_MOBILE(dev))
13891 return false;
13892
13893 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
13894 return false;
13895
Damien Lespiaue3589902014-02-07 19:12:50 +000013896 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000013897 return false;
13898
13899 return true;
13900}
13901
Jesse Barnes84b4e042014-06-25 08:24:29 -070013902static bool intel_crt_present(struct drm_device *dev)
13903{
13904 struct drm_i915_private *dev_priv = dev->dev_private;
13905
Damien Lespiau884497e2013-12-03 13:56:23 +000013906 if (INTEL_INFO(dev)->gen >= 9)
13907 return false;
13908
Damien Lespiaucf404ce2014-10-01 20:04:15 +010013909 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070013910 return false;
13911
13912 if (IS_CHERRYVIEW(dev))
13913 return false;
13914
13915 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
13916 return false;
13917
13918 return true;
13919}
13920
Jesse Barnes79e53942008-11-07 14:24:08 -080013921static void intel_setup_outputs(struct drm_device *dev)
13922{
Eric Anholt725e30a2009-01-22 13:01:02 -080013923 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010013924 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013925 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080013926
Daniel Vetterc9093352013-06-06 22:22:47 +020013927 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080013928
Jesse Barnes84b4e042014-06-25 08:24:29 -070013929 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020013930 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013931
Vandana Kannanc776eb22014-08-19 12:05:01 +053013932 if (IS_BROXTON(dev)) {
13933 /*
13934 * FIXME: Broxton doesn't support port detection via the
13935 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
13936 * detect the ports.
13937 */
13938 intel_ddi_init(dev, PORT_A);
13939 intel_ddi_init(dev, PORT_B);
13940 intel_ddi_init(dev, PORT_C);
13941 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013942 int found;
13943
Jesse Barnesde31fac2015-03-06 15:53:32 -080013944 /*
13945 * Haswell uses DDI functions to detect digital outputs.
13946 * On SKL pre-D0 the strap isn't connected, so we assume
13947 * it's there.
13948 */
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013949 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080013950 /* WaIgnoreDDIAStrap: skl */
13951 if (found ||
13952 (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013953 intel_ddi_init(dev, PORT_A);
13954
13955 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
13956 * register */
13957 found = I915_READ(SFUSE_STRAP);
13958
13959 if (found & SFUSE_STRAP_DDIB_DETECTED)
13960 intel_ddi_init(dev, PORT_B);
13961 if (found & SFUSE_STRAP_DDIC_DETECTED)
13962 intel_ddi_init(dev, PORT_C);
13963 if (found & SFUSE_STRAP_DDID_DETECTED)
13964 intel_ddi_init(dev, PORT_D);
13965 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013966 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020013967 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020013968
13969 if (has_edp_a(dev))
13970 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013971
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013972 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080013973 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010013974 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013975 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013976 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013977 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013978 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013979 }
13980
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013981 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013982 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013983
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013984 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013985 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013986
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013987 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013988 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013989
Daniel Vetter270b3042012-10-27 15:52:05 +020013990 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013991 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070013992 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013993 /*
13994 * The DP_DETECTED bit is the latched state of the DDC
13995 * SDA pin at boot. However since eDP doesn't require DDC
13996 * (no way to plug in a DP->HDMI dongle) the DDC pins for
13997 * eDP ports may have been muxed to an alternate function.
13998 * Thus we can't rely on the DP_DETECTED bit alone to detect
13999 * eDP ports. Consult the VBT as well as DP_DETECTED to
14000 * detect eDP ports.
14001 */
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014002 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
14003 !intel_dp_is_edp(dev, PORT_B))
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014004 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
14005 PORT_B);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014006 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
14007 intel_dp_is_edp(dev, PORT_B))
14008 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014009
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014010 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
14011 !intel_dp_is_edp(dev, PORT_C))
Jesse Barnes6f6005a2013-08-09 09:34:35 -070014012 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
14013 PORT_C);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014014 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
14015 intel_dp_is_edp(dev, PORT_C))
14016 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014017
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014018 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014019 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014020 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
14021 PORT_D);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014022 /* eDP not supported on port D, so don't check VBT */
14023 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
14024 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014025 }
14026
Jani Nikula3cfca972013-08-27 15:12:26 +030014027 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +080014028 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014029 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014030
Paulo Zanonie2debe92013-02-18 19:00:27 -030014031 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014032 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014033 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014034 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
14035 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014036 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014037 }
Ma Ling27185ae2009-08-24 13:50:23 +080014038
Imre Deake7281ea2013-05-08 13:14:08 +030014039 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014040 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014041 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014042
14043 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014044
Paulo Zanonie2debe92013-02-18 19:00:27 -030014045 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014046 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014047 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014048 }
Ma Ling27185ae2009-08-24 13:50:23 +080014049
Paulo Zanonie2debe92013-02-18 19:00:27 -030014050 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014051
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014052 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
14053 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014054 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014055 }
Imre Deake7281ea2013-05-08 13:14:08 +030014056 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014057 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014058 }
Ma Ling27185ae2009-08-24 13:50:23 +080014059
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014060 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014061 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014062 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014063 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014064 intel_dvo_init(dev);
14065
Zhenyu Wang103a1962009-11-27 11:44:36 +080014066 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014067 intel_tv_init(dev);
14068
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014069 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014070
Damien Lespiaub2784e12014-08-05 11:29:37 +010014071 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014072 encoder->base.possible_crtcs = encoder->crtc_mask;
14073 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014074 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014075 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014076
Paulo Zanonidde86e22012-12-01 12:04:25 -020014077 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014078
14079 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014080}
14081
14082static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14083{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014084 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014085 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014086
Daniel Vetteref2d6332014-02-10 18:00:38 +010014087 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014088 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014089 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014090 drm_gem_object_unreference(&intel_fb->obj->base);
14091 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014092 kfree(intel_fb);
14093}
14094
14095static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014096 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014097 unsigned int *handle)
14098{
14099 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014100 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014101
Chris Wilson05394f32010-11-08 19:18:58 +000014102 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014103}
14104
14105static const struct drm_framebuffer_funcs intel_fb_funcs = {
14106 .destroy = intel_user_framebuffer_destroy,
14107 .create_handle = intel_user_framebuffer_create_handle,
14108};
14109
Damien Lespiaub3218032015-02-27 11:15:18 +000014110static
14111u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14112 uint32_t pixel_format)
14113{
14114 u32 gen = INTEL_INFO(dev)->gen;
14115
14116 if (gen >= 9) {
14117 /* "The stride in bytes must not exceed the of the size of 8K
14118 * pixels and 32K bytes."
14119 */
14120 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
14121 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
14122 return 32*1024;
14123 } else if (gen >= 4) {
14124 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14125 return 16*1024;
14126 else
14127 return 32*1024;
14128 } else if (gen >= 3) {
14129 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14130 return 8*1024;
14131 else
14132 return 16*1024;
14133 } else {
14134 /* XXX DSPC is limited to 4k tiled */
14135 return 8*1024;
14136 }
14137}
14138
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014139static int intel_framebuffer_init(struct drm_device *dev,
14140 struct intel_framebuffer *intel_fb,
14141 struct drm_mode_fb_cmd2 *mode_cmd,
14142 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014143{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014144 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014145 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014146 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014147
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014148 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14149
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014150 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14151 /* Enforce that fb modifier and tiling mode match, but only for
14152 * X-tiled. This is needed for FBC. */
14153 if (!!(obj->tiling_mode == I915_TILING_X) !=
14154 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14155 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14156 return -EINVAL;
14157 }
14158 } else {
14159 if (obj->tiling_mode == I915_TILING_X)
14160 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14161 else if (obj->tiling_mode == I915_TILING_Y) {
14162 DRM_DEBUG("No Y tiling for legacy addfb\n");
14163 return -EINVAL;
14164 }
14165 }
14166
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014167 /* Passed in modifier sanity checking. */
14168 switch (mode_cmd->modifier[0]) {
14169 case I915_FORMAT_MOD_Y_TILED:
14170 case I915_FORMAT_MOD_Yf_TILED:
14171 if (INTEL_INFO(dev)->gen < 9) {
14172 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14173 mode_cmd->modifier[0]);
14174 return -EINVAL;
14175 }
14176 case DRM_FORMAT_MOD_NONE:
14177 case I915_FORMAT_MOD_X_TILED:
14178 break;
14179 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014180 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14181 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014182 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014183 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014184
Damien Lespiaub3218032015-02-27 11:15:18 +000014185 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
14186 mode_cmd->pixel_format);
14187 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14188 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14189 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014190 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014191 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014192
Damien Lespiaub3218032015-02-27 11:15:18 +000014193 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14194 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014195 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014196 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14197 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014198 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014199 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014200 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014201 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014202
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014203 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014204 mode_cmd->pitches[0] != obj->stride) {
14205 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14206 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014207 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014208 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014209
Ville Syrjälä57779d02012-10-31 17:50:14 +020014210 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014211 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014212 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014213 case DRM_FORMAT_RGB565:
14214 case DRM_FORMAT_XRGB8888:
14215 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014216 break;
14217 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014218 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014219 DRM_DEBUG("unsupported pixel format: %s\n",
14220 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014221 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014222 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014223 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014224 case DRM_FORMAT_ABGR8888:
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014225 if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
14226 DRM_DEBUG("unsupported pixel format: %s\n",
14227 drm_get_format_name(mode_cmd->pixel_format));
14228 return -EINVAL;
14229 }
14230 break;
14231 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014232 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014233 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014234 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014235 DRM_DEBUG("unsupported pixel format: %s\n",
14236 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014237 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014238 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014239 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014240 case DRM_FORMAT_ABGR2101010:
14241 if (!IS_VALLEYVIEW(dev)) {
14242 DRM_DEBUG("unsupported pixel format: %s\n",
14243 drm_get_format_name(mode_cmd->pixel_format));
14244 return -EINVAL;
14245 }
14246 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014247 case DRM_FORMAT_YUYV:
14248 case DRM_FORMAT_UYVY:
14249 case DRM_FORMAT_YVYU:
14250 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014251 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014252 DRM_DEBUG("unsupported pixel format: %s\n",
14253 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014254 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014255 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014256 break;
14257 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014258 DRM_DEBUG("unsupported pixel format: %s\n",
14259 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014260 return -EINVAL;
14261 }
14262
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014263 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14264 if (mode_cmd->offsets[0] != 0)
14265 return -EINVAL;
14266
Damien Lespiauec2c9812015-01-20 12:51:45 +000014267 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014268 mode_cmd->pixel_format,
14269 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014270 /* FIXME drm helper for size checks (especially planar formats)? */
14271 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14272 return -EINVAL;
14273
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014274 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14275 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020014276 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014277
Jesse Barnes79e53942008-11-07 14:24:08 -080014278 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14279 if (ret) {
14280 DRM_ERROR("framebuffer init failed %d\n", ret);
14281 return ret;
14282 }
14283
Jesse Barnes79e53942008-11-07 14:24:08 -080014284 return 0;
14285}
14286
Jesse Barnes79e53942008-11-07 14:24:08 -080014287static struct drm_framebuffer *
14288intel_user_framebuffer_create(struct drm_device *dev,
14289 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014290 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014291{
Chris Wilson05394f32010-11-08 19:18:58 +000014292 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014293
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014294 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
14295 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014296 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014297 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014298
Chris Wilsond2dff872011-04-19 08:36:26 +010014299 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080014300}
14301
Daniel Vetter4520f532013-10-09 09:18:51 +020014302#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020014303static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014304{
14305}
14306#endif
14307
Jesse Barnes79e53942008-11-07 14:24:08 -080014308static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014309 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014310 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014311 .atomic_check = intel_atomic_check,
14312 .atomic_commit = intel_atomic_commit,
Jesse Barnes79e53942008-11-07 14:24:08 -080014313};
14314
Jesse Barnese70236a2009-09-21 10:42:27 -070014315/* Set up chip specific display functions */
14316static void intel_init_display(struct drm_device *dev)
14317{
14318 struct drm_i915_private *dev_priv = dev->dev_private;
14319
Daniel Vetteree9300b2013-06-03 22:40:22 +020014320 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14321 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030014322 else if (IS_CHERRYVIEW(dev))
14323 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020014324 else if (IS_VALLEYVIEW(dev))
14325 dev_priv->display.find_dpll = vlv_find_best_dpll;
14326 else if (IS_PINEVIEW(dev))
14327 dev_priv->display.find_dpll = pnv_find_best_dpll;
14328 else
14329 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14330
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014331 if (INTEL_INFO(dev)->gen >= 9) {
14332 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014333 dev_priv->display.get_initial_plane_config =
14334 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014335 dev_priv->display.crtc_compute_clock =
14336 haswell_crtc_compute_clock;
14337 dev_priv->display.crtc_enable = haswell_crtc_enable;
14338 dev_priv->display.crtc_disable = haswell_crtc_disable;
14339 dev_priv->display.off = ironlake_crtc_off;
14340 dev_priv->display.update_primary_plane =
14341 skylake_update_primary_plane;
14342 } else if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014343 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014344 dev_priv->display.get_initial_plane_config =
14345 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014346 dev_priv->display.crtc_compute_clock =
14347 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014348 dev_priv->display.crtc_enable = haswell_crtc_enable;
14349 dev_priv->display.crtc_disable = haswell_crtc_disable;
Daniel Vetterdf8ad702014-06-25 22:02:03 +030014350 dev_priv->display.off = ironlake_crtc_off;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014351 dev_priv->display.update_primary_plane =
14352 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030014353 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014354 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014355 dev_priv->display.get_initial_plane_config =
14356 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014357 dev_priv->display.crtc_compute_clock =
14358 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014359 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14360 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014361 dev_priv->display.off = ironlake_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070014362 dev_priv->display.update_primary_plane =
14363 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014364 } else if (IS_VALLEYVIEW(dev)) {
14365 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014366 dev_priv->display.get_initial_plane_config =
14367 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014368 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014369 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14370 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14371 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070014372 dev_priv->display.update_primary_plane =
14373 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014374 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014375 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014376 dev_priv->display.get_initial_plane_config =
14377 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014378 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014379 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14380 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014381 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070014382 dev_priv->display.update_primary_plane =
14383 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014384 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014385
Jesse Barnese70236a2009-09-21 10:42:27 -070014386 /* Returns the core display clock speed */
Ville Syrjälä1652d192015-03-31 14:12:01 +030014387 if (IS_SKYLAKE(dev))
14388 dev_priv->display.get_display_clock_speed =
14389 skylake_get_display_clock_speed;
14390 else if (IS_BROADWELL(dev))
14391 dev_priv->display.get_display_clock_speed =
14392 broadwell_get_display_clock_speed;
14393 else if (IS_HASWELL(dev))
14394 dev_priv->display.get_display_clock_speed =
14395 haswell_get_display_clock_speed;
14396 else if (IS_VALLEYVIEW(dev))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070014397 dev_priv->display.get_display_clock_speed =
14398 valleyview_get_display_clock_speed;
Ville Syrjäläb37a6432015-03-31 14:11:54 +030014399 else if (IS_GEN5(dev))
14400 dev_priv->display.get_display_clock_speed =
14401 ilk_get_display_clock_speed;
Ville Syrjäläa7c66cd2015-03-31 14:11:56 +030014402 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
14403 IS_GEN6(dev) || IS_IVYBRIDGE(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -070014404 dev_priv->display.get_display_clock_speed =
14405 i945_get_display_clock_speed;
14406 else if (IS_I915G(dev))
14407 dev_priv->display.get_display_clock_speed =
14408 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014409 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014410 dev_priv->display.get_display_clock_speed =
14411 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014412 else if (IS_PINEVIEW(dev))
14413 dev_priv->display.get_display_clock_speed =
14414 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014415 else if (IS_I915GM(dev))
14416 dev_priv->display.get_display_clock_speed =
14417 i915gm_get_display_clock_speed;
14418 else if (IS_I865G(dev))
14419 dev_priv->display.get_display_clock_speed =
14420 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020014421 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014422 dev_priv->display.get_display_clock_speed =
14423 i855_get_display_clock_speed;
14424 else /* 852, 830 */
14425 dev_priv->display.get_display_clock_speed =
14426 i830_get_display_clock_speed;
14427
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014428 if (IS_GEN5(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014429 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014430 } else if (IS_GEN6(dev)) {
14431 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014432 } else if (IS_IVYBRIDGE(dev)) {
14433 /* FIXME: detect B0+ stepping and use auto training */
14434 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Paulo Zanoni059b2fe2014-09-02 16:53:57 -030014435 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014436 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Jesse Barnes30a970c2013-11-04 13:48:12 -080014437 } else if (IS_VALLEYVIEW(dev)) {
14438 dev_priv->display.modeset_global_resources =
14439 valleyview_modeset_global_resources;
Vandana Kannanf8437dd12014-11-24 13:37:39 +053014440 } else if (IS_BROXTON(dev)) {
14441 dev_priv->display.modeset_global_resources =
14442 broxton_modeset_global_resources;
Jesse Barnese70236a2009-09-21 10:42:27 -070014443 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014444
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014445 switch (INTEL_INFO(dev)->gen) {
14446 case 2:
14447 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14448 break;
14449
14450 case 3:
14451 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14452 break;
14453
14454 case 4:
14455 case 5:
14456 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14457 break;
14458
14459 case 6:
14460 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14461 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014462 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070014463 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014464 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14465 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000014466 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000014467 /* Drop through - unsupported since execlist only. */
14468 default:
14469 /* Default just returns -ENODEV to indicate unsupported */
14470 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014471 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020014472
14473 intel_panel_init_backlight_funcs(dev);
Ville Syrjäläe39b9992014-09-04 14:53:14 +030014474
14475 mutex_init(&dev_priv->pps_mutex);
Jesse Barnese70236a2009-09-21 10:42:27 -070014476}
14477
Jesse Barnesb690e962010-07-19 13:53:12 -070014478/*
14479 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14480 * resume, or other times. This quirk makes sure that's the case for
14481 * affected systems.
14482 */
Akshay Joshi0206e352011-08-16 15:34:10 -040014483static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070014484{
14485 struct drm_i915_private *dev_priv = dev->dev_private;
14486
14487 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014488 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014489}
14490
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014491static void quirk_pipeb_force(struct drm_device *dev)
14492{
14493 struct drm_i915_private *dev_priv = dev->dev_private;
14494
14495 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14496 DRM_INFO("applying pipe b force quirk\n");
14497}
14498
Keith Packard435793d2011-07-12 14:56:22 -070014499/*
14500 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14501 */
14502static void quirk_ssc_force_disable(struct drm_device *dev)
14503{
14504 struct drm_i915_private *dev_priv = dev->dev_private;
14505 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014506 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070014507}
14508
Carsten Emde4dca20e2012-03-15 15:56:26 +010014509/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010014510 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14511 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010014512 */
14513static void quirk_invert_brightness(struct drm_device *dev)
14514{
14515 struct drm_i915_private *dev_priv = dev->dev_private;
14516 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014517 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014518}
14519
Scot Doyle9c72cc62014-07-03 23:27:50 +000014520/* Some VBT's incorrectly indicate no backlight is present */
14521static void quirk_backlight_present(struct drm_device *dev)
14522{
14523 struct drm_i915_private *dev_priv = dev->dev_private;
14524 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14525 DRM_INFO("applying backlight present quirk\n");
14526}
14527
Jesse Barnesb690e962010-07-19 13:53:12 -070014528struct intel_quirk {
14529 int device;
14530 int subsystem_vendor;
14531 int subsystem_device;
14532 void (*hook)(struct drm_device *dev);
14533};
14534
Egbert Eich5f85f172012-10-14 15:46:38 +020014535/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14536struct intel_dmi_quirk {
14537 void (*hook)(struct drm_device *dev);
14538 const struct dmi_system_id (*dmi_id_list)[];
14539};
14540
14541static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14542{
14543 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14544 return 1;
14545}
14546
14547static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14548 {
14549 .dmi_id_list = &(const struct dmi_system_id[]) {
14550 {
14551 .callback = intel_dmi_reverse_brightness,
14552 .ident = "NCR Corporation",
14553 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14554 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14555 },
14556 },
14557 { } /* terminating entry */
14558 },
14559 .hook = quirk_invert_brightness,
14560 },
14561};
14562
Ben Widawskyc43b5632012-04-16 14:07:40 -070014563static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070014564 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14565 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14566
Jesse Barnesb690e962010-07-19 13:53:12 -070014567 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14568 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14569
Ville Syrjälä5f080c02014-08-15 01:22:06 +030014570 /* 830 needs to leave pipe A & dpll A up */
14571 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14572
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014573 /* 830 needs to leave pipe B & dpll B up */
14574 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14575
Keith Packard435793d2011-07-12 14:56:22 -070014576 /* Lenovo U160 cannot use SSC on LVDS */
14577 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020014578
14579 /* Sony Vaio Y cannot use SSC on LVDS */
14580 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010014581
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010014582 /* Acer Aspire 5734Z must invert backlight brightness */
14583 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14584
14585 /* Acer/eMachines G725 */
14586 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14587
14588 /* Acer/eMachines e725 */
14589 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14590
14591 /* Acer/Packard Bell NCL20 */
14592 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14593
14594 /* Acer Aspire 4736Z */
14595 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020014596
14597 /* Acer Aspire 5336 */
14598 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000014599
14600 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14601 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000014602
Scot Doyledfb3d47b2014-08-21 16:08:02 +000014603 /* Acer C720 Chromebook (Core i3 4005U) */
14604 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14605
jens steinb2a96012014-10-28 20:25:53 +010014606 /* Apple Macbook 2,1 (Core 2 T7400) */
14607 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14608
Scot Doyled4967d82014-07-03 23:27:52 +000014609 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14610 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000014611
14612 /* HP Chromebook 14 (Celeron 2955U) */
14613 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020014614
14615 /* Dell Chromebook 11 */
14616 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070014617};
14618
14619static void intel_init_quirks(struct drm_device *dev)
14620{
14621 struct pci_dev *d = dev->pdev;
14622 int i;
14623
14624 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14625 struct intel_quirk *q = &intel_quirks[i];
14626
14627 if (d->device == q->device &&
14628 (d->subsystem_vendor == q->subsystem_vendor ||
14629 q->subsystem_vendor == PCI_ANY_ID) &&
14630 (d->subsystem_device == q->subsystem_device ||
14631 q->subsystem_device == PCI_ANY_ID))
14632 q->hook(dev);
14633 }
Egbert Eich5f85f172012-10-14 15:46:38 +020014634 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14635 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14636 intel_dmi_quirks[i].hook(dev);
14637 }
Jesse Barnesb690e962010-07-19 13:53:12 -070014638}
14639
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014640/* Disable the VGA plane that we never use */
14641static void i915_disable_vga(struct drm_device *dev)
14642{
14643 struct drm_i915_private *dev_priv = dev->dev_private;
14644 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020014645 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014646
Ville Syrjälä2b37c612014-01-22 21:32:38 +020014647 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014648 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070014649 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014650 sr1 = inb(VGA_SR_DATA);
14651 outb(sr1 | 1<<5, VGA_SR_DATA);
14652 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
14653 udelay(300);
14654
Ville Syrjälä01f5a622014-12-16 18:38:37 +020014655 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014656 POSTING_READ(vga_reg);
14657}
14658
Daniel Vetterf8175862012-04-10 15:50:11 +020014659void intel_modeset_init_hw(struct drm_device *dev)
14660{
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030014661 intel_prepare_ddi(dev);
14662
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +030014663 if (IS_VALLEYVIEW(dev))
14664 vlv_update_cdclk(dev);
14665
Daniel Vetterf8175862012-04-10 15:50:11 +020014666 intel_init_clock_gating(dev);
14667
Daniel Vetter8090c6b2012-06-24 16:42:32 +020014668 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020014669}
14670
Jesse Barnes79e53942008-11-07 14:24:08 -080014671void intel_modeset_init(struct drm_device *dev)
14672{
Jesse Barnes652c3932009-08-17 13:31:43 -070014673 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000014674 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014675 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080014676 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080014677
14678 drm_mode_config_init(dev);
14679
14680 dev->mode_config.min_width = 0;
14681 dev->mode_config.min_height = 0;
14682
Dave Airlie019d96c2011-09-29 16:20:42 +010014683 dev->mode_config.preferred_depth = 24;
14684 dev->mode_config.prefer_shadow = 1;
14685
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000014686 dev->mode_config.allow_fb_modifiers = true;
14687
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020014688 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080014689
Jesse Barnesb690e962010-07-19 13:53:12 -070014690 intel_init_quirks(dev);
14691
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030014692 intel_init_pm(dev);
14693
Ben Widawskye3c74752013-04-05 13:12:39 -070014694 if (INTEL_INFO(dev)->num_pipes == 0)
14695 return;
14696
Jesse Barnese70236a2009-09-21 10:42:27 -070014697 intel_init_display(dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014698 intel_init_audio(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070014699
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014700 if (IS_GEN2(dev)) {
14701 dev->mode_config.max_width = 2048;
14702 dev->mode_config.max_height = 2048;
14703 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070014704 dev->mode_config.max_width = 4096;
14705 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080014706 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014707 dev->mode_config.max_width = 8192;
14708 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080014709 }
Damien Lespiau068be562014-03-28 14:17:49 +000014710
Ville Syrjälädc41c152014-08-13 11:57:05 +030014711 if (IS_845G(dev) || IS_I865G(dev)) {
14712 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
14713 dev->mode_config.cursor_height = 1023;
14714 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000014715 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
14716 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
14717 } else {
14718 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
14719 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
14720 }
14721
Ben Widawsky5d4545a2013-01-17 12:45:15 -080014722 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080014723
Zhao Yakui28c97732009-10-09 11:39:41 +080014724 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014725 INTEL_INFO(dev)->num_pipes,
14726 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080014727
Damien Lespiau055e3932014-08-18 13:49:10 +010014728 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014729 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000014730 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000014731 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070014732 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030014733 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000014734 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070014735 }
Jesse Barnes79e53942008-11-07 14:24:08 -080014736 }
14737
Jesse Barnesf42bb702013-12-16 16:34:23 -080014738 intel_init_dpio(dev);
14739
Daniel Vettere72f9fb2013-06-05 13:34:06 +020014740 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014741
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014742 /* Just disable it once at startup */
14743 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014744 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000014745
14746 /* Just in case the BIOS is doing something questionable. */
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020014747 intel_fbc_disable(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080014748
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014749 drm_modeset_lock_all(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080014750 intel_modeset_setup_hw_state(dev, false);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014751 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014752
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014753 for_each_intel_crtc(dev, crtc) {
Jesse Barnes46f297f2014-03-07 08:57:48 -080014754 if (!crtc->active)
14755 continue;
14756
Jesse Barnes46f297f2014-03-07 08:57:48 -080014757 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080014758 * Note that reserving the BIOS fb up front prevents us
14759 * from stuffing other stolen allocations like the ring
14760 * on top. This prevents some ugliness at boot time, and
14761 * can even allow for smooth boot transitions if the BIOS
14762 * fb is large enough for the active pipe configuration.
14763 */
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014764 if (dev_priv->display.get_initial_plane_config) {
14765 dev_priv->display.get_initial_plane_config(crtc,
Jesse Barnes46f297f2014-03-07 08:57:48 -080014766 &crtc->plane_config);
14767 /*
14768 * If the fb is shared between multiple heads, we'll
14769 * just get the first one.
14770 */
Daniel Vetterf6936e22015-03-26 12:17:05 +010014771 intel_find_initial_plane_obj(crtc, &crtc->plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014772 }
Jesse Barnes46f297f2014-03-07 08:57:48 -080014773 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010014774}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080014775
Daniel Vetter7fad7982012-07-04 17:51:47 +020014776static void intel_enable_pipe_a(struct drm_device *dev)
14777{
14778 struct intel_connector *connector;
14779 struct drm_connector *crt = NULL;
14780 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030014781 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020014782
14783 /* We can't just switch on the pipe A, we need to set things up with a
14784 * proper mode and output configuration. As a gross hack, enable pipe A
14785 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014786 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020014787 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
14788 crt = &connector->base;
14789 break;
14790 }
14791 }
14792
14793 if (!crt)
14794 return;
14795
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030014796 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020014797 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020014798}
14799
Daniel Vetterfa555832012-10-10 23:14:00 +020014800static bool
14801intel_check_plane_mapping(struct intel_crtc *crtc)
14802{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014803 struct drm_device *dev = crtc->base.dev;
14804 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020014805 u32 reg, val;
14806
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014807 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020014808 return true;
14809
14810 reg = DSPCNTR(!crtc->plane);
14811 val = I915_READ(reg);
14812
14813 if ((val & DISPLAY_PLANE_ENABLE) &&
14814 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
14815 return false;
14816
14817 return true;
14818}
14819
Daniel Vetter24929352012-07-02 20:28:59 +020014820static void intel_sanitize_crtc(struct intel_crtc *crtc)
14821{
14822 struct drm_device *dev = crtc->base.dev;
14823 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020014824 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020014825
Daniel Vetter24929352012-07-02 20:28:59 +020014826 /* Clear any frame start delays used for debugging left by the BIOS */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014827 reg = PIPECONF(crtc->config->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020014828 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
14829
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030014830 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010014831 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030014832 if (crtc->active) {
14833 update_scanline_offset(crtc);
Daniel Vetter96256042015-02-13 21:03:42 +010014834 drm_crtc_vblank_on(&crtc->base);
14835 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030014836
Daniel Vetter24929352012-07-02 20:28:59 +020014837 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020014838 * disable the crtc (and hence change the state) if it is wrong. Note
14839 * that gen4+ has a fixed plane -> pipe mapping. */
14840 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020014841 struct intel_connector *connector;
14842 bool plane;
14843
Daniel Vetter24929352012-07-02 20:28:59 +020014844 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
14845 crtc->base.base.id);
14846
14847 /* Pipe has the wrong plane attached and the plane is active.
14848 * Temporarily change the plane mapping and disable everything
14849 * ... */
14850 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030014851 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020014852 crtc->plane = !plane;
Maarten Lankhorstce22dba2015-04-21 17:12:56 +030014853 intel_crtc_disable_planes(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020014854 dev_priv->display.crtc_disable(&crtc->base);
14855 crtc->plane = plane;
14856
14857 /* ... and break all links. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014858 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020014859 if (connector->encoder->base.crtc != &crtc->base)
14860 continue;
14861
Egbert Eich7f1950f2014-04-25 10:56:22 +020014862 connector->base.dpms = DRM_MODE_DPMS_OFF;
14863 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020014864 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020014865 /* multiple connectors may have the same encoder:
14866 * handle them and break crtc link separately */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014867 for_each_intel_connector(dev, connector)
Egbert Eich7f1950f2014-04-25 10:56:22 +020014868 if (connector->encoder->base.crtc == &crtc->base) {
14869 connector->encoder->base.crtc = NULL;
14870 connector->encoder->connectors_active = false;
14871 }
Daniel Vetter24929352012-07-02 20:28:59 +020014872
14873 WARN_ON(crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080014874 crtc->base.state->enable = false;
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020014875 crtc->base.state->active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020014876 crtc->base.enabled = false;
14877 }
Daniel Vetter24929352012-07-02 20:28:59 +020014878
Daniel Vetter7fad7982012-07-04 17:51:47 +020014879 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
14880 crtc->pipe == PIPE_A && !crtc->active) {
14881 /* BIOS forgot to enable pipe A, this mostly happens after
14882 * resume. Force-enable the pipe to fix this, the update_dpms
14883 * call below we restore the pipe to the right state, but leave
14884 * the required bits on. */
14885 intel_enable_pipe_a(dev);
14886 }
14887
Daniel Vetter24929352012-07-02 20:28:59 +020014888 /* Adjust the state of the output pipe according to whether we
14889 * have active connectors/encoders. */
14890 intel_crtc_update_dpms(&crtc->base);
14891
Matt Roper83d65732015-02-25 13:12:16 -080014892 if (crtc->active != crtc->base.state->enable) {
Daniel Vetter24929352012-07-02 20:28:59 +020014893 struct intel_encoder *encoder;
14894
14895 /* This can happen either due to bugs in the get_hw_state
14896 * functions or because the pipe is force-enabled due to the
14897 * pipe A quirk. */
14898 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
14899 crtc->base.base.id,
Matt Roper83d65732015-02-25 13:12:16 -080014900 crtc->base.state->enable ? "enabled" : "disabled",
Daniel Vetter24929352012-07-02 20:28:59 +020014901 crtc->active ? "enabled" : "disabled");
14902
Matt Roper83d65732015-02-25 13:12:16 -080014903 crtc->base.state->enable = crtc->active;
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020014904 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020014905 crtc->base.enabled = crtc->active;
14906
14907 /* Because we only establish the connector -> encoder ->
14908 * crtc links if something is active, this means the
14909 * crtc is now deactivated. Break the links. connector
14910 * -> encoder links are only establish when things are
14911 * actually up, hence no need to break them. */
14912 WARN_ON(crtc->active);
14913
14914 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
14915 WARN_ON(encoder->connectors_active);
14916 encoder->base.crtc = NULL;
14917 }
14918 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020014919
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030014920 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010014921 /*
14922 * We start out with underrun reporting disabled to avoid races.
14923 * For correct bookkeeping mark this on active crtcs.
14924 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020014925 * Also on gmch platforms we dont have any hardware bits to
14926 * disable the underrun reporting. Which means we need to start
14927 * out with underrun reporting disabled also on inactive pipes,
14928 * since otherwise we'll complain about the garbage we read when
14929 * e.g. coming up after runtime pm.
14930 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010014931 * No protection against concurrent access is required - at
14932 * worst a fifo underrun happens which also sets this to false.
14933 */
14934 crtc->cpu_fifo_underrun_disabled = true;
14935 crtc->pch_fifo_underrun_disabled = true;
14936 }
Daniel Vetter24929352012-07-02 20:28:59 +020014937}
14938
14939static void intel_sanitize_encoder(struct intel_encoder *encoder)
14940{
14941 struct intel_connector *connector;
14942 struct drm_device *dev = encoder->base.dev;
14943
14944 /* We need to check both for a crtc link (meaning that the
14945 * encoder is active and trying to read from a pipe) and the
14946 * pipe itself being active. */
14947 bool has_active_crtc = encoder->base.crtc &&
14948 to_intel_crtc(encoder->base.crtc)->active;
14949
14950 if (encoder->connectors_active && !has_active_crtc) {
14951 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
14952 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014953 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020014954
14955 /* Connector is active, but has no active pipe. This is
14956 * fallout from our resume register restoring. Disable
14957 * the encoder manually again. */
14958 if (encoder->base.crtc) {
14959 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
14960 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014961 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020014962 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030014963 if (encoder->post_disable)
14964 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020014965 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020014966 encoder->base.crtc = NULL;
14967 encoder->connectors_active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020014968
14969 /* Inconsistent output/port/pipe state happens presumably due to
14970 * a bug in one of the get_hw_state functions. Or someplace else
14971 * in our code, like the register restore mess on resume. Clamp
14972 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014973 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020014974 if (connector->encoder != encoder)
14975 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020014976 connector->base.dpms = DRM_MODE_DPMS_OFF;
14977 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020014978 }
14979 }
14980 /* Enabled encoders without active connectors will be fixed in
14981 * the crtc fixup. */
14982}
14983
Imre Deak04098752014-02-18 00:02:16 +020014984void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014985{
14986 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020014987 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014988
Imre Deak04098752014-02-18 00:02:16 +020014989 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
14990 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
14991 i915_disable_vga(dev);
14992 }
14993}
14994
14995void i915_redisable_vga(struct drm_device *dev)
14996{
14997 struct drm_i915_private *dev_priv = dev->dev_private;
14998
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030014999 /* This function can be called both from intel_modeset_setup_hw_state or
15000 * at a very early point in our resume sequence, where the power well
15001 * structures are not yet restored. Since this function is at a very
15002 * paranoid "someone might have enabled VGA while we were not looking"
15003 * level, just check if the power well is enabled instead of trying to
15004 * follow the "don't touch the power well if we don't need it" policy
15005 * the rest of the driver uses. */
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015006 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015007 return;
15008
Imre Deak04098752014-02-18 00:02:16 +020015009 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015010}
15011
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015012static bool primary_get_hw_state(struct intel_crtc *crtc)
15013{
15014 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
15015
15016 if (!crtc->active)
15017 return false;
15018
15019 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
15020}
15021
Daniel Vetter30e984d2013-06-05 13:34:17 +020015022static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015023{
15024 struct drm_i915_private *dev_priv = dev->dev_private;
15025 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015026 struct intel_crtc *crtc;
15027 struct intel_encoder *encoder;
15028 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020015029 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015030
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015031 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015032 struct drm_plane *primary = crtc->base.primary;
15033 struct intel_plane_state *plane_state;
15034
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015035 memset(crtc->config, 0, sizeof(*crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +020015036
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015037 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
Daniel Vetter99535992014-04-13 12:00:33 +020015038
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015039 crtc->active = dev_priv->display.get_pipe_config(crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015040 crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015041
Matt Roper83d65732015-02-25 13:12:16 -080015042 crtc->base.state->enable = crtc->active;
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020015043 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020015044 crtc->base.enabled = crtc->active;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015045
15046 plane_state = to_intel_plane_state(primary->state);
15047 plane_state->visible = primary_get_hw_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020015048
15049 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15050 crtc->base.base.id,
15051 crtc->active ? "enabled" : "disabled");
15052 }
15053
Daniel Vetter53589012013-06-05 13:34:16 +020015054 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15055 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15056
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015057 pll->on = pll->get_hw_state(dev_priv, pll,
15058 &pll->config.hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020015059 pll->active = 0;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015060 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015061 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015062 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
Daniel Vetter53589012013-06-05 13:34:16 +020015063 pll->active++;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015064 pll->config.crtc_mask |= 1 << crtc->pipe;
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015065 }
Daniel Vetter53589012013-06-05 13:34:16 +020015066 }
Daniel Vetter53589012013-06-05 13:34:16 +020015067
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015068 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015069 pll->name, pll->config.crtc_mask, pll->on);
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015070
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015071 if (pll->config.crtc_mask)
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015072 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
Daniel Vetter53589012013-06-05 13:34:16 +020015073 }
15074
Damien Lespiaub2784e12014-08-05 11:29:37 +010015075 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015076 pipe = 0;
15077
15078 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015079 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15080 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015081 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015082 } else {
15083 encoder->base.crtc = NULL;
15084 }
15085
15086 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015087 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020015088 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015089 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015090 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015091 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015092 }
15093
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015094 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015095 if (connector->get_hw_state(connector)) {
15096 connector->base.dpms = DRM_MODE_DPMS_ON;
15097 connector->encoder->connectors_active = true;
15098 connector->base.encoder = &connector->encoder->base;
15099 } else {
15100 connector->base.dpms = DRM_MODE_DPMS_OFF;
15101 connector->base.encoder = NULL;
15102 }
15103 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15104 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030015105 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015106 connector->base.encoder ? "enabled" : "disabled");
15107 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015108}
15109
15110/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
15111 * and i915 state tracking structures. */
15112void intel_modeset_setup_hw_state(struct drm_device *dev,
15113 bool force_restore)
15114{
15115 struct drm_i915_private *dev_priv = dev->dev_private;
15116 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015117 struct intel_crtc *crtc;
15118 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015119 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015120
15121 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015122
Jesse Barnesbabea612013-06-26 18:57:38 +030015123 /*
15124 * Now that we have the config, copy it to each CRTC struct
15125 * Note that this could go away if we move to using crtc_config
15126 * checking everywhere.
15127 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015128 for_each_intel_crtc(dev, crtc) {
Jani Nikulad330a952014-01-21 11:24:25 +020015129 if (crtc->active && i915.fastboot) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015130 intel_mode_from_pipe_config(&crtc->base.mode,
15131 crtc->config);
Jesse Barnesbabea612013-06-26 18:57:38 +030015132 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
15133 crtc->base.base.id);
15134 drm_mode_debug_printmodeline(&crtc->base.mode);
15135 }
15136 }
15137
Daniel Vetter24929352012-07-02 20:28:59 +020015138 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010015139 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015140 intel_sanitize_encoder(encoder);
15141 }
15142
Damien Lespiau055e3932014-08-18 13:49:10 +010015143 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020015144 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15145 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015146 intel_dump_pipe_config(crtc, crtc->config,
15147 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015148 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015149
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015150 intel_modeset_update_connector_atomic_state(dev);
15151
Daniel Vetter35c95372013-07-17 06:55:04 +020015152 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15153 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15154
15155 if (!pll->on || pll->active)
15156 continue;
15157
15158 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15159
15160 pll->disable(dev_priv, pll);
15161 pll->on = false;
15162 }
15163
Pradeep Bhat30789992014-11-04 17:06:45 +000015164 if (IS_GEN9(dev))
15165 skl_wm_get_hw_state(dev);
15166 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015167 ilk_wm_get_hw_state(dev);
15168
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015169 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015170 i915_redisable_vga(dev);
15171
Daniel Vetterf30da182013-04-11 20:22:50 +020015172 /*
15173 * We need to use raw interfaces for restoring state to avoid
15174 * checking (bogus) intermediate states.
15175 */
Damien Lespiau055e3932014-08-18 13:49:10 +010015176 for_each_pipe(dev_priv, pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070015177 struct drm_crtc *crtc =
15178 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020015179
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020015180 intel_crtc_restore_mode(crtc);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015181 }
15182 } else {
15183 intel_modeset_update_staged_output_state(dev);
15184 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020015185
15186 intel_modeset_check_state(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015187}
15188
15189void intel_modeset_gem_init(struct drm_device *dev)
15190{
Jesse Barnes92122782014-10-09 12:57:42 -070015191 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015192 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070015193 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015194 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015195
Imre Deakae484342014-03-31 15:10:44 +030015196 mutex_lock(&dev->struct_mutex);
15197 intel_init_gt_powersave(dev);
15198 mutex_unlock(&dev->struct_mutex);
15199
Jesse Barnes92122782014-10-09 12:57:42 -070015200 /*
15201 * There may be no VBT; and if the BIOS enabled SSC we can
15202 * just keep using it to avoid unnecessary flicker. Whereas if the
15203 * BIOS isn't using it, don't assume it will work even if the VBT
15204 * indicates as much.
15205 */
15206 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
15207 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15208 DREF_SSC1_ENABLE);
15209
Chris Wilson1833b132012-05-09 11:56:28 +010015210 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020015211
15212 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015213
15214 /*
15215 * Make sure any fbs we allocated at startup are properly
15216 * pinned & fenced. When we do the allocation it's too early
15217 * for this.
15218 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010015219 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070015220 obj = intel_fb_obj(c->primary->fb);
15221 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080015222 continue;
15223
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015224 mutex_lock(&dev->struct_mutex);
15225 ret = intel_pin_and_fence_fb_obj(c->primary,
15226 c->primary->fb,
15227 c->primary->state,
15228 NULL);
15229 mutex_unlock(&dev->struct_mutex);
15230 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080015231 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15232 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100015233 drm_framebuffer_unreference(c->primary->fb);
15234 c->primary->fb = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080015235 update_state_fb(c->primary);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015236 }
15237 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015238
15239 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015240}
15241
Imre Deak4932e2c2014-02-11 17:12:48 +020015242void intel_connector_unregister(struct intel_connector *intel_connector)
15243{
15244 struct drm_connector *connector = &intel_connector->base;
15245
15246 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010015247 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015248}
15249
Jesse Barnes79e53942008-11-07 14:24:08 -080015250void intel_modeset_cleanup(struct drm_device *dev)
15251{
Jesse Barnes652c3932009-08-17 13:31:43 -070015252 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid9255d52013-09-26 20:05:59 -030015253 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070015254
Imre Deak2eb52522014-11-19 15:30:05 +020015255 intel_disable_gt_powersave(dev);
15256
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015257 intel_backlight_unregister(dev);
15258
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015259 /*
15260 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020015261 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015262 * experience fancy races otherwise.
15263 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020015264 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070015265
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015266 /*
15267 * Due to the hpd irq storm handling the hotplug work can re-arm the
15268 * poll handlers. Hence disable polling after hpd handling is shut down.
15269 */
Keith Packardf87ea762010-10-03 19:36:26 -070015270 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015271
Jesse Barnes652c3932009-08-17 13:31:43 -070015272 mutex_lock(&dev->struct_mutex);
15273
Jesse Barnes723bfd72010-10-07 16:01:13 -070015274 intel_unregister_dsm_handler();
15275
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020015276 intel_fbc_disable(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070015277
Kristian Høgsberg69341a52009-11-11 12:19:17 -050015278 mutex_unlock(&dev->struct_mutex);
15279
Chris Wilson1630fe72011-07-08 12:22:42 +010015280 /* flush any delayed tasks or pending work */
15281 flush_scheduled_work();
15282
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015283 /* destroy the backlight and sysfs files before encoders/connectors */
15284 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020015285 struct intel_connector *intel_connector;
15286
15287 intel_connector = to_intel_connector(connector);
15288 intel_connector->unregister(intel_connector);
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015289 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030015290
Jesse Barnes79e53942008-11-07 14:24:08 -080015291 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010015292
15293 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030015294
15295 mutex_lock(&dev->struct_mutex);
15296 intel_cleanup_gt_powersave(dev);
15297 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080015298}
15299
Dave Airlie28d52042009-09-21 14:33:58 +100015300/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080015301 * Return which encoder is currently attached for connector.
15302 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010015303struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080015304{
Chris Wilsondf0e9242010-09-09 16:20:55 +010015305 return &intel_attached_encoder(connector)->base;
15306}
Jesse Barnes79e53942008-11-07 14:24:08 -080015307
Chris Wilsondf0e9242010-09-09 16:20:55 +010015308void intel_connector_attach_encoder(struct intel_connector *connector,
15309 struct intel_encoder *encoder)
15310{
15311 connector->encoder = encoder;
15312 drm_mode_connector_attach_encoder(&connector->base,
15313 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080015314}
Dave Airlie28d52042009-09-21 14:33:58 +100015315
15316/*
15317 * set vga decode state - true == enable VGA decode
15318 */
15319int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15320{
15321 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000015322 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100015323 u16 gmch_ctrl;
15324
Chris Wilson75fa0412014-02-07 18:37:02 -020015325 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15326 DRM_ERROR("failed to read control word\n");
15327 return -EIO;
15328 }
15329
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020015330 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15331 return 0;
15332
Dave Airlie28d52042009-09-21 14:33:58 +100015333 if (state)
15334 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15335 else
15336 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020015337
15338 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15339 DRM_ERROR("failed to write control word\n");
15340 return -EIO;
15341 }
15342
Dave Airlie28d52042009-09-21 14:33:58 +100015343 return 0;
15344}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015345
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015346struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015347
15348 u32 power_well_driver;
15349
Chris Wilson63b66e52013-08-08 15:12:06 +020015350 int num_transcoders;
15351
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015352 struct intel_cursor_error_state {
15353 u32 control;
15354 u32 position;
15355 u32 base;
15356 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010015357 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015358
15359 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015360 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015361 u32 source;
Imre Deakf301b1e12014-04-18 15:55:04 +030015362 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010015363 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015364
15365 struct intel_plane_error_state {
15366 u32 control;
15367 u32 stride;
15368 u32 size;
15369 u32 pos;
15370 u32 addr;
15371 u32 surface;
15372 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010015373 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020015374
15375 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015376 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020015377 enum transcoder cpu_transcoder;
15378
15379 u32 conf;
15380
15381 u32 htotal;
15382 u32 hblank;
15383 u32 hsync;
15384 u32 vtotal;
15385 u32 vblank;
15386 u32 vsync;
15387 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015388};
15389
15390struct intel_display_error_state *
15391intel_display_capture_error_state(struct drm_device *dev)
15392{
Jani Nikulafbee40d2014-03-31 14:27:18 +030015393 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015394 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020015395 int transcoders[] = {
15396 TRANSCODER_A,
15397 TRANSCODER_B,
15398 TRANSCODER_C,
15399 TRANSCODER_EDP,
15400 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015401 int i;
15402
Chris Wilson63b66e52013-08-08 15:12:06 +020015403 if (INTEL_INFO(dev)->num_pipes == 0)
15404 return NULL;
15405
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015406 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015407 if (error == NULL)
15408 return NULL;
15409
Imre Deak190be112013-11-25 17:15:31 +020015410 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015411 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15412
Damien Lespiau055e3932014-08-18 13:49:10 +010015413 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020015414 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015415 __intel_display_power_is_enabled(dev_priv,
15416 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020015417 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015418 continue;
15419
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030015420 error->cursor[i].control = I915_READ(CURCNTR(i));
15421 error->cursor[i].position = I915_READ(CURPOS(i));
15422 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015423
15424 error->plane[i].control = I915_READ(DSPCNTR(i));
15425 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015426 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030015427 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015428 error->plane[i].pos = I915_READ(DSPPOS(i));
15429 }
Paulo Zanonica291362013-03-06 20:03:14 -030015430 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15431 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015432 if (INTEL_INFO(dev)->gen >= 4) {
15433 error->plane[i].surface = I915_READ(DSPSURF(i));
15434 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15435 }
15436
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015437 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e12014-04-18 15:55:04 +030015438
Sonika Jindal3abfce72014-07-21 15:23:43 +053015439 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e12014-04-18 15:55:04 +030015440 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020015441 }
15442
15443 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
15444 if (HAS_DDI(dev_priv->dev))
15445 error->num_transcoders++; /* Account for eDP. */
15446
15447 for (i = 0; i < error->num_transcoders; i++) {
15448 enum transcoder cpu_transcoder = transcoders[i];
15449
Imre Deakddf9c532013-11-27 22:02:02 +020015450 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015451 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020015452 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015453 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015454 continue;
15455
Chris Wilson63b66e52013-08-08 15:12:06 +020015456 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15457
15458 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15459 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15460 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15461 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15462 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15463 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15464 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015465 }
15466
15467 return error;
15468}
15469
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015470#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15471
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015472void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015473intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015474 struct drm_device *dev,
15475 struct intel_display_error_state *error)
15476{
Damien Lespiau055e3932014-08-18 13:49:10 +010015477 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015478 int i;
15479
Chris Wilson63b66e52013-08-08 15:12:06 +020015480 if (!error)
15481 return;
15482
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015483 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020015484 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015485 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015486 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010015487 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015488 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020015489 err_printf(m, " Power: %s\n",
15490 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015491 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e12014-04-18 15:55:04 +030015492 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015493
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015494 err_printf(m, "Plane [%d]:\n", i);
15495 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15496 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015497 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015498 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15499 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015500 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030015501 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015502 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015503 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015504 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15505 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015506 }
15507
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015508 err_printf(m, "Cursor [%d]:\n", i);
15509 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15510 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15511 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015512 }
Chris Wilson63b66e52013-08-08 15:12:06 +020015513
15514 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010015515 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020015516 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015517 err_printf(m, " Power: %s\n",
15518 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020015519 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15520 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15521 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15522 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15523 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15524 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15525 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15526 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015527}
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015528
15529void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
15530{
15531 struct intel_crtc *crtc;
15532
15533 for_each_intel_crtc(dev, crtc) {
15534 struct intel_unpin_work *work;
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015535
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015536 spin_lock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015537
15538 work = crtc->unpin_work;
15539
15540 if (work && work->event &&
15541 work->event->base.file_priv == file) {
15542 kfree(work->event);
15543 work->event = NULL;
15544 }
15545
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015546 spin_unlock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015547 }
15548}