blob: afdc065e0af1604cc868cfee0133ff1da61dc04f [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 supported by all gen */
49#define COMMON_PRIMARY_FORMATS \
50 DRM_FORMAT_C8, \
51 DRM_FORMAT_RGB565, \
52 DRM_FORMAT_XRGB8888, \
53 DRM_FORMAT_ARGB8888
54
55/* Primary plane formats for gen <= 3 */
56static const uint32_t intel_primary_formats_gen2[] = {
57 COMMON_PRIMARY_FORMATS,
58 DRM_FORMAT_XRGB1555,
59 DRM_FORMAT_ARGB1555,
60};
61
62/* Primary plane formats for gen >= 4 */
63static const uint32_t intel_primary_formats_gen4[] = {
64 COMMON_PRIMARY_FORMATS, \
65 DRM_FORMAT_XBGR8888,
66 DRM_FORMAT_ABGR8888,
67 DRM_FORMAT_XRGB2101010,
68 DRM_FORMAT_ARGB2101010,
69 DRM_FORMAT_XBGR2101010,
70 DRM_FORMAT_ABGR2101010,
71};
72
Matt Roper3d7d6512014-06-10 08:28:13 -070073/* Cursor formats */
74static const uint32_t intel_cursor_formats[] = {
75 DRM_FORMAT_ARGB8888,
76};
77
Chris Wilson6b383a72010-09-13 13:54:26 +010078static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
Jesse Barnes79e53942008-11-07 14:24:08 -080079
Jesse Barnesf1f644d2013-06-27 00:39:25 +030080static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020081 struct intel_crtc_state *pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +030082static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020083 struct intel_crtc_state *pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030084
Damien Lespiaue7457a92013-08-08 22:28:59 +010085static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020086 int x, int y, struct drm_framebuffer *old_fb,
87 struct drm_atomic_state *state);
Jesse Barneseb1bfe82014-02-12 12:26:25 -080088static int intel_framebuffer_init(struct drm_device *dev,
89 struct intel_framebuffer *ifb,
90 struct drm_mode_fb_cmd2 *mode_cmd,
91 struct drm_i915_gem_object *obj);
Daniel Vetter5b18e572014-04-24 23:55:06 +020092static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
93static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +020094static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -070095 struct intel_link_m_n *m_n,
96 struct intel_link_m_n *m2_n2);
Daniel Vetter29407aa2014-04-24 23:55:08 +020097static void ironlake_set_pipeconf(struct drm_crtc *crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +020098static void haswell_set_pipeconf(struct drm_crtc *crtc);
99static void intel_set_pipe_csc(struct drm_crtc *crtc);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200100static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200101 const struct intel_crtc_state *pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200102static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200103 const struct intel_crtc_state *pipe_config);
Matt Roperea2c67b2014-12-23 10:41:52 -0800104static void intel_begin_crtc_commit(struct drm_crtc *crtc);
105static void intel_finish_crtc_commit(struct drm_crtc *crtc);
Chandra Konduru549e2bf2015-04-07 15:28:38 -0700106static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
107 struct intel_crtc_state *crtc_state);
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200108static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
109 int num_connectors);
Maarten Lankhorstce22dba2015-04-21 17:12:56 +0300110static void intel_crtc_enable_planes(struct drm_crtc *crtc);
111static void intel_crtc_disable_planes(struct drm_crtc *crtc);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100112
Dave Airlie0e32b392014-05-02 14:02:48 +1000113static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
114{
115 if (!connector->mst_port)
116 return connector->encoder;
117 else
118 return &connector->mst_port->mst_encoders[pipe]->base;
119}
120
Jesse Barnes79e53942008-11-07 14:24:08 -0800121typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400122 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -0800123} intel_range_t;
124
125typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400126 int dot_limit;
127 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800128} intel_p2_t;
129
Ma Lingd4906092009-03-18 20:13:27 +0800130typedef struct intel_limit intel_limit_t;
131struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -0400132 intel_range_t dot, vco, n, m, m1, m2, p, p1;
133 intel_p2_t p2;
Ma Lingd4906092009-03-18 20:13:27 +0800134};
Jesse Barnes79e53942008-11-07 14:24:08 -0800135
Daniel Vetterd2acd212012-10-20 20:57:43 +0200136int
137intel_pch_rawclk(struct drm_device *dev)
138{
139 struct drm_i915_private *dev_priv = dev->dev_private;
140
141 WARN_ON(!HAS_PCH_SPLIT(dev));
142
143 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
144}
145
Chris Wilson021357a2010-09-07 20:54:59 +0100146static inline u32 /* units of 100MHz */
147intel_fdi_link_freq(struct drm_device *dev)
148{
Chris Wilson8b99e682010-10-13 09:59:17 +0100149 if (IS_GEN5(dev)) {
150 struct drm_i915_private *dev_priv = dev->dev_private;
151 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
152 } else
153 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +0100154}
155
Daniel Vetter5d536e22013-07-06 12:52:06 +0200156static const intel_limit_t intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400157 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200158 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200159 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400160 .m = { .min = 96, .max = 140 },
161 .m1 = { .min = 18, .max = 26 },
162 .m2 = { .min = 6, .max = 16 },
163 .p = { .min = 4, .max = 128 },
164 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700165 .p2 = { .dot_limit = 165000,
166 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700167};
168
Daniel Vetter5d536e22013-07-06 12:52:06 +0200169static const intel_limit_t intel_limits_i8xx_dvo = {
170 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200171 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200172 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200173 .m = { .min = 96, .max = 140 },
174 .m1 = { .min = 18, .max = 26 },
175 .m2 = { .min = 6, .max = 16 },
176 .p = { .min = 4, .max = 128 },
177 .p1 = { .min = 2, .max = 33 },
178 .p2 = { .dot_limit = 165000,
179 .p2_slow = 4, .p2_fast = 4 },
180};
181
Keith Packarde4b36692009-06-05 19:22:17 -0700182static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400183 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200184 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200185 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400186 .m = { .min = 96, .max = 140 },
187 .m1 = { .min = 18, .max = 26 },
188 .m2 = { .min = 6, .max = 16 },
189 .p = { .min = 4, .max = 128 },
190 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700191 .p2 = { .dot_limit = 165000,
192 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700193};
Eric Anholt273e27c2011-03-30 13:01:10 -0700194
Keith Packarde4b36692009-06-05 19:22:17 -0700195static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400196 .dot = { .min = 20000, .max = 400000 },
197 .vco = { .min = 1400000, .max = 2800000 },
198 .n = { .min = 1, .max = 6 },
199 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100200 .m1 = { .min = 8, .max = 18 },
201 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400202 .p = { .min = 5, .max = 80 },
203 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700204 .p2 = { .dot_limit = 200000,
205 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700206};
207
208static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400209 .dot = { .min = 20000, .max = 400000 },
210 .vco = { .min = 1400000, .max = 2800000 },
211 .n = { .min = 1, .max = 6 },
212 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100213 .m1 = { .min = 8, .max = 18 },
214 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400215 .p = { .min = 7, .max = 98 },
216 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700217 .p2 = { .dot_limit = 112000,
218 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700219};
220
Eric Anholt273e27c2011-03-30 13:01:10 -0700221
Keith Packarde4b36692009-06-05 19:22:17 -0700222static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700223 .dot = { .min = 25000, .max = 270000 },
224 .vco = { .min = 1750000, .max = 3500000},
225 .n = { .min = 1, .max = 4 },
226 .m = { .min = 104, .max = 138 },
227 .m1 = { .min = 17, .max = 23 },
228 .m2 = { .min = 5, .max = 11 },
229 .p = { .min = 10, .max = 30 },
230 .p1 = { .min = 1, .max = 3},
231 .p2 = { .dot_limit = 270000,
232 .p2_slow = 10,
233 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800234 },
Keith Packarde4b36692009-06-05 19:22:17 -0700235};
236
237static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700238 .dot = { .min = 22000, .max = 400000 },
239 .vco = { .min = 1750000, .max = 3500000},
240 .n = { .min = 1, .max = 4 },
241 .m = { .min = 104, .max = 138 },
242 .m1 = { .min = 16, .max = 23 },
243 .m2 = { .min = 5, .max = 11 },
244 .p = { .min = 5, .max = 80 },
245 .p1 = { .min = 1, .max = 8},
246 .p2 = { .dot_limit = 165000,
247 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700248};
249
250static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700251 .dot = { .min = 20000, .max = 115000 },
252 .vco = { .min = 1750000, .max = 3500000 },
253 .n = { .min = 1, .max = 3 },
254 .m = { .min = 104, .max = 138 },
255 .m1 = { .min = 17, .max = 23 },
256 .m2 = { .min = 5, .max = 11 },
257 .p = { .min = 28, .max = 112 },
258 .p1 = { .min = 2, .max = 8 },
259 .p2 = { .dot_limit = 0,
260 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800261 },
Keith Packarde4b36692009-06-05 19:22:17 -0700262};
263
264static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700265 .dot = { .min = 80000, .max = 224000 },
266 .vco = { .min = 1750000, .max = 3500000 },
267 .n = { .min = 1, .max = 3 },
268 .m = { .min = 104, .max = 138 },
269 .m1 = { .min = 17, .max = 23 },
270 .m2 = { .min = 5, .max = 11 },
271 .p = { .min = 14, .max = 42 },
272 .p1 = { .min = 2, .max = 6 },
273 .p2 = { .dot_limit = 0,
274 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800275 },
Keith Packarde4b36692009-06-05 19:22:17 -0700276};
277
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500278static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400279 .dot = { .min = 20000, .max = 400000},
280 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700281 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400282 .n = { .min = 3, .max = 6 },
283 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700284 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400285 .m1 = { .min = 0, .max = 0 },
286 .m2 = { .min = 0, .max = 254 },
287 .p = { .min = 5, .max = 80 },
288 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700289 .p2 = { .dot_limit = 200000,
290 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700291};
292
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500293static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400294 .dot = { .min = 20000, .max = 400000 },
295 .vco = { .min = 1700000, .max = 3500000 },
296 .n = { .min = 3, .max = 6 },
297 .m = { .min = 2, .max = 256 },
298 .m1 = { .min = 0, .max = 0 },
299 .m2 = { .min = 0, .max = 254 },
300 .p = { .min = 7, .max = 112 },
301 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700302 .p2 = { .dot_limit = 112000,
303 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700304};
305
Eric Anholt273e27c2011-03-30 13:01:10 -0700306/* Ironlake / Sandybridge
307 *
308 * We calculate clock using (register_value + 2) for N/M1/M2, so here
309 * the range value for them is (actual_value - 2).
310 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800311static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700312 .dot = { .min = 25000, .max = 350000 },
313 .vco = { .min = 1760000, .max = 3510000 },
314 .n = { .min = 1, .max = 5 },
315 .m = { .min = 79, .max = 127 },
316 .m1 = { .min = 12, .max = 22 },
317 .m2 = { .min = 5, .max = 9 },
318 .p = { .min = 5, .max = 80 },
319 .p1 = { .min = 1, .max = 8 },
320 .p2 = { .dot_limit = 225000,
321 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700322};
323
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800324static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700325 .dot = { .min = 25000, .max = 350000 },
326 .vco = { .min = 1760000, .max = 3510000 },
327 .n = { .min = 1, .max = 3 },
328 .m = { .min = 79, .max = 118 },
329 .m1 = { .min = 12, .max = 22 },
330 .m2 = { .min = 5, .max = 9 },
331 .p = { .min = 28, .max = 112 },
332 .p1 = { .min = 2, .max = 8 },
333 .p2 = { .dot_limit = 225000,
334 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800335};
336
337static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700338 .dot = { .min = 25000, .max = 350000 },
339 .vco = { .min = 1760000, .max = 3510000 },
340 .n = { .min = 1, .max = 3 },
341 .m = { .min = 79, .max = 127 },
342 .m1 = { .min = 12, .max = 22 },
343 .m2 = { .min = 5, .max = 9 },
344 .p = { .min = 14, .max = 56 },
345 .p1 = { .min = 2, .max = 8 },
346 .p2 = { .dot_limit = 225000,
347 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800348};
349
Eric Anholt273e27c2011-03-30 13:01:10 -0700350/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800351static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700352 .dot = { .min = 25000, .max = 350000 },
353 .vco = { .min = 1760000, .max = 3510000 },
354 .n = { .min = 1, .max = 2 },
355 .m = { .min = 79, .max = 126 },
356 .m1 = { .min = 12, .max = 22 },
357 .m2 = { .min = 5, .max = 9 },
358 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400359 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700360 .p2 = { .dot_limit = 225000,
361 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800362};
363
364static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700365 .dot = { .min = 25000, .max = 350000 },
366 .vco = { .min = 1760000, .max = 3510000 },
367 .n = { .min = 1, .max = 3 },
368 .m = { .min = 79, .max = 126 },
369 .m1 = { .min = 12, .max = 22 },
370 .m2 = { .min = 5, .max = 9 },
371 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400372 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700373 .p2 = { .dot_limit = 225000,
374 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800375};
376
Ville Syrjälädc730512013-09-24 21:26:30 +0300377static const intel_limit_t intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300378 /*
379 * These are the data rate limits (measured in fast clocks)
380 * since those are the strictest limits we have. The fast
381 * clock and actual rate limits are more relaxed, so checking
382 * them would make no difference.
383 */
384 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200385 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700386 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700387 .m1 = { .min = 2, .max = 3 },
388 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300389 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300390 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700391};
392
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300393static const intel_limit_t intel_limits_chv = {
394 /*
395 * These are the data rate limits (measured in fast clocks)
396 * since those are the strictest limits we have. The fast
397 * clock and actual rate limits are more relaxed, so checking
398 * them would make no difference.
399 */
400 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200401 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300402 .n = { .min = 1, .max = 1 },
403 .m1 = { .min = 2, .max = 2 },
404 .m2 = { .min = 24 << 22, .max = 175 << 22 },
405 .p1 = { .min = 2, .max = 4 },
406 .p2 = { .p2_slow = 1, .p2_fast = 14 },
407};
408
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200409static const intel_limit_t intel_limits_bxt = {
410 /* FIXME: find real dot limits */
411 .dot = { .min = 0, .max = INT_MAX },
412 .vco = { .min = 4800000, .max = 6480000 },
413 .n = { .min = 1, .max = 1 },
414 .m1 = { .min = 2, .max = 2 },
415 /* FIXME: find real m2 limits */
416 .m2 = { .min = 2 << 22, .max = 255 << 22 },
417 .p1 = { .min = 2, .max = 4 },
418 .p2 = { .p2_slow = 1, .p2_fast = 20 },
419};
420
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300421static void vlv_clock(int refclk, intel_clock_t *clock)
422{
423 clock->m = clock->m1 * clock->m2;
424 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200425 if (WARN_ON(clock->n == 0 || clock->p == 0))
426 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300427 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
428 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300429}
430
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300431/**
432 * Returns whether any output on the specified pipe is of the specified type
433 */
Damien Lespiau40935612014-10-29 11:16:59 +0000434bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300435{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300436 struct drm_device *dev = crtc->base.dev;
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300437 struct intel_encoder *encoder;
438
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300439 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300440 if (encoder->type == type)
441 return true;
442
443 return false;
444}
445
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200446/**
447 * Returns whether any output on the specified pipe will have the specified
448 * type after a staged modeset is complete, i.e., the same as
449 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
450 * encoder->crtc.
451 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200452static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
453 int type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200454{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200455 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300456 struct drm_connector *connector;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200457 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200458 struct intel_encoder *encoder;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200459 int i, num_connectors = 0;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200460
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300461 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200462 if (connector_state->crtc != crtc_state->base.crtc)
463 continue;
464
465 num_connectors++;
466
467 encoder = to_intel_encoder(connector_state->best_encoder);
468 if (encoder->type == type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200469 return true;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200470 }
471
472 WARN_ON(num_connectors == 0);
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200473
474 return false;
475}
476
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200477static const intel_limit_t *
478intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800479{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200480 struct drm_device *dev = crtc_state->base.crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800481 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800482
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200483 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100484 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000485 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800486 limit = &intel_limits_ironlake_dual_lvds_100m;
487 else
488 limit = &intel_limits_ironlake_dual_lvds;
489 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000490 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800491 limit = &intel_limits_ironlake_single_lvds_100m;
492 else
493 limit = &intel_limits_ironlake_single_lvds;
494 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200495 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800496 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800497
498 return limit;
499}
500
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200501static const intel_limit_t *
502intel_g4x_limit(struct intel_crtc_state *crtc_state)
Ma Ling044c7c42009-03-18 20:13:23 +0800503{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200504 struct drm_device *dev = crtc_state->base.crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800505 const intel_limit_t *limit;
506
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200507 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100508 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700509 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800510 else
Keith Packarde4b36692009-06-05 19:22:17 -0700511 limit = &intel_limits_g4x_single_channel_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200512 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
513 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700514 limit = &intel_limits_g4x_hdmi;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200515 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700516 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800517 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700518 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800519
520 return limit;
521}
522
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200523static const intel_limit_t *
524intel_limit(struct intel_crtc_state *crtc_state, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800525{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200526 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800527 const intel_limit_t *limit;
528
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200529 if (IS_BROXTON(dev))
530 limit = &intel_limits_bxt;
531 else if (HAS_PCH_SPLIT(dev))
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200532 limit = intel_ironlake_limit(crtc_state, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800533 else if (IS_G4X(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200534 limit = intel_g4x_limit(crtc_state);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500535 } else if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200536 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500537 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800538 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500539 limit = &intel_limits_pineview_sdvo;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300540 } else if (IS_CHERRYVIEW(dev)) {
541 limit = &intel_limits_chv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700542 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjälädc730512013-09-24 21:26:30 +0300543 limit = &intel_limits_vlv;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100544 } else if (!IS_GEN2(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200545 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100546 limit = &intel_limits_i9xx_lvds;
547 else
548 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800549 } else {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200550 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700551 limit = &intel_limits_i8xx_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200552 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Keith Packarde4b36692009-06-05 19:22:17 -0700553 limit = &intel_limits_i8xx_dvo;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200554 else
555 limit = &intel_limits_i8xx_dac;
Jesse Barnes79e53942008-11-07 14:24:08 -0800556 }
557 return limit;
558}
559
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500560/* m1 is reserved as 0 in Pineview, n is a ring counter */
561static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800562{
Shaohua Li21778322009-02-23 15:19:16 +0800563 clock->m = clock->m2 + 2;
564 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200565 if (WARN_ON(clock->n == 0 || clock->p == 0))
566 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300567 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
568 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Shaohua Li21778322009-02-23 15:19:16 +0800569}
570
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200571static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
572{
573 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
574}
575
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200576static void i9xx_clock(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800577{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200578 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800579 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200580 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
581 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300582 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
583 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Jesse Barnes79e53942008-11-07 14:24:08 -0800584}
585
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300586static void chv_clock(int refclk, intel_clock_t *clock)
587{
588 clock->m = clock->m1 * clock->m2;
589 clock->p = clock->p1 * clock->p2;
590 if (WARN_ON(clock->n == 0 || clock->p == 0))
591 return;
592 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
593 clock->n << 22);
594 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
595}
596
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800597#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800598/**
599 * Returns whether the given set of divisors are valid for a given refclk with
600 * the given connectors.
601 */
602
Chris Wilson1b894b52010-12-14 20:04:54 +0000603static bool intel_PLL_is_valid(struct drm_device *dev,
604 const intel_limit_t *limit,
605 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800606{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300607 if (clock->n < limit->n.min || limit->n.max < clock->n)
608 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800609 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400610 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800611 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400612 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800613 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400614 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300615
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200616 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300617 if (clock->m1 <= clock->m2)
618 INTELPllInvalid("m1 <= m2\n");
619
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200620 if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300621 if (clock->p < limit->p.min || limit->p.max < clock->p)
622 INTELPllInvalid("p out of range\n");
623 if (clock->m < limit->m.min || limit->m.max < clock->m)
624 INTELPllInvalid("m out of range\n");
625 }
626
Jesse Barnes79e53942008-11-07 14:24:08 -0800627 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400628 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800629 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
630 * connector, etc., rather than just a single range.
631 */
632 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400633 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800634
635 return true;
636}
637
Ma Lingd4906092009-03-18 20:13:27 +0800638static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200639i9xx_find_best_dpll(const intel_limit_t *limit,
640 struct intel_crtc_state *crtc_state,
Sean Paulcec2f352012-01-10 15:09:36 -0800641 int target, int refclk, intel_clock_t *match_clock,
642 intel_clock_t *best_clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800643{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200644 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300645 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800646 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800647 int err = target;
648
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200649 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800650 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100651 * For LVDS just rely on its current settings for dual-channel.
652 * We haven't figured out how to reliably set up different
653 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800654 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100655 if (intel_is_dual_link_lvds(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800656 clock.p2 = limit->p2.p2_fast;
657 else
658 clock.p2 = limit->p2.p2_slow;
659 } else {
660 if (target < limit->p2.dot_limit)
661 clock.p2 = limit->p2.p2_slow;
662 else
663 clock.p2 = limit->p2.p2_fast;
664 }
665
Akshay Joshi0206e352011-08-16 15:34:10 -0400666 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800667
Zhao Yakui42158662009-11-20 11:24:18 +0800668 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
669 clock.m1++) {
670 for (clock.m2 = limit->m2.min;
671 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200672 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800673 break;
674 for (clock.n = limit->n.min;
675 clock.n <= limit->n.max; clock.n++) {
676 for (clock.p1 = limit->p1.min;
677 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800678 int this_err;
679
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200680 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000681 if (!intel_PLL_is_valid(dev, limit,
682 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800683 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800684 if (match_clock &&
685 clock.p != match_clock->p)
686 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800687
688 this_err = abs(clock.dot - target);
689 if (this_err < err) {
690 *best_clock = clock;
691 err = this_err;
692 }
693 }
694 }
695 }
696 }
697
698 return (err != target);
699}
700
Ma Lingd4906092009-03-18 20:13:27 +0800701static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200702pnv_find_best_dpll(const intel_limit_t *limit,
703 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200704 int target, int refclk, intel_clock_t *match_clock,
705 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200706{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200707 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300708 struct drm_device *dev = crtc->base.dev;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200709 intel_clock_t clock;
710 int err = target;
711
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200712 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200713 /*
714 * For LVDS just rely on its current settings for dual-channel.
715 * We haven't figured out how to reliably set up different
716 * single/dual channel state, if we even can.
717 */
718 if (intel_is_dual_link_lvds(dev))
719 clock.p2 = limit->p2.p2_fast;
720 else
721 clock.p2 = limit->p2.p2_slow;
722 } else {
723 if (target < limit->p2.dot_limit)
724 clock.p2 = limit->p2.p2_slow;
725 else
726 clock.p2 = limit->p2.p2_fast;
727 }
728
729 memset(best_clock, 0, sizeof(*best_clock));
730
731 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
732 clock.m1++) {
733 for (clock.m2 = limit->m2.min;
734 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200735 for (clock.n = limit->n.min;
736 clock.n <= limit->n.max; clock.n++) {
737 for (clock.p1 = limit->p1.min;
738 clock.p1 <= limit->p1.max; clock.p1++) {
739 int this_err;
740
741 pineview_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800742 if (!intel_PLL_is_valid(dev, limit,
743 &clock))
744 continue;
745 if (match_clock &&
746 clock.p != match_clock->p)
747 continue;
748
749 this_err = abs(clock.dot - target);
750 if (this_err < err) {
751 *best_clock = clock;
752 err = this_err;
753 }
754 }
755 }
756 }
757 }
758
759 return (err != target);
760}
761
Ma Lingd4906092009-03-18 20:13:27 +0800762static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200763g4x_find_best_dpll(const intel_limit_t *limit,
764 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200765 int target, int refclk, intel_clock_t *match_clock,
766 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800767{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200768 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300769 struct drm_device *dev = crtc->base.dev;
Ma Lingd4906092009-03-18 20:13:27 +0800770 intel_clock_t clock;
771 int max_n;
772 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400773 /* approximately equals target * 0.00585 */
774 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800775 found = false;
776
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200777 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100778 if (intel_is_dual_link_lvds(dev))
Ma Lingd4906092009-03-18 20:13:27 +0800779 clock.p2 = limit->p2.p2_fast;
780 else
781 clock.p2 = limit->p2.p2_slow;
782 } else {
783 if (target < limit->p2.dot_limit)
784 clock.p2 = limit->p2.p2_slow;
785 else
786 clock.p2 = limit->p2.p2_fast;
787 }
788
789 memset(best_clock, 0, sizeof(*best_clock));
790 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200791 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800792 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200793 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800794 for (clock.m1 = limit->m1.max;
795 clock.m1 >= limit->m1.min; clock.m1--) {
796 for (clock.m2 = limit->m2.max;
797 clock.m2 >= limit->m2.min; clock.m2--) {
798 for (clock.p1 = limit->p1.max;
799 clock.p1 >= limit->p1.min; clock.p1--) {
800 int this_err;
801
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200802 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000803 if (!intel_PLL_is_valid(dev, limit,
804 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800805 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000806
807 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800808 if (this_err < err_most) {
809 *best_clock = clock;
810 err_most = this_err;
811 max_n = clock.n;
812 found = true;
813 }
814 }
815 }
816 }
817 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800818 return found;
819}
Ma Lingd4906092009-03-18 20:13:27 +0800820
Imre Deakd5dd62b2015-03-17 11:40:03 +0200821/*
822 * Check if the calculated PLL configuration is more optimal compared to the
823 * best configuration and error found so far. Return the calculated error.
824 */
825static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
826 const intel_clock_t *calculated_clock,
827 const intel_clock_t *best_clock,
828 unsigned int best_error_ppm,
829 unsigned int *error_ppm)
830{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200831 /*
832 * For CHV ignore the error and consider only the P value.
833 * Prefer a bigger P value based on HW requirements.
834 */
835 if (IS_CHERRYVIEW(dev)) {
836 *error_ppm = 0;
837
838 return calculated_clock->p > best_clock->p;
839 }
840
Imre Deak24be4e42015-03-17 11:40:04 +0200841 if (WARN_ON_ONCE(!target_freq))
842 return false;
843
Imre Deakd5dd62b2015-03-17 11:40:03 +0200844 *error_ppm = div_u64(1000000ULL *
845 abs(target_freq - calculated_clock->dot),
846 target_freq);
847 /*
848 * Prefer a better P value over a better (smaller) error if the error
849 * is small. Ensure this preference for future configurations too by
850 * setting the error to 0.
851 */
852 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
853 *error_ppm = 0;
854
855 return true;
856 }
857
858 return *error_ppm + 10 < best_error_ppm;
859}
860
Zhenyu Wang2c072452009-06-05 15:38:42 +0800861static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200862vlv_find_best_dpll(const intel_limit_t *limit,
863 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200864 int target, int refclk, intel_clock_t *match_clock,
865 intel_clock_t *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700866{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200867 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300868 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300869 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300870 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300871 /* min update 19.2 MHz */
872 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300873 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700874
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300875 target *= 5; /* fast clock */
876
877 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700878
879 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300880 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300881 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300882 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300883 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300884 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700885 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300886 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200887 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300888
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300889 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
890 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300891
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300892 vlv_clock(refclk, &clock);
893
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300894 if (!intel_PLL_is_valid(dev, limit,
895 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300896 continue;
897
Imre Deakd5dd62b2015-03-17 11:40:03 +0200898 if (!vlv_PLL_is_optimal(dev, target,
899 &clock,
900 best_clock,
901 bestppm, &ppm))
902 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300903
Imre Deakd5dd62b2015-03-17 11:40:03 +0200904 *best_clock = clock;
905 bestppm = ppm;
906 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700907 }
908 }
909 }
910 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700911
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300912 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700913}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700914
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300915static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200916chv_find_best_dpll(const intel_limit_t *limit,
917 struct intel_crtc_state *crtc_state,
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300918 int target, int refclk, intel_clock_t *match_clock,
919 intel_clock_t *best_clock)
920{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200921 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300922 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200923 unsigned int best_error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300924 intel_clock_t clock;
925 uint64_t m2;
926 int found = false;
927
928 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200929 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300930
931 /*
932 * Based on hardware doc, the n always set to 1, and m1 always
933 * set to 2. If requires to support 200Mhz refclk, we need to
934 * revisit this because n may not 1 anymore.
935 */
936 clock.n = 1, clock.m1 = 2;
937 target *= 5; /* fast clock */
938
939 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
940 for (clock.p2 = limit->p2.p2_fast;
941 clock.p2 >= limit->p2.p2_slow;
942 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +0200943 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300944
945 clock.p = clock.p1 * clock.p2;
946
947 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
948 clock.n) << 22, refclk * clock.m1);
949
950 if (m2 > INT_MAX/clock.m1)
951 continue;
952
953 clock.m2 = m2;
954
955 chv_clock(refclk, &clock);
956
957 if (!intel_PLL_is_valid(dev, limit, &clock))
958 continue;
959
Imre Deak9ca3ba02015-03-17 11:40:05 +0200960 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
961 best_error_ppm, &error_ppm))
962 continue;
963
964 *best_clock = clock;
965 best_error_ppm = error_ppm;
966 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300967 }
968 }
969
970 return found;
971}
972
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200973bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
974 intel_clock_t *best_clock)
975{
976 int refclk = i9xx_get_refclk(crtc_state, 0);
977
978 return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
979 target_clock, refclk, NULL, best_clock);
980}
981
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300982bool intel_crtc_active(struct drm_crtc *crtc)
983{
984 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
985
986 /* Be paranoid as we can arrive here with only partial
987 * state retrieved from the hardware during setup.
988 *
Damien Lespiau241bfc32013-09-25 16:45:37 +0100989 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300990 * as Haswell has gained clock readout/fastboot support.
991 *
Dave Airlie66e514c2014-04-03 07:51:54 +1000992 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300993 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -0700994 *
995 * FIXME: The intel_crtc->active here should be switched to
996 * crtc->state->active once we have proper CRTC states wired up
997 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300998 */
Matt Roperc3d1f432015-03-09 10:19:23 -0700999 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001000 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001001}
1002
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001003enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1004 enum pipe pipe)
1005{
1006 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1007 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1008
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001009 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001010}
1011
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001012static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1013{
1014 struct drm_i915_private *dev_priv = dev->dev_private;
1015 u32 reg = PIPEDSL(pipe);
1016 u32 line1, line2;
1017 u32 line_mask;
1018
1019 if (IS_GEN2(dev))
1020 line_mask = DSL_LINEMASK_GEN2;
1021 else
1022 line_mask = DSL_LINEMASK_GEN3;
1023
1024 line1 = I915_READ(reg) & line_mask;
1025 mdelay(5);
1026 line2 = I915_READ(reg) & line_mask;
1027
1028 return line1 == line2;
1029}
1030
Keith Packardab7ad7f2010-10-03 00:33:06 -07001031/*
1032 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001033 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001034 *
1035 * After disabling a pipe, we can't wait for vblank in the usual way,
1036 * spinning on the vblank interrupt status bit, since we won't actually
1037 * see an interrupt when the pipe is disabled.
1038 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001039 * On Gen4 and above:
1040 * wait for the pipe register state bit to turn off
1041 *
1042 * Otherwise:
1043 * wait for the display line value to settle (it usually
1044 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001045 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001046 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001047static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001048{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001049 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001050 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001051 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001052 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001053
Keith Packardab7ad7f2010-10-03 00:33:06 -07001054 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001055 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001056
Keith Packardab7ad7f2010-10-03 00:33:06 -07001057 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +01001058 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1059 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001060 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001061 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001062 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001063 if (wait_for(pipe_dsl_stopped(dev, pipe), 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 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001066}
1067
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001068/*
1069 * ibx_digital_port_connected - is the specified port connected?
1070 * @dev_priv: i915 private structure
1071 * @port: the port to test
1072 *
1073 * Returns true if @port is connected, false otherwise.
1074 */
1075bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
1076 struct intel_digital_port *port)
1077{
1078 u32 bit;
1079
Damien Lespiauc36346e2012-12-13 16:09:03 +00001080 if (HAS_PCH_IBX(dev_priv->dev)) {
Robin Schroereba905b2014-05-18 02:24:50 +02001081 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +00001082 case PORT_B:
1083 bit = SDE_PORTB_HOTPLUG;
1084 break;
1085 case PORT_C:
1086 bit = SDE_PORTC_HOTPLUG;
1087 break;
1088 case PORT_D:
1089 bit = SDE_PORTD_HOTPLUG;
1090 break;
1091 default:
1092 return true;
1093 }
1094 } else {
Robin Schroereba905b2014-05-18 02:24:50 +02001095 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +00001096 case PORT_B:
1097 bit = SDE_PORTB_HOTPLUG_CPT;
1098 break;
1099 case PORT_C:
1100 bit = SDE_PORTC_HOTPLUG_CPT;
1101 break;
1102 case PORT_D:
1103 bit = SDE_PORTD_HOTPLUG_CPT;
1104 break;
1105 default:
1106 return true;
1107 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001108 }
1109
1110 return I915_READ(SDEISR) & bit;
1111}
1112
Jesse Barnesb24e7172011-01-04 15:09:30 -08001113static const char *state_string(bool enabled)
1114{
1115 return enabled ? "on" : "off";
1116}
1117
1118/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001119void assert_pll(struct drm_i915_private *dev_priv,
1120 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001121{
1122 int reg;
1123 u32 val;
1124 bool cur_state;
1125
1126 reg = DPLL(pipe);
1127 val = I915_READ(reg);
1128 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001129 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001130 "PLL state assertion failure (expected %s, current %s)\n",
1131 state_string(state), state_string(cur_state));
1132}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001133
Jani Nikula23538ef2013-08-27 15:12:22 +03001134/* XXX: the dsi pll is shared between MIPI DSI ports */
1135static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1136{
1137 u32 val;
1138 bool cur_state;
1139
1140 mutex_lock(&dev_priv->dpio_lock);
1141 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1142 mutex_unlock(&dev_priv->dpio_lock);
1143
1144 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001145 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001146 "DSI PLL state assertion failure (expected %s, current %s)\n",
1147 state_string(state), state_string(cur_state));
1148}
1149#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1150#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1151
Daniel Vetter55607e82013-06-16 21:42:39 +02001152struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +02001153intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -08001154{
Daniel Vettere2b78262013-06-07 23:10:03 +02001155 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1156
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001157 if (crtc->config->shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +02001158 return NULL;
1159
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001160 return &dev_priv->shared_dplls[crtc->config->shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +02001161}
1162
Jesse Barnesb24e7172011-01-04 15:09:30 -08001163/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +02001164void assert_shared_dpll(struct drm_i915_private *dev_priv,
1165 struct intel_shared_dpll *pll,
1166 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001167{
Jesse Barnes040484a2011-01-03 12:14:26 -08001168 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +02001169 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001170
Chris Wilson92b27b02012-05-20 18:10:50 +01001171 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +02001172 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001173 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001174
Daniel Vetter53589012013-06-05 13:34:16 +02001175 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Rob Clarke2c719b2014-12-15 13:56:32 -05001176 I915_STATE_WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +02001177 "%s assertion failure (expected %s, current %s)\n",
1178 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001179}
Jesse Barnes040484a2011-01-03 12:14:26 -08001180
1181static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1182 enum pipe pipe, bool state)
1183{
1184 int reg;
1185 u32 val;
1186 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001187 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1188 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001189
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001190 if (HAS_DDI(dev_priv->dev)) {
1191 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -02001192 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001193 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -02001194 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001195 } else {
1196 reg = FDI_TX_CTL(pipe);
1197 val = I915_READ(reg);
1198 cur_state = !!(val & FDI_TX_ENABLE);
1199 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001200 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001201 "FDI TX state assertion failure (expected %s, current %s)\n",
1202 state_string(state), state_string(cur_state));
1203}
1204#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1205#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1206
1207static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1208 enum pipe pipe, bool state)
1209{
1210 int reg;
1211 u32 val;
1212 bool cur_state;
1213
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001214 reg = FDI_RX_CTL(pipe);
1215 val = I915_READ(reg);
1216 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001217 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001218 "FDI RX state assertion failure (expected %s, current %s)\n",
1219 state_string(state), state_string(cur_state));
1220}
1221#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1222#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1223
1224static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1225 enum pipe pipe)
1226{
1227 int reg;
1228 u32 val;
1229
1230 /* ILK FDI PLL is always enabled */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001231 if (INTEL_INFO(dev_priv->dev)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001232 return;
1233
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001234 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001235 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001236 return;
1237
Jesse Barnes040484a2011-01-03 12:14:26 -08001238 reg = FDI_TX_CTL(pipe);
1239 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001240 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 -08001241}
1242
Daniel Vetter55607e82013-06-16 21:42:39 +02001243void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1244 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001245{
1246 int reg;
1247 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001248 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001249
1250 reg = FDI_RX_CTL(pipe);
1251 val = I915_READ(reg);
Daniel Vetter55607e82013-06-16 21:42:39 +02001252 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001253 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001254 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1255 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001256}
1257
Daniel Vetterb680c372014-09-19 18:27:27 +02001258void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1259 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001260{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001261 struct drm_device *dev = dev_priv->dev;
1262 int pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001263 u32 val;
1264 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001265 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001266
Jani Nikulabedd4db2014-08-22 15:04:13 +03001267 if (WARN_ON(HAS_DDI(dev)))
1268 return;
1269
1270 if (HAS_PCH_SPLIT(dev)) {
1271 u32 port_sel;
1272
Jesse Barnesea0760c2011-01-04 15:09:32 -08001273 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001274 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1275
1276 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1277 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1278 panel_pipe = PIPE_B;
1279 /* XXX: else fix for eDP */
1280 } else if (IS_VALLEYVIEW(dev)) {
1281 /* presumably write lock depends on pipe, not port select */
1282 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1283 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001284 } else {
1285 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001286 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1287 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001288 }
1289
1290 val = I915_READ(pp_reg);
1291 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001292 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001293 locked = false;
1294
Rob Clarke2c719b2014-12-15 13:56:32 -05001295 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001296 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001297 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001298}
1299
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001300static void assert_cursor(struct drm_i915_private *dev_priv,
1301 enum pipe pipe, bool state)
1302{
1303 struct drm_device *dev = dev_priv->dev;
1304 bool cur_state;
1305
Paulo Zanonid9d82082014-02-27 16:30:56 -03001306 if (IS_845G(dev) || IS_I865G(dev))
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001307 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001308 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001309 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001310
Rob Clarke2c719b2014-12-15 13:56:32 -05001311 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001312 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1313 pipe_name(pipe), state_string(state), state_string(cur_state));
1314}
1315#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1316#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1317
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001318void assert_pipe(struct drm_i915_private *dev_priv,
1319 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001320{
1321 int reg;
1322 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001323 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001324 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1325 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001326
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001327 /* if we need the pipe quirk it must be always on */
1328 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1329 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001330 state = true;
1331
Daniel Vetterf458ebb2014-09-30 10:56:39 +02001332 if (!intel_display_power_is_enabled(dev_priv,
Paulo Zanonib97186f2013-05-03 12:15:36 -03001333 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001334 cur_state = false;
1335 } else {
1336 reg = PIPECONF(cpu_transcoder);
1337 val = I915_READ(reg);
1338 cur_state = !!(val & PIPECONF_ENABLE);
1339 }
1340
Rob Clarke2c719b2014-12-15 13:56:32 -05001341 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001342 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001343 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001344}
1345
Chris Wilson931872f2012-01-16 23:01:13 +00001346static void assert_plane(struct drm_i915_private *dev_priv,
1347 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001348{
1349 int reg;
1350 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001351 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001352
1353 reg = DSPCNTR(plane);
1354 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001355 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001356 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001357 "plane %c assertion failure (expected %s, current %s)\n",
1358 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001359}
1360
Chris Wilson931872f2012-01-16 23:01:13 +00001361#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1362#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1363
Jesse Barnesb24e7172011-01-04 15:09:30 -08001364static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1365 enum pipe pipe)
1366{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001367 struct drm_device *dev = dev_priv->dev;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001368 int reg, i;
1369 u32 val;
1370 int cur_pipe;
1371
Ville Syrjälä653e1022013-06-04 13:49:05 +03001372 /* Primary planes are fixed to pipes on gen4+ */
1373 if (INTEL_INFO(dev)->gen >= 4) {
Adam Jackson28c057942011-10-07 14:38:42 -04001374 reg = DSPCNTR(pipe);
1375 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001376 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001377 "plane %c assertion failure, should be disabled but not\n",
1378 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001379 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001380 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001381
Jesse Barnesb24e7172011-01-04 15:09:30 -08001382 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001383 for_each_pipe(dev_priv, i) {
Jesse Barnesb24e7172011-01-04 15:09:30 -08001384 reg = DSPCNTR(i);
1385 val = I915_READ(reg);
1386 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1387 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001388 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001389 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1390 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001391 }
1392}
1393
Jesse Barnes19332d72013-03-28 09:55:38 -07001394static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1395 enum pipe pipe)
1396{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001397 struct drm_device *dev = dev_priv->dev;
Damien Lespiau1fe47782014-03-03 17:31:47 +00001398 int reg, sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001399 u32 val;
1400
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001401 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001402 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001403 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001404 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001405 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1406 sprite, pipe_name(pipe));
1407 }
1408 } else if (IS_VALLEYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001409 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +00001410 reg = SPCNTR(pipe, sprite);
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001411 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001412 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001413 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001414 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001415 }
1416 } else if (INTEL_INFO(dev)->gen >= 7) {
1417 reg = SPRCTL(pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001418 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001419 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001420 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001421 plane_name(pipe), pipe_name(pipe));
1422 } else if (INTEL_INFO(dev)->gen >= 5) {
1423 reg = DVSCNTR(pipe);
1424 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001425 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001426 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1427 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001428 }
1429}
1430
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001431static void assert_vblank_disabled(struct drm_crtc *crtc)
1432{
Rob Clarke2c719b2014-12-15 13:56:32 -05001433 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001434 drm_crtc_vblank_put(crtc);
1435}
1436
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001437static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
Jesse Barnes92f25842011-01-04 15:09:34 -08001438{
1439 u32 val;
1440 bool enabled;
1441
Rob Clarke2c719b2014-12-15 13:56:32 -05001442 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001443
Jesse Barnes92f25842011-01-04 15:09:34 -08001444 val = I915_READ(PCH_DREF_CONTROL);
1445 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1446 DREF_SUPERSPREAD_SOURCE_MASK));
Rob Clarke2c719b2014-12-15 13:56:32 -05001447 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
Jesse Barnes92f25842011-01-04 15:09:34 -08001448}
1449
Daniel Vetterab9412b2013-05-03 11:49:46 +02001450static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1451 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001452{
1453 int reg;
1454 u32 val;
1455 bool enabled;
1456
Daniel Vetterab9412b2013-05-03 11:49:46 +02001457 reg = PCH_TRANSCONF(pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001458 val = I915_READ(reg);
1459 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001460 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001461 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1462 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001463}
1464
Keith Packard4e634382011-08-06 10:39:45 -07001465static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1466 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001467{
1468 if ((val & DP_PORT_EN) == 0)
1469 return false;
1470
1471 if (HAS_PCH_CPT(dev_priv->dev)) {
1472 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1473 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1474 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1475 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001476 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1477 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1478 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001479 } else {
1480 if ((val & DP_PIPE_MASK) != (pipe << 30))
1481 return false;
1482 }
1483 return true;
1484}
1485
Keith Packard1519b992011-08-06 10:35:34 -07001486static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1487 enum pipe pipe, u32 val)
1488{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001489 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001490 return false;
1491
1492 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001493 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001494 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001495 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1496 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1497 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001498 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001499 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001500 return false;
1501 }
1502 return true;
1503}
1504
1505static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1506 enum pipe pipe, u32 val)
1507{
1508 if ((val & LVDS_PORT_EN) == 0)
1509 return false;
1510
1511 if (HAS_PCH_CPT(dev_priv->dev)) {
1512 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1513 return false;
1514 } else {
1515 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1516 return false;
1517 }
1518 return true;
1519}
1520
1521static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1522 enum pipe pipe, u32 val)
1523{
1524 if ((val & ADPA_DAC_ENABLE) == 0)
1525 return false;
1526 if (HAS_PCH_CPT(dev_priv->dev)) {
1527 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1528 return false;
1529 } else {
1530 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1531 return false;
1532 }
1533 return true;
1534}
1535
Jesse Barnes291906f2011-02-02 12:28:03 -08001536static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001537 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001538{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001539 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001540 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001541 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001542 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001543
Rob Clarke2c719b2014-12-15 13:56:32 -05001544 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001545 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001546 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001547}
1548
1549static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1550 enum pipe pipe, int reg)
1551{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001552 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001553 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001554 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001555 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001556
Rob Clarke2c719b2014-12-15 13:56:32 -05001557 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001558 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001559 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001560}
1561
1562static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1563 enum pipe pipe)
1564{
1565 int reg;
1566 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001567
Keith Packardf0575e92011-07-25 22:12:43 -07001568 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1569 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1570 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001571
1572 reg = PCH_ADPA;
1573 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001574 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001575 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001576 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001577
1578 reg = PCH_LVDS;
1579 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001580 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001581 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001582 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001583
Paulo Zanonie2debe92013-02-18 19:00:27 -03001584 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1585 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1586 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001587}
1588
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001589static void intel_init_dpio(struct drm_device *dev)
1590{
1591 struct drm_i915_private *dev_priv = dev->dev_private;
1592
1593 if (!IS_VALLEYVIEW(dev))
1594 return;
1595
Chon Ming Leea09cadd2014-04-09 13:28:14 +03001596 /*
1597 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1598 * CHV x1 PHY (DP/HDMI D)
1599 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1600 */
1601 if (IS_CHERRYVIEW(dev)) {
1602 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1603 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1604 } else {
1605 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1606 }
Jesse Barnes5382f5f352013-12-16 16:34:24 -08001607}
1608
Ville Syrjäläd288f652014-10-28 13:20:22 +02001609static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001610 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001611{
Daniel Vetter426115c2013-07-11 22:13:42 +02001612 struct drm_device *dev = crtc->base.dev;
1613 struct drm_i915_private *dev_priv = dev->dev_private;
1614 int reg = DPLL(crtc->pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02001615 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001616
Daniel Vetter426115c2013-07-11 22:13:42 +02001617 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001618
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001619 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001620 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1621
1622 /* PLL is protected by panel, make sure we can write it */
Jani Nikula6a9e7362014-08-22 15:06:35 +03001623 if (IS_MOBILE(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001624 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001625
Daniel Vetter426115c2013-07-11 22:13:42 +02001626 I915_WRITE(reg, dpll);
1627 POSTING_READ(reg);
1628 udelay(150);
1629
1630 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1631 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1632
Ville Syrjäläd288f652014-10-28 13:20:22 +02001633 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
Daniel Vetter426115c2013-07-11 22:13:42 +02001634 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001635
1636 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001637 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001638 POSTING_READ(reg);
1639 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001640 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001641 POSTING_READ(reg);
1642 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001643 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001644 POSTING_READ(reg);
1645 udelay(150); /* wait for warmup */
1646}
1647
Ville Syrjäläd288f652014-10-28 13:20:22 +02001648static void chv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001649 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001650{
1651 struct drm_device *dev = crtc->base.dev;
1652 struct drm_i915_private *dev_priv = dev->dev_private;
1653 int pipe = crtc->pipe;
1654 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001655 u32 tmp;
1656
1657 assert_pipe_disabled(dev_priv, crtc->pipe);
1658
1659 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1660
1661 mutex_lock(&dev_priv->dpio_lock);
1662
1663 /* Enable back the 10bit clock to display controller */
1664 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1665 tmp |= DPIO_DCLKP_EN;
1666 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1667
1668 /*
1669 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1670 */
1671 udelay(1);
1672
1673 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001674 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001675
1676 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001677 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001678 DRM_ERROR("PLL %d failed to lock\n", pipe);
1679
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001680 /* not sure when this should be written */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001681 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001682 POSTING_READ(DPLL_MD(pipe));
1683
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001684 mutex_unlock(&dev_priv->dpio_lock);
1685}
1686
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001687static int intel_num_dvo_pipes(struct drm_device *dev)
1688{
1689 struct intel_crtc *crtc;
1690 int count = 0;
1691
1692 for_each_intel_crtc(dev, crtc)
1693 count += crtc->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001694 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001695
1696 return count;
1697}
1698
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001699static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001700{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001701 struct drm_device *dev = crtc->base.dev;
1702 struct drm_i915_private *dev_priv = dev->dev_private;
1703 int reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001704 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001705
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001706 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001707
1708 /* No really, not for ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001709 BUG_ON(INTEL_INFO(dev)->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001710
1711 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001712 if (IS_MOBILE(dev) && !IS_I830(dev))
1713 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001714
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001715 /* Enable DVO 2x clock on both PLLs if necessary */
1716 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1717 /*
1718 * It appears to be important that we don't enable this
1719 * for the current pipe before otherwise configuring the
1720 * PLL. No idea how this should be handled if multiple
1721 * DVO outputs are enabled simultaneosly.
1722 */
1723 dpll |= DPLL_DVO_2X_MODE;
1724 I915_WRITE(DPLL(!crtc->pipe),
1725 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1726 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001727
1728 /* Wait for the clocks to stabilize. */
1729 POSTING_READ(reg);
1730 udelay(150);
1731
1732 if (INTEL_INFO(dev)->gen >= 4) {
1733 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001734 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001735 } else {
1736 /* The pixel multiplier can only be updated once the
1737 * DPLL is enabled and the clocks are stable.
1738 *
1739 * So write it again.
1740 */
1741 I915_WRITE(reg, dpll);
1742 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001743
1744 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001745 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001746 POSTING_READ(reg);
1747 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001748 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001749 POSTING_READ(reg);
1750 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001751 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001752 POSTING_READ(reg);
1753 udelay(150); /* wait for warmup */
1754}
1755
1756/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001757 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001758 * @dev_priv: i915 private structure
1759 * @pipe: pipe PLL to disable
1760 *
1761 * Disable the PLL for @pipe, making sure the pipe is off first.
1762 *
1763 * Note! This is for pre-ILK only.
1764 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001765static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001766{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001767 struct drm_device *dev = crtc->base.dev;
1768 struct drm_i915_private *dev_priv = dev->dev_private;
1769 enum pipe pipe = crtc->pipe;
1770
1771 /* Disable DVO 2x clock on both PLLs if necessary */
1772 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001773 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001774 intel_num_dvo_pipes(dev) == 1) {
1775 I915_WRITE(DPLL(PIPE_B),
1776 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1777 I915_WRITE(DPLL(PIPE_A),
1778 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1779 }
1780
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001781 /* Don't disable pipe or pipe PLLs if needed */
1782 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1783 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001784 return;
1785
1786 /* Make sure the pipe isn't still relying on us */
1787 assert_pipe_disabled(dev_priv, pipe);
1788
Daniel Vetter50b44a42013-06-05 13:34:33 +02001789 I915_WRITE(DPLL(pipe), 0);
1790 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001791}
1792
Jesse Barnesf6071162013-10-01 10:41:38 -07001793static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1794{
1795 u32 val = 0;
1796
1797 /* Make sure the pipe isn't still relying on us */
1798 assert_pipe_disabled(dev_priv, pipe);
1799
Imre Deake5cbfbf2014-01-09 17:08:16 +02001800 /*
1801 * Leave integrated clock source and reference clock enabled for pipe B.
1802 * The latter is needed for VGA hotplug / manual detection.
1803 */
Jesse Barnesf6071162013-10-01 10:41:38 -07001804 if (pipe == PIPE_B)
Imre Deake5cbfbf2014-01-09 17:08:16 +02001805 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07001806 I915_WRITE(DPLL(pipe), val);
1807 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001808
1809}
1810
1811static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1812{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001813 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001814 u32 val;
1815
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001816 /* Make sure the pipe isn't still relying on us */
1817 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001818
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001819 /* Set PLL en = 0 */
Ville Syrjäläd17ec4c2014-06-28 02:03:59 +03001820 val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001821 if (pipe != PIPE_A)
1822 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1823 I915_WRITE(DPLL(pipe), val);
1824 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001825
1826 mutex_lock(&dev_priv->dpio_lock);
1827
1828 /* Disable 10bit clock to display controller */
1829 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1830 val &= ~DPIO_DCLKP_EN;
1831 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1832
Ville Syrjälä61407f62014-05-27 16:32:55 +03001833 /* disable left/right clock distribution */
1834 if (pipe != PIPE_B) {
1835 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1836 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1837 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1838 } else {
1839 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1840 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1841 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1842 }
1843
Ville Syrjäläd7520482014-04-09 13:28:59 +03001844 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001845}
1846
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001847void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1848 struct intel_digital_port *dport)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001849{
1850 u32 port_mask;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001851 int dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001852
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001853 switch (dport->port) {
1854 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001855 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001856 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001857 break;
1858 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001859 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001860 dpll_reg = DPLL(0);
1861 break;
1862 case PORT_D:
1863 port_mask = DPLL_PORTD_READY_MASK;
1864 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001865 break;
1866 default:
1867 BUG();
1868 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001869
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001870 if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
Jesse Barnes89b667f2013-04-18 14:51:36 -07001871 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001872 port_name(dport->port), I915_READ(dpll_reg));
Jesse Barnes89b667f2013-04-18 14:51:36 -07001873}
1874
Daniel Vetterb14b1052014-04-24 23:55:13 +02001875static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1876{
1877 struct drm_device *dev = crtc->base.dev;
1878 struct drm_i915_private *dev_priv = dev->dev_private;
1879 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1880
Chris Wilsonbe19f0f2014-05-28 16:16:42 +01001881 if (WARN_ON(pll == NULL))
1882 return;
1883
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001884 WARN_ON(!pll->config.crtc_mask);
Daniel Vetterb14b1052014-04-24 23:55:13 +02001885 if (pll->active == 0) {
1886 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1887 WARN_ON(pll->on);
1888 assert_shared_dpll_disabled(dev_priv, pll);
1889
1890 pll->mode_set(dev_priv, pll);
1891 }
1892}
1893
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001894/**
Daniel Vetter85b38942014-04-24 23:55:14 +02001895 * intel_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001896 * @dev_priv: i915 private structure
1897 * @pipe: pipe PLL to enable
1898 *
1899 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1900 * drives the transcoder clock.
1901 */
Daniel Vetter85b38942014-04-24 23:55:14 +02001902static void intel_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001903{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001904 struct drm_device *dev = crtc->base.dev;
1905 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001906 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001907
Daniel Vetter87a875b2013-06-05 13:34:19 +02001908 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001909 return;
1910
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001911 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001912 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001913
Damien Lespiau74dd6922014-07-29 18:06:17 +01001914 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
Daniel Vetter46edb022013-06-05 13:34:12 +02001915 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001916 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001917
Daniel Vettercdbd2312013-06-05 13:34:03 +02001918 if (pll->active++) {
1919 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001920 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001921 return;
1922 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001923 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001924
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001925 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1926
Daniel Vetter46edb022013-06-05 13:34:12 +02001927 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001928 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001929 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001930}
1931
Damien Lespiauf6daaec2014-08-09 23:00:56 +01001932static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001933{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001934 struct drm_device *dev = crtc->base.dev;
1935 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001936 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001937
Jesse Barnes92f25842011-01-04 15:09:34 -08001938 /* PCH only available on ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001939 BUG_ON(INTEL_INFO(dev)->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001940 if (WARN_ON(pll == NULL))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001941 return;
1942
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001943 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001944 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001945
Daniel Vetter46edb022013-06-05 13:34:12 +02001946 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1947 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001948 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001949
Chris Wilson48da64a2012-05-13 20:16:12 +01001950 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001951 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001952 return;
1953 }
1954
Daniel Vettere9d69442013-06-05 13:34:15 +02001955 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001956 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001957 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001958 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001959
Daniel Vetter46edb022013-06-05 13:34:12 +02001960 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001961 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001962 pll->on = false;
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001963
1964 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
Jesse Barnes92f25842011-01-04 15:09:34 -08001965}
1966
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001967static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1968 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001969{
Daniel Vetter23670b322012-11-01 09:15:30 +01001970 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001971 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001972 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001973 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001974
1975 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001976 BUG_ON(!HAS_PCH_SPLIT(dev));
Jesse Barnes040484a2011-01-03 12:14:26 -08001977
1978 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001979 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001980 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001981
1982 /* FDI must be feeding us bits for PCH ports */
1983 assert_fdi_tx_enabled(dev_priv, pipe);
1984 assert_fdi_rx_enabled(dev_priv, pipe);
1985
Daniel Vetter23670b322012-11-01 09:15:30 +01001986 if (HAS_PCH_CPT(dev)) {
1987 /* Workaround: Set the timing override bit before enabling the
1988 * pch transcoder. */
1989 reg = TRANS_CHICKEN2(pipe);
1990 val = I915_READ(reg);
1991 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1992 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001993 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001994
Daniel Vetterab9412b2013-05-03 11:49:46 +02001995 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001996 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001997 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001998
1999 if (HAS_PCH_IBX(dev_priv->dev)) {
2000 /*
2001 * make the BPC in transcoder be consistent with
2002 * that in pipeconf reg.
2003 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002004 val &= ~PIPECONF_BPC_MASK;
2005 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07002006 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002007
2008 val &= ~TRANS_INTERLACE_MASK;
2009 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002010 if (HAS_PCH_IBX(dev_priv->dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002011 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002012 val |= TRANS_LEGACY_INTERLACED_ILK;
2013 else
2014 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002015 else
2016 val |= TRANS_PROGRESSIVE;
2017
Jesse Barnes040484a2011-01-03 12:14:26 -08002018 I915_WRITE(reg, val | TRANS_ENABLE);
2019 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002020 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08002021}
2022
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002023static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02002024 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08002025{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002026 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002027
2028 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03002029 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002030
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002031 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01002032 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02002033 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002034
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002035 /* Workaround: set timing override bit. */
2036 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002037 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002038 I915_WRITE(_TRANSA_CHICKEN2, val);
2039
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02002040 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02002041 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002042
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02002043 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
2044 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02002045 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002046 else
2047 val |= TRANS_PROGRESSIVE;
2048
Daniel Vetterab9412b2013-05-03 11:49:46 +02002049 I915_WRITE(LPT_TRANSCONF, val);
2050 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02002051 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002052}
2053
Paulo Zanonib8a4f402012-10-31 18:12:42 -02002054static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
2055 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08002056{
Daniel Vetter23670b322012-11-01 09:15:30 +01002057 struct drm_device *dev = dev_priv->dev;
2058 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08002059
2060 /* FDI relies on the transcoder */
2061 assert_fdi_tx_disabled(dev_priv, pipe);
2062 assert_fdi_rx_disabled(dev_priv, pipe);
2063
Jesse Barnes291906f2011-02-02 12:28:03 -08002064 /* Ports must be off as well */
2065 assert_pch_ports_disabled(dev_priv, pipe);
2066
Daniel Vetterab9412b2013-05-03 11:49:46 +02002067 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002068 val = I915_READ(reg);
2069 val &= ~TRANS_ENABLE;
2070 I915_WRITE(reg, val);
2071 /* wait for PCH transcoder off, transcoder state */
2072 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002073 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01002074
2075 if (!HAS_PCH_IBX(dev)) {
2076 /* Workaround: Clear the timing override chicken bit again. */
2077 reg = TRANS_CHICKEN2(pipe);
2078 val = I915_READ(reg);
2079 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2080 I915_WRITE(reg, val);
2081 }
Jesse Barnes040484a2011-01-03 12:14:26 -08002082}
2083
Paulo Zanoniab4d9662012-10-31 18:12:55 -02002084static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002085{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002086 u32 val;
2087
Daniel Vetterab9412b2013-05-03 11:49:46 +02002088 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002089 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02002090 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002091 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02002092 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02002093 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002094
2095 /* Workaround: clear timing override bit. */
2096 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002097 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002098 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08002099}
2100
2101/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002102 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02002103 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08002104 *
Paulo Zanoni03722642014-01-17 13:51:09 -02002105 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08002106 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002107 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02002108static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002109{
Paulo Zanoni03722642014-01-17 13:51:09 -02002110 struct drm_device *dev = crtc->base.dev;
2111 struct drm_i915_private *dev_priv = dev->dev_private;
2112 enum pipe pipe = crtc->pipe;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002113 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2114 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002115 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002116 int reg;
2117 u32 val;
2118
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002119 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002120 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002121 assert_sprites_disabled(dev_priv, pipe);
2122
Paulo Zanoni681e5812012-12-06 11:12:38 -02002123 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002124 pch_transcoder = TRANSCODER_A;
2125 else
2126 pch_transcoder = pipe;
2127
Jesse Barnesb24e7172011-01-04 15:09:30 -08002128 /*
2129 * A pipe without a PLL won't actually be able to drive bits from
2130 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2131 * need the check.
2132 */
Imre Deak50360402015-01-16 00:55:16 -08002133 if (HAS_GMCH_DISPLAY(dev_priv->dev))
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002134 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03002135 assert_dsi_pll_enabled(dev_priv);
2136 else
2137 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002138 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002139 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002140 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002141 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002142 assert_fdi_tx_pll_enabled(dev_priv,
2143 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08002144 }
2145 /* FIXME: assert CPU port conditions for SNB+ */
2146 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08002147
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002148 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002149 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002150 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002151 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2152 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002153 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002154 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002155
2156 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002157 POSTING_READ(reg);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002158}
2159
2160/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002161 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002162 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002163 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002164 * Disable the pipe of @crtc, making sure that various hardware
2165 * specific requirements are met, if applicable, e.g. plane
2166 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002167 *
2168 * Will wait until the pipe has shut down before returning.
2169 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002170static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002171{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002172 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002173 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002174 enum pipe pipe = crtc->pipe;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002175 int reg;
2176 u32 val;
2177
2178 /*
2179 * Make sure planes won't keep trying to pump pixels to us,
2180 * or we might hang the display.
2181 */
2182 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002183 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002184 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002185
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002186 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002187 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002188 if ((val & PIPECONF_ENABLE) == 0)
2189 return;
2190
Ville Syrjälä67adc642014-08-15 01:21:57 +03002191 /*
2192 * Double wide has implications for planes
2193 * so best keep it disabled when not needed.
2194 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002195 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002196 val &= ~PIPECONF_DOUBLE_WIDE;
2197
2198 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002199 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2200 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002201 val &= ~PIPECONF_ENABLE;
2202
2203 I915_WRITE(reg, val);
2204 if ((val & PIPECONF_ENABLE) == 0)
2205 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002206}
2207
Keith Packardd74362c2011-07-28 14:47:14 -07002208/*
2209 * Plane regs are double buffered, going from enabled->disabled needs a
2210 * trigger in order to latch. The display address reg provides this.
2211 */
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002212void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
2213 enum plane plane)
Keith Packardd74362c2011-07-28 14:47:14 -07002214{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00002215 struct drm_device *dev = dev_priv->dev;
2216 u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002217
2218 I915_WRITE(reg, I915_READ(reg));
2219 POSTING_READ(reg);
Keith Packardd74362c2011-07-28 14:47:14 -07002220}
2221
Jesse Barnesb24e7172011-01-04 15:09:30 -08002222/**
Matt Roper262ca2b2014-03-18 17:22:55 -07002223 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002224 * @plane: plane to be enabled
2225 * @crtc: crtc for the plane
Jesse Barnesb24e7172011-01-04 15:09:30 -08002226 *
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002227 * Enable @plane on @crtc, making sure that the pipe is running first.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002228 */
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002229static void intel_enable_primary_hw_plane(struct drm_plane *plane,
2230 struct drm_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002231{
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002232 struct drm_device *dev = plane->dev;
2233 struct drm_i915_private *dev_priv = dev->dev_private;
2234 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002235
2236 /* If the pipe isn't enabled, we can't pump pixels and may hang */
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002237 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002238 to_intel_plane_state(plane->state)->visible = true;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002239
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002240 dev_priv->display.update_primary_plane(crtc, plane->fb,
2241 crtc->x, crtc->y);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002242}
2243
Chris Wilson693db182013-03-05 14:52:39 +00002244static bool need_vtd_wa(struct drm_device *dev)
2245{
2246#ifdef CONFIG_INTEL_IOMMU
2247 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2248 return true;
2249#endif
2250 return false;
2251}
2252
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002253unsigned int
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002254intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
2255 uint64_t fb_format_modifier)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002256{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002257 unsigned int tile_height;
2258 uint32_t pixel_bytes;
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002259
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002260 switch (fb_format_modifier) {
2261 case DRM_FORMAT_MOD_NONE:
2262 tile_height = 1;
2263 break;
2264 case I915_FORMAT_MOD_X_TILED:
2265 tile_height = IS_GEN2(dev) ? 16 : 8;
2266 break;
2267 case I915_FORMAT_MOD_Y_TILED:
2268 tile_height = 32;
2269 break;
2270 case I915_FORMAT_MOD_Yf_TILED:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002271 pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
2272 switch (pixel_bytes) {
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002273 default:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002274 case 1:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002275 tile_height = 64;
2276 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002277 case 2:
2278 case 4:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002279 tile_height = 32;
2280 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002281 case 8:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002282 tile_height = 16;
2283 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002284 case 16:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002285 WARN_ONCE(1,
2286 "128-bit pixels are not supported for display!");
2287 tile_height = 16;
2288 break;
2289 }
2290 break;
2291 default:
2292 MISSING_CASE(fb_format_modifier);
2293 tile_height = 1;
2294 break;
2295 }
Daniel Vetter091df6c2015-02-10 17:16:10 +00002296
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002297 return tile_height;
2298}
2299
2300unsigned int
2301intel_fb_align_height(struct drm_device *dev, unsigned int height,
2302 uint32_t pixel_format, uint64_t fb_format_modifier)
2303{
2304 return ALIGN(height, intel_tile_height(dev, pixel_format,
2305 fb_format_modifier));
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002306}
2307
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002308static int
2309intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
2310 const struct drm_plane_state *plane_state)
2311{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002312 struct intel_rotation_info *info = &view->rotation_info;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002313
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002314 *view = i915_ggtt_view_normal;
2315
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002316 if (!plane_state)
2317 return 0;
2318
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002319 if (!intel_rotation_90_or_270(plane_state->rotation))
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002320 return 0;
2321
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002322 *view = i915_ggtt_view_rotated;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002323
2324 info->height = fb->height;
2325 info->pixel_format = fb->pixel_format;
2326 info->pitch = fb->pitches[0];
2327 info->fb_modifier = fb->modifier[0];
2328
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002329 return 0;
2330}
2331
Chris Wilson127bd2a2010-07-23 23:32:05 +01002332int
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002333intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2334 struct drm_framebuffer *fb,
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002335 const struct drm_plane_state *plane_state,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002336 struct intel_engine_cs *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002337{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002338 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002339 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002340 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002341 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002342 u32 alignment;
2343 int ret;
2344
Matt Roperebcdd392014-07-09 16:22:11 -07002345 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2346
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002347 switch (fb->modifier[0]) {
2348 case DRM_FORMAT_MOD_NONE:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002349 if (INTEL_INFO(dev)->gen >= 9)
2350 alignment = 256 * 1024;
2351 else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
Chris Wilson534843d2010-07-05 18:01:46 +01002352 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002353 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01002354 alignment = 4 * 1024;
2355 else
2356 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002357 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002358 case I915_FORMAT_MOD_X_TILED:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002359 if (INTEL_INFO(dev)->gen >= 9)
2360 alignment = 256 * 1024;
2361 else {
2362 /* pin() will align the object as required by fence */
2363 alignment = 0;
2364 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002365 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002366 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiau1327b9a2015-02-27 11:15:20 +00002367 case I915_FORMAT_MOD_Yf_TILED:
2368 if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2369 "Y tiling bo slipped through, driver bug!\n"))
2370 return -EINVAL;
2371 alignment = 1 * 1024 * 1024;
2372 break;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002373 default:
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002374 MISSING_CASE(fb->modifier[0]);
2375 return -EINVAL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002376 }
2377
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002378 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2379 if (ret)
2380 return ret;
2381
Chris Wilson693db182013-03-05 14:52:39 +00002382 /* Note that the w/a also requires 64 PTE of padding following the
2383 * bo. We currently fill all unused PTE with the shadow page and so
2384 * we should always have valid PTE following the scanout preventing
2385 * the VT-d warning.
2386 */
2387 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2388 alignment = 256 * 1024;
2389
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002390 /*
2391 * Global gtt pte registers are special registers which actually forward
2392 * writes to a chunk of system memory. Which means that there is no risk
2393 * that the register values disappear as soon as we call
2394 * intel_runtime_pm_put(), so it is correct to wrap only the
2395 * pin/unpin/fence and not more.
2396 */
2397 intel_runtime_pm_get(dev_priv);
2398
Chris Wilsonce453d82011-02-21 14:43:56 +00002399 dev_priv->mm.interruptible = false;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00002400 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002401 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002402 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00002403 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002404
2405 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2406 * fence, whereas 965+ only requires a fence if using
2407 * framebuffer compression. For simplicity, we always install
2408 * a fence as the cost is not that onerous.
2409 */
Chris Wilson06d98132012-04-17 15:31:24 +01002410 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002411 if (ret)
2412 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002413
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002414 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002415
Chris Wilsonce453d82011-02-21 14:43:56 +00002416 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002417 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002418 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002419
2420err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002421 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilsonce453d82011-02-21 14:43:56 +00002422err_interruptible:
2423 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002424 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002425 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002426}
2427
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002428static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
2429 const struct drm_plane_state *plane_state)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002430{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002431 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002432 struct i915_ggtt_view view;
2433 int ret;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002434
Matt Roperebcdd392014-07-09 16:22:11 -07002435 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2436
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002437 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2438 WARN_ONCE(ret, "Couldn't get view from plane state!");
2439
Chris Wilson1690e1e2011-12-14 13:57:08 +01002440 i915_gem_object_unpin_fence(obj);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002441 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002442}
2443
Daniel Vetterc2c75132012-07-05 12:17:30 +02002444/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2445 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00002446unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2447 unsigned int tiling_mode,
2448 unsigned int cpp,
2449 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002450{
Chris Wilsonbc752862013-02-21 20:04:31 +00002451 if (tiling_mode != I915_TILING_NONE) {
2452 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002453
Chris Wilsonbc752862013-02-21 20:04:31 +00002454 tile_rows = *y / 8;
2455 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002456
Chris Wilsonbc752862013-02-21 20:04:31 +00002457 tiles = *x / (512/cpp);
2458 *x %= 512/cpp;
2459
2460 return tile_rows * pitch * 8 + tiles * 4096;
2461 } else {
2462 unsigned int offset;
2463
2464 offset = *y * pitch + *x * cpp;
2465 *y = 0;
2466 *x = (offset & 4095) / cpp;
2467 return offset & -4096;
2468 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002469}
2470
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002471static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002472{
2473 switch (format) {
2474 case DISPPLANE_8BPP:
2475 return DRM_FORMAT_C8;
2476 case DISPPLANE_BGRX555:
2477 return DRM_FORMAT_XRGB1555;
2478 case DISPPLANE_BGRX565:
2479 return DRM_FORMAT_RGB565;
2480 default:
2481 case DISPPLANE_BGRX888:
2482 return DRM_FORMAT_XRGB8888;
2483 case DISPPLANE_RGBX888:
2484 return DRM_FORMAT_XBGR8888;
2485 case DISPPLANE_BGRX101010:
2486 return DRM_FORMAT_XRGB2101010;
2487 case DISPPLANE_RGBX101010:
2488 return DRM_FORMAT_XBGR2101010;
2489 }
2490}
2491
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002492static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2493{
2494 switch (format) {
2495 case PLANE_CTL_FORMAT_RGB_565:
2496 return DRM_FORMAT_RGB565;
2497 default:
2498 case PLANE_CTL_FORMAT_XRGB_8888:
2499 if (rgb_order) {
2500 if (alpha)
2501 return DRM_FORMAT_ABGR8888;
2502 else
2503 return DRM_FORMAT_XBGR8888;
2504 } else {
2505 if (alpha)
2506 return DRM_FORMAT_ARGB8888;
2507 else
2508 return DRM_FORMAT_XRGB8888;
2509 }
2510 case PLANE_CTL_FORMAT_XRGB_2101010:
2511 if (rgb_order)
2512 return DRM_FORMAT_XBGR2101010;
2513 else
2514 return DRM_FORMAT_XRGB2101010;
2515 }
2516}
2517
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002518static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002519intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2520 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002521{
2522 struct drm_device *dev = crtc->base.dev;
2523 struct drm_i915_gem_object *obj = NULL;
2524 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002525 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002526 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2527 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2528 PAGE_SIZE);
2529
2530 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002531
Chris Wilsonff2652e2014-03-10 08:07:02 +00002532 if (plane_config->size == 0)
2533 return false;
2534
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002535 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2536 base_aligned,
2537 base_aligned,
2538 size_aligned);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002539 if (!obj)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002540 return false;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002541
Damien Lespiau49af4492015-01-20 12:51:44 +00002542 obj->tiling_mode = plane_config->tiling;
2543 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002544 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002545
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002546 mode_cmd.pixel_format = fb->pixel_format;
2547 mode_cmd.width = fb->width;
2548 mode_cmd.height = fb->height;
2549 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002550 mode_cmd.modifier[0] = fb->modifier[0];
2551 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002552
2553 mutex_lock(&dev->struct_mutex);
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002554 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002555 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002556 DRM_DEBUG_KMS("intel fb init failed\n");
2557 goto out_unref_obj;
2558 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002559 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002560
Daniel Vetterf6936e22015-03-26 12:17:05 +01002561 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002562 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002563
2564out_unref_obj:
2565 drm_gem_object_unreference(&obj->base);
2566 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002567 return false;
2568}
2569
Matt Roperafd65eb2015-02-03 13:10:04 -08002570/* Update plane->state->fb to match plane->fb after driver-internal updates */
2571static void
2572update_state_fb(struct drm_plane *plane)
2573{
2574 if (plane->fb == plane->state->fb)
2575 return;
2576
2577 if (plane->state->fb)
2578 drm_framebuffer_unreference(plane->state->fb);
2579 plane->state->fb = plane->fb;
2580 if (plane->state->fb)
2581 drm_framebuffer_reference(plane->state->fb);
2582}
2583
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002584static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002585intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2586 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002587{
2588 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002589 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002590 struct drm_crtc *c;
2591 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002592 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002593 struct drm_plane *primary = intel_crtc->base.primary;
2594 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002595
Damien Lespiau2d140302015-02-05 17:22:18 +00002596 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002597 return;
2598
Daniel Vetterf6936e22015-03-26 12:17:05 +01002599 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002600 fb = &plane_config->fb->base;
2601 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002602 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002603
Damien Lespiau2d140302015-02-05 17:22:18 +00002604 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002605
2606 /*
2607 * Failed to alloc the obj, check to see if we should share
2608 * an fb with another CRTC instead
2609 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002610 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002611 i = to_intel_crtc(c);
2612
2613 if (c == &intel_crtc->base)
2614 continue;
2615
Matt Roper2ff8fde2014-07-08 07:50:07 -07002616 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002617 continue;
2618
Daniel Vetter88595ac2015-03-26 12:42:24 +01002619 fb = c->primary->fb;
2620 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002621 continue;
2622
Daniel Vetter88595ac2015-03-26 12:42:24 +01002623 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002624 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002625 drm_framebuffer_reference(fb);
2626 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002627 }
2628 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002629
2630 return;
2631
2632valid_fb:
2633 obj = intel_fb_obj(fb);
2634 if (obj->tiling_mode != I915_TILING_NONE)
2635 dev_priv->preserve_bios_swizzle = true;
2636
2637 primary->fb = fb;
2638 primary->state->crtc = &intel_crtc->base;
2639 primary->crtc = &intel_crtc->base;
2640 update_state_fb(primary);
2641 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002642}
2643
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002644static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2645 struct drm_framebuffer *fb,
2646 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002647{
2648 struct drm_device *dev = crtc->dev;
2649 struct drm_i915_private *dev_priv = dev->dev_private;
2650 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002651 struct drm_plane *primary = crtc->primary;
2652 bool visible = to_intel_plane_state(primary->state)->visible;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002653 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002654 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002655 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002656 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002657 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302658 int pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002659
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002660 if (!visible || !fb) {
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002661 I915_WRITE(reg, 0);
2662 if (INTEL_INFO(dev)->gen >= 4)
2663 I915_WRITE(DSPSURF(plane), 0);
2664 else
2665 I915_WRITE(DSPADDR(plane), 0);
2666 POSTING_READ(reg);
2667 return;
2668 }
2669
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002670 obj = intel_fb_obj(fb);
2671 if (WARN_ON(obj == NULL))
2672 return;
2673
2674 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2675
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002676 dspcntr = DISPPLANE_GAMMA_ENABLE;
2677
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002678 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002679
2680 if (INTEL_INFO(dev)->gen < 4) {
2681 if (intel_crtc->pipe == PIPE_B)
2682 dspcntr |= DISPPLANE_SEL_PIPE_B;
2683
2684 /* pipesrc and dspsize control the size that is scaled from,
2685 * which should always be the user's requested size.
2686 */
2687 I915_WRITE(DSPSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002688 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2689 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002690 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002691 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2692 I915_WRITE(PRIMSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002693 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2694 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002695 I915_WRITE(PRIMPOS(plane), 0);
2696 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002697 }
2698
Ville Syrjälä57779d02012-10-31 17:50:14 +02002699 switch (fb->pixel_format) {
2700 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002701 dspcntr |= DISPPLANE_8BPP;
2702 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002703 case DRM_FORMAT_XRGB1555:
2704 case DRM_FORMAT_ARGB1555:
2705 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002706 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002707 case DRM_FORMAT_RGB565:
2708 dspcntr |= DISPPLANE_BGRX565;
2709 break;
2710 case DRM_FORMAT_XRGB8888:
2711 case DRM_FORMAT_ARGB8888:
2712 dspcntr |= DISPPLANE_BGRX888;
2713 break;
2714 case DRM_FORMAT_XBGR8888:
2715 case DRM_FORMAT_ABGR8888:
2716 dspcntr |= DISPPLANE_RGBX888;
2717 break;
2718 case DRM_FORMAT_XRGB2101010:
2719 case DRM_FORMAT_ARGB2101010:
2720 dspcntr |= DISPPLANE_BGRX101010;
2721 break;
2722 case DRM_FORMAT_XBGR2101010:
2723 case DRM_FORMAT_ABGR2101010:
2724 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002725 break;
2726 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002727 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002728 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002729
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002730 if (INTEL_INFO(dev)->gen >= 4 &&
2731 obj->tiling_mode != I915_TILING_NONE)
2732 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002733
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002734 if (IS_G4X(dev))
2735 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2736
Ville Syrjäläb98971272014-08-27 16:51:22 +03002737 linear_offset = y * fb->pitches[0] + x * pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002738
Daniel Vetterc2c75132012-07-05 12:17:30 +02002739 if (INTEL_INFO(dev)->gen >= 4) {
2740 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002741 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002742 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002743 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002744 linear_offset -= intel_crtc->dspaddr_offset;
2745 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002746 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002747 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002748
Matt Roper8e7d6882015-01-21 16:35:41 -08002749 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302750 dspcntr |= DISPPLANE_ROTATE_180;
2751
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002752 x += (intel_crtc->config->pipe_src_w - 1);
2753 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302754
2755 /* Finding the last pixel of the last line of the display
2756 data and adding to linear_offset*/
2757 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002758 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2759 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302760 }
2761
2762 I915_WRITE(reg, dspcntr);
2763
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002764 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002765 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002766 I915_WRITE(DSPSURF(plane),
2767 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002768 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002769 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002770 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002771 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002772 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002773}
2774
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002775static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2776 struct drm_framebuffer *fb,
2777 int x, int y)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002778{
2779 struct drm_device *dev = crtc->dev;
2780 struct drm_i915_private *dev_priv = dev->dev_private;
2781 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002782 struct drm_plane *primary = crtc->primary;
2783 bool visible = to_intel_plane_state(primary->state)->visible;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002784 struct drm_i915_gem_object *obj;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002785 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002786 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002787 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002788 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302789 int pixel_size;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002790
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002791 if (!visible || !fb) {
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002792 I915_WRITE(reg, 0);
2793 I915_WRITE(DSPSURF(plane), 0);
2794 POSTING_READ(reg);
2795 return;
2796 }
2797
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002798 obj = intel_fb_obj(fb);
2799 if (WARN_ON(obj == NULL))
2800 return;
2801
2802 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2803
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002804 dspcntr = DISPPLANE_GAMMA_ENABLE;
2805
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002806 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002807
2808 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2809 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2810
Ville Syrjälä57779d02012-10-31 17:50:14 +02002811 switch (fb->pixel_format) {
2812 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002813 dspcntr |= DISPPLANE_8BPP;
2814 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002815 case DRM_FORMAT_RGB565:
2816 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002817 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002818 case DRM_FORMAT_XRGB8888:
2819 case DRM_FORMAT_ARGB8888:
2820 dspcntr |= DISPPLANE_BGRX888;
2821 break;
2822 case DRM_FORMAT_XBGR8888:
2823 case DRM_FORMAT_ABGR8888:
2824 dspcntr |= DISPPLANE_RGBX888;
2825 break;
2826 case DRM_FORMAT_XRGB2101010:
2827 case DRM_FORMAT_ARGB2101010:
2828 dspcntr |= DISPPLANE_BGRX101010;
2829 break;
2830 case DRM_FORMAT_XBGR2101010:
2831 case DRM_FORMAT_ABGR2101010:
2832 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002833 break;
2834 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002835 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002836 }
2837
2838 if (obj->tiling_mode != I915_TILING_NONE)
2839 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002840
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002841 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002842 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002843
Ville Syrjäläb98971272014-08-27 16:51:22 +03002844 linear_offset = y * fb->pitches[0] + x * pixel_size;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002845 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002846 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002847 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002848 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002849 linear_offset -= intel_crtc->dspaddr_offset;
Matt Roper8e7d6882015-01-21 16:35:41 -08002850 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302851 dspcntr |= DISPPLANE_ROTATE_180;
2852
2853 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002854 x += (intel_crtc->config->pipe_src_w - 1);
2855 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302856
2857 /* Finding the last pixel of the last line of the display
2858 data and adding to linear_offset*/
2859 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002860 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2861 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302862 }
2863 }
2864
2865 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002866
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002867 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002868 I915_WRITE(DSPSURF(plane),
2869 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002870 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002871 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2872 } else {
2873 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2874 I915_WRITE(DSPLINOFF(plane), linear_offset);
2875 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002876 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002877}
2878
Damien Lespiaub3218032015-02-27 11:15:18 +00002879u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2880 uint32_t pixel_format)
2881{
2882 u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2883
2884 /*
2885 * The stride is either expressed as a multiple of 64 bytes
2886 * chunks for linear buffers or in number of tiles for tiled
2887 * buffers.
2888 */
2889 switch (fb_modifier) {
2890 case DRM_FORMAT_MOD_NONE:
2891 return 64;
2892 case I915_FORMAT_MOD_X_TILED:
2893 if (INTEL_INFO(dev)->gen == 2)
2894 return 128;
2895 return 512;
2896 case I915_FORMAT_MOD_Y_TILED:
2897 /* No need to check for old gens and Y tiling since this is
2898 * about the display engine and those will be blocked before
2899 * we get here.
2900 */
2901 return 128;
2902 case I915_FORMAT_MOD_Yf_TILED:
2903 if (bits_per_pixel == 8)
2904 return 64;
2905 else
2906 return 128;
2907 default:
2908 MISSING_CASE(fb_modifier);
2909 return 64;
2910 }
2911}
2912
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002913unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
2914 struct drm_i915_gem_object *obj)
2915{
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002916 const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002917
2918 if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002919 view = &i915_ggtt_view_rotated;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002920
2921 return i915_gem_obj_ggtt_offset_view(obj, view);
2922}
2923
Chandra Kondurua1b22782015-04-07 15:28:45 -07002924/*
2925 * This function detaches (aka. unbinds) unused scalers in hardware
2926 */
2927void skl_detach_scalers(struct intel_crtc *intel_crtc)
2928{
2929 struct drm_device *dev;
2930 struct drm_i915_private *dev_priv;
2931 struct intel_crtc_scaler_state *scaler_state;
2932 int i;
2933
2934 if (!intel_crtc || !intel_crtc->config)
2935 return;
2936
2937 dev = intel_crtc->base.dev;
2938 dev_priv = dev->dev_private;
2939 scaler_state = &intel_crtc->config->scaler_state;
2940
2941 /* loop through and disable scalers that aren't in use */
2942 for (i = 0; i < intel_crtc->num_scalers; i++) {
2943 if (!scaler_state->scalers[i].in_use) {
2944 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, i), 0);
2945 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, i), 0);
2946 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, i), 0);
2947 DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
2948 intel_crtc->base.base.id, intel_crtc->pipe, i);
2949 }
2950 }
2951}
2952
Chandra Konduru6156a452015-04-27 13:48:39 -07002953u32 skl_plane_ctl_format(uint32_t pixel_format)
2954{
2955 u32 plane_ctl_format = 0;
2956 switch (pixel_format) {
2957 case DRM_FORMAT_RGB565:
2958 plane_ctl_format = PLANE_CTL_FORMAT_RGB_565;
2959 break;
2960 case DRM_FORMAT_XBGR8888:
2961 plane_ctl_format = PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
2962 break;
2963 case DRM_FORMAT_XRGB8888:
2964 plane_ctl_format = PLANE_CTL_FORMAT_XRGB_8888;
2965 break;
2966 /*
2967 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2968 * to be already pre-multiplied. We need to add a knob (or a different
2969 * DRM_FORMAT) for user-space to configure that.
2970 */
2971 case DRM_FORMAT_ABGR8888:
2972 plane_ctl_format = PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
2973 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
2974 break;
2975 case DRM_FORMAT_ARGB8888:
2976 plane_ctl_format = PLANE_CTL_FORMAT_XRGB_8888 |
2977 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
2978 break;
2979 case DRM_FORMAT_XRGB2101010:
2980 plane_ctl_format = PLANE_CTL_FORMAT_XRGB_2101010;
2981 break;
2982 case DRM_FORMAT_XBGR2101010:
2983 plane_ctl_format = PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
2984 break;
2985 case DRM_FORMAT_YUYV:
2986 plane_ctl_format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
2987 break;
2988 case DRM_FORMAT_YVYU:
2989 plane_ctl_format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
2990 break;
2991 case DRM_FORMAT_UYVY:
2992 plane_ctl_format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
2993 break;
2994 case DRM_FORMAT_VYUY:
2995 plane_ctl_format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
2996 break;
2997 default:
2998 BUG();
2999 }
3000 return plane_ctl_format;
3001}
3002
3003u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
3004{
3005 u32 plane_ctl_tiling = 0;
3006 switch (fb_modifier) {
3007 case DRM_FORMAT_MOD_NONE:
3008 break;
3009 case I915_FORMAT_MOD_X_TILED:
3010 plane_ctl_tiling = PLANE_CTL_TILED_X;
3011 break;
3012 case I915_FORMAT_MOD_Y_TILED:
3013 plane_ctl_tiling = PLANE_CTL_TILED_Y;
3014 break;
3015 case I915_FORMAT_MOD_Yf_TILED:
3016 plane_ctl_tiling = PLANE_CTL_TILED_YF;
3017 break;
3018 default:
3019 MISSING_CASE(fb_modifier);
3020 }
3021 return plane_ctl_tiling;
3022}
3023
3024u32 skl_plane_ctl_rotation(unsigned int rotation)
3025{
3026 u32 plane_ctl_rotation = 0;
3027 switch (rotation) {
3028 case BIT(DRM_ROTATE_0):
3029 break;
3030 case BIT(DRM_ROTATE_90):
3031 plane_ctl_rotation = PLANE_CTL_ROTATE_90;
3032 break;
3033 case BIT(DRM_ROTATE_180):
3034 plane_ctl_rotation = PLANE_CTL_ROTATE_180;
3035 break;
3036 case BIT(DRM_ROTATE_270):
3037 plane_ctl_rotation = PLANE_CTL_ROTATE_270;
3038 break;
3039 default:
3040 MISSING_CASE(rotation);
3041 }
3042
3043 return plane_ctl_rotation;
3044}
3045
Damien Lespiau70d21f02013-07-03 21:06:04 +01003046static void skylake_update_primary_plane(struct drm_crtc *crtc,
3047 struct drm_framebuffer *fb,
3048 int x, int y)
3049{
3050 struct drm_device *dev = crtc->dev;
3051 struct drm_i915_private *dev_priv = dev->dev_private;
3052 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03003053 struct drm_plane *plane = crtc->primary;
3054 bool visible = to_intel_plane_state(plane->state)->visible;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003055 struct drm_i915_gem_object *obj;
3056 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303057 u32 plane_ctl, stride_div, stride;
3058 u32 tile_height, plane_offset, plane_size;
3059 unsigned int rotation;
3060 int x_offset, y_offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003061 unsigned long surf_addr;
Chandra Konduru6156a452015-04-27 13:48:39 -07003062 struct intel_crtc_state *crtc_state = intel_crtc->config;
3063 struct intel_plane_state *plane_state;
3064 int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
3065 int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
3066 int scaler_id = -1;
3067
Chandra Konduru6156a452015-04-27 13:48:39 -07003068 plane_state = to_intel_plane_state(plane->state);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003069
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03003070 if (!visible || !fb) {
Damien Lespiau70d21f02013-07-03 21:06:04 +01003071 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3072 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3073 POSTING_READ(PLANE_CTL(pipe, 0));
3074 return;
3075 }
3076
3077 plane_ctl = PLANE_CTL_ENABLE |
3078 PLANE_CTL_PIPE_GAMMA_ENABLE |
3079 PLANE_CTL_PIPE_CSC_ENABLE;
3080
Chandra Konduru6156a452015-04-27 13:48:39 -07003081 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3082 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003083 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303084
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303085 rotation = plane->state->rotation;
Chandra Konduru6156a452015-04-27 13:48:39 -07003086 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003087
Damien Lespiaub3218032015-02-27 11:15:18 +00003088 obj = intel_fb_obj(fb);
3089 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3090 fb->pixel_format);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303091 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
3092
Chandra Konduru6156a452015-04-27 13:48:39 -07003093 /*
3094 * FIXME: intel_plane_state->src, dst aren't set when transitional
3095 * update_plane helpers are called from legacy paths.
3096 * Once full atomic crtc is available, below check can be avoided.
3097 */
3098 if (drm_rect_width(&plane_state->src)) {
3099 scaler_id = plane_state->scaler_id;
3100 src_x = plane_state->src.x1 >> 16;
3101 src_y = plane_state->src.y1 >> 16;
3102 src_w = drm_rect_width(&plane_state->src) >> 16;
3103 src_h = drm_rect_height(&plane_state->src) >> 16;
3104 dst_x = plane_state->dst.x1;
3105 dst_y = plane_state->dst.y1;
3106 dst_w = drm_rect_width(&plane_state->dst);
3107 dst_h = drm_rect_height(&plane_state->dst);
3108
3109 WARN_ON(x != src_x || y != src_y);
3110 } else {
3111 src_w = intel_crtc->config->pipe_src_w;
3112 src_h = intel_crtc->config->pipe_src_h;
3113 }
3114
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303115 if (intel_rotation_90_or_270(rotation)) {
3116 /* stride = Surface height in tiles */
3117 tile_height = intel_tile_height(dev, fb->bits_per_pixel,
3118 fb->modifier[0]);
3119 stride = DIV_ROUND_UP(fb->height, tile_height);
Chandra Konduru6156a452015-04-27 13:48:39 -07003120 x_offset = stride * tile_height - y - src_h;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303121 y_offset = x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003122 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303123 } else {
3124 stride = fb->pitches[0] / stride_div;
3125 x_offset = x;
3126 y_offset = y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003127 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303128 }
3129 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003130
Damien Lespiau70d21f02013-07-03 21:06:04 +01003131 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303132 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3133 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3134 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003135
3136 if (scaler_id >= 0) {
3137 uint32_t ps_ctrl = 0;
3138
3139 WARN_ON(!dst_w || !dst_h);
3140 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3141 crtc_state->scaler_state.scalers[scaler_id].mode;
3142 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3143 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3144 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3145 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3146 I915_WRITE(PLANE_POS(pipe, 0), 0);
3147 } else {
3148 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3149 }
3150
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003151 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003152
3153 POSTING_READ(PLANE_SURF(pipe, 0));
3154}
3155
Jesse Barnes17638cd2011-06-24 12:19:23 -07003156/* Assume fb object is pinned & idle & fenced and just update base pointers */
3157static int
3158intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3159 int x, int y, enum mode_set_atomic state)
3160{
3161 struct drm_device *dev = crtc->dev;
3162 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003163
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01003164 if (dev_priv->display.disable_fbc)
3165 dev_priv->display.disable_fbc(dev);
Jesse Barnes81255562010-08-02 12:07:50 -07003166
Daniel Vetter29b9bde2014-04-24 23:55:01 +02003167 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3168
3169 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07003170}
3171
Ville Syrjälä75147472014-11-24 18:28:11 +02003172static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003173{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003174 struct drm_crtc *crtc;
3175
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003176 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003177 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3178 enum plane plane = intel_crtc->plane;
3179
3180 intel_prepare_page_flip(dev, plane);
3181 intel_finish_page_flip_plane(dev, plane);
3182 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003183}
3184
3185static void intel_update_primary_planes(struct drm_device *dev)
3186{
3187 struct drm_i915_private *dev_priv = dev->dev_private;
3188 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003189
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003190 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003191 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3192
Rob Clark51fd3712013-11-19 12:10:12 -05003193 drm_modeset_lock(&crtc->mutex, NULL);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003194 /*
3195 * FIXME: Once we have proper support for primary planes (and
3196 * disabling them without disabling the entire crtc) allow again
Dave Airlie66e514c2014-04-03 07:51:54 +10003197 * a NULL crtc->primary->fb.
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003198 */
Matt Roperf4510a22014-04-01 15:22:40 -07003199 if (intel_crtc->active && crtc->primary->fb)
Matt Roper262ca2b2014-03-18 17:22:55 -07003200 dev_priv->display.update_primary_plane(crtc,
Dave Airlie66e514c2014-04-03 07:51:54 +10003201 crtc->primary->fb,
Matt Roper262ca2b2014-03-18 17:22:55 -07003202 crtc->x,
3203 crtc->y);
Rob Clark51fd3712013-11-19 12:10:12 -05003204 drm_modeset_unlock(&crtc->mutex);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003205 }
3206}
3207
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03003208void intel_crtc_reset(struct intel_crtc *crtc)
3209{
3210 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3211
3212 if (!crtc->active)
3213 return;
3214
3215 intel_crtc_disable_planes(&crtc->base);
3216 dev_priv->display.crtc_disable(&crtc->base);
3217 dev_priv->display.crtc_enable(&crtc->base);
3218 intel_crtc_enable_planes(&crtc->base);
3219}
3220
Ville Syrjälä75147472014-11-24 18:28:11 +02003221void intel_prepare_reset(struct drm_device *dev)
3222{
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003223 struct drm_i915_private *dev_priv = to_i915(dev);
3224 struct intel_crtc *crtc;
3225
Ville Syrjälä75147472014-11-24 18:28:11 +02003226 /* no reset support for gen2 */
3227 if (IS_GEN2(dev))
3228 return;
3229
3230 /* reset doesn't touch the display */
3231 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3232 return;
3233
3234 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003235
3236 /*
3237 * Disabling the crtcs gracefully seems nicer. Also the
3238 * g33 docs say we should at least disable all the planes.
3239 */
3240 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03003241 if (!crtc->active)
3242 continue;
3243
3244 intel_crtc_disable_planes(&crtc->base);
3245 dev_priv->display.crtc_disable(&crtc->base);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003246 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003247}
3248
3249void intel_finish_reset(struct drm_device *dev)
3250{
3251 struct drm_i915_private *dev_priv = to_i915(dev);
3252
3253 /*
3254 * Flips in the rings will be nuked by the reset,
3255 * so complete all pending flips so that user space
3256 * will get its events and not get stuck.
3257 */
3258 intel_complete_page_flips(dev);
3259
3260 /* no reset support for gen2 */
3261 if (IS_GEN2(dev))
3262 return;
3263
3264 /* reset doesn't touch the display */
3265 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3266 /*
3267 * Flips in the rings have been nuked by the reset,
3268 * so update the base address of all primary
3269 * planes to the the last fb to make sure we're
3270 * showing the correct fb after a reset.
3271 */
3272 intel_update_primary_planes(dev);
3273 return;
3274 }
3275
3276 /*
3277 * The display has been reset as well,
3278 * so need a full re-initialization.
3279 */
3280 intel_runtime_pm_disable_interrupts(dev_priv);
3281 intel_runtime_pm_enable_interrupts(dev_priv);
3282
3283 intel_modeset_init_hw(dev);
3284
3285 spin_lock_irq(&dev_priv->irq_lock);
3286 if (dev_priv->display.hpd_irq_setup)
3287 dev_priv->display.hpd_irq_setup(dev);
3288 spin_unlock_irq(&dev_priv->irq_lock);
3289
3290 intel_modeset_setup_hw_state(dev, true);
3291
3292 intel_hpd_init(dev_priv);
3293
3294 drm_modeset_unlock_all(dev);
3295}
3296
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003297static int
Chris Wilson14667a42012-04-03 17:58:35 +01003298intel_finish_fb(struct drm_framebuffer *old_fb)
3299{
Matt Roper2ff8fde2014-07-08 07:50:07 -07003300 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
Chris Wilson14667a42012-04-03 17:58:35 +01003301 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
3302 bool was_interruptible = dev_priv->mm.interruptible;
3303 int ret;
3304
Chris Wilson14667a42012-04-03 17:58:35 +01003305 /* Big Hammer, we also need to ensure that any pending
3306 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3307 * current scanout is retired before unpinning the old
3308 * framebuffer.
3309 *
3310 * This should only fail upon a hung GPU, in which case we
3311 * can safely continue.
3312 */
3313 dev_priv->mm.interruptible = false;
3314 ret = i915_gem_object_finish_gpu(obj);
3315 dev_priv->mm.interruptible = was_interruptible;
3316
3317 return ret;
3318}
3319
Chris Wilson7d5e3792014-03-04 13:15:08 +00003320static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3321{
3322 struct drm_device *dev = crtc->dev;
3323 struct drm_i915_private *dev_priv = dev->dev_private;
3324 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003325 bool pending;
3326
3327 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3328 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3329 return false;
3330
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003331 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003332 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003333 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003334
3335 return pending;
3336}
3337
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003338static void intel_update_pipe_size(struct intel_crtc *crtc)
3339{
3340 struct drm_device *dev = crtc->base.dev;
3341 struct drm_i915_private *dev_priv = dev->dev_private;
3342 const struct drm_display_mode *adjusted_mode;
3343
3344 if (!i915.fastboot)
3345 return;
3346
3347 /*
3348 * Update pipe size and adjust fitter if needed: the reason for this is
3349 * that in compute_mode_changes we check the native mode (not the pfit
3350 * mode) to see if we can flip rather than do a full mode set. In the
3351 * fastboot case, we'll flip, but if we don't update the pipesrc and
3352 * pfit state, we'll end up with a big fb scanned out into the wrong
3353 * sized surface.
3354 *
3355 * To fix this properly, we need to hoist the checks up into
3356 * compute_mode_changes (or above), check the actual pfit state and
3357 * whether the platform allows pfit disable with pipe active, and only
3358 * then update the pipesrc and pfit state, even on the flip path.
3359 */
3360
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003361 adjusted_mode = &crtc->config->base.adjusted_mode;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003362
3363 I915_WRITE(PIPESRC(crtc->pipe),
3364 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3365 (adjusted_mode->crtc_vdisplay - 1));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003366 if (!crtc->config->pch_pfit.enabled &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03003367 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3368 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003369 I915_WRITE(PF_CTL(crtc->pipe), 0);
3370 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3371 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3372 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003373 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3374 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003375}
3376
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003377static void intel_fdi_normal_train(struct drm_crtc *crtc)
3378{
3379 struct drm_device *dev = crtc->dev;
3380 struct drm_i915_private *dev_priv = dev->dev_private;
3381 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3382 int pipe = intel_crtc->pipe;
3383 u32 reg, temp;
3384
3385 /* enable normal train */
3386 reg = FDI_TX_CTL(pipe);
3387 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003388 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003389 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3390 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003391 } else {
3392 temp &= ~FDI_LINK_TRAIN_NONE;
3393 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003394 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003395 I915_WRITE(reg, temp);
3396
3397 reg = FDI_RX_CTL(pipe);
3398 temp = I915_READ(reg);
3399 if (HAS_PCH_CPT(dev)) {
3400 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3401 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3402 } else {
3403 temp &= ~FDI_LINK_TRAIN_NONE;
3404 temp |= FDI_LINK_TRAIN_NONE;
3405 }
3406 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3407
3408 /* wait one idle pattern time */
3409 POSTING_READ(reg);
3410 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003411
3412 /* IVB wants error correction enabled */
3413 if (IS_IVYBRIDGE(dev))
3414 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3415 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003416}
3417
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003418/* The FDI link training functions for ILK/Ibexpeak. */
3419static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3420{
3421 struct drm_device *dev = crtc->dev;
3422 struct drm_i915_private *dev_priv = dev->dev_private;
3423 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3424 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003425 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003426
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003427 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003428 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003429
Adam Jacksone1a44742010-06-25 15:32:14 -04003430 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3431 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003432 reg = FDI_RX_IMR(pipe);
3433 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003434 temp &= ~FDI_RX_SYMBOL_LOCK;
3435 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003436 I915_WRITE(reg, temp);
3437 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003438 udelay(150);
3439
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003440 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003441 reg = FDI_TX_CTL(pipe);
3442 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003443 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003444 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003445 temp &= ~FDI_LINK_TRAIN_NONE;
3446 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003447 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003448
Chris Wilson5eddb702010-09-11 13:48:45 +01003449 reg = FDI_RX_CTL(pipe);
3450 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003451 temp &= ~FDI_LINK_TRAIN_NONE;
3452 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003453 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3454
3455 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003456 udelay(150);
3457
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003458 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003459 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3460 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3461 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003462
Chris Wilson5eddb702010-09-11 13:48:45 +01003463 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003464 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003465 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003466 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3467
3468 if ((temp & FDI_RX_BIT_LOCK)) {
3469 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003470 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003471 break;
3472 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003473 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003474 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003475 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003476
3477 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003478 reg = FDI_TX_CTL(pipe);
3479 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003480 temp &= ~FDI_LINK_TRAIN_NONE;
3481 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003482 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003483
Chris Wilson5eddb702010-09-11 13:48:45 +01003484 reg = FDI_RX_CTL(pipe);
3485 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003486 temp &= ~FDI_LINK_TRAIN_NONE;
3487 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003488 I915_WRITE(reg, temp);
3489
3490 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003491 udelay(150);
3492
Chris Wilson5eddb702010-09-11 13:48:45 +01003493 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003494 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003495 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003496 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3497
3498 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003499 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003500 DRM_DEBUG_KMS("FDI train 2 done.\n");
3501 break;
3502 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003503 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003504 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003505 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003506
3507 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003508
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003509}
3510
Akshay Joshi0206e352011-08-16 15:34:10 -04003511static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003512 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3513 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3514 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3515 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3516};
3517
3518/* The FDI link training functions for SNB/Cougarpoint. */
3519static void gen6_fdi_link_train(struct drm_crtc *crtc)
3520{
3521 struct drm_device *dev = crtc->dev;
3522 struct drm_i915_private *dev_priv = dev->dev_private;
3523 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3524 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05003525 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003526
Adam Jacksone1a44742010-06-25 15:32:14 -04003527 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3528 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003529 reg = FDI_RX_IMR(pipe);
3530 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003531 temp &= ~FDI_RX_SYMBOL_LOCK;
3532 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003533 I915_WRITE(reg, temp);
3534
3535 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003536 udelay(150);
3537
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003538 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003539 reg = FDI_TX_CTL(pipe);
3540 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003541 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003542 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003543 temp &= ~FDI_LINK_TRAIN_NONE;
3544 temp |= FDI_LINK_TRAIN_PATTERN_1;
3545 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3546 /* SNB-B */
3547 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003548 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003549
Daniel Vetterd74cf322012-10-26 10:58:13 +02003550 I915_WRITE(FDI_RX_MISC(pipe),
3551 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3552
Chris Wilson5eddb702010-09-11 13:48:45 +01003553 reg = FDI_RX_CTL(pipe);
3554 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003555 if (HAS_PCH_CPT(dev)) {
3556 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3557 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3558 } else {
3559 temp &= ~FDI_LINK_TRAIN_NONE;
3560 temp |= FDI_LINK_TRAIN_PATTERN_1;
3561 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003562 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3563
3564 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003565 udelay(150);
3566
Akshay Joshi0206e352011-08-16 15:34:10 -04003567 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003568 reg = FDI_TX_CTL(pipe);
3569 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003570 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3571 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003572 I915_WRITE(reg, temp);
3573
3574 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003575 udelay(500);
3576
Sean Paulfa37d392012-03-02 12:53:39 -05003577 for (retry = 0; retry < 5; retry++) {
3578 reg = FDI_RX_IIR(pipe);
3579 temp = I915_READ(reg);
3580 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3581 if (temp & FDI_RX_BIT_LOCK) {
3582 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3583 DRM_DEBUG_KMS("FDI train 1 done.\n");
3584 break;
3585 }
3586 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003587 }
Sean Paulfa37d392012-03-02 12:53:39 -05003588 if (retry < 5)
3589 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003590 }
3591 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003592 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003593
3594 /* Train 2 */
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_NONE;
3598 temp |= FDI_LINK_TRAIN_PATTERN_2;
3599 if (IS_GEN6(dev)) {
3600 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3601 /* SNB-B */
3602 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3603 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003604 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003605
Chris Wilson5eddb702010-09-11 13:48:45 +01003606 reg = FDI_RX_CTL(pipe);
3607 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003608 if (HAS_PCH_CPT(dev)) {
3609 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3610 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3611 } else {
3612 temp &= ~FDI_LINK_TRAIN_NONE;
3613 temp |= FDI_LINK_TRAIN_PATTERN_2;
3614 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003615 I915_WRITE(reg, temp);
3616
3617 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003618 udelay(150);
3619
Akshay Joshi0206e352011-08-16 15:34:10 -04003620 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003621 reg = FDI_TX_CTL(pipe);
3622 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003623 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3624 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003625 I915_WRITE(reg, temp);
3626
3627 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003628 udelay(500);
3629
Sean Paulfa37d392012-03-02 12:53:39 -05003630 for (retry = 0; retry < 5; retry++) {
3631 reg = FDI_RX_IIR(pipe);
3632 temp = I915_READ(reg);
3633 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3634 if (temp & FDI_RX_SYMBOL_LOCK) {
3635 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3636 DRM_DEBUG_KMS("FDI train 2 done.\n");
3637 break;
3638 }
3639 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003640 }
Sean Paulfa37d392012-03-02 12:53:39 -05003641 if (retry < 5)
3642 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003643 }
3644 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003645 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003646
3647 DRM_DEBUG_KMS("FDI train done.\n");
3648}
3649
Jesse Barnes357555c2011-04-28 15:09:55 -07003650/* Manual link training for Ivy Bridge A0 parts */
3651static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3652{
3653 struct drm_device *dev = crtc->dev;
3654 struct drm_i915_private *dev_priv = dev->dev_private;
3655 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3656 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003657 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003658
3659 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3660 for train result */
3661 reg = FDI_RX_IMR(pipe);
3662 temp = I915_READ(reg);
3663 temp &= ~FDI_RX_SYMBOL_LOCK;
3664 temp &= ~FDI_RX_BIT_LOCK;
3665 I915_WRITE(reg, temp);
3666
3667 POSTING_READ(reg);
3668 udelay(150);
3669
Daniel Vetter01a415f2012-10-27 15:58:40 +02003670 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3671 I915_READ(FDI_RX_IIR(pipe)));
3672
Jesse Barnes139ccd32013-08-19 11:04:55 -07003673 /* Try each vswing and preemphasis setting twice before moving on */
3674 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3675 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003676 reg = FDI_TX_CTL(pipe);
3677 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003678 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3679 temp &= ~FDI_TX_ENABLE;
3680 I915_WRITE(reg, temp);
3681
3682 reg = FDI_RX_CTL(pipe);
3683 temp = I915_READ(reg);
3684 temp &= ~FDI_LINK_TRAIN_AUTO;
3685 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3686 temp &= ~FDI_RX_ENABLE;
3687 I915_WRITE(reg, temp);
3688
3689 /* enable CPU FDI TX and PCH FDI RX */
3690 reg = FDI_TX_CTL(pipe);
3691 temp = I915_READ(reg);
3692 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003693 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003694 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003695 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003696 temp |= snb_b_fdi_train_param[j/2];
3697 temp |= FDI_COMPOSITE_SYNC;
3698 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3699
3700 I915_WRITE(FDI_RX_MISC(pipe),
3701 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3702
3703 reg = FDI_RX_CTL(pipe);
3704 temp = I915_READ(reg);
3705 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3706 temp |= FDI_COMPOSITE_SYNC;
3707 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3708
3709 POSTING_READ(reg);
3710 udelay(1); /* should be 0.5us */
3711
3712 for (i = 0; i < 4; i++) {
3713 reg = FDI_RX_IIR(pipe);
3714 temp = I915_READ(reg);
3715 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3716
3717 if (temp & FDI_RX_BIT_LOCK ||
3718 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3719 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3720 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3721 i);
3722 break;
3723 }
3724 udelay(1); /* should be 0.5us */
3725 }
3726 if (i == 4) {
3727 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3728 continue;
3729 }
3730
3731 /* Train 2 */
3732 reg = FDI_TX_CTL(pipe);
3733 temp = I915_READ(reg);
3734 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3735 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3736 I915_WRITE(reg, temp);
3737
3738 reg = FDI_RX_CTL(pipe);
3739 temp = I915_READ(reg);
3740 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3741 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003742 I915_WRITE(reg, temp);
3743
3744 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003745 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003746
Jesse Barnes139ccd32013-08-19 11:04:55 -07003747 for (i = 0; i < 4; i++) {
3748 reg = FDI_RX_IIR(pipe);
3749 temp = I915_READ(reg);
3750 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003751
Jesse Barnes139ccd32013-08-19 11:04:55 -07003752 if (temp & FDI_RX_SYMBOL_LOCK ||
3753 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3754 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3755 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3756 i);
3757 goto train_done;
3758 }
3759 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003760 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003761 if (i == 4)
3762 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003763 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003764
Jesse Barnes139ccd32013-08-19 11:04:55 -07003765train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003766 DRM_DEBUG_KMS("FDI train done.\n");
3767}
3768
Daniel Vetter88cefb62012-08-12 19:27:14 +02003769static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003770{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003771 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003772 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003773 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003774 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003775
Jesse Barnesc64e3112010-09-10 11:27:03 -07003776
Jesse Barnes0e23b992010-09-10 11:10:00 -07003777 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003778 reg = FDI_RX_CTL(pipe);
3779 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003780 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003781 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003782 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003783 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3784
3785 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003786 udelay(200);
3787
3788 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003789 temp = I915_READ(reg);
3790 I915_WRITE(reg, temp | FDI_PCDCLK);
3791
3792 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003793 udelay(200);
3794
Paulo Zanoni20749732012-11-23 15:30:38 -02003795 /* Enable CPU FDI TX PLL, always on for Ironlake */
3796 reg = FDI_TX_CTL(pipe);
3797 temp = I915_READ(reg);
3798 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3799 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003800
Paulo Zanoni20749732012-11-23 15:30:38 -02003801 POSTING_READ(reg);
3802 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003803 }
3804}
3805
Daniel Vetter88cefb62012-08-12 19:27:14 +02003806static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3807{
3808 struct drm_device *dev = intel_crtc->base.dev;
3809 struct drm_i915_private *dev_priv = dev->dev_private;
3810 int pipe = intel_crtc->pipe;
3811 u32 reg, temp;
3812
3813 /* Switch from PCDclk to Rawclk */
3814 reg = FDI_RX_CTL(pipe);
3815 temp = I915_READ(reg);
3816 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3817
3818 /* Disable CPU FDI TX PLL */
3819 reg = FDI_TX_CTL(pipe);
3820 temp = I915_READ(reg);
3821 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3822
3823 POSTING_READ(reg);
3824 udelay(100);
3825
3826 reg = FDI_RX_CTL(pipe);
3827 temp = I915_READ(reg);
3828 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3829
3830 /* Wait for the clocks to turn off. */
3831 POSTING_READ(reg);
3832 udelay(100);
3833}
3834
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003835static void ironlake_fdi_disable(struct drm_crtc *crtc)
3836{
3837 struct drm_device *dev = crtc->dev;
3838 struct drm_i915_private *dev_priv = dev->dev_private;
3839 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3840 int pipe = intel_crtc->pipe;
3841 u32 reg, temp;
3842
3843 /* disable CPU FDI tx and PCH FDI rx */
3844 reg = FDI_TX_CTL(pipe);
3845 temp = I915_READ(reg);
3846 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3847 POSTING_READ(reg);
3848
3849 reg = FDI_RX_CTL(pipe);
3850 temp = I915_READ(reg);
3851 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003852 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003853 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3854
3855 POSTING_READ(reg);
3856 udelay(100);
3857
3858 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003859 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003860 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003861
3862 /* still set train pattern 1 */
3863 reg = FDI_TX_CTL(pipe);
3864 temp = I915_READ(reg);
3865 temp &= ~FDI_LINK_TRAIN_NONE;
3866 temp |= FDI_LINK_TRAIN_PATTERN_1;
3867 I915_WRITE(reg, temp);
3868
3869 reg = FDI_RX_CTL(pipe);
3870 temp = I915_READ(reg);
3871 if (HAS_PCH_CPT(dev)) {
3872 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3873 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3874 } else {
3875 temp &= ~FDI_LINK_TRAIN_NONE;
3876 temp |= FDI_LINK_TRAIN_PATTERN_1;
3877 }
3878 /* BPC in FDI rx is consistent with that in PIPECONF */
3879 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003880 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003881 I915_WRITE(reg, temp);
3882
3883 POSTING_READ(reg);
3884 udelay(100);
3885}
3886
Chris Wilson5dce5b932014-01-20 10:17:36 +00003887bool intel_has_pending_fb_unpin(struct drm_device *dev)
3888{
3889 struct intel_crtc *crtc;
3890
3891 /* Note that we don't need to be called with mode_config.lock here
3892 * as our list of CRTC objects is static for the lifetime of the
3893 * device and so cannot disappear as we iterate. Similarly, we can
3894 * happily treat the predicates as racy, atomic checks as userspace
3895 * cannot claim and pin a new fb without at least acquring the
3896 * struct_mutex and so serialising with us.
3897 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003898 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003899 if (atomic_read(&crtc->unpin_work_count) == 0)
3900 continue;
3901
3902 if (crtc->unpin_work)
3903 intel_wait_for_vblank(dev, crtc->pipe);
3904
3905 return true;
3906 }
3907
3908 return false;
3909}
3910
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003911static void page_flip_completed(struct intel_crtc *intel_crtc)
3912{
3913 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3914 struct intel_unpin_work *work = intel_crtc->unpin_work;
3915
3916 /* ensure that the unpin work is consistent wrt ->pending. */
3917 smp_rmb();
3918 intel_crtc->unpin_work = NULL;
3919
3920 if (work->event)
3921 drm_send_vblank_event(intel_crtc->base.dev,
3922 intel_crtc->pipe,
3923 work->event);
3924
3925 drm_crtc_vblank_put(&intel_crtc->base);
3926
3927 wake_up_all(&dev_priv->pending_flip_queue);
3928 queue_work(dev_priv->wq, &work->work);
3929
3930 trace_i915_flip_complete(intel_crtc->plane,
3931 work->pending_flip_obj);
3932}
3933
Ville Syrjälä46a55d32014-05-21 14:04:46 +03003934void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003935{
Chris Wilson0f911282012-04-17 10:05:38 +01003936 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003937 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003938
Daniel Vetter2c10d572012-12-20 21:24:07 +01003939 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Chris Wilson9c787942014-09-05 07:13:25 +01003940 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3941 !intel_crtc_has_pending_flip(crtc),
3942 60*HZ) == 0)) {
3943 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003944
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003945 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003946 if (intel_crtc->unpin_work) {
3947 WARN_ONCE(1, "Removing stuck page flip\n");
3948 page_flip_completed(intel_crtc);
3949 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003950 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003951 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003952
Chris Wilson975d5682014-08-20 13:13:34 +01003953 if (crtc->primary->fb) {
3954 mutex_lock(&dev->struct_mutex);
3955 intel_finish_fb(crtc->primary->fb);
3956 mutex_unlock(&dev->struct_mutex);
3957 }
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003958}
3959
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003960/* Program iCLKIP clock to the desired frequency */
3961static void lpt_program_iclkip(struct drm_crtc *crtc)
3962{
3963 struct drm_device *dev = crtc->dev;
3964 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003965 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003966 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3967 u32 temp;
3968
Daniel Vetter09153002012-12-12 14:06:44 +01003969 mutex_lock(&dev_priv->dpio_lock);
3970
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003971 /* It is necessary to ungate the pixclk gate prior to programming
3972 * the divisors, and gate it back when it is done.
3973 */
3974 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3975
3976 /* Disable SSCCTL */
3977 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003978 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3979 SBI_SSCCTL_DISABLE,
3980 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003981
3982 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003983 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003984 auxdiv = 1;
3985 divsel = 0x41;
3986 phaseinc = 0x20;
3987 } else {
3988 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003989 * but the adjusted_mode->crtc_clock in in KHz. To get the
3990 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003991 * convert the virtual clock precision to KHz here for higher
3992 * precision.
3993 */
3994 u32 iclk_virtual_root_freq = 172800 * 1000;
3995 u32 iclk_pi_range = 64;
3996 u32 desired_divisor, msb_divisor_value, pi_value;
3997
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003998 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003999 msb_divisor_value = desired_divisor / iclk_pi_range;
4000 pi_value = desired_divisor % iclk_pi_range;
4001
4002 auxdiv = 0;
4003 divsel = msb_divisor_value - 2;
4004 phaseinc = pi_value;
4005 }
4006
4007 /* This should not happen with any sane values */
4008 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
4009 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
4010 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
4011 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
4012
4013 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 +03004014 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004015 auxdiv,
4016 divsel,
4017 phasedir,
4018 phaseinc);
4019
4020 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004021 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004022 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4023 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4024 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4025 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4026 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4027 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004028 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004029
4030 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004031 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004032 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4033 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004034 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004035
4036 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004037 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004038 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004039 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004040
4041 /* Wait for initialization time */
4042 udelay(24);
4043
4044 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01004045
4046 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004047}
4048
Daniel Vetter275f01b22013-05-03 11:49:47 +02004049static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4050 enum pipe pch_transcoder)
4051{
4052 struct drm_device *dev = crtc->base.dev;
4053 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004054 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02004055
4056 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4057 I915_READ(HTOTAL(cpu_transcoder)));
4058 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4059 I915_READ(HBLANK(cpu_transcoder)));
4060 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4061 I915_READ(HSYNC(cpu_transcoder)));
4062
4063 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4064 I915_READ(VTOTAL(cpu_transcoder)));
4065 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4066 I915_READ(VBLANK(cpu_transcoder)));
4067 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4068 I915_READ(VSYNC(cpu_transcoder)));
4069 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4070 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4071}
4072
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004073static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004074{
4075 struct drm_i915_private *dev_priv = dev->dev_private;
4076 uint32_t temp;
4077
4078 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004079 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004080 return;
4081
4082 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4083 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4084
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004085 temp &= ~FDI_BC_BIFURCATION_SELECT;
4086 if (enable)
4087 temp |= FDI_BC_BIFURCATION_SELECT;
4088
4089 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004090 I915_WRITE(SOUTH_CHICKEN1, temp);
4091 POSTING_READ(SOUTH_CHICKEN1);
4092}
4093
4094static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4095{
4096 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004097
4098 switch (intel_crtc->pipe) {
4099 case PIPE_A:
4100 break;
4101 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004102 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004103 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004104 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004105 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004106
4107 break;
4108 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004109 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004110
4111 break;
4112 default:
4113 BUG();
4114 }
4115}
4116
Jesse Barnesf67a5592011-01-05 10:31:48 -08004117/*
4118 * Enable PCH resources required for PCH ports:
4119 * - PCH PLLs
4120 * - FDI training & RX/TX
4121 * - update transcoder timings
4122 * - DP transcoding bits
4123 * - transcoder
4124 */
4125static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004126{
4127 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004128 struct drm_i915_private *dev_priv = dev->dev_private;
4129 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4130 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004131 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004132
Daniel Vetterab9412b2013-05-03 11:49:46 +02004133 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004134
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004135 if (IS_IVYBRIDGE(dev))
4136 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4137
Daniel Vettercd986ab2012-10-26 10:58:12 +02004138 /* Write the TU size bits before fdi link training, so that error
4139 * detection works. */
4140 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4141 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4142
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004143 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004144 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004145
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004146 /* We need to program the right clock selection before writing the pixel
4147 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004148 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004149 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004150
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004151 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004152 temp |= TRANS_DPLL_ENABLE(pipe);
4153 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004154 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004155 temp |= sel;
4156 else
4157 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004158 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004159 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004160
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004161 /* XXX: pch pll's can be enabled any time before we enable the PCH
4162 * transcoder, and we actually should do this to not upset any PCH
4163 * transcoder that already use the clock when we share it.
4164 *
4165 * Note that enable_shared_dpll tries to do the right thing, but
4166 * get_shared_dpll unconditionally resets the pll - we need that to have
4167 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004168 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004169
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004170 /* set transcoder timing, panel must allow it */
4171 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004172 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004173
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004174 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004175
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004176 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004177 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004178 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01004179 reg = TRANS_DP_CTL(pipe);
4180 temp = I915_READ(reg);
4181 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004182 TRANS_DP_SYNC_MASK |
4183 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01004184 temp |= (TRANS_DP_OUTPUT_ENABLE |
4185 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004186 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004187
4188 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004189 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004190 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004191 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004192
4193 switch (intel_trans_dp_port_sel(crtc)) {
4194 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004195 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004196 break;
4197 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004198 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004199 break;
4200 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004201 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004202 break;
4203 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004204 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004205 }
4206
Chris Wilson5eddb702010-09-11 13:48:45 +01004207 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004208 }
4209
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004210 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004211}
4212
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004213static void lpt_pch_enable(struct drm_crtc *crtc)
4214{
4215 struct drm_device *dev = crtc->dev;
4216 struct drm_i915_private *dev_priv = dev->dev_private;
4217 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004218 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004219
Daniel Vetterab9412b2013-05-03 11:49:46 +02004220 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004221
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004222 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004223
Paulo Zanoni0540e482012-10-31 18:12:40 -02004224 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004225 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004226
Paulo Zanoni937bb612012-10-31 18:12:47 -02004227 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004228}
4229
Daniel Vetter716c2e52014-06-25 22:02:02 +03004230void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004231{
Daniel Vettere2b78262013-06-07 23:10:03 +02004232 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004233
4234 if (pll == NULL)
4235 return;
4236
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02004237 if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004238 WARN(1, "bad %s crtc mask\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004239 return;
4240 }
4241
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02004242 pll->config.crtc_mask &= ~(1 << crtc->pipe);
4243 if (pll->config.crtc_mask == 0) {
Daniel Vetterf4a091c2013-06-10 17:28:22 +02004244 WARN_ON(pll->on);
4245 WARN_ON(pll->active);
4246 }
4247
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004248 crtc->config->shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004249}
4250
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004251struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4252 struct intel_crtc_state *crtc_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004253{
Daniel Vettere2b78262013-06-07 23:10:03 +02004254 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004255 struct intel_shared_dpll *pll;
Daniel Vettere2b78262013-06-07 23:10:03 +02004256 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004257
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004258 if (HAS_PCH_IBX(dev_priv->dev)) {
4259 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02004260 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004261 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004262
Daniel Vetter46edb022013-06-05 13:34:12 +02004263 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4264 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004265
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004266 WARN_ON(pll->new_config->crtc_mask);
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004267
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004268 goto found;
4269 }
4270
Satheeshakrishna Mbcddf612014-08-22 09:49:10 +05304271 if (IS_BROXTON(dev_priv->dev)) {
4272 /* PLL is attached to port in bxt */
4273 struct intel_encoder *encoder;
4274 struct intel_digital_port *intel_dig_port;
4275
4276 encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
4277 if (WARN_ON(!encoder))
4278 return NULL;
4279
4280 intel_dig_port = enc_to_dig_port(&encoder->base);
4281 /* 1:1 mapping between ports and PLLs */
4282 i = (enum intel_dpll_id)intel_dig_port->port;
4283 pll = &dev_priv->shared_dplls[i];
4284 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4285 crtc->base.base.id, pll->name);
4286 WARN_ON(pll->new_config->crtc_mask);
4287
4288 goto found;
4289 }
4290
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004291 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4292 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004293
4294 /* Only want to check enabled timings first */
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004295 if (pll->new_config->crtc_mask == 0)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004296 continue;
4297
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004298 if (memcmp(&crtc_state->dpll_hw_state,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004299 &pll->new_config->hw_state,
4300 sizeof(pll->new_config->hw_state)) == 0) {
4301 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004302 crtc->base.base.id, pll->name,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004303 pll->new_config->crtc_mask,
4304 pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004305 goto found;
4306 }
4307 }
4308
4309 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004310 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4311 pll = &dev_priv->shared_dplls[i];
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004312 if (pll->new_config->crtc_mask == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02004313 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4314 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004315 goto found;
4316 }
4317 }
4318
4319 return NULL;
4320
4321found:
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004322 if (pll->new_config->crtc_mask == 0)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004323 pll->new_config->hw_state = crtc_state->dpll_hw_state;
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004324
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004325 crtc_state->shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02004326 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4327 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02004328
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004329 pll->new_config->crtc_mask |= 1 << crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004330
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004331 return pll;
4332}
4333
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004334/**
4335 * intel_shared_dpll_start_config - start a new PLL staged config
4336 * @dev_priv: DRM device
4337 * @clear_pipes: mask of pipes that will have their PLLs freed
4338 *
4339 * Starts a new PLL staged config, copying the current config but
4340 * releasing the references of pipes specified in clear_pipes.
4341 */
4342static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
4343 unsigned clear_pipes)
4344{
4345 struct intel_shared_dpll *pll;
4346 enum intel_dpll_id i;
4347
4348 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4349 pll = &dev_priv->shared_dplls[i];
4350
4351 pll->new_config = kmemdup(&pll->config, sizeof pll->config,
4352 GFP_KERNEL);
4353 if (!pll->new_config)
4354 goto cleanup;
4355
4356 pll->new_config->crtc_mask &= ~clear_pipes;
4357 }
4358
4359 return 0;
4360
4361cleanup:
4362 while (--i >= 0) {
4363 pll = &dev_priv->shared_dplls[i];
Ander Conselvan de Oliveiraf354d732014-11-07 14:07:41 +02004364 kfree(pll->new_config);
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004365 pll->new_config = NULL;
4366 }
4367
4368 return -ENOMEM;
4369}
4370
4371static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
4372{
4373 struct intel_shared_dpll *pll;
4374 enum intel_dpll_id i;
4375
4376 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4377 pll = &dev_priv->shared_dplls[i];
4378
4379 WARN_ON(pll->new_config == &pll->config);
4380
4381 pll->config = *pll->new_config;
4382 kfree(pll->new_config);
4383 pll->new_config = NULL;
4384 }
4385}
4386
4387static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
4388{
4389 struct intel_shared_dpll *pll;
4390 enum intel_dpll_id i;
4391
4392 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4393 pll = &dev_priv->shared_dplls[i];
4394
4395 WARN_ON(pll->new_config == &pll->config);
4396
4397 kfree(pll->new_config);
4398 pll->new_config = NULL;
4399 }
4400}
4401
Daniel Vettera1520312013-05-03 11:49:50 +02004402static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004403{
4404 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01004405 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004406 u32 temp;
4407
4408 temp = I915_READ(dslreg);
4409 udelay(500);
4410 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004411 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004412 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004413 }
4414}
4415
Chandra Kondurua1b22782015-04-07 15:28:45 -07004416/**
4417 * skl_update_scaler_users - Stages update to crtc's scaler state
4418 * @intel_crtc: crtc
4419 * @crtc_state: crtc_state
4420 * @plane: plane (NULL indicates crtc is requesting update)
4421 * @plane_state: plane's state
4422 * @force_detach: request unconditional detachment of scaler
4423 *
4424 * This function updates scaler state for requested plane or crtc.
4425 * To request scaler usage update for a plane, caller shall pass plane pointer.
4426 * To request scaler usage update for crtc, caller shall pass plane pointer
4427 * as NULL.
4428 *
4429 * Return
4430 * 0 - scaler_usage updated successfully
4431 * error - requested scaling cannot be supported or other error condition
4432 */
4433int
4434skl_update_scaler_users(
4435 struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state,
4436 struct intel_plane *intel_plane, struct intel_plane_state *plane_state,
4437 int force_detach)
4438{
4439 int need_scaling;
4440 int idx;
4441 int src_w, src_h, dst_w, dst_h;
4442 int *scaler_id;
4443 struct drm_framebuffer *fb;
4444 struct intel_crtc_scaler_state *scaler_state;
Chandra Konduru6156a452015-04-27 13:48:39 -07004445 unsigned int rotation;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004446
4447 if (!intel_crtc || !crtc_state)
4448 return 0;
4449
4450 scaler_state = &crtc_state->scaler_state;
4451
4452 idx = intel_plane ? drm_plane_index(&intel_plane->base) : SKL_CRTC_INDEX;
4453 fb = intel_plane ? plane_state->base.fb : NULL;
4454
4455 if (intel_plane) {
4456 src_w = drm_rect_width(&plane_state->src) >> 16;
4457 src_h = drm_rect_height(&plane_state->src) >> 16;
4458 dst_w = drm_rect_width(&plane_state->dst);
4459 dst_h = drm_rect_height(&plane_state->dst);
4460 scaler_id = &plane_state->scaler_id;
Chandra Konduru6156a452015-04-27 13:48:39 -07004461 rotation = plane_state->base.rotation;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004462 } else {
4463 struct drm_display_mode *adjusted_mode =
4464 &crtc_state->base.adjusted_mode;
4465 src_w = crtc_state->pipe_src_w;
4466 src_h = crtc_state->pipe_src_h;
4467 dst_w = adjusted_mode->hdisplay;
4468 dst_h = adjusted_mode->vdisplay;
4469 scaler_id = &scaler_state->scaler_id;
Chandra Konduru6156a452015-04-27 13:48:39 -07004470 rotation = DRM_ROTATE_0;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004471 }
Chandra Konduru6156a452015-04-27 13:48:39 -07004472
4473 need_scaling = intel_rotation_90_or_270(rotation) ?
4474 (src_h != dst_w || src_w != dst_h):
4475 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004476
4477 /*
4478 * if plane is being disabled or scaler is no more required or force detach
4479 * - free scaler binded to this plane/crtc
4480 * - in order to do this, update crtc->scaler_usage
4481 *
4482 * Here scaler state in crtc_state is set free so that
4483 * scaler can be assigned to other user. Actual register
4484 * update to free the scaler is done in plane/panel-fit programming.
4485 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4486 */
4487 if (force_detach || !need_scaling || (intel_plane &&
4488 (!fb || !plane_state->visible))) {
4489 if (*scaler_id >= 0) {
4490 scaler_state->scaler_users &= ~(1 << idx);
4491 scaler_state->scalers[*scaler_id].in_use = 0;
4492
4493 DRM_DEBUG_KMS("Staged freeing scaler id %d.%d from %s:%d "
4494 "crtc_state = %p scaler_users = 0x%x\n",
4495 intel_crtc->pipe, *scaler_id, intel_plane ? "PLANE" : "CRTC",
4496 intel_plane ? intel_plane->base.base.id :
4497 intel_crtc->base.base.id, crtc_state,
4498 scaler_state->scaler_users);
4499 *scaler_id = -1;
4500 }
4501 return 0;
4502 }
4503
4504 /* range checks */
4505 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4506 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4507
4508 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4509 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
4510 DRM_DEBUG_KMS("%s:%d scaler_user index %u.%u: src %ux%u dst %ux%u "
4511 "size is out of scaler range\n",
4512 intel_plane ? "PLANE" : "CRTC",
4513 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4514 intel_crtc->pipe, idx, src_w, src_h, dst_w, dst_h);
4515 return -EINVAL;
4516 }
4517
4518 /* check colorkey */
4519 if (intel_plane && intel_plane->ckey.flags != I915_SET_COLORKEY_NONE) {
4520 DRM_DEBUG_KMS("PLANE:%d scaling with color key not allowed",
4521 intel_plane->base.base.id);
4522 return -EINVAL;
4523 }
4524
4525 /* Check src format */
4526 if (intel_plane) {
4527 switch (fb->pixel_format) {
4528 case DRM_FORMAT_RGB565:
4529 case DRM_FORMAT_XBGR8888:
4530 case DRM_FORMAT_XRGB8888:
4531 case DRM_FORMAT_ABGR8888:
4532 case DRM_FORMAT_ARGB8888:
4533 case DRM_FORMAT_XRGB2101010:
4534 case DRM_FORMAT_ARGB2101010:
4535 case DRM_FORMAT_XBGR2101010:
4536 case DRM_FORMAT_ABGR2101010:
4537 case DRM_FORMAT_YUYV:
4538 case DRM_FORMAT_YVYU:
4539 case DRM_FORMAT_UYVY:
4540 case DRM_FORMAT_VYUY:
4541 break;
4542 default:
4543 DRM_DEBUG_KMS("PLANE:%d FB:%d unsupported scaling format 0x%x\n",
4544 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4545 return -EINVAL;
4546 }
4547 }
4548
4549 /* mark this plane as a scaler user in crtc_state */
4550 scaler_state->scaler_users |= (1 << idx);
4551 DRM_DEBUG_KMS("%s:%d staged scaling request for %ux%u->%ux%u "
4552 "crtc_state = %p scaler_users = 0x%x\n",
4553 intel_plane ? "PLANE" : "CRTC",
4554 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4555 src_w, src_h, dst_w, dst_h, crtc_state, scaler_state->scaler_users);
4556 return 0;
4557}
4558
4559static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004560{
4561 struct drm_device *dev = crtc->base.dev;
4562 struct drm_i915_private *dev_priv = dev->dev_private;
4563 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004564 struct intel_crtc_scaler_state *scaler_state =
4565 &crtc->config->scaler_state;
4566
4567 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4568
4569 /* To update pfit, first update scaler state */
4570 skl_update_scaler_users(crtc, crtc->config, NULL, NULL, !enable);
4571 intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
4572 skl_detach_scalers(crtc);
4573 if (!enable)
4574 return;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004575
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004576 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004577 int id;
4578
4579 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4580 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4581 return;
4582 }
4583
4584 id = scaler_state->scaler_id;
4585 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4586 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4587 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4588 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4589
4590 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004591 }
4592}
4593
Jesse Barnesb074cec2013-04-25 12:55:02 -07004594static void ironlake_pfit_enable(struct intel_crtc *crtc)
4595{
4596 struct drm_device *dev = crtc->base.dev;
4597 struct drm_i915_private *dev_priv = dev->dev_private;
4598 int pipe = crtc->pipe;
4599
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004600 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004601 /* Force use of hard-coded filter coefficients
4602 * as some pre-programmed values are broken,
4603 * e.g. x201.
4604 */
4605 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4606 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4607 PF_PIPE_SEL_IVB(pipe));
4608 else
4609 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004610 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4611 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004612 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004613}
4614
Matt Roper4a3b8762014-12-23 10:41:51 -08004615static void intel_enable_sprite_planes(struct drm_crtc *crtc)
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004616{
4617 struct drm_device *dev = crtc->dev;
4618 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07004619 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004620 struct intel_plane *intel_plane;
4621
Matt Roperaf2b6532014-04-01 15:22:32 -07004622 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4623 intel_plane = to_intel_plane(plane);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004624 if (intel_plane->pipe == pipe)
4625 intel_plane_restore(&intel_plane->base);
Matt Roperaf2b6532014-04-01 15:22:32 -07004626 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004627}
4628
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004629void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004630{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004631 struct drm_device *dev = crtc->base.dev;
4632 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004633
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004634 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004635 return;
4636
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004637 /* We can only enable IPS after we enable a plane and wait for a vblank */
4638 intel_wait_for_vblank(dev, crtc->pipe);
4639
Paulo Zanonid77e4532013-09-24 13:52:55 -03004640 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004641 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004642 mutex_lock(&dev_priv->rps.hw_lock);
4643 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4644 mutex_unlock(&dev_priv->rps.hw_lock);
4645 /* Quoting Art Runyan: "its not safe to expect any particular
4646 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004647 * mailbox." Moreover, the mailbox may return a bogus state,
4648 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004649 */
4650 } else {
4651 I915_WRITE(IPS_CTL, IPS_ENABLE);
4652 /* The bit only becomes 1 in the next vblank, so this wait here
4653 * is essentially intel_wait_for_vblank. If we don't have this
4654 * and don't wait for vblanks until the end of crtc_enable, then
4655 * the HW state readout code will complain that the expected
4656 * IPS_CTL value is not the one we read. */
4657 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4658 DRM_ERROR("Timed out waiting for IPS enable\n");
4659 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004660}
4661
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004662void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004663{
4664 struct drm_device *dev = crtc->base.dev;
4665 struct drm_i915_private *dev_priv = dev->dev_private;
4666
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004667 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004668 return;
4669
4670 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004671 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004672 mutex_lock(&dev_priv->rps.hw_lock);
4673 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4674 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004675 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4676 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4677 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004678 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004679 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004680 POSTING_READ(IPS_CTL);
4681 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004682
4683 /* We need to wait for a vblank before we can disable the plane. */
4684 intel_wait_for_vblank(dev, crtc->pipe);
4685}
4686
4687/** Loads the palette/gamma unit for the CRTC with the prepared values */
4688static void intel_crtc_load_lut(struct drm_crtc *crtc)
4689{
4690 struct drm_device *dev = crtc->dev;
4691 struct drm_i915_private *dev_priv = dev->dev_private;
4692 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4693 enum pipe pipe = intel_crtc->pipe;
4694 int palreg = PALETTE(pipe);
4695 int i;
4696 bool reenable_ips = false;
4697
4698 /* The clocks have to be on to load the palette. */
Matt Roper83d65732015-02-25 13:12:16 -08004699 if (!crtc->state->enable || !intel_crtc->active)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004700 return;
4701
Imre Deak50360402015-01-16 00:55:16 -08004702 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03004703 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004704 assert_dsi_pll_enabled(dev_priv);
4705 else
4706 assert_pll_enabled(dev_priv, pipe);
4707 }
4708
4709 /* use legacy palette for Ironlake */
Sonika Jindal7a1db492014-07-22 11:18:27 +05304710 if (!HAS_GMCH_DISPLAY(dev))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004711 palreg = LGC_PALETTE(pipe);
4712
4713 /* Workaround : Do not read or write the pipe palette/gamma data while
4714 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4715 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004716 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03004717 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4718 GAMMA_MODE_MODE_SPLIT)) {
4719 hsw_disable_ips(intel_crtc);
4720 reenable_ips = true;
4721 }
4722
4723 for (i = 0; i < 256; i++) {
4724 I915_WRITE(palreg + 4 * i,
4725 (intel_crtc->lut_r[i] << 16) |
4726 (intel_crtc->lut_g[i] << 8) |
4727 intel_crtc->lut_b[i]);
4728 }
4729
4730 if (reenable_ips)
4731 hsw_enable_ips(intel_crtc);
4732}
4733
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004734static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004735{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004736 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004737 struct drm_device *dev = intel_crtc->base.dev;
4738 struct drm_i915_private *dev_priv = dev->dev_private;
4739
4740 mutex_lock(&dev->struct_mutex);
4741 dev_priv->mm.interruptible = false;
4742 (void) intel_overlay_switch_off(intel_crtc->overlay);
4743 dev_priv->mm.interruptible = true;
4744 mutex_unlock(&dev->struct_mutex);
4745 }
4746
4747 /* Let userspace switch the overlay on again. In most cases userspace
4748 * has to recompute where to put it anyway.
4749 */
4750}
4751
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004752/**
4753 * intel_post_enable_primary - Perform operations after enabling primary plane
4754 * @crtc: the CRTC whose primary plane was just enabled
4755 *
4756 * Performs potentially sleeping operations that must be done after the primary
4757 * plane is enabled, such as updating FBC and IPS. Note that this may be
4758 * called due to an explicit primary plane update, or due to an implicit
4759 * re-enable that is caused when a sprite plane is updated to no longer
4760 * completely hide the primary plane.
4761 */
4762static void
4763intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004764{
4765 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004766 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004767 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4768 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004769
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004770 /*
4771 * BDW signals flip done immediately if the plane
4772 * is disabled, even if the plane enable is already
4773 * armed to occur at the next vblank :(
4774 */
4775 if (IS_BROADWELL(dev))
4776 intel_wait_for_vblank(dev, pipe);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004777
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004778 /*
4779 * FIXME IPS should be fine as long as one plane is
4780 * enabled, but in practice it seems to have problems
4781 * when going from primary only to sprite only and vice
4782 * versa.
4783 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004784 hsw_enable_ips(intel_crtc);
4785
4786 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004787 intel_fbc_update(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004788 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf99d7062014-06-19 16:01:59 +02004789
4790 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004791 * Gen2 reports pipe underruns whenever all planes are disabled.
4792 * So don't enable underrun reporting before at least some planes
4793 * are enabled.
4794 * FIXME: Need to fix the logic to work when we turn off all planes
4795 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004796 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004797 if (IS_GEN2(dev))
4798 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4799
4800 /* Underruns don't raise interrupts, so check manually. */
4801 if (HAS_GMCH_DISPLAY(dev))
4802 i9xx_check_fifo_underruns(dev_priv);
4803}
4804
4805/**
4806 * intel_pre_disable_primary - Perform operations before disabling primary plane
4807 * @crtc: the CRTC whose primary plane is to be disabled
4808 *
4809 * Performs potentially sleeping operations that must be done before the
4810 * primary plane is disabled, such as updating FBC and IPS. Note that this may
4811 * be called due to an explicit primary plane update, or due to an implicit
4812 * disable that is caused when a sprite plane completely hides the primary
4813 * plane.
4814 */
4815static void
4816intel_pre_disable_primary(struct drm_crtc *crtc)
4817{
4818 struct drm_device *dev = crtc->dev;
4819 struct drm_i915_private *dev_priv = dev->dev_private;
4820 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4821 int pipe = intel_crtc->pipe;
4822
4823 /*
4824 * Gen2 reports pipe underruns whenever all planes are disabled.
4825 * So diasble underrun reporting before all the planes get disabled.
4826 * FIXME: Need to fix the logic to work when we turn off all planes
4827 * but leave the pipe running.
4828 */
4829 if (IS_GEN2(dev))
4830 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4831
4832 /*
4833 * Vblank time updates from the shadow to live plane control register
4834 * are blocked if the memory self-refresh mode is active at that
4835 * moment. So to make sure the plane gets truly disabled, disable
4836 * first the self-refresh mode. The self-refresh enable bit in turn
4837 * will be checked/applied by the HW only at the next frame start
4838 * event which is after the vblank start event, so we need to have a
4839 * wait-for-vblank between disabling the plane and the pipe.
4840 */
4841 if (HAS_GMCH_DISPLAY(dev))
4842 intel_set_memory_cxsr(dev_priv, false);
4843
4844 mutex_lock(&dev->struct_mutex);
4845 if (dev_priv->fbc.crtc == intel_crtc)
4846 intel_fbc_disable(dev);
4847 mutex_unlock(&dev->struct_mutex);
4848
4849 /*
4850 * FIXME IPS should be fine as long as one plane is
4851 * enabled, but in practice it seems to have problems
4852 * when going from primary only to sprite only and vice
4853 * versa.
4854 */
4855 hsw_disable_ips(intel_crtc);
4856}
4857
4858static void intel_crtc_enable_planes(struct drm_crtc *crtc)
4859{
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004860 intel_enable_primary_hw_plane(crtc->primary, crtc);
4861 intel_enable_sprite_planes(crtc);
4862 intel_crtc_update_cursor(crtc, true);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004863
4864 intel_post_enable_primary(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004865}
4866
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004867static void intel_crtc_disable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004868{
4869 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004870 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004871 struct intel_plane *intel_plane;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004872 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004873
4874 intel_crtc_wait_for_pending_flips(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004875
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004876 intel_pre_disable_primary(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004877
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004878 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004879 for_each_intel_plane(dev, intel_plane) {
4880 if (intel_plane->pipe == pipe) {
4881 struct drm_crtc *from = intel_plane->base.crtc;
4882
4883 intel_plane->disable_plane(&intel_plane->base,
4884 from ?: crtc, true);
4885 }
4886 }
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004887
Daniel Vetterf99d7062014-06-19 16:01:59 +02004888 /*
4889 * FIXME: Once we grow proper nuclear flip support out of this we need
4890 * to compute the mask of flip planes precisely. For the time being
4891 * consider this a flip to a NULL plane.
4892 */
4893 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004894}
4895
Jesse Barnesf67a5592011-01-05 10:31:48 -08004896static void ironlake_crtc_enable(struct drm_crtc *crtc)
4897{
4898 struct drm_device *dev = crtc->dev;
4899 struct drm_i915_private *dev_priv = dev->dev_private;
4900 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004901 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004902 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004903
Matt Roper83d65732015-02-25 13:12:16 -08004904 WARN_ON(!crtc->state->enable);
Daniel Vetter08a48462012-07-02 11:43:47 +02004905
Jesse Barnesf67a5592011-01-05 10:31:48 -08004906 if (intel_crtc->active)
4907 return;
4908
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004909 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004910 intel_prepare_shared_dpll(intel_crtc);
4911
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004912 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304913 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004914
4915 intel_set_pipe_timings(intel_crtc);
4916
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004917 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004918 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004919 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004920 }
4921
4922 ironlake_set_pipeconf(crtc);
4923
Jesse Barnesf67a5592011-01-05 10:31:48 -08004924 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004925
Daniel Vettera72e4c92014-09-30 10:56:47 +02004926 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4927 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004928
Daniel Vetterf6736a12013-06-05 13:34:30 +02004929 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004930 if (encoder->pre_enable)
4931 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004932
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004933 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004934 /* Note: FDI PLL enabling _must_ be done before we enable the
4935 * cpu pipes, hence this is separate from all the other fdi/pch
4936 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004937 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004938 } else {
4939 assert_fdi_tx_disabled(dev_priv, pipe);
4940 assert_fdi_rx_disabled(dev_priv, pipe);
4941 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004942
Jesse Barnesb074cec2013-04-25 12:55:02 -07004943 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004944
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004945 /*
4946 * On ILK+ LUT must be loaded before the pipe is running but with
4947 * clocks enabled
4948 */
4949 intel_crtc_load_lut(crtc);
4950
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004951 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004952 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004953
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004954 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004955 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004956
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004957 assert_vblank_disabled(crtc);
4958 drm_crtc_vblank_on(crtc);
4959
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004960 for_each_encoder_on_crtc(dev, crtc, encoder)
4961 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004962
4963 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004964 cpt_verify_modeset(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004965}
4966
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004967/* IPS only exists on ULT machines and is tied to pipe A. */
4968static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4969{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004970 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004971}
4972
Paulo Zanonie4916942013-09-20 16:21:19 -03004973/*
4974 * This implements the workaround described in the "notes" section of the mode
4975 * set sequence documentation. When going from no pipes or single pipe to
4976 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4977 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4978 */
4979static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4980{
4981 struct drm_device *dev = crtc->base.dev;
4982 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4983
4984 /* We want to get the other_active_crtc only if there's only 1 other
4985 * active crtc. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004986 for_each_intel_crtc(dev, crtc_it) {
Paulo Zanonie4916942013-09-20 16:21:19 -03004987 if (!crtc_it->active || crtc_it == crtc)
4988 continue;
4989
4990 if (other_active_crtc)
4991 return;
4992
4993 other_active_crtc = crtc_it;
4994 }
4995 if (!other_active_crtc)
4996 return;
4997
4998 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4999 intel_wait_for_vblank(dev, other_active_crtc->pipe);
5000}
5001
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005002static void haswell_crtc_enable(struct drm_crtc *crtc)
5003{
5004 struct drm_device *dev = crtc->dev;
5005 struct drm_i915_private *dev_priv = dev->dev_private;
5006 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5007 struct intel_encoder *encoder;
5008 int pipe = intel_crtc->pipe;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005009
Matt Roper83d65732015-02-25 13:12:16 -08005010 WARN_ON(!crtc->state->enable);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005011
5012 if (intel_crtc->active)
5013 return;
5014
Daniel Vetterdf8ad702014-06-25 22:02:03 +03005015 if (intel_crtc_to_shared_dpll(intel_crtc))
5016 intel_enable_shared_dpll(intel_crtc);
5017
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005018 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05305019 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02005020
5021 intel_set_pipe_timings(intel_crtc);
5022
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005023 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
5024 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
5025 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07005026 }
5027
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005028 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02005029 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005030 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02005031 }
5032
5033 haswell_set_pipeconf(crtc);
5034
5035 intel_set_pipe_csc(crtc);
5036
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005037 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03005038
Daniel Vettera72e4c92014-09-30 10:56:47 +02005039 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005040 for_each_encoder_on_crtc(dev, crtc, encoder)
5041 if (encoder->pre_enable)
5042 encoder->pre_enable(encoder);
5043
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005044 if (intel_crtc->config->has_pch_encoder) {
Daniel Vettera72e4c92014-09-30 10:56:47 +02005045 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5046 true);
Imre Deak4fe94672014-06-25 22:01:49 +03005047 dev_priv->display.fdi_link_train(crtc);
5048 }
5049
Paulo Zanoni1f544382012-10-24 11:32:00 -02005050 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005051
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005052 if (INTEL_INFO(dev)->gen == 9)
Chandra Kondurua1b22782015-04-07 15:28:45 -07005053 skylake_pfit_update(intel_crtc, 1);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005054 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00005055 ironlake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005056 else
5057 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005058
5059 /*
5060 * On ILK+ LUT must be loaded before the pipe is running but with
5061 * clocks enabled
5062 */
5063 intel_crtc_load_lut(crtc);
5064
Paulo Zanoni1f544382012-10-24 11:32:00 -02005065 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00005066 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005067
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005068 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005069 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005070
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005071 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02005072 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005073
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005074 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10005075 intel_ddi_set_vc_payload_alloc(crtc, true);
5076
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005077 assert_vblank_disabled(crtc);
5078 drm_crtc_vblank_on(crtc);
5079
Jani Nikula8807e552013-08-30 19:40:32 +03005080 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005081 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005082 intel_opregion_notify_encoder(encoder, true);
5083 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005084
Paulo Zanonie4916942013-09-20 16:21:19 -03005085 /* If we change the relative order between pipe/planes enabling, we need
5086 * to change the workaround. */
5087 haswell_mode_set_planes_workaround(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005088}
5089
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005090static void ironlake_pfit_disable(struct intel_crtc *crtc)
5091{
5092 struct drm_device *dev = crtc->base.dev;
5093 struct drm_i915_private *dev_priv = dev->dev_private;
5094 int pipe = crtc->pipe;
5095
5096 /* To avoid upsetting the power well on haswell only disable the pfit if
5097 * it's in use. The hw state code will make sure we get this right. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005098 if (crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005099 I915_WRITE(PF_CTL(pipe), 0);
5100 I915_WRITE(PF_WIN_POS(pipe), 0);
5101 I915_WRITE(PF_WIN_SZ(pipe), 0);
5102 }
5103}
5104
Jesse Barnes6be4a602010-09-10 10:26:01 -07005105static void ironlake_crtc_disable(struct drm_crtc *crtc)
5106{
5107 struct drm_device *dev = crtc->dev;
5108 struct drm_i915_private *dev_priv = dev->dev_private;
5109 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005110 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005111 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01005112 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005113
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005114 if (!intel_crtc->active)
5115 return;
5116
Daniel Vetterea9d7582012-07-10 10:42:52 +02005117 for_each_encoder_on_crtc(dev, crtc, encoder)
5118 encoder->disable(encoder);
5119
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005120 drm_crtc_vblank_off(crtc);
5121 assert_vblank_disabled(crtc);
5122
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005123 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005124 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Daniel Vetterd925c592013-06-05 13:34:04 +02005125
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005126 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005127
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005128 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005129
Daniel Vetterbf49ec82012-09-06 22:15:40 +02005130 for_each_encoder_on_crtc(dev, crtc, encoder)
5131 if (encoder->post_disable)
5132 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005133
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005134 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005135 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005136
Daniel Vetterd925c592013-06-05 13:34:04 +02005137 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005138
Daniel Vetterd925c592013-06-05 13:34:04 +02005139 if (HAS_PCH_CPT(dev)) {
5140 /* disable TRANS_DP_CTL */
5141 reg = TRANS_DP_CTL(pipe);
5142 temp = I915_READ(reg);
5143 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5144 TRANS_DP_PORT_SEL_MASK);
5145 temp |= TRANS_DP_PORT_SEL_NONE;
5146 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005147
Daniel Vetterd925c592013-06-05 13:34:04 +02005148 /* disable DPLL_SEL */
5149 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005150 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005151 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005152 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005153
5154 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005155 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02005156
5157 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005158 }
5159
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005160 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005161 intel_update_watermarks(crtc);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01005162
5163 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005164 intel_fbc_update(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01005165 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005166}
5167
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005168static void haswell_crtc_disable(struct drm_crtc *crtc)
5169{
5170 struct drm_device *dev = crtc->dev;
5171 struct drm_i915_private *dev_priv = dev->dev_private;
5172 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5173 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005174 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005175
5176 if (!intel_crtc->active)
5177 return;
5178
Jani Nikula8807e552013-08-30 19:40:32 +03005179 for_each_encoder_on_crtc(dev, crtc, encoder) {
5180 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005181 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005182 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005183
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005184 drm_crtc_vblank_off(crtc);
5185 assert_vblank_disabled(crtc);
5186
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005187 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005188 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5189 false);
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005190 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005191
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005192 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005193 intel_ddi_set_vc_payload_alloc(crtc, false);
5194
Paulo Zanoniad80a812012-10-24 16:06:19 -02005195 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005196
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005197 if (INTEL_INFO(dev)->gen == 9)
Chandra Kondurua1b22782015-04-07 15:28:45 -07005198 skylake_pfit_update(intel_crtc, 0);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005199 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00005200 ironlake_pfit_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005201 else
5202 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005203
Paulo Zanoni1f544382012-10-24 11:32:00 -02005204 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005205
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005206 if (intel_crtc->config->has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02005207 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02005208 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02005209 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005210
Imre Deak97b040a2014-06-25 22:01:50 +03005211 for_each_encoder_on_crtc(dev, crtc, encoder)
5212 if (encoder->post_disable)
5213 encoder->post_disable(encoder);
5214
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005215 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005216 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005217
5218 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005219 intel_fbc_update(dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005220 mutex_unlock(&dev->struct_mutex);
Daniel Vetterdf8ad702014-06-25 22:02:03 +03005221
5222 if (intel_crtc_to_shared_dpll(intel_crtc))
5223 intel_disable_shared_dpll(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005224}
5225
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005226static void ironlake_crtc_off(struct drm_crtc *crtc)
5227{
5228 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005229 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005230}
5231
Paulo Zanoni6441ab52012-10-05 12:05:58 -03005232
Jesse Barnes2dd24552013-04-25 12:55:01 -07005233static void i9xx_pfit_enable(struct intel_crtc *crtc)
5234{
5235 struct drm_device *dev = crtc->base.dev;
5236 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005237 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005238
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005239 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005240 return;
5241
Daniel Vetterc0b03412013-05-28 12:05:54 +02005242 /*
5243 * The panel fitter should only be adjusted whilst the pipe is disabled,
5244 * according to register description and PRM.
5245 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005246 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5247 assert_pipe_disabled(dev_priv, crtc->pipe);
5248
Jesse Barnesb074cec2013-04-25 12:55:02 -07005249 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5250 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005251
5252 /* Border color in case we don't scale up to the full screen. Black by
5253 * default, change to something else for debugging. */
5254 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005255}
5256
Dave Airlied05410f2014-06-05 13:22:59 +10005257static enum intel_display_power_domain port_to_power_domain(enum port port)
5258{
5259 switch (port) {
5260 case PORT_A:
5261 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
5262 case PORT_B:
5263 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
5264 case PORT_C:
5265 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
5266 case PORT_D:
5267 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
5268 default:
5269 WARN_ON_ONCE(1);
5270 return POWER_DOMAIN_PORT_OTHER;
5271 }
5272}
5273
Imre Deak77d22dc2014-03-05 16:20:52 +02005274#define for_each_power_domain(domain, mask) \
5275 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
5276 if ((1 << (domain)) & (mask))
5277
Imre Deak319be8a2014-03-04 19:22:57 +02005278enum intel_display_power_domain
5279intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005280{
Imre Deak319be8a2014-03-04 19:22:57 +02005281 struct drm_device *dev = intel_encoder->base.dev;
5282 struct intel_digital_port *intel_dig_port;
5283
5284 switch (intel_encoder->type) {
5285 case INTEL_OUTPUT_UNKNOWN:
5286 /* Only DDI platforms should ever use this output type */
5287 WARN_ON_ONCE(!HAS_DDI(dev));
5288 case INTEL_OUTPUT_DISPLAYPORT:
5289 case INTEL_OUTPUT_HDMI:
5290 case INTEL_OUTPUT_EDP:
5291 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005292 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005293 case INTEL_OUTPUT_DP_MST:
5294 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5295 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005296 case INTEL_OUTPUT_ANALOG:
5297 return POWER_DOMAIN_PORT_CRT;
5298 case INTEL_OUTPUT_DSI:
5299 return POWER_DOMAIN_PORT_DSI;
5300 default:
5301 return POWER_DOMAIN_PORT_OTHER;
5302 }
5303}
5304
5305static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
5306{
5307 struct drm_device *dev = crtc->dev;
5308 struct intel_encoder *intel_encoder;
5309 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5310 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005311 unsigned long mask;
5312 enum transcoder transcoder;
5313
5314 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
5315
5316 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5317 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005318 if (intel_crtc->config->pch_pfit.enabled ||
5319 intel_crtc->config->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005320 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5321
Imre Deak319be8a2014-03-04 19:22:57 +02005322 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5323 mask |= BIT(intel_display_port_power_domain(intel_encoder));
5324
Imre Deak77d22dc2014-03-05 16:20:52 +02005325 return mask;
5326}
5327
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005328static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
Imre Deak77d22dc2014-03-05 16:20:52 +02005329{
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005330 struct drm_device *dev = state->dev;
Imre Deak77d22dc2014-03-05 16:20:52 +02005331 struct drm_i915_private *dev_priv = dev->dev_private;
5332 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
5333 struct intel_crtc *crtc;
5334
5335 /*
5336 * First get all needed power domains, then put all unneeded, to avoid
5337 * any unnecessary toggling of the power wells.
5338 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005339 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005340 enum intel_display_power_domain domain;
5341
Matt Roper83d65732015-02-25 13:12:16 -08005342 if (!crtc->base.state->enable)
Imre Deak77d22dc2014-03-05 16:20:52 +02005343 continue;
5344
Imre Deak319be8a2014-03-04 19:22:57 +02005345 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
Imre Deak77d22dc2014-03-05 16:20:52 +02005346
5347 for_each_power_domain(domain, pipe_domains[crtc->pipe])
5348 intel_display_power_get(dev_priv, domain);
5349 }
5350
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005351 if (dev_priv->display.modeset_global_resources)
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005352 dev_priv->display.modeset_global_resources(state);
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005353
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005354 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005355 enum intel_display_power_domain domain;
5356
5357 for_each_power_domain(domain, crtc->enabled_power_domains)
5358 intel_display_power_put(dev_priv, domain);
5359
5360 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
5361 }
5362
5363 intel_display_set_init_power(dev_priv, false);
5364}
5365
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305366void broxton_set_cdclk(struct drm_device *dev, int frequency)
5367{
5368 struct drm_i915_private *dev_priv = dev->dev_private;
5369 uint32_t divider;
5370 uint32_t ratio;
5371 uint32_t current_freq;
5372 int ret;
5373
5374 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5375 switch (frequency) {
5376 case 144000:
5377 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5378 ratio = BXT_DE_PLL_RATIO(60);
5379 break;
5380 case 288000:
5381 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5382 ratio = BXT_DE_PLL_RATIO(60);
5383 break;
5384 case 384000:
5385 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5386 ratio = BXT_DE_PLL_RATIO(60);
5387 break;
5388 case 576000:
5389 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5390 ratio = BXT_DE_PLL_RATIO(60);
5391 break;
5392 case 624000:
5393 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5394 ratio = BXT_DE_PLL_RATIO(65);
5395 break;
5396 case 19200:
5397 /*
5398 * Bypass frequency with DE PLL disabled. Init ratio, divider
5399 * to suppress GCC warning.
5400 */
5401 ratio = 0;
5402 divider = 0;
5403 break;
5404 default:
5405 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5406
5407 return;
5408 }
5409
5410 mutex_lock(&dev_priv->rps.hw_lock);
5411 /* Inform power controller of upcoming frequency change */
5412 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5413 0x80000000);
5414 mutex_unlock(&dev_priv->rps.hw_lock);
5415
5416 if (ret) {
5417 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5418 ret, frequency);
5419 return;
5420 }
5421
5422 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5423 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5424 current_freq = current_freq * 500 + 1000;
5425
5426 /*
5427 * DE PLL has to be disabled when
5428 * - setting to 19.2MHz (bypass, PLL isn't used)
5429 * - before setting to 624MHz (PLL needs toggling)
5430 * - before setting to any frequency from 624MHz (PLL needs toggling)
5431 */
5432 if (frequency == 19200 || frequency == 624000 ||
5433 current_freq == 624000) {
5434 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5435 /* Timeout 200us */
5436 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5437 1))
5438 DRM_ERROR("timout waiting for DE PLL unlock\n");
5439 }
5440
5441 if (frequency != 19200) {
5442 uint32_t val;
5443
5444 val = I915_READ(BXT_DE_PLL_CTL);
5445 val &= ~BXT_DE_PLL_RATIO_MASK;
5446 val |= ratio;
5447 I915_WRITE(BXT_DE_PLL_CTL, val);
5448
5449 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5450 /* Timeout 200us */
5451 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5452 DRM_ERROR("timeout waiting for DE PLL lock\n");
5453
5454 val = I915_READ(CDCLK_CTL);
5455 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5456 val |= divider;
5457 /*
5458 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5459 * enable otherwise.
5460 */
5461 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5462 if (frequency >= 500000)
5463 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5464
5465 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5466 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5467 val |= (frequency - 1000) / 500;
5468 I915_WRITE(CDCLK_CTL, val);
5469 }
5470
5471 mutex_lock(&dev_priv->rps.hw_lock);
5472 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5473 DIV_ROUND_UP(frequency, 25000));
5474 mutex_unlock(&dev_priv->rps.hw_lock);
5475
5476 if (ret) {
5477 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5478 ret, frequency);
5479 return;
5480 }
5481
5482 dev_priv->cdclk_freq = frequency;
5483}
5484
5485void broxton_init_cdclk(struct drm_device *dev)
5486{
5487 struct drm_i915_private *dev_priv = dev->dev_private;
5488 uint32_t val;
5489
5490 /*
5491 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5492 * or else the reset will hang because there is no PCH to respond.
5493 * Move the handshake programming to initialization sequence.
5494 * Previously was left up to BIOS.
5495 */
5496 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5497 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5498 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5499
5500 /* Enable PG1 for cdclk */
5501 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5502
5503 /* check if cd clock is enabled */
5504 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5505 DRM_DEBUG_KMS("Display already initialized\n");
5506 return;
5507 }
5508
5509 /*
5510 * FIXME:
5511 * - The initial CDCLK needs to be read from VBT.
5512 * Need to make this change after VBT has changes for BXT.
5513 * - check if setting the max (or any) cdclk freq is really necessary
5514 * here, it belongs to modeset time
5515 */
5516 broxton_set_cdclk(dev, 624000);
5517
5518 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005519 POSTING_READ(DBUF_CTL);
5520
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305521 udelay(10);
5522
5523 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5524 DRM_ERROR("DBuf power enable timeout!\n");
5525}
5526
5527void broxton_uninit_cdclk(struct drm_device *dev)
5528{
5529 struct drm_i915_private *dev_priv = dev->dev_private;
5530
5531 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005532 POSTING_READ(DBUF_CTL);
5533
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305534 udelay(10);
5535
5536 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5537 DRM_ERROR("DBuf power disable timeout!\n");
5538
5539 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5540 broxton_set_cdclk(dev, 19200);
5541
5542 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5543}
5544
Ville Syrjälädfcab172014-06-13 13:37:47 +03005545/* returns HPLL frequency in kHz */
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005546static int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005547{
Jesse Barnes586f49d2013-11-04 16:06:59 -08005548 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08005549
Jesse Barnes586f49d2013-11-04 16:06:59 -08005550 /* Obtain SKU information */
5551 mutex_lock(&dev_priv->dpio_lock);
5552 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
5553 CCK_FUSE_HPLL_FREQ_MASK;
5554 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005555
Ville Syrjälädfcab172014-06-13 13:37:47 +03005556 return vco_freq[hpll_freq] * 1000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005557}
5558
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005559static void vlv_update_cdclk(struct drm_device *dev)
5560{
5561 struct drm_i915_private *dev_priv = dev->dev_private;
5562
Vandana Kannan164dfd22014-11-24 13:37:41 +05305563 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
Ville Syrjälä43dc52c2014-10-07 17:41:20 +03005564 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
Vandana Kannan164dfd22014-11-24 13:37:41 +05305565 dev_priv->cdclk_freq);
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005566
5567 /*
5568 * Program the gmbus_freq based on the cdclk frequency.
5569 * BSpec erroneously claims we should aim for 4MHz, but
5570 * in fact 1MHz is the correct frequency.
5571 */
Vandana Kannan164dfd22014-11-24 13:37:41 +05305572 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005573}
5574
Jesse Barnes30a970c2013-11-04 13:48:12 -08005575/* Adjust CDclk dividers to allow high res or save power if possible */
5576static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5577{
5578 struct drm_i915_private *dev_priv = dev->dev_private;
5579 u32 val, cmd;
5580
Vandana Kannan164dfd22014-11-24 13:37:41 +05305581 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5582 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005583
Ville Syrjälädfcab172014-06-13 13:37:47 +03005584 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005585 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005586 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005587 cmd = 1;
5588 else
5589 cmd = 0;
5590
5591 mutex_lock(&dev_priv->rps.hw_lock);
5592 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5593 val &= ~DSPFREQGUAR_MASK;
5594 val |= (cmd << DSPFREQGUAR_SHIFT);
5595 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5596 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5597 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5598 50)) {
5599 DRM_ERROR("timed out waiting for CDclk change\n");
5600 }
5601 mutex_unlock(&dev_priv->rps.hw_lock);
5602
Ville Syrjälädfcab172014-06-13 13:37:47 +03005603 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005604 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005605
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005606 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005607
5608 mutex_lock(&dev_priv->dpio_lock);
5609 /* adjust cdclk divider */
5610 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjälä9cf33db2014-06-13 13:37:48 +03005611 val &= ~DISPLAY_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005612 val |= divider;
5613 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005614
5615 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5616 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5617 50))
5618 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005619 mutex_unlock(&dev_priv->dpio_lock);
5620 }
5621
5622 mutex_lock(&dev_priv->dpio_lock);
5623 /* adjust self-refresh exit latency value */
5624 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5625 val &= ~0x7f;
5626
5627 /*
5628 * For high bandwidth configs, we set a higher latency in the bunit
5629 * so that the core display fetch happens in time to avoid underruns.
5630 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005631 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005632 val |= 4500 / 250; /* 4.5 usec */
5633 else
5634 val |= 3000 / 250; /* 3.0 usec */
5635 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
5636 mutex_unlock(&dev_priv->dpio_lock);
5637
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005638 vlv_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005639}
5640
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005641static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5642{
5643 struct drm_i915_private *dev_priv = dev->dev_private;
5644 u32 val, cmd;
5645
Vandana Kannan164dfd22014-11-24 13:37:41 +05305646 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5647 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005648
5649 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005650 case 333333:
5651 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005652 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005653 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005654 break;
5655 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005656 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005657 return;
5658 }
5659
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005660 /*
5661 * Specs are full of misinformation, but testing on actual
5662 * hardware has shown that we just need to write the desired
5663 * CCK divider into the Punit register.
5664 */
5665 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5666
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005667 mutex_lock(&dev_priv->rps.hw_lock);
5668 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5669 val &= ~DSPFREQGUAR_MASK_CHV;
5670 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5671 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5672 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5673 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5674 50)) {
5675 DRM_ERROR("timed out waiting for CDclk change\n");
5676 }
5677 mutex_unlock(&dev_priv->rps.hw_lock);
5678
5679 vlv_update_cdclk(dev);
5680}
5681
Jesse Barnes30a970c2013-11-04 13:48:12 -08005682static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5683 int max_pixclk)
5684{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005685 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005686 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005687
Jesse Barnes30a970c2013-11-04 13:48:12 -08005688 /*
5689 * Really only a few cases to deal with, as only 4 CDclks are supported:
5690 * 200MHz
5691 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005692 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005693 * 400MHz (VLV only)
5694 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5695 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005696 *
5697 * We seem to get an unstable or solid color picture at 200MHz.
5698 * Not sure what's wrong. For now use 200MHz only when all pipes
5699 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005700 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005701 if (!IS_CHERRYVIEW(dev_priv) &&
5702 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005703 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005704 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005705 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005706 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005707 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005708 else
5709 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005710}
5711
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305712static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5713 int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005714{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305715 /*
5716 * FIXME:
5717 * - remove the guardband, it's not needed on BXT
5718 * - set 19.2MHz bypass frequency if there are no active pipes
5719 */
5720 if (max_pixclk > 576000*9/10)
5721 return 624000;
5722 else if (max_pixclk > 384000*9/10)
5723 return 576000;
5724 else if (max_pixclk > 288000*9/10)
5725 return 384000;
5726 else if (max_pixclk > 144000*9/10)
5727 return 288000;
5728 else
5729 return 144000;
5730}
5731
Jesse Barnes30a970c2013-11-04 13:48:12 -08005732/* compute the max pixel clock for new configuration */
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005733static int intel_mode_max_pixclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005734{
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005735 struct drm_device *dev = state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005736 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005737 struct intel_crtc_state *crtc_state;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005738 int max_pixclk = 0;
5739
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005740 for_each_intel_crtc(dev, intel_crtc) {
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005741 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
5742 if (IS_ERR(crtc_state))
5743 return PTR_ERR(crtc_state);
5744
5745 if (!crtc_state->base.enable)
5746 continue;
5747
5748 max_pixclk = max(max_pixclk,
5749 crtc_state->base.adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005750 }
5751
5752 return max_pixclk;
5753}
5754
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005755static int valleyview_modeset_global_pipes(struct drm_atomic_state *state,
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005756 unsigned *prepare_pipes)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005757{
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005758 struct drm_i915_private *dev_priv = to_i915(state->dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005759 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005760 int max_pixclk = intel_mode_max_pixclk(state);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305761 int cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005762
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005763 if (max_pixclk < 0)
5764 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005765
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305766 if (IS_VALLEYVIEW(dev_priv))
5767 cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
5768 else
5769 cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
5770
5771 if (cdclk == dev_priv->cdclk_freq)
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005772 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005773
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005774 /* disable/enable all currently active pipes while we change cdclk */
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005775 for_each_intel_crtc(state->dev, intel_crtc)
Matt Roper83d65732015-02-25 13:12:16 -08005776 if (intel_crtc->base.state->enable)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005777 *prepare_pipes |= (1 << intel_crtc->pipe);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005778
5779 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005780}
5781
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005782static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5783{
5784 unsigned int credits, default_credits;
5785
5786 if (IS_CHERRYVIEW(dev_priv))
5787 default_credits = PFI_CREDIT(12);
5788 else
5789 default_credits = PFI_CREDIT(8);
5790
Vandana Kannan164dfd22014-11-24 13:37:41 +05305791 if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005792 /* CHV suggested value is 31 or 63 */
5793 if (IS_CHERRYVIEW(dev_priv))
5794 credits = PFI_CREDIT_31;
5795 else
5796 credits = PFI_CREDIT(15);
5797 } else {
5798 credits = default_credits;
5799 }
5800
5801 /*
5802 * WA - write default credits before re-programming
5803 * FIXME: should we also set the resend bit here?
5804 */
5805 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5806 default_credits);
5807
5808 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5809 credits | PFI_CREDIT_RESEND);
5810
5811 /*
5812 * FIXME is this guaranteed to clear
5813 * immediately or should we poll for it?
5814 */
5815 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
5816}
5817
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005818static void valleyview_modeset_global_resources(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005819{
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005820 struct drm_device *dev = state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005821 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005822 int max_pixclk = intel_mode_max_pixclk(state);
5823 int req_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005824
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005825 /* The only reason this can fail is if we fail to add the crtc_state
5826 * to the atomic state. But that can't happen since the call to
5827 * intel_mode_max_pixclk() in valleyview_modeset_global_pipes() (which
5828 * can't have failed otherwise the mode set would be aborted) added all
5829 * the states already. */
5830 if (WARN_ON(max_pixclk < 0))
5831 return;
5832
5833 req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005834
Vandana Kannan164dfd22014-11-24 13:37:41 +05305835 if (req_cdclk != dev_priv->cdclk_freq) {
Imre Deak738c05c2014-11-19 16:25:37 +02005836 /*
5837 * FIXME: We can end up here with all power domains off, yet
5838 * with a CDCLK frequency other than the minimum. To account
5839 * for this take the PIPE-A power domain, which covers the HW
5840 * blocks needed for the following programming. This can be
5841 * removed once it's guaranteed that we get here either with
5842 * the minimum CDCLK set, or the required power domains
5843 * enabled.
5844 */
5845 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
5846
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005847 if (IS_CHERRYVIEW(dev))
5848 cherryview_set_cdclk(dev, req_cdclk);
5849 else
5850 valleyview_set_cdclk(dev, req_cdclk);
Imre Deak738c05c2014-11-19 16:25:37 +02005851
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005852 vlv_program_pfi_credits(dev_priv);
5853
Imre Deak738c05c2014-11-19 16:25:37 +02005854 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005855 }
Jesse Barnes30a970c2013-11-04 13:48:12 -08005856}
5857
Jesse Barnes89b667f2013-04-18 14:51:36 -07005858static void valleyview_crtc_enable(struct drm_crtc *crtc)
5859{
5860 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02005861 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005862 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5863 struct intel_encoder *encoder;
5864 int pipe = intel_crtc->pipe;
Jani Nikula23538ef2013-08-27 15:12:22 +03005865 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005866
Matt Roper83d65732015-02-25 13:12:16 -08005867 WARN_ON(!crtc->state->enable);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005868
5869 if (intel_crtc->active)
5870 return;
5871
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005872 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
Shobhit Kumar8525a232014-06-25 12:20:39 +05305873
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005874 if (!is_dsi) {
5875 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005876 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005877 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005878 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005879 }
Daniel Vetter5b18e572014-04-24 23:55:06 +02005880
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005881 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05305882 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005883
5884 intel_set_pipe_timings(intel_crtc);
5885
Ville Syrjäläc14b0482014-10-16 20:52:34 +03005886 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
5887 struct drm_i915_private *dev_priv = dev->dev_private;
5888
5889 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
5890 I915_WRITE(CHV_CANVAS(pipe), 0);
5891 }
5892
Daniel Vetter5b18e572014-04-24 23:55:06 +02005893 i9xx_set_pipeconf(intel_crtc);
5894
Jesse Barnes89b667f2013-04-18 14:51:36 -07005895 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005896
Daniel Vettera72e4c92014-09-30 10:56:47 +02005897 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005898
Jesse Barnes89b667f2013-04-18 14:51:36 -07005899 for_each_encoder_on_crtc(dev, crtc, encoder)
5900 if (encoder->pre_pll_enable)
5901 encoder->pre_pll_enable(encoder);
5902
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005903 if (!is_dsi) {
5904 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005905 chv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005906 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005907 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005908 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07005909
5910 for_each_encoder_on_crtc(dev, crtc, encoder)
5911 if (encoder->pre_enable)
5912 encoder->pre_enable(encoder);
5913
Jesse Barnes2dd24552013-04-25 12:55:01 -07005914 i9xx_pfit_enable(intel_crtc);
5915
Ville Syrjälä63cbb072013-06-04 13:48:59 +03005916 intel_crtc_load_lut(crtc);
5917
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005918 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005919 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02005920
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005921 assert_vblank_disabled(crtc);
5922 drm_crtc_vblank_on(crtc);
5923
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005924 for_each_encoder_on_crtc(dev, crtc, encoder)
5925 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005926}
5927
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005928static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
5929{
5930 struct drm_device *dev = crtc->base.dev;
5931 struct drm_i915_private *dev_priv = dev->dev_private;
5932
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005933 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
5934 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005935}
5936
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005937static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005938{
5939 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02005940 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08005941 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005942 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08005943 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08005944
Matt Roper83d65732015-02-25 13:12:16 -08005945 WARN_ON(!crtc->state->enable);
Daniel Vetter08a48462012-07-02 11:43:47 +02005946
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005947 if (intel_crtc->active)
5948 return;
5949
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005950 i9xx_set_pll_dividers(intel_crtc);
5951
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005952 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05305953 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005954
5955 intel_set_pipe_timings(intel_crtc);
5956
Daniel Vetter5b18e572014-04-24 23:55:06 +02005957 i9xx_set_pipeconf(intel_crtc);
5958
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005959 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01005960
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005961 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005962 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005963
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02005964 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02005965 if (encoder->pre_enable)
5966 encoder->pre_enable(encoder);
5967
Daniel Vetterf6736a12013-06-05 13:34:30 +02005968 i9xx_enable_pll(intel_crtc);
5969
Jesse Barnes2dd24552013-04-25 12:55:01 -07005970 i9xx_pfit_enable(intel_crtc);
5971
Ville Syrjälä63cbb072013-06-04 13:48:59 +03005972 intel_crtc_load_lut(crtc);
5973
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005974 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005975 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02005976
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005977 assert_vblank_disabled(crtc);
5978 drm_crtc_vblank_on(crtc);
5979
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005980 for_each_encoder_on_crtc(dev, crtc, encoder)
5981 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005982}
5983
Daniel Vetter87476d62013-04-11 16:29:06 +02005984static void i9xx_pfit_disable(struct intel_crtc *crtc)
5985{
5986 struct drm_device *dev = crtc->base.dev;
5987 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02005988
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005989 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02005990 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02005991
5992 assert_pipe_disabled(dev_priv, crtc->pipe);
5993
Daniel Vetter328d8e82013-05-08 10:36:31 +02005994 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
5995 I915_READ(PFIT_CONTROL));
5996 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02005997}
5998
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005999static void i9xx_crtc_disable(struct drm_crtc *crtc)
6000{
6001 struct drm_device *dev = crtc->dev;
6002 struct drm_i915_private *dev_priv = dev->dev_private;
6003 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006004 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006005 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006006
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006007 if (!intel_crtc->active)
6008 return;
6009
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006010 /*
6011 * On gen2 planes are double buffered but the pipe isn't, so we must
6012 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03006013 * We also need to wait on all gmch platforms because of the
6014 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006015 */
Imre Deak564ed192014-06-13 14:54:21 +03006016 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006017
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006018 for_each_encoder_on_crtc(dev, crtc, encoder)
6019 encoder->disable(encoder);
6020
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006021 drm_crtc_vblank_off(crtc);
6022 assert_vblank_disabled(crtc);
6023
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006024 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006025
Daniel Vetter87476d62013-04-11 16:29:06 +02006026 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006027
Jesse Barnes89b667f2013-04-18 14:51:36 -07006028 for_each_encoder_on_crtc(dev, crtc, encoder)
6029 if (encoder->post_disable)
6030 encoder->post_disable(encoder);
6031
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006032 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006033 if (IS_CHERRYVIEW(dev))
6034 chv_disable_pll(dev_priv, pipe);
6035 else if (IS_VALLEYVIEW(dev))
6036 vlv_disable_pll(dev_priv, pipe);
6037 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006038 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006039 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006040
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006041 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006042 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006043
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006044 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03006045 intel_update_watermarks(crtc);
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006046
Daniel Vetterefa96242014-04-24 23:55:02 +02006047 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02006048 intel_fbc_update(dev);
Daniel Vetterefa96242014-04-24 23:55:02 +02006049 mutex_unlock(&dev->struct_mutex);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006050}
6051
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006052static void i9xx_crtc_off(struct drm_crtc *crtc)
6053{
6054}
6055
Borun Fub04c5bd2014-07-12 10:02:27 +05306056/* Master function to enable/disable CRTC and corresponding power wells */
6057void intel_crtc_control(struct drm_crtc *crtc, bool enable)
Chris Wilsoncdd59982010-09-08 16:30:16 +01006058{
Chris Wilsoncdd59982010-09-08 16:30:16 +01006059 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006060 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006061 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006062 enum intel_display_power_domain domain;
6063 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006064
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006065 if (enable) {
6066 if (!intel_crtc->active) {
Daniel Vettere1e9fb82014-06-25 22:02:04 +03006067 domains = get_crtc_power_domains(crtc);
6068 for_each_power_domain(domain, domains)
6069 intel_display_power_get(dev_priv, domain);
6070 intel_crtc->enabled_power_domains = domains;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006071
6072 dev_priv->display.crtc_enable(crtc);
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03006073 intel_crtc_enable_planes(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006074 }
6075 } else {
6076 if (intel_crtc->active) {
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03006077 intel_crtc_disable_planes(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006078 dev_priv->display.crtc_disable(crtc);
6079
Daniel Vettere1e9fb82014-06-25 22:02:04 +03006080 domains = intel_crtc->enabled_power_domains;
6081 for_each_power_domain(domain, domains)
6082 intel_display_power_put(dev_priv, domain);
6083 intel_crtc->enabled_power_domains = 0;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006084 }
6085 }
Borun Fub04c5bd2014-07-12 10:02:27 +05306086}
6087
6088/**
6089 * Sets the power management mode of the pipe and plane.
6090 */
6091void intel_crtc_update_dpms(struct drm_crtc *crtc)
6092{
6093 struct drm_device *dev = crtc->dev;
6094 struct intel_encoder *intel_encoder;
6095 bool enable = false;
6096
6097 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
6098 enable |= intel_encoder->connectors_active;
6099
6100 intel_crtc_control(crtc, enable);
Daniel Vetter976f8a22012-07-08 22:34:21 +02006101}
6102
Daniel Vetter976f8a22012-07-08 22:34:21 +02006103static void intel_crtc_disable(struct drm_crtc *crtc)
6104{
6105 struct drm_device *dev = crtc->dev;
6106 struct drm_connector *connector;
6107 struct drm_i915_private *dev_priv = dev->dev_private;
6108
6109 /* crtc should still be enabled when we disable it. */
Matt Roper83d65732015-02-25 13:12:16 -08006110 WARN_ON(!crtc->state->enable);
Daniel Vetter976f8a22012-07-08 22:34:21 +02006111
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03006112 intel_crtc_disable_planes(crtc);
Daniel Vetter976f8a22012-07-08 22:34:21 +02006113 dev_priv->display.crtc_disable(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006114 dev_priv->display.off(crtc);
6115
Matt Roper70a101f2015-04-08 18:56:53 -07006116 drm_plane_helper_disable(crtc->primary);
Daniel Vetter976f8a22012-07-08 22:34:21 +02006117
6118 /* Update computed state. */
6119 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
6120 if (!connector->encoder || !connector->encoder->crtc)
6121 continue;
6122
6123 if (connector->encoder->crtc != crtc)
6124 continue;
6125
6126 connector->dpms = DRM_MODE_DPMS_OFF;
6127 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01006128 }
6129}
6130
Chris Wilsonea5b2132010-08-04 13:50:23 +01006131void intel_encoder_destroy(struct drm_encoder *encoder)
6132{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006133 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006134
Chris Wilsonea5b2132010-08-04 13:50:23 +01006135 drm_encoder_cleanup(encoder);
6136 kfree(intel_encoder);
6137}
6138
Damien Lespiau92373292013-08-08 22:28:57 +01006139/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006140 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
6141 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01006142static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006143{
6144 if (mode == DRM_MODE_DPMS_ON) {
6145 encoder->connectors_active = true;
6146
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006147 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006148 } else {
6149 encoder->connectors_active = false;
6150
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006151 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006152 }
6153}
6154
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006155/* Cross check the actual hw state with our own modeset state tracking (and it's
6156 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02006157static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006158{
6159 if (connector->get_hw_state(connector)) {
6160 struct intel_encoder *encoder = connector->encoder;
6161 struct drm_crtc *crtc;
6162 bool encoder_enabled;
6163 enum pipe pipe;
6164
6165 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6166 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03006167 connector->base.name);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006168
Dave Airlie0e32b392014-05-02 14:02:48 +10006169 /* there is no real hw state for MST connectors */
6170 if (connector->mst_port)
6171 return;
6172
Rob Clarke2c719b2014-12-15 13:56:32 -05006173 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006174 "wrong connector dpms state\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006175 I915_STATE_WARN(connector->base.encoder != &encoder->base,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006176 "active connector not linked to encoder\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006177
Dave Airlie36cd7442014-05-02 13:44:18 +10006178 if (encoder) {
Rob Clarke2c719b2014-12-15 13:56:32 -05006179 I915_STATE_WARN(!encoder->connectors_active,
Dave Airlie36cd7442014-05-02 13:44:18 +10006180 "encoder->connectors_active not set\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006181
Dave Airlie36cd7442014-05-02 13:44:18 +10006182 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -05006183 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
6184 if (I915_STATE_WARN_ON(!encoder->base.crtc))
Dave Airlie36cd7442014-05-02 13:44:18 +10006185 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006186
Dave Airlie36cd7442014-05-02 13:44:18 +10006187 crtc = encoder->base.crtc;
6188
Matt Roper83d65732015-02-25 13:12:16 -08006189 I915_STATE_WARN(!crtc->state->enable,
6190 "crtc not enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006191 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
6192 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
Dave Airlie36cd7442014-05-02 13:44:18 +10006193 "encoder active on the wrong pipe\n");
6194 }
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006195 }
6196}
6197
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006198int intel_connector_init(struct intel_connector *connector)
6199{
6200 struct drm_connector_state *connector_state;
6201
6202 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6203 if (!connector_state)
6204 return -ENOMEM;
6205
6206 connector->base.state = connector_state;
6207 return 0;
6208}
6209
6210struct intel_connector *intel_connector_alloc(void)
6211{
6212 struct intel_connector *connector;
6213
6214 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6215 if (!connector)
6216 return NULL;
6217
6218 if (intel_connector_init(connector) < 0) {
6219 kfree(connector);
6220 return NULL;
6221 }
6222
6223 return connector;
6224}
6225
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006226/* Even simpler default implementation, if there's really no special case to
6227 * consider. */
6228void intel_connector_dpms(struct drm_connector *connector, int mode)
6229{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006230 /* All the simple cases only support two dpms states. */
6231 if (mode != DRM_MODE_DPMS_ON)
6232 mode = DRM_MODE_DPMS_OFF;
6233
6234 if (mode == connector->dpms)
6235 return;
6236
6237 connector->dpms = mode;
6238
6239 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dcf2013-09-29 19:15:07 +01006240 if (connector->encoder)
6241 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006242
Daniel Vetterb9805142012-08-31 17:37:33 +02006243 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006244}
6245
Daniel Vetterf0947c32012-07-02 13:10:34 +02006246/* Simple connector->get_hw_state implementation for encoders that support only
6247 * one connector and no cloning and hence the encoder state determines the state
6248 * of the connector. */
6249bool intel_connector_get_hw_state(struct intel_connector *connector)
6250{
Daniel Vetter24929352012-07-02 20:28:59 +02006251 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006252 struct intel_encoder *encoder = connector->encoder;
6253
6254 return encoder->get_hw_state(encoder, &pipe);
6255}
6256
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006257static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006258{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006259 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6260 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006261
6262 return 0;
6263}
6264
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006265static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006266 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006267{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006268 struct drm_atomic_state *state = pipe_config->base.state;
6269 struct intel_crtc *other_crtc;
6270 struct intel_crtc_state *other_crtc_state;
6271
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006272 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6273 pipe_name(pipe), pipe_config->fdi_lanes);
6274 if (pipe_config->fdi_lanes > 4) {
6275 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6276 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006277 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006278 }
6279
Paulo Zanonibafb6552013-11-02 21:07:44 -07006280 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006281 if (pipe_config->fdi_lanes > 2) {
6282 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6283 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006284 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006285 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006286 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006287 }
6288 }
6289
6290 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006291 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006292
6293 /* Ivybridge 3 pipe is really complicated */
6294 switch (pipe) {
6295 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006296 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006297 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006298 if (pipe_config->fdi_lanes <= 2)
6299 return 0;
6300
6301 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6302 other_crtc_state =
6303 intel_atomic_get_crtc_state(state, other_crtc);
6304 if (IS_ERR(other_crtc_state))
6305 return PTR_ERR(other_crtc_state);
6306
6307 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006308 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6309 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006310 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006311 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006312 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006313 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006314 if (pipe_config->fdi_lanes > 2) {
6315 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6316 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006317 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006318 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006319
6320 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6321 other_crtc_state =
6322 intel_atomic_get_crtc_state(state, other_crtc);
6323 if (IS_ERR(other_crtc_state))
6324 return PTR_ERR(other_crtc_state);
6325
6326 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006327 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006328 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006329 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006330 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006331 default:
6332 BUG();
6333 }
6334}
6335
Daniel Vettere29c22c2013-02-21 00:00:16 +01006336#define RETRY 1
6337static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006338 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006339{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006340 struct drm_device *dev = intel_crtc->base.dev;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006341 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006342 int lane, link_bw, fdi_dotclock, ret;
6343 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006344
Daniel Vettere29c22c2013-02-21 00:00:16 +01006345retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006346 /* FDI is a binary signal running at ~2.7GHz, encoding
6347 * each output octet as 10 bits. The actual frequency
6348 * is stored as a divider into a 100MHz clock, and the
6349 * mode pixel clock is stored in units of 1KHz.
6350 * Hence the bw of each lane in terms of the mode signal
6351 * is:
6352 */
6353 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6354
Damien Lespiau241bfc32013-09-25 16:45:37 +01006355 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006356
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006357 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006358 pipe_config->pipe_bpp);
6359
6360 pipe_config->fdi_lanes = lane;
6361
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006362 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006363 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006364
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006365 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6366 intel_crtc->pipe, pipe_config);
6367 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006368 pipe_config->pipe_bpp -= 2*3;
6369 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6370 pipe_config->pipe_bpp);
6371 needs_recompute = true;
6372 pipe_config->bw_constrained = true;
6373
6374 goto retry;
6375 }
6376
6377 if (needs_recompute)
6378 return RETRY;
6379
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006380 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006381}
6382
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006383static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006384 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006385{
Jani Nikulad330a952014-01-21 11:24:25 +02006386 pipe_config->ips_enabled = i915.enable_ips &&
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03006387 hsw_crtc_supports_ips(crtc) &&
Jesse Barnesb6dfdc92013-07-25 10:06:50 -07006388 pipe_config->pipe_bpp <= 24;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006389}
6390
Daniel Vettera43f6e02013-06-07 23:10:32 +02006391static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006392 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006393{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006394 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006395 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006396 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chandra Kondurud03c93d2015-04-09 16:42:46 -07006397 int ret;
Chris Wilson89749352010-09-12 18:25:19 +01006398
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006399 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006400 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006401 int clock_limit =
6402 dev_priv->display.get_display_clock_speed(dev);
6403
6404 /*
6405 * Enable pixel doubling when the dot clock
6406 * is > 90% of the (display) core speed.
6407 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03006408 * GDG double wide on either pipe,
6409 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006410 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03006411 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01006412 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006413 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006414 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006415 }
6416
Damien Lespiau241bfc32013-09-25 16:45:37 +01006417 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006418 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006419 }
Chris Wilson89749352010-09-12 18:25:19 +01006420
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006421 /*
6422 * Pipe horizontal size must be even in:
6423 * - DVO ganged mode
6424 * - LVDS dual channel mode
6425 * - Double wide pipe
6426 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006427 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006428 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6429 pipe_config->pipe_src_w &= ~1;
6430
Damien Lespiau8693a822013-05-03 18:48:11 +01006431 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6432 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006433 */
6434 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
6435 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006436 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006437
Damien Lespiauf5adf942013-06-24 18:29:34 +01006438 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006439 hsw_compute_ips_config(crtc, pipe_config);
6440
Daniel Vetter877d48d2013-04-19 11:24:43 +02006441 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006442 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006443
Chandra Kondurud03c93d2015-04-09 16:42:46 -07006444 /* FIXME: remove below call once atomic mode set is place and all crtc
6445 * related checks called from atomic_crtc_check function */
6446 ret = 0;
6447 DRM_DEBUG_KMS("intel_crtc = %p drm_state (pipe_config->base.state) = %p\n",
6448 crtc, pipe_config->base.state);
6449 ret = intel_atomic_setup_scalers(dev, crtc, pipe_config);
6450
6451 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006452}
6453
Ville Syrjälä1652d192015-03-31 14:12:01 +03006454static int skylake_get_display_clock_speed(struct drm_device *dev)
6455{
6456 struct drm_i915_private *dev_priv = to_i915(dev);
6457 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6458 uint32_t cdctl = I915_READ(CDCLK_CTL);
6459 uint32_t linkrate;
6460
6461 if (!(lcpll1 & LCPLL_PLL_ENABLE)) {
6462 WARN(1, "LCPLL1 not enabled\n");
6463 return 24000; /* 24MHz is the cd freq with NSSC ref */
6464 }
6465
6466 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6467 return 540000;
6468
6469 linkrate = (I915_READ(DPLL_CTRL1) &
Damien Lespiau71cd8422015-04-30 16:39:17 +01006470 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006471
Damien Lespiau71cd8422015-04-30 16:39:17 +01006472 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6473 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006474 /* vco 8640 */
6475 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6476 case CDCLK_FREQ_450_432:
6477 return 432000;
6478 case CDCLK_FREQ_337_308:
6479 return 308570;
6480 case CDCLK_FREQ_675_617:
6481 return 617140;
6482 default:
6483 WARN(1, "Unknown cd freq selection\n");
6484 }
6485 } else {
6486 /* vco 8100 */
6487 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6488 case CDCLK_FREQ_450_432:
6489 return 450000;
6490 case CDCLK_FREQ_337_308:
6491 return 337500;
6492 case CDCLK_FREQ_675_617:
6493 return 675000;
6494 default:
6495 WARN(1, "Unknown cd freq selection\n");
6496 }
6497 }
6498
6499 /* error case, do as if DPLL0 isn't enabled */
6500 return 24000;
6501}
6502
6503static int broadwell_get_display_clock_speed(struct drm_device *dev)
6504{
6505 struct drm_i915_private *dev_priv = dev->dev_private;
6506 uint32_t lcpll = I915_READ(LCPLL_CTL);
6507 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6508
6509 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6510 return 800000;
6511 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6512 return 450000;
6513 else if (freq == LCPLL_CLK_FREQ_450)
6514 return 450000;
6515 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6516 return 540000;
6517 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6518 return 337500;
6519 else
6520 return 675000;
6521}
6522
6523static int haswell_get_display_clock_speed(struct drm_device *dev)
6524{
6525 struct drm_i915_private *dev_priv = dev->dev_private;
6526 uint32_t lcpll = I915_READ(LCPLL_CTL);
6527 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6528
6529 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6530 return 800000;
6531 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6532 return 450000;
6533 else if (freq == LCPLL_CLK_FREQ_450)
6534 return 450000;
6535 else if (IS_HSW_ULT(dev))
6536 return 337500;
6537 else
6538 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006539}
6540
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006541static int valleyview_get_display_clock_speed(struct drm_device *dev)
6542{
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006543 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006544 u32 val;
6545 int divider;
6546
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006547 if (dev_priv->hpll_freq == 0)
6548 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
6549
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006550 mutex_lock(&dev_priv->dpio_lock);
6551 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
6552 mutex_unlock(&dev_priv->dpio_lock);
6553
6554 divider = val & DISPLAY_FREQUENCY_VALUES;
6555
Ville Syrjälä7d007f42014-06-13 13:37:53 +03006556 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
6557 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
6558 "cdclk change in progress\n");
6559
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006560 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006561}
6562
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006563static int ilk_get_display_clock_speed(struct drm_device *dev)
6564{
6565 return 450000;
6566}
6567
Jesse Barnese70236a2009-09-21 10:42:27 -07006568static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006569{
Jesse Barnese70236a2009-09-21 10:42:27 -07006570 return 400000;
6571}
Jesse Barnes79e53942008-11-07 14:24:08 -08006572
Jesse Barnese70236a2009-09-21 10:42:27 -07006573static int i915_get_display_clock_speed(struct drm_device *dev)
6574{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006575 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006576}
Jesse Barnes79e53942008-11-07 14:24:08 -08006577
Jesse Barnese70236a2009-09-21 10:42:27 -07006578static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6579{
6580 return 200000;
6581}
Jesse Barnes79e53942008-11-07 14:24:08 -08006582
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006583static int pnv_get_display_clock_speed(struct drm_device *dev)
6584{
6585 u16 gcfgc = 0;
6586
6587 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6588
6589 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6590 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006591 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006592 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006593 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006594 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006595 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006596 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6597 return 200000;
6598 default:
6599 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6600 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006601 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006602 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006603 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006604 }
6605}
6606
Jesse Barnese70236a2009-09-21 10:42:27 -07006607static int i915gm_get_display_clock_speed(struct drm_device *dev)
6608{
6609 u16 gcfgc = 0;
6610
6611 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6612
6613 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006614 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006615 else {
6616 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6617 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006618 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006619 default:
6620 case GC_DISPLAY_CLOCK_190_200_MHZ:
6621 return 190000;
6622 }
6623 }
6624}
Jesse Barnes79e53942008-11-07 14:24:08 -08006625
Jesse Barnese70236a2009-09-21 10:42:27 -07006626static int i865_get_display_clock_speed(struct drm_device *dev)
6627{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006628 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006629}
6630
6631static int i855_get_display_clock_speed(struct drm_device *dev)
6632{
6633 u16 hpllcc = 0;
6634 /* Assume that the hardware is in the high speed state. This
6635 * should be the default.
6636 */
6637 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6638 case GC_CLOCK_133_200:
6639 case GC_CLOCK_100_200:
6640 return 200000;
6641 case GC_CLOCK_166_250:
6642 return 250000;
6643 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006644 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006645 }
6646
6647 /* Shouldn't happen */
6648 return 0;
6649}
6650
6651static int i830_get_display_clock_speed(struct drm_device *dev)
6652{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006653 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006654}
6655
Zhenyu Wang2c072452009-06-05 15:38:42 +08006656static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006657intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006658{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006659 while (*num > DATA_LINK_M_N_MASK ||
6660 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08006661 *num >>= 1;
6662 *den >>= 1;
6663 }
6664}
6665
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006666static void compute_m_n(unsigned int m, unsigned int n,
6667 uint32_t *ret_m, uint32_t *ret_n)
6668{
6669 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
6670 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6671 intel_reduce_m_n_ratio(ret_m, ret_n);
6672}
6673
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006674void
6675intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6676 int pixel_clock, int link_clock,
6677 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006678{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006679 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006680
6681 compute_m_n(bits_per_pixel * pixel_clock,
6682 link_clock * nlanes * 8,
6683 &m_n->gmch_m, &m_n->gmch_n);
6684
6685 compute_m_n(pixel_clock, link_clock,
6686 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08006687}
6688
Chris Wilsona7615032011-01-12 17:04:08 +00006689static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
6690{
Jani Nikulad330a952014-01-21 11:24:25 +02006691 if (i915.panel_use_ssc >= 0)
6692 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006693 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07006694 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00006695}
6696
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006697static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
6698 int num_connectors)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006699{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006700 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006701 struct drm_i915_private *dev_priv = dev->dev_private;
6702 int refclk;
6703
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006704 WARN_ON(!crtc_state->base.state);
6705
Imre Deak5ab7b0b2015-03-06 03:29:25 +02006706 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02006707 refclk = 100000;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006708 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006709 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02006710 refclk = dev_priv->vbt.lvds_ssc_freq;
6711 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006712 } else if (!IS_GEN2(dev)) {
6713 refclk = 96000;
6714 } else {
6715 refclk = 48000;
6716 }
6717
6718 return refclk;
6719}
6720
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006721static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006722{
Daniel Vetter7df00d72013-05-21 21:54:55 +02006723 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006724}
Daniel Vetterf47709a2013-03-28 10:42:02 +01006725
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006726static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
6727{
6728 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006729}
6730
Daniel Vetterf47709a2013-03-28 10:42:02 +01006731static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006732 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08006733 intel_clock_t *reduced_clock)
6734{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006735 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006736 u32 fp, fp2 = 0;
6737
6738 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006739 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006740 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006741 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006742 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006743 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006744 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006745 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006746 }
6747
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006748 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006749
Daniel Vetterf47709a2013-03-28 10:42:02 +01006750 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006751 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07006752 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006753 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01006754 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006755 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006756 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006757 }
6758}
6759
Chon Ming Lee5e69f972013-09-05 20:41:49 +08006760static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
6761 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07006762{
6763 u32 reg_val;
6764
6765 /*
6766 * PLLB opamp always calibrates to max value of 0x3f, force enable it
6767 * and set it to a reasonable value instead.
6768 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006769 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006770 reg_val &= 0xffffff00;
6771 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006772 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006773
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006774 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006775 reg_val &= 0x8cffffff;
6776 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006777 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006778
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006779 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006780 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006781 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006782
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006783 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006784 reg_val &= 0x00ffffff;
6785 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006786 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006787}
6788
Daniel Vetterb5518422013-05-03 11:49:48 +02006789static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
6790 struct intel_link_m_n *m_n)
6791{
6792 struct drm_device *dev = crtc->base.dev;
6793 struct drm_i915_private *dev_priv = dev->dev_private;
6794 int pipe = crtc->pipe;
6795
Daniel Vettere3b95f12013-05-03 11:49:49 +02006796 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6797 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
6798 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
6799 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02006800}
6801
6802static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07006803 struct intel_link_m_n *m_n,
6804 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02006805{
6806 struct drm_device *dev = crtc->base.dev;
6807 struct drm_i915_private *dev_priv = dev->dev_private;
6808 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006809 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02006810
6811 if (INTEL_INFO(dev)->gen >= 5) {
6812 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
6813 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
6814 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
6815 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07006816 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
6817 * for gen < 8) and if DRRS is supported (to make sure the
6818 * registers are not unnecessarily accessed).
6819 */
Durgadoss R44395bf2015-02-13 15:33:02 +05306820 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006821 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07006822 I915_WRITE(PIPE_DATA_M2(transcoder),
6823 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
6824 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
6825 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
6826 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
6827 }
Daniel Vetterb5518422013-05-03 11:49:48 +02006828 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02006829 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6830 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
6831 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
6832 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02006833 }
6834}
6835
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306836void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006837{
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306838 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
6839
6840 if (m_n == M1_N1) {
6841 dp_m_n = &crtc->config->dp_m_n;
6842 dp_m2_n2 = &crtc->config->dp_m2_n2;
6843 } else if (m_n == M2_N2) {
6844
6845 /*
6846 * M2_N2 registers are not supported. Hence m2_n2 divider value
6847 * needs to be programmed into M1_N1.
6848 */
6849 dp_m_n = &crtc->config->dp_m2_n2;
6850 } else {
6851 DRM_ERROR("Unsupported divider value\n");
6852 return;
6853 }
6854
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006855 if (crtc->config->has_pch_encoder)
6856 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006857 else
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306858 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006859}
6860
Ville Syrjäläd288f652014-10-28 13:20:22 +02006861static void vlv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006862 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006863{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006864 u32 dpll, dpll_md;
6865
6866 /*
6867 * Enable DPIO clock input. We should never disable the reference
6868 * clock for pipe B, since VGA hotplug / manual detection depends
6869 * on it.
6870 */
6871 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
6872 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
6873 /* We should never disable this, set it here for state tracking */
6874 if (crtc->pipe == PIPE_B)
6875 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
6876 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02006877 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006878
Ville Syrjäläd288f652014-10-28 13:20:22 +02006879 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006880 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02006881 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006882}
6883
Ville Syrjäläd288f652014-10-28 13:20:22 +02006884static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006885 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006886{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006887 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006888 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01006889 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006890 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006891 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006892 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006893
Daniel Vetter09153002012-12-12 14:06:44 +01006894 mutex_lock(&dev_priv->dpio_lock);
6895
Ville Syrjäläd288f652014-10-28 13:20:22 +02006896 bestn = pipe_config->dpll.n;
6897 bestm1 = pipe_config->dpll.m1;
6898 bestm2 = pipe_config->dpll.m2;
6899 bestp1 = pipe_config->dpll.p1;
6900 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006901
Jesse Barnes89b667f2013-04-18 14:51:36 -07006902 /* See eDP HDMI DPIO driver vbios notes doc */
6903
6904 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006905 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08006906 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006907
6908 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006909 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006910
6911 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006912 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006913 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006914 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006915
6916 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006917 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006918
6919 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006920 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
6921 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
6922 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006923 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07006924
6925 /*
6926 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
6927 * but we don't support that).
6928 * Note: don't use the DAC post divider as it seems unstable.
6929 */
6930 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006931 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006932
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006933 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006934 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006935
Jesse Barnes89b667f2013-04-18 14:51:36 -07006936 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02006937 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006938 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
6939 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006940 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03006941 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006942 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006943 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006944 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006945
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02006946 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07006947 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006948 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006949 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006950 0x0df40000);
6951 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006952 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006953 0x0df70000);
6954 } else { /* HDMI or VGA */
6955 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006956 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006957 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006958 0x0df70000);
6959 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006960 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006961 0x0df40000);
6962 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006963
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006964 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006965 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006966 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
6967 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006968 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006969 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006970
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006971 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Daniel Vetter09153002012-12-12 14:06:44 +01006972 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006973}
6974
Ville Syrjäläd288f652014-10-28 13:20:22 +02006975static void chv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006976 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006977{
Ville Syrjäläd288f652014-10-28 13:20:22 +02006978 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006979 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
6980 DPLL_VCO_ENABLE;
6981 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02006982 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006983
Ville Syrjäläd288f652014-10-28 13:20:22 +02006984 pipe_config->dpll_hw_state.dpll_md =
6985 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006986}
6987
Ville Syrjäläd288f652014-10-28 13:20:22 +02006988static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006989 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006990{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006991 struct drm_device *dev = crtc->base.dev;
6992 struct drm_i915_private *dev_priv = dev->dev_private;
6993 int pipe = crtc->pipe;
6994 int dpll_reg = DPLL(crtc->pipe);
6995 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306996 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006997 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05306998 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306999 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007000
Ville Syrjäläd288f652014-10-28 13:20:22 +02007001 bestn = pipe_config->dpll.n;
7002 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7003 bestm1 = pipe_config->dpll.m1;
7004 bestm2 = pipe_config->dpll.m2 >> 22;
7005 bestp1 = pipe_config->dpll.p1;
7006 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307007 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307008 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307009 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007010
7011 /*
7012 * Enable Refclk and SSC
7013 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007014 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007015 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007016
7017 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007018
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007019 /* p1 and p2 divider */
7020 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7021 5 << DPIO_CHV_S1_DIV_SHIFT |
7022 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7023 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7024 1 << DPIO_CHV_K_DIV_SHIFT);
7025
7026 /* Feedback post-divider - m2 */
7027 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7028
7029 /* Feedback refclk divider - n and m1 */
7030 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7031 DPIO_CHV_M1_DIV_BY_2 |
7032 1 << DPIO_CHV_N_DIV_SHIFT);
7033
7034 /* M2 fraction division */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307035 if (bestm2_frac)
7036 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007037
7038 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307039 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7040 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7041 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7042 if (bestm2_frac)
7043 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7044 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007045
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307046 /* Program digital lock detect threshold */
7047 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7048 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7049 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7050 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7051 if (!bestm2_frac)
7052 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7053 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7054
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007055 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307056 if (vco == 5400000) {
7057 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7058 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7059 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7060 tribuf_calcntr = 0x9;
7061 } else if (vco <= 6200000) {
7062 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7063 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7064 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7065 tribuf_calcntr = 0x9;
7066 } else if (vco <= 6480000) {
7067 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7068 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7069 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7070 tribuf_calcntr = 0x8;
7071 } else {
7072 /* Not supported. Apply the same limits as in the max case */
7073 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7074 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7075 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7076 tribuf_calcntr = 0;
7077 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007078 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7079
Ville Syrjälä968040b2015-03-11 22:52:08 +02007080 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307081 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7082 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7083 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7084
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007085 /* AFC Recal */
7086 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7087 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7088 DPIO_AFC_RECAL);
7089
7090 mutex_unlock(&dev_priv->dpio_lock);
7091}
7092
Ville Syrjäläd288f652014-10-28 13:20:22 +02007093/**
7094 * vlv_force_pll_on - forcibly enable just the PLL
7095 * @dev_priv: i915 private structure
7096 * @pipe: pipe PLL to enable
7097 * @dpll: PLL configuration
7098 *
7099 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7100 * in cases where we need the PLL enabled even when @pipe is not going to
7101 * be enabled.
7102 */
7103void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7104 const struct dpll *dpll)
7105{
7106 struct intel_crtc *crtc =
7107 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007108 struct intel_crtc_state pipe_config = {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007109 .base.crtc = &crtc->base,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007110 .pixel_multiplier = 1,
7111 .dpll = *dpll,
7112 };
7113
7114 if (IS_CHERRYVIEW(dev)) {
7115 chv_update_pll(crtc, &pipe_config);
7116 chv_prepare_pll(crtc, &pipe_config);
7117 chv_enable_pll(crtc, &pipe_config);
7118 } else {
7119 vlv_update_pll(crtc, &pipe_config);
7120 vlv_prepare_pll(crtc, &pipe_config);
7121 vlv_enable_pll(crtc, &pipe_config);
7122 }
7123}
7124
7125/**
7126 * vlv_force_pll_off - forcibly disable just the PLL
7127 * @dev_priv: i915 private structure
7128 * @pipe: pipe PLL to disable
7129 *
7130 * Disable the PLL for @pipe. To be used in cases where we need
7131 * the PLL enabled even when @pipe is not going to be enabled.
7132 */
7133void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7134{
7135 if (IS_CHERRYVIEW(dev))
7136 chv_disable_pll(to_i915(dev), pipe);
7137 else
7138 vlv_disable_pll(to_i915(dev), pipe);
7139}
7140
Daniel Vetterf47709a2013-03-28 10:42:02 +01007141static void i9xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007142 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01007143 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007144 int num_connectors)
7145{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007146 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007147 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007148 u32 dpll;
7149 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007150 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007151
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007152 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307153
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007154 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7155 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007156
7157 dpll = DPLL_VGA_MODE_DIS;
7158
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007159 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007160 dpll |= DPLLB_MODE_LVDS;
7161 else
7162 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007163
Daniel Vetteref1b4602013-06-01 17:17:04 +02007164 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007165 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007166 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007167 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007168
7169 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007170 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007171
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007172 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007173 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007174
7175 /* compute bitmask from p1 value */
7176 if (IS_PINEVIEW(dev))
7177 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7178 else {
7179 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7180 if (IS_G4X(dev) && reduced_clock)
7181 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7182 }
7183 switch (clock->p2) {
7184 case 5:
7185 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7186 break;
7187 case 7:
7188 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7189 break;
7190 case 10:
7191 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7192 break;
7193 case 14:
7194 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7195 break;
7196 }
7197 if (INTEL_INFO(dev)->gen >= 4)
7198 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7199
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007200 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007201 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007202 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007203 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7204 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7205 else
7206 dpll |= PLL_REF_INPUT_DREFCLK;
7207
7208 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007209 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007210
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007211 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007212 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007213 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007214 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007215 }
7216}
7217
Daniel Vetterf47709a2013-03-28 10:42:02 +01007218static void i8xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007219 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01007220 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007221 int num_connectors)
7222{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007223 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007224 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007225 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007226 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007227
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007228 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307229
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007230 dpll = DPLL_VGA_MODE_DIS;
7231
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007232 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007233 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7234 } else {
7235 if (clock->p1 == 2)
7236 dpll |= PLL_P1_DIVIDE_BY_TWO;
7237 else
7238 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7239 if (clock->p2 == 4)
7240 dpll |= PLL_P2_DIVIDE_BY_4;
7241 }
7242
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007243 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007244 dpll |= DPLL_DVO_2X_MODE;
7245
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007246 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007247 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7248 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7249 else
7250 dpll |= PLL_REF_INPUT_DREFCLK;
7251
7252 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007253 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007254}
7255
Daniel Vetter8a654f32013-06-01 17:16:22 +02007256static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007257{
7258 struct drm_device *dev = intel_crtc->base.dev;
7259 struct drm_i915_private *dev_priv = dev->dev_private;
7260 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007261 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02007262 struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007263 &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007264 uint32_t crtc_vtotal, crtc_vblank_end;
7265 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007266
7267 /* We need to be careful not to changed the adjusted mode, for otherwise
7268 * the hw state checker will get angry at the mismatch. */
7269 crtc_vtotal = adjusted_mode->crtc_vtotal;
7270 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007271
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007272 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007273 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007274 crtc_vtotal -= 1;
7275 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007276
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007277 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007278 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7279 else
7280 vsyncshift = adjusted_mode->crtc_hsync_start -
7281 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007282 if (vsyncshift < 0)
7283 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007284 }
7285
7286 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007287 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007288
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007289 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007290 (adjusted_mode->crtc_hdisplay - 1) |
7291 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007292 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007293 (adjusted_mode->crtc_hblank_start - 1) |
7294 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007295 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007296 (adjusted_mode->crtc_hsync_start - 1) |
7297 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7298
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007299 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007300 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007301 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007302 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007303 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007304 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007305 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007306 (adjusted_mode->crtc_vsync_start - 1) |
7307 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7308
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007309 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7310 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7311 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7312 * bits. */
7313 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7314 (pipe == PIPE_B || pipe == PIPE_C))
7315 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7316
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007317 /* pipesrc controls the size that is scaled from, which should
7318 * always be the user's requested size.
7319 */
7320 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007321 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7322 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007323}
7324
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007325static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007326 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007327{
7328 struct drm_device *dev = crtc->base.dev;
7329 struct drm_i915_private *dev_priv = dev->dev_private;
7330 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7331 uint32_t tmp;
7332
7333 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007334 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7335 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007336 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007337 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7338 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007339 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007340 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7341 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007342
7343 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007344 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7345 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007346 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007347 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7348 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007349 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007350 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7351 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007352
7353 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007354 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7355 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7356 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007357 }
7358
7359 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007360 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7361 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7362
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007363 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7364 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007365}
7366
Daniel Vetterf6a83282014-02-11 15:28:57 -08007367void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007368 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007369{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007370 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7371 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7372 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7373 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007374
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007375 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7376 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7377 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7378 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007379
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007380 mode->flags = pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007381
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007382 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7383 mode->flags |= pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007384}
7385
Daniel Vetter84b046f2013-02-19 18:48:54 +01007386static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7387{
7388 struct drm_device *dev = intel_crtc->base.dev;
7389 struct drm_i915_private *dev_priv = dev->dev_private;
7390 uint32_t pipeconf;
7391
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007392 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007393
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007394 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7395 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7396 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007397
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007398 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007399 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007400
Daniel Vetterff9ce462013-04-24 14:57:17 +02007401 /* only g4x and later have fancy bpc/dither controls */
7402 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007403 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007404 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007405 pipeconf |= PIPECONF_DITHER_EN |
7406 PIPECONF_DITHER_TYPE_SP;
7407
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007408 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007409 case 18:
7410 pipeconf |= PIPECONF_6BPC;
7411 break;
7412 case 24:
7413 pipeconf |= PIPECONF_8BPC;
7414 break;
7415 case 30:
7416 pipeconf |= PIPECONF_10BPC;
7417 break;
7418 default:
7419 /* Case prevented by intel_choose_pipe_bpp_dither. */
7420 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007421 }
7422 }
7423
7424 if (HAS_PIPE_CXSR(dev)) {
7425 if (intel_crtc->lowfreq_avail) {
7426 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7427 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7428 } else {
7429 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007430 }
7431 }
7432
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007433 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007434 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007435 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007436 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7437 else
7438 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7439 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007440 pipeconf |= PIPECONF_PROGRESSIVE;
7441
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007442 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007443 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007444
Daniel Vetter84b046f2013-02-19 18:48:54 +01007445 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7446 POSTING_READ(PIPECONF(intel_crtc->pipe));
7447}
7448
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007449static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7450 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007451{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007452 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007453 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc751ce42010-03-25 11:48:48 -07007454 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07007455 intel_clock_t clock, reduced_clock;
Daniel Vettera16af722013-04-30 14:01:44 +02007456 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007457 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01007458 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08007459 const intel_limit_t *limit;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007460 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007461 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007462 struct drm_connector_state *connector_state;
7463 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -08007464
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007465 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007466 if (connector_state->crtc != &crtc->base)
7467 continue;
7468
7469 encoder = to_intel_encoder(connector_state->best_encoder);
7470
Chris Wilson5eddb702010-09-11 13:48:45 +01007471 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007472 case INTEL_OUTPUT_LVDS:
7473 is_lvds = true;
7474 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007475 case INTEL_OUTPUT_DSI:
7476 is_dsi = true;
7477 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007478 default:
7479 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007480 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05007481
Eric Anholtc751ce42010-03-25 11:48:48 -07007482 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08007483 }
7484
Jani Nikulaf2335332013-09-13 11:03:09 +03007485 if (is_dsi)
Daniel Vetter5b18e572014-04-24 23:55:06 +02007486 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007487
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007488 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007489 refclk = i9xx_get_refclk(crtc_state, num_connectors);
Jani Nikulaf2335332013-09-13 11:03:09 +03007490
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007491 /*
7492 * Returns a set of divisors for the desired target clock with
7493 * the given refclk, or FALSE. The returned values represent
7494 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7495 * 2) / p1 / p2.
7496 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007497 limit = intel_limit(crtc_state, refclk);
7498 ok = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007499 crtc_state->port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007500 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03007501 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007502 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7503 return -EINVAL;
7504 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007505
Jani Nikulaf2335332013-09-13 11:03:09 +03007506 if (is_lvds && dev_priv->lvds_downclock_avail) {
7507 /*
7508 * Ensure we match the reduced clock's P to the target
7509 * clock. If the clocks don't match, we can't switch
7510 * the display clock by using the FP0/FP1. In such case
7511 * we will disable the LVDS downclock feature.
7512 */
7513 has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007514 dev_priv->display.find_dpll(limit, crtc_state,
Jani Nikulaf2335332013-09-13 11:03:09 +03007515 dev_priv->lvds_downclock,
7516 refclk, &clock,
7517 &reduced_clock);
7518 }
7519 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007520 crtc_state->dpll.n = clock.n;
7521 crtc_state->dpll.m1 = clock.m1;
7522 crtc_state->dpll.m2 = clock.m2;
7523 crtc_state->dpll.p1 = clock.p1;
7524 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007525 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007526
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007527 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007528 i8xx_update_pll(crtc, crtc_state,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307529 has_reduced_clock ? &reduced_clock : NULL,
7530 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007531 } else if (IS_CHERRYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007532 chv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007533 } else if (IS_VALLEYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007534 vlv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007535 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007536 i9xx_update_pll(crtc, crtc_state,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007537 has_reduced_clock ? &reduced_clock : NULL,
Robin Schroereba905b2014-05-18 02:24:50 +02007538 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007539 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007540
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007541 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007542}
7543
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007544static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007545 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007546{
7547 struct drm_device *dev = crtc->base.dev;
7548 struct drm_i915_private *dev_priv = dev->dev_private;
7549 uint32_t tmp;
7550
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007551 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7552 return;
7553
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007554 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007555 if (!(tmp & PFIT_ENABLE))
7556 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007557
Daniel Vetter06922822013-07-11 13:35:40 +02007558 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007559 if (INTEL_INFO(dev)->gen < 4) {
7560 if (crtc->pipe != PIPE_B)
7561 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007562 } else {
7563 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7564 return;
7565 }
7566
Daniel Vetter06922822013-07-11 13:35:40 +02007567 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007568 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7569 if (INTEL_INFO(dev)->gen < 5)
7570 pipe_config->gmch_pfit.lvds_border_bits =
7571 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7572}
7573
Jesse Barnesacbec812013-09-20 11:29:32 -07007574static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007575 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07007576{
7577 struct drm_device *dev = crtc->base.dev;
7578 struct drm_i915_private *dev_priv = dev->dev_private;
7579 int pipe = pipe_config->cpu_transcoder;
7580 intel_clock_t clock;
7581 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07007582 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07007583
Shobhit Kumarf573de52014-07-30 20:32:37 +05307584 /* In case of MIPI DPLL will not even be used */
7585 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7586 return;
7587
Jesse Barnesacbec812013-09-20 11:29:32 -07007588 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007589 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Jesse Barnesacbec812013-09-20 11:29:32 -07007590 mutex_unlock(&dev_priv->dpio_lock);
7591
7592 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7593 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7594 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7595 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7596 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7597
Ville Syrjäläf6466282013-10-14 14:50:31 +03007598 vlv_clock(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007599
Ville Syrjäläf6466282013-10-14 14:50:31 +03007600 /* clock.dot is the fast clock */
7601 pipe_config->port_clock = clock.dot / 5;
Jesse Barnesacbec812013-09-20 11:29:32 -07007602}
7603
Damien Lespiau5724dbd2015-01-20 12:51:52 +00007604static void
7605i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7606 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007607{
7608 struct drm_device *dev = crtc->base.dev;
7609 struct drm_i915_private *dev_priv = dev->dev_private;
7610 u32 val, base, offset;
7611 int pipe = crtc->pipe, plane = crtc->plane;
7612 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00007613 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007614 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00007615 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007616
Damien Lespiau42a7b082015-02-05 19:35:13 +00007617 val = I915_READ(DSPCNTR(plane));
7618 if (!(val & DISPLAY_PLANE_ENABLE))
7619 return;
7620
Damien Lespiaud9806c92015-01-21 14:07:19 +00007621 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00007622 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007623 DRM_DEBUG_KMS("failed to alloc fb\n");
7624 return;
7625 }
7626
Damien Lespiau1b842c82015-01-21 13:50:54 +00007627 fb = &intel_fb->base;
7628
Daniel Vetter18c52472015-02-10 17:16:09 +00007629 if (INTEL_INFO(dev)->gen >= 4) {
7630 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007631 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00007632 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
7633 }
7634 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007635
7636 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00007637 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007638 fb->pixel_format = fourcc;
7639 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007640
7641 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007642 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007643 offset = I915_READ(DSPTILEOFF(plane));
7644 else
7645 offset = I915_READ(DSPLINOFF(plane));
7646 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7647 } else {
7648 base = I915_READ(DSPADDR(plane));
7649 }
7650 plane_config->base = base;
7651
7652 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007653 fb->width = ((val >> 16) & 0xfff) + 1;
7654 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007655
7656 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007657 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007658
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007659 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00007660 fb->pixel_format,
7661 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007662
Daniel Vetterf37b5c22015-02-10 23:12:27 +01007663 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007664
Damien Lespiau2844a922015-01-20 12:51:48 +00007665 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7666 pipe_name(pipe), plane, fb->width, fb->height,
7667 fb->bits_per_pixel, base, fb->pitches[0],
7668 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007669
Damien Lespiau2d140302015-02-05 17:22:18 +00007670 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007671}
7672
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007673static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007674 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007675{
7676 struct drm_device *dev = crtc->base.dev;
7677 struct drm_i915_private *dev_priv = dev->dev_private;
7678 int pipe = pipe_config->cpu_transcoder;
7679 enum dpio_channel port = vlv_pipe_to_channel(pipe);
7680 intel_clock_t clock;
7681 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
7682 int refclk = 100000;
7683
7684 mutex_lock(&dev_priv->dpio_lock);
7685 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
7686 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
7687 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
7688 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
7689 mutex_unlock(&dev_priv->dpio_lock);
7690
7691 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
7692 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
7693 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
7694 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
7695 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
7696
7697 chv_clock(refclk, &clock);
7698
7699 /* clock.dot is the fast clock */
7700 pipe_config->port_clock = clock.dot / 5;
7701}
7702
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007703static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007704 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007705{
7706 struct drm_device *dev = crtc->base.dev;
7707 struct drm_i915_private *dev_priv = dev->dev_private;
7708 uint32_t tmp;
7709
Daniel Vetterf458ebb2014-09-30 10:56:39 +02007710 if (!intel_display_power_is_enabled(dev_priv,
7711 POWER_DOMAIN_PIPE(crtc->pipe)))
Imre Deakb5482bd2014-03-05 16:20:55 +02007712 return false;
7713
Daniel Vettere143a212013-07-04 12:01:15 +02007714 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007715 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02007716
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007717 tmp = I915_READ(PIPECONF(crtc->pipe));
7718 if (!(tmp & PIPECONF_ENABLE))
7719 return false;
7720
Ville Syrjälä42571ae2013-09-06 23:29:00 +03007721 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
7722 switch (tmp & PIPECONF_BPC_MASK) {
7723 case PIPECONF_6BPC:
7724 pipe_config->pipe_bpp = 18;
7725 break;
7726 case PIPECONF_8BPC:
7727 pipe_config->pipe_bpp = 24;
7728 break;
7729 case PIPECONF_10BPC:
7730 pipe_config->pipe_bpp = 30;
7731 break;
7732 default:
7733 break;
7734 }
7735 }
7736
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02007737 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
7738 pipe_config->limited_color_range = true;
7739
Ville Syrjälä282740f2013-09-04 18:30:03 +03007740 if (INTEL_INFO(dev)->gen < 4)
7741 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
7742
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007743 intel_get_pipe_timings(crtc, pipe_config);
7744
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007745 i9xx_get_pfit_config(crtc, pipe_config);
7746
Daniel Vetter6c49f242013-06-06 12:45:25 +02007747 if (INTEL_INFO(dev)->gen >= 4) {
7748 tmp = I915_READ(DPLL_MD(crtc->pipe));
7749 pipe_config->pixel_multiplier =
7750 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
7751 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007752 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02007753 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
7754 tmp = I915_READ(DPLL(crtc->pipe));
7755 pipe_config->pixel_multiplier =
7756 ((tmp & SDVO_MULTIPLIER_MASK)
7757 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
7758 } else {
7759 /* Note that on i915G/GM the pixel multiplier is in the sdvo
7760 * port and will be fixed up in the encoder->get_config
7761 * function. */
7762 pipe_config->pixel_multiplier = 1;
7763 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007764 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
7765 if (!IS_VALLEYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03007766 /*
7767 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
7768 * on 830. Filter it out here so that we don't
7769 * report errors due to that.
7770 */
7771 if (IS_I830(dev))
7772 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
7773
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007774 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
7775 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03007776 } else {
7777 /* Mask out read-only status bits. */
7778 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
7779 DPLL_PORTC_READY_MASK |
7780 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007781 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02007782
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007783 if (IS_CHERRYVIEW(dev))
7784 chv_crtc_clock_get(crtc, pipe_config);
7785 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07007786 vlv_crtc_clock_get(crtc, pipe_config);
7787 else
7788 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03007789
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007790 return true;
7791}
7792
Paulo Zanonidde86e22012-12-01 12:04:25 -02007793static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07007794{
7795 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007796 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007797 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007798 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07007799 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07007800 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07007801 bool has_ck505 = false;
7802 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007803
7804 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01007805 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07007806 switch (encoder->type) {
7807 case INTEL_OUTPUT_LVDS:
7808 has_panel = true;
7809 has_lvds = true;
7810 break;
7811 case INTEL_OUTPUT_EDP:
7812 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03007813 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07007814 has_cpu_edp = true;
7815 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007816 default:
7817 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007818 }
7819 }
7820
Keith Packard99eb6a02011-09-26 14:29:12 -07007821 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007822 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07007823 can_ssc = has_ck505;
7824 } else {
7825 has_ck505 = false;
7826 can_ssc = true;
7827 }
7828
Imre Deak2de69052013-05-08 13:14:04 +03007829 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
7830 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007831
7832 /* Ironlake: try to setup display ref clock before DPLL
7833 * enabling. This is only under driver's control after
7834 * PCH B stepping, previous chipset stepping should be
7835 * ignoring this setting.
7836 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007837 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007838
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007839 /* As we must carefully and slowly disable/enable each source in turn,
7840 * compute the final state we want first and check if we need to
7841 * make any changes at all.
7842 */
7843 final = val;
7844 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07007845 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007846 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07007847 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007848 final |= DREF_NONSPREAD_SOURCE_ENABLE;
7849
7850 final &= ~DREF_SSC_SOURCE_MASK;
7851 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
7852 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007853
Keith Packard199e5d72011-09-22 12:01:57 -07007854 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007855 final |= DREF_SSC_SOURCE_ENABLE;
7856
7857 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7858 final |= DREF_SSC1_ENABLE;
7859
7860 if (has_cpu_edp) {
7861 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7862 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
7863 else
7864 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
7865 } else
7866 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
7867 } else {
7868 final |= DREF_SSC_SOURCE_DISABLE;
7869 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
7870 }
7871
7872 if (final == val)
7873 return;
7874
7875 /* Always enable nonspread source */
7876 val &= ~DREF_NONSPREAD_SOURCE_MASK;
7877
7878 if (has_ck505)
7879 val |= DREF_NONSPREAD_CK505_ENABLE;
7880 else
7881 val |= DREF_NONSPREAD_SOURCE_ENABLE;
7882
7883 if (has_panel) {
7884 val &= ~DREF_SSC_SOURCE_MASK;
7885 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007886
Keith Packard199e5d72011-09-22 12:01:57 -07007887 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07007888 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07007889 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007890 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02007891 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007892 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007893
7894 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007895 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007896 POSTING_READ(PCH_DREF_CONTROL);
7897 udelay(200);
7898
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007899 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007900
7901 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07007902 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07007903 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07007904 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007905 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02007906 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007907 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07007908 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007909 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007910
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007911 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007912 POSTING_READ(PCH_DREF_CONTROL);
7913 udelay(200);
7914 } else {
7915 DRM_DEBUG_KMS("Disabling SSC entirely\n");
7916
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007917 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07007918
7919 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007920 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007921
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007922 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007923 POSTING_READ(PCH_DREF_CONTROL);
7924 udelay(200);
7925
7926 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007927 val &= ~DREF_SSC_SOURCE_MASK;
7928 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007929
7930 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007931 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007932
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007933 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007934 POSTING_READ(PCH_DREF_CONTROL);
7935 udelay(200);
7936 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007937
7938 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007939}
7940
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007941static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02007942{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007943 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02007944
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007945 tmp = I915_READ(SOUTH_CHICKEN2);
7946 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
7947 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007948
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007949 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
7950 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
7951 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02007952
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007953 tmp = I915_READ(SOUTH_CHICKEN2);
7954 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
7955 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007956
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007957 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
7958 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
7959 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007960}
7961
7962/* WaMPhyProgramming:hsw */
7963static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
7964{
7965 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02007966
7967 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
7968 tmp &= ~(0xFF << 24);
7969 tmp |= (0x12 << 24);
7970 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
7971
Paulo Zanonidde86e22012-12-01 12:04:25 -02007972 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
7973 tmp |= (1 << 11);
7974 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
7975
7976 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
7977 tmp |= (1 << 11);
7978 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
7979
Paulo Zanonidde86e22012-12-01 12:04:25 -02007980 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
7981 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7982 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
7983
7984 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
7985 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7986 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
7987
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007988 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
7989 tmp &= ~(7 << 13);
7990 tmp |= (5 << 13);
7991 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007992
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007993 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
7994 tmp &= ~(7 << 13);
7995 tmp |= (5 << 13);
7996 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007997
7998 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
7999 tmp &= ~0xFF;
8000 tmp |= 0x1C;
8001 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8002
8003 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8004 tmp &= ~0xFF;
8005 tmp |= 0x1C;
8006 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8007
8008 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8009 tmp &= ~(0xFF << 16);
8010 tmp |= (0x1C << 16);
8011 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8012
8013 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8014 tmp &= ~(0xFF << 16);
8015 tmp |= (0x1C << 16);
8016 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8017
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008018 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8019 tmp |= (1 << 27);
8020 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008021
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008022 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8023 tmp |= (1 << 27);
8024 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008025
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008026 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8027 tmp &= ~(0xF << 28);
8028 tmp |= (4 << 28);
8029 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008030
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008031 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8032 tmp &= ~(0xF << 28);
8033 tmp |= (4 << 28);
8034 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008035}
8036
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008037/* Implements 3 different sequences from BSpec chapter "Display iCLK
8038 * Programming" based on the parameters passed:
8039 * - Sequence to enable CLKOUT_DP
8040 * - Sequence to enable CLKOUT_DP without spread
8041 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8042 */
8043static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8044 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008045{
8046 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008047 uint32_t reg, tmp;
8048
8049 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8050 with_spread = true;
8051 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
8052 with_fdi, "LP PCH doesn't have FDI\n"))
8053 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008054
8055 mutex_lock(&dev_priv->dpio_lock);
8056
8057 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8058 tmp &= ~SBI_SSCCTL_DISABLE;
8059 tmp |= SBI_SSCCTL_PATHALT;
8060 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8061
8062 udelay(24);
8063
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008064 if (with_spread) {
8065 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8066 tmp &= ~SBI_SSCCTL_PATHALT;
8067 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008068
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008069 if (with_fdi) {
8070 lpt_reset_fdi_mphy(dev_priv);
8071 lpt_program_fdi_mphy(dev_priv);
8072 }
8073 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008074
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008075 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8076 SBI_GEN0 : SBI_DBUFF0;
8077 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8078 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8079 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008080
8081 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008082}
8083
Paulo Zanoni47701c32013-07-23 11:19:25 -03008084/* Sequence to disable CLKOUT_DP */
8085static void lpt_disable_clkout_dp(struct drm_device *dev)
8086{
8087 struct drm_i915_private *dev_priv = dev->dev_private;
8088 uint32_t reg, tmp;
8089
8090 mutex_lock(&dev_priv->dpio_lock);
8091
8092 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8093 SBI_GEN0 : SBI_DBUFF0;
8094 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8095 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8096 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8097
8098 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8099 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8100 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8101 tmp |= SBI_SSCCTL_PATHALT;
8102 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8103 udelay(32);
8104 }
8105 tmp |= SBI_SSCCTL_DISABLE;
8106 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8107 }
8108
8109 mutex_unlock(&dev_priv->dpio_lock);
8110}
8111
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008112static void lpt_init_pch_refclk(struct drm_device *dev)
8113{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008114 struct intel_encoder *encoder;
8115 bool has_vga = false;
8116
Damien Lespiaub2784e12014-08-05 11:29:37 +01008117 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008118 switch (encoder->type) {
8119 case INTEL_OUTPUT_ANALOG:
8120 has_vga = true;
8121 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008122 default:
8123 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008124 }
8125 }
8126
Paulo Zanoni47701c32013-07-23 11:19:25 -03008127 if (has_vga)
8128 lpt_enable_clkout_dp(dev, true, true);
8129 else
8130 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008131}
8132
Paulo Zanonidde86e22012-12-01 12:04:25 -02008133/*
8134 * Initialize reference clocks when the driver loads
8135 */
8136void intel_init_pch_refclk(struct drm_device *dev)
8137{
8138 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8139 ironlake_init_pch_refclk(dev);
8140 else if (HAS_PCH_LPT(dev))
8141 lpt_init_pch_refclk(dev);
8142}
8143
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008144static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008145{
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008146 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008147 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008148 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008149 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008150 struct drm_connector_state *connector_state;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008151 struct intel_encoder *encoder;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008152 int num_connectors = 0, i;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008153 bool is_lvds = false;
8154
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008155 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008156 if (connector_state->crtc != crtc_state->base.crtc)
8157 continue;
8158
8159 encoder = to_intel_encoder(connector_state->best_encoder);
8160
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008161 switch (encoder->type) {
8162 case INTEL_OUTPUT_LVDS:
8163 is_lvds = true;
8164 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008165 default:
8166 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008167 }
8168 num_connectors++;
8169 }
8170
8171 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008172 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008173 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008174 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008175 }
8176
8177 return 120000;
8178}
8179
Daniel Vetter6ff93602013-04-19 11:24:36 +02008180static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008181{
8182 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8183 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8184 int pipe = intel_crtc->pipe;
8185 uint32_t val;
8186
Daniel Vetter78114072013-06-13 00:54:57 +02008187 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008188
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008189 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008190 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008191 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008192 break;
8193 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008194 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008195 break;
8196 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008197 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008198 break;
8199 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008200 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008201 break;
8202 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008203 /* Case prevented by intel_choose_pipe_bpp_dither. */
8204 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008205 }
8206
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008207 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008208 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8209
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008210 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008211 val |= PIPECONF_INTERLACED_ILK;
8212 else
8213 val |= PIPECONF_PROGRESSIVE;
8214
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008215 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008216 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008217
Paulo Zanonic8203562012-09-12 10:06:29 -03008218 I915_WRITE(PIPECONF(pipe), val);
8219 POSTING_READ(PIPECONF(pipe));
8220}
8221
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008222/*
8223 * Set up the pipe CSC unit.
8224 *
8225 * Currently only full range RGB to limited range RGB conversion
8226 * is supported, but eventually this should handle various
8227 * RGB<->YCbCr scenarios as well.
8228 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01008229static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008230{
8231 struct drm_device *dev = crtc->dev;
8232 struct drm_i915_private *dev_priv = dev->dev_private;
8233 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8234 int pipe = intel_crtc->pipe;
8235 uint16_t coeff = 0x7800; /* 1.0 */
8236
8237 /*
8238 * TODO: Check what kind of values actually come out of the pipe
8239 * with these coeff/postoff values and adjust to get the best
8240 * accuracy. Perhaps we even need to take the bpc value into
8241 * consideration.
8242 */
8243
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008244 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008245 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8246
8247 /*
8248 * GY/GU and RY/RU should be the other way around according
8249 * to BSpec, but reality doesn't agree. Just set them up in
8250 * a way that results in the correct picture.
8251 */
8252 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8253 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8254
8255 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8256 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8257
8258 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8259 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8260
8261 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8262 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8263 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8264
8265 if (INTEL_INFO(dev)->gen > 6) {
8266 uint16_t postoff = 0;
8267
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008268 if (intel_crtc->config->limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02008269 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008270
8271 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8272 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8273 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8274
8275 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8276 } else {
8277 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8278
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008279 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008280 mode |= CSC_BLACK_SCREEN_OFFSET;
8281
8282 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8283 }
8284}
8285
Daniel Vetter6ff93602013-04-19 11:24:36 +02008286static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008287{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008288 struct drm_device *dev = crtc->dev;
8289 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008290 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008291 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008292 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008293 uint32_t val;
8294
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008295 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008296
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008297 if (IS_HASWELL(dev) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008298 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8299
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008300 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008301 val |= PIPECONF_INTERLACED_ILK;
8302 else
8303 val |= PIPECONF_PROGRESSIVE;
8304
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008305 I915_WRITE(PIPECONF(cpu_transcoder), val);
8306 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008307
8308 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8309 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008310
Satheeshakrishna M3cdf1222014-04-08 15:46:53 +05308311 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008312 val = 0;
8313
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008314 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008315 case 18:
8316 val |= PIPEMISC_DITHER_6_BPC;
8317 break;
8318 case 24:
8319 val |= PIPEMISC_DITHER_8_BPC;
8320 break;
8321 case 30:
8322 val |= PIPEMISC_DITHER_10_BPC;
8323 break;
8324 case 36:
8325 val |= PIPEMISC_DITHER_12_BPC;
8326 break;
8327 default:
8328 /* Case prevented by pipe_config_set_bpp. */
8329 BUG();
8330 }
8331
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008332 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008333 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8334
8335 I915_WRITE(PIPEMISC(pipe), val);
8336 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008337}
8338
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008339static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008340 struct intel_crtc_state *crtc_state,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008341 intel_clock_t *clock,
8342 bool *has_reduced_clock,
8343 intel_clock_t *reduced_clock)
8344{
8345 struct drm_device *dev = crtc->dev;
8346 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008347 int refclk;
8348 const intel_limit_t *limit;
Daniel Vettera16af722013-04-30 14:01:44 +02008349 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008350
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008351 is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008352
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008353 refclk = ironlake_get_refclk(crtc_state);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008354
8355 /*
8356 * Returns a set of divisors for the desired target clock with the given
8357 * refclk, or FALSE. The returned values represent the clock equation:
8358 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8359 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008360 limit = intel_limit(crtc_state, refclk);
8361 ret = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008362 crtc_state->port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008363 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008364 if (!ret)
8365 return false;
8366
8367 if (is_lvds && dev_priv->lvds_downclock_avail) {
8368 /*
8369 * Ensure we match the reduced clock's P to the target clock.
8370 * If the clocks don't match, we can't switch the display clock
8371 * by using the FP0/FP1. In such case we will disable the LVDS
8372 * downclock feature.
8373 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02008374 *has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008375 dev_priv->display.find_dpll(limit, crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008376 dev_priv->lvds_downclock,
8377 refclk, clock,
8378 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008379 }
8380
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008381 return true;
8382}
8383
Paulo Zanonid4b19312012-11-29 11:29:32 -02008384int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8385{
8386 /*
8387 * Account for spread spectrum to avoid
8388 * oversubscribing the link. Max center spread
8389 * is 2.5%; use 5% for safety's sake.
8390 */
8391 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008392 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008393}
8394
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008395static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008396{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008397 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008398}
8399
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008400static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008401 struct intel_crtc_state *crtc_state,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008402 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008403 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008404{
8405 struct drm_crtc *crtc = &intel_crtc->base;
8406 struct drm_device *dev = crtc->dev;
8407 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008408 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008409 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008410 struct drm_connector_state *connector_state;
8411 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008412 uint32_t dpll;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008413 int factor, num_connectors = 0, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008414 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008415
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008416 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008417 if (connector_state->crtc != crtc_state->base.crtc)
8418 continue;
8419
8420 encoder = to_intel_encoder(connector_state->best_encoder);
8421
8422 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008423 case INTEL_OUTPUT_LVDS:
8424 is_lvds = true;
8425 break;
8426 case INTEL_OUTPUT_SDVO:
8427 case INTEL_OUTPUT_HDMI:
8428 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008429 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008430 default:
8431 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008432 }
8433
8434 num_connectors++;
8435 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008436
Chris Wilsonc1858122010-12-03 21:35:48 +00008437 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008438 factor = 21;
8439 if (is_lvds) {
8440 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008441 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008442 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008443 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008444 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008445 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008446
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008447 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02008448 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00008449
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008450 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8451 *fp2 |= FP_CB_TUNE;
8452
Chris Wilson5eddb702010-09-11 13:48:45 +01008453 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008454
Eric Anholta07d6782011-03-30 13:01:08 -07008455 if (is_lvds)
8456 dpll |= DPLLB_MODE_LVDS;
8457 else
8458 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008459
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008460 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008461 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008462
8463 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008464 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008465 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008466 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008467
Eric Anholta07d6782011-03-30 13:01:08 -07008468 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008469 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008470 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008471 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008472
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008473 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008474 case 5:
8475 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8476 break;
8477 case 7:
8478 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8479 break;
8480 case 10:
8481 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8482 break;
8483 case 14:
8484 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8485 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008486 }
8487
Daniel Vetterb4c09f32013-04-30 14:01:42 +02008488 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008489 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008490 else
8491 dpll |= PLL_REF_INPUT_DREFCLK;
8492
Daniel Vetter959e16d2013-06-05 13:34:21 +02008493 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008494}
8495
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008496static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8497 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008498{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008499 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008500 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008501 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03008502 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01008503 bool is_lvds = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008504 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008505
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03008506 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
Jesse Barnes79e53942008-11-07 14:24:08 -08008507
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008508 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8509 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
8510
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008511 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008512 &has_reduced_clock, &reduced_clock);
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008513 if (!ok && !crtc_state->clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008514 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8515 return -EINVAL;
8516 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01008517 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008518 if (!crtc_state->clock_set) {
8519 crtc_state->dpll.n = clock.n;
8520 crtc_state->dpll.m1 = clock.m1;
8521 crtc_state->dpll.m2 = clock.m2;
8522 crtc_state->dpll.p1 = clock.p1;
8523 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008524 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008525
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008526 /* 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 +02008527 if (crtc_state->has_pch_encoder) {
8528 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008529 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008530 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008531
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008532 dpll = ironlake_compute_dpll(crtc, crtc_state,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008533 &fp, &reduced_clock,
8534 has_reduced_clock ? &fp2 : NULL);
8535
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008536 crtc_state->dpll_hw_state.dpll = dpll;
8537 crtc_state->dpll_hw_state.fp0 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008538 if (has_reduced_clock)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008539 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008540 else
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008541 crtc_state->dpll_hw_state.fp1 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008542
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008543 pll = intel_get_shared_dpll(crtc, crtc_state);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008544 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03008545 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008546 pipe_name(crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07008547 return -EINVAL;
8548 }
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008549 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008550
Rodrigo Viviab585de2015-03-24 12:40:09 -07008551 if (is_lvds && has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008552 crtc->lowfreq_avail = true;
Daniel Vetterbcd644e2013-06-05 13:34:22 +02008553 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008554 crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008555
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008556 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008557}
8558
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008559static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8560 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008561{
8562 struct drm_device *dev = crtc->base.dev;
8563 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008564 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008565
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008566 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8567 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8568 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8569 & ~TU_SIZE_MASK;
8570 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8571 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8572 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8573}
8574
8575static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8576 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008577 struct intel_link_m_n *m_n,
8578 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008579{
8580 struct drm_device *dev = crtc->base.dev;
8581 struct drm_i915_private *dev_priv = dev->dev_private;
8582 enum pipe pipe = crtc->pipe;
8583
8584 if (INTEL_INFO(dev)->gen >= 5) {
8585 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8586 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8587 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8588 & ~TU_SIZE_MASK;
8589 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8590 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8591 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008592 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8593 * gen < 8) and if DRRS is supported (to make sure the
8594 * registers are not unnecessarily read).
8595 */
8596 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008597 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008598 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8599 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8600 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8601 & ~TU_SIZE_MASK;
8602 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8603 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8604 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8605 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008606 } else {
8607 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8608 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8609 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8610 & ~TU_SIZE_MASK;
8611 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8612 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8613 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8614 }
8615}
8616
8617void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008618 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008619{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02008620 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008621 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8622 else
8623 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008624 &pipe_config->dp_m_n,
8625 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008626}
8627
Daniel Vetter72419202013-04-04 13:28:53 +02008628static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008629 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02008630{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008631 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008632 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02008633}
8634
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008635static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008636 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008637{
8638 struct drm_device *dev = crtc->base.dev;
8639 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07008640 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8641 uint32_t ps_ctrl = 0;
8642 int id = -1;
8643 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008644
Chandra Kondurua1b22782015-04-07 15:28:45 -07008645 /* find scaler attached to this pipe */
8646 for (i = 0; i < crtc->num_scalers; i++) {
8647 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8648 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8649 id = i;
8650 pipe_config->pch_pfit.enabled = true;
8651 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8652 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8653 break;
8654 }
8655 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008656
Chandra Kondurua1b22782015-04-07 15:28:45 -07008657 scaler_state->scaler_id = id;
8658 if (id >= 0) {
8659 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8660 } else {
8661 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008662 }
8663}
8664
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008665static void
8666skylake_get_initial_plane_config(struct intel_crtc *crtc,
8667 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008668{
8669 struct drm_device *dev = crtc->base.dev;
8670 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00008671 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008672 int pipe = crtc->pipe;
8673 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008674 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008675 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008676 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008677
Damien Lespiaud9806c92015-01-21 14:07:19 +00008678 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008679 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008680 DRM_DEBUG_KMS("failed to alloc fb\n");
8681 return;
8682 }
8683
Damien Lespiau1b842c82015-01-21 13:50:54 +00008684 fb = &intel_fb->base;
8685
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008686 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00008687 if (!(val & PLANE_CTL_ENABLE))
8688 goto error;
8689
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008690 pixel_format = val & PLANE_CTL_FORMAT_MASK;
8691 fourcc = skl_format_to_fourcc(pixel_format,
8692 val & PLANE_CTL_ORDER_RGBX,
8693 val & PLANE_CTL_ALPHA_MASK);
8694 fb->pixel_format = fourcc;
8695 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
8696
Damien Lespiau40f46282015-02-27 11:15:21 +00008697 tiling = val & PLANE_CTL_TILED_MASK;
8698 switch (tiling) {
8699 case PLANE_CTL_TILED_LINEAR:
8700 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
8701 break;
8702 case PLANE_CTL_TILED_X:
8703 plane_config->tiling = I915_TILING_X;
8704 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8705 break;
8706 case PLANE_CTL_TILED_Y:
8707 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
8708 break;
8709 case PLANE_CTL_TILED_YF:
8710 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
8711 break;
8712 default:
8713 MISSING_CASE(tiling);
8714 goto error;
8715 }
8716
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008717 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
8718 plane_config->base = base;
8719
8720 offset = I915_READ(PLANE_OFFSET(pipe, 0));
8721
8722 val = I915_READ(PLANE_SIZE(pipe, 0));
8723 fb->height = ((val >> 16) & 0xfff) + 1;
8724 fb->width = ((val >> 0) & 0x1fff) + 1;
8725
8726 val = I915_READ(PLANE_STRIDE(pipe, 0));
Damien Lespiau40f46282015-02-27 11:15:21 +00008727 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
8728 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008729 fb->pitches[0] = (val & 0x3ff) * stride_mult;
8730
8731 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008732 fb->pixel_format,
8733 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008734
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008735 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008736
8737 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8738 pipe_name(pipe), fb->width, fb->height,
8739 fb->bits_per_pixel, base, fb->pitches[0],
8740 plane_config->size);
8741
Damien Lespiau2d140302015-02-05 17:22:18 +00008742 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008743 return;
8744
8745error:
8746 kfree(fb);
8747}
8748
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008749static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008750 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008751{
8752 struct drm_device *dev = crtc->base.dev;
8753 struct drm_i915_private *dev_priv = dev->dev_private;
8754 uint32_t tmp;
8755
8756 tmp = I915_READ(PF_CTL(crtc->pipe));
8757
8758 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008759 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008760 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
8761 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02008762
8763 /* We currently do not free assignements of panel fitters on
8764 * ivb/hsw (since we don't use the higher upscaling modes which
8765 * differentiates them) so just WARN about this case for now. */
8766 if (IS_GEN7(dev)) {
8767 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
8768 PF_PIPE_SEL_IVB(crtc->pipe));
8769 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008770 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008771}
8772
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008773static void
8774ironlake_get_initial_plane_config(struct intel_crtc *crtc,
8775 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008776{
8777 struct drm_device *dev = crtc->base.dev;
8778 struct drm_i915_private *dev_priv = dev->dev_private;
8779 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008780 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008781 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008782 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008783 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008784 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008785
Damien Lespiau42a7b082015-02-05 19:35:13 +00008786 val = I915_READ(DSPCNTR(pipe));
8787 if (!(val & DISPLAY_PLANE_ENABLE))
8788 return;
8789
Damien Lespiaud9806c92015-01-21 14:07:19 +00008790 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008791 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008792 DRM_DEBUG_KMS("failed to alloc fb\n");
8793 return;
8794 }
8795
Damien Lespiau1b842c82015-01-21 13:50:54 +00008796 fb = &intel_fb->base;
8797
Daniel Vetter18c52472015-02-10 17:16:09 +00008798 if (INTEL_INFO(dev)->gen >= 4) {
8799 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008800 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008801 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8802 }
8803 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008804
8805 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008806 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008807 fb->pixel_format = fourcc;
8808 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008809
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008810 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008811 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008812 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008813 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00008814 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008815 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008816 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008817 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008818 }
8819 plane_config->base = base;
8820
8821 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008822 fb->width = ((val >> 16) & 0xfff) + 1;
8823 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008824
8825 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008826 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008827
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008828 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008829 fb->pixel_format,
8830 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008831
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008832 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008833
Damien Lespiau2844a922015-01-20 12:51:48 +00008834 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8835 pipe_name(pipe), fb->width, fb->height,
8836 fb->bits_per_pixel, base, fb->pitches[0],
8837 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008838
Damien Lespiau2d140302015-02-05 17:22:18 +00008839 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008840}
8841
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008842static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008843 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008844{
8845 struct drm_device *dev = crtc->base.dev;
8846 struct drm_i915_private *dev_priv = dev->dev_private;
8847 uint32_t tmp;
8848
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008849 if (!intel_display_power_is_enabled(dev_priv,
8850 POWER_DOMAIN_PIPE(crtc->pipe)))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03008851 return false;
8852
Daniel Vettere143a212013-07-04 12:01:15 +02008853 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008854 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02008855
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008856 tmp = I915_READ(PIPECONF(crtc->pipe));
8857 if (!(tmp & PIPECONF_ENABLE))
8858 return false;
8859
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008860 switch (tmp & PIPECONF_BPC_MASK) {
8861 case PIPECONF_6BPC:
8862 pipe_config->pipe_bpp = 18;
8863 break;
8864 case PIPECONF_8BPC:
8865 pipe_config->pipe_bpp = 24;
8866 break;
8867 case PIPECONF_10BPC:
8868 pipe_config->pipe_bpp = 30;
8869 break;
8870 case PIPECONF_12BPC:
8871 pipe_config->pipe_bpp = 36;
8872 break;
8873 default:
8874 break;
8875 }
8876
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008877 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
8878 pipe_config->limited_color_range = true;
8879
Daniel Vetterab9412b2013-05-03 11:49:46 +02008880 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02008881 struct intel_shared_dpll *pll;
8882
Daniel Vetter88adfff2013-03-28 10:42:01 +01008883 pipe_config->has_pch_encoder = true;
8884
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008885 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
8886 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8887 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02008888
8889 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008890
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008891 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02008892 pipe_config->shared_dpll =
8893 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008894 } else {
8895 tmp = I915_READ(PCH_DPLL_SEL);
8896 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8897 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
8898 else
8899 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
8900 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02008901
8902 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
8903
8904 WARN_ON(!pll->get_hw_state(dev_priv, pll,
8905 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02008906
8907 tmp = pipe_config->dpll_hw_state.dpll;
8908 pipe_config->pixel_multiplier =
8909 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
8910 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03008911
8912 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008913 } else {
8914 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008915 }
8916
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008917 intel_get_pipe_timings(crtc, pipe_config);
8918
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008919 ironlake_get_pfit_config(crtc, pipe_config);
8920
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008921 return true;
8922}
8923
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008924static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
8925{
8926 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008927 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008928
Damien Lespiaud3fcc802014-05-13 23:32:22 +01008929 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05008930 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008931 pipe_name(crtc->pipe));
8932
Rob Clarke2c719b2014-12-15 13:56:32 -05008933 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
8934 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
8935 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
8936 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
8937 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
8938 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008939 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03008940 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05008941 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03008942 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008943 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008944 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008945 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008946 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008947 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008948
Paulo Zanoni9926ada2014-04-01 19:39:47 -03008949 /*
8950 * In theory we can still leave IRQs enabled, as long as only the HPD
8951 * interrupts remain enabled. We used to check for that, but since it's
8952 * gen-specific and since we only disable LCPLL after we fully disable
8953 * the interrupts, the check below should be enough.
8954 */
Rob Clarke2c719b2014-12-15 13:56:32 -05008955 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008956}
8957
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008958static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
8959{
8960 struct drm_device *dev = dev_priv->dev;
8961
8962 if (IS_HASWELL(dev))
8963 return I915_READ(D_COMP_HSW);
8964 else
8965 return I915_READ(D_COMP_BDW);
8966}
8967
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008968static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
8969{
8970 struct drm_device *dev = dev_priv->dev;
8971
8972 if (IS_HASWELL(dev)) {
8973 mutex_lock(&dev_priv->rps.hw_lock);
8974 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
8975 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03008976 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008977 mutex_unlock(&dev_priv->rps.hw_lock);
8978 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008979 I915_WRITE(D_COMP_BDW, val);
8980 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008981 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008982}
8983
8984/*
8985 * This function implements pieces of two sequences from BSpec:
8986 * - Sequence for display software to disable LCPLL
8987 * - Sequence for display software to allow package C8+
8988 * The steps implemented here are just the steps that actually touch the LCPLL
8989 * register. Callers should take care of disabling all the display engine
8990 * functions, doing the mode unset, fixing interrupts, etc.
8991 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03008992static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
8993 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008994{
8995 uint32_t val;
8996
8997 assert_can_disable_lcpll(dev_priv);
8998
8999 val = I915_READ(LCPLL_CTL);
9000
9001 if (switch_to_fclk) {
9002 val |= LCPLL_CD_SOURCE_FCLK;
9003 I915_WRITE(LCPLL_CTL, val);
9004
9005 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9006 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9007 DRM_ERROR("Switching to FCLK failed\n");
9008
9009 val = I915_READ(LCPLL_CTL);
9010 }
9011
9012 val |= LCPLL_PLL_DISABLE;
9013 I915_WRITE(LCPLL_CTL, val);
9014 POSTING_READ(LCPLL_CTL);
9015
9016 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9017 DRM_ERROR("LCPLL still locked\n");
9018
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009019 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009020 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009021 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009022 ndelay(100);
9023
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009024 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9025 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009026 DRM_ERROR("D_COMP RCOMP still in progress\n");
9027
9028 if (allow_power_down) {
9029 val = I915_READ(LCPLL_CTL);
9030 val |= LCPLL_POWER_DOWN_ALLOW;
9031 I915_WRITE(LCPLL_CTL, val);
9032 POSTING_READ(LCPLL_CTL);
9033 }
9034}
9035
9036/*
9037 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9038 * source.
9039 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009040static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009041{
9042 uint32_t val;
9043
9044 val = I915_READ(LCPLL_CTL);
9045
9046 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9047 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9048 return;
9049
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009050 /*
9051 * Make sure we're not on PC8 state before disabling PC8, otherwise
9052 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009053 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009054 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009055
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009056 if (val & LCPLL_POWER_DOWN_ALLOW) {
9057 val &= ~LCPLL_POWER_DOWN_ALLOW;
9058 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009059 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009060 }
9061
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009062 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009063 val |= D_COMP_COMP_FORCE;
9064 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009065 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009066
9067 val = I915_READ(LCPLL_CTL);
9068 val &= ~LCPLL_PLL_DISABLE;
9069 I915_WRITE(LCPLL_CTL, val);
9070
9071 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9072 DRM_ERROR("LCPLL not locked yet\n");
9073
9074 if (val & LCPLL_CD_SOURCE_FCLK) {
9075 val = I915_READ(LCPLL_CTL);
9076 val &= ~LCPLL_CD_SOURCE_FCLK;
9077 I915_WRITE(LCPLL_CTL, val);
9078
9079 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9080 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9081 DRM_ERROR("Switching back to LCPLL failed\n");
9082 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009083
Mika Kuoppala59bad942015-01-16 11:34:40 +02009084 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009085}
9086
Paulo Zanoni765dab672014-03-07 20:08:18 -03009087/*
9088 * Package states C8 and deeper are really deep PC states that can only be
9089 * reached when all the devices on the system allow it, so even if the graphics
9090 * device allows PC8+, it doesn't mean the system will actually get to these
9091 * states. Our driver only allows PC8+ when going into runtime PM.
9092 *
9093 * The requirements for PC8+ are that all the outputs are disabled, the power
9094 * well is disabled and most interrupts are disabled, and these are also
9095 * requirements for runtime PM. When these conditions are met, we manually do
9096 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9097 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9098 * hang the machine.
9099 *
9100 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9101 * the state of some registers, so when we come back from PC8+ we need to
9102 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9103 * need to take care of the registers kept by RC6. Notice that this happens even
9104 * if we don't put the device in PCI D3 state (which is what currently happens
9105 * because of the runtime PM support).
9106 *
9107 * For more, read "Display Sequences for Package C8" on the hardware
9108 * documentation.
9109 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009110void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009111{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009112 struct drm_device *dev = dev_priv->dev;
9113 uint32_t val;
9114
Paulo Zanonic67a4702013-08-19 13:18:09 -03009115 DRM_DEBUG_KMS("Enabling package C8+\n");
9116
Paulo Zanonic67a4702013-08-19 13:18:09 -03009117 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9118 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9119 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9120 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9121 }
9122
9123 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009124 hsw_disable_lcpll(dev_priv, true, true);
9125}
9126
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009127void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009128{
9129 struct drm_device *dev = dev_priv->dev;
9130 uint32_t val;
9131
Paulo Zanonic67a4702013-08-19 13:18:09 -03009132 DRM_DEBUG_KMS("Disabling package C8+\n");
9133
9134 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009135 lpt_init_pch_refclk(dev);
9136
9137 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9138 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9139 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9140 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9141 }
9142
9143 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009144}
9145
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309146static void broxton_modeset_global_resources(struct drm_atomic_state *state)
9147{
9148 struct drm_device *dev = state->dev;
9149 struct drm_i915_private *dev_priv = dev->dev_private;
9150 int max_pixclk = intel_mode_max_pixclk(state);
9151 int req_cdclk;
9152
9153 /* see the comment in valleyview_modeset_global_resources */
9154 if (WARN_ON(max_pixclk < 0))
9155 return;
9156
9157 req_cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
9158
9159 if (req_cdclk != dev_priv->cdclk_freq)
9160 broxton_set_cdclk(dev, req_cdclk);
9161}
9162
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009163static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9164 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009165{
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009166 if (!intel_ddi_pll_select(crtc, crtc_state))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009167 return -EINVAL;
Daniel Vetter716c2e52014-06-25 22:02:02 +03009168
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009169 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009170
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009171 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009172}
9173
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309174static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9175 enum port port,
9176 struct intel_crtc_state *pipe_config)
9177{
9178 switch (port) {
9179 case PORT_A:
9180 pipe_config->ddi_pll_sel = SKL_DPLL0;
9181 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9182 break;
9183 case PORT_B:
9184 pipe_config->ddi_pll_sel = SKL_DPLL1;
9185 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9186 break;
9187 case PORT_C:
9188 pipe_config->ddi_pll_sel = SKL_DPLL2;
9189 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9190 break;
9191 default:
9192 DRM_ERROR("Incorrect port type\n");
9193 }
9194}
9195
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009196static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9197 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009198 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009199{
Damien Lespiau3148ade2014-11-21 16:14:56 +00009200 u32 temp, dpll_ctl1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009201
9202 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9203 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9204
9205 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009206 case SKL_DPLL0:
9207 /*
9208 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9209 * of the shared DPLL framework and thus needs to be read out
9210 * separately
9211 */
9212 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9213 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9214 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009215 case SKL_DPLL1:
9216 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9217 break;
9218 case SKL_DPLL2:
9219 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9220 break;
9221 case SKL_DPLL3:
9222 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9223 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009224 }
9225}
9226
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009227static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9228 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009229 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009230{
9231 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9232
9233 switch (pipe_config->ddi_pll_sel) {
9234 case PORT_CLK_SEL_WRPLL1:
9235 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9236 break;
9237 case PORT_CLK_SEL_WRPLL2:
9238 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9239 break;
9240 }
9241}
9242
Daniel Vetter26804af2014-06-25 22:01:55 +03009243static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009244 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009245{
9246 struct drm_device *dev = crtc->base.dev;
9247 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009248 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009249 enum port port;
9250 uint32_t tmp;
9251
9252 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9253
9254 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9255
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009256 if (IS_SKYLAKE(dev))
9257 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309258 else if (IS_BROXTON(dev))
9259 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009260 else
9261 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009262
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009263 if (pipe_config->shared_dpll >= 0) {
9264 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9265
9266 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9267 &pipe_config->dpll_hw_state));
9268 }
9269
Daniel Vetter26804af2014-06-25 22:01:55 +03009270 /*
9271 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9272 * DDI E. So just check whether this pipe is wired to DDI E and whether
9273 * the PCH transcoder is on.
9274 */
Damien Lespiauca370452013-12-03 13:56:24 +00009275 if (INTEL_INFO(dev)->gen < 9 &&
9276 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009277 pipe_config->has_pch_encoder = true;
9278
9279 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9280 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9281 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9282
9283 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9284 }
9285}
9286
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009287static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009288 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009289{
9290 struct drm_device *dev = crtc->base.dev;
9291 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009292 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009293 uint32_t tmp;
9294
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009295 if (!intel_display_power_is_enabled(dev_priv,
Imre Deakb5482bd2014-03-05 16:20:55 +02009296 POWER_DOMAIN_PIPE(crtc->pipe)))
9297 return false;
9298
Daniel Vettere143a212013-07-04 12:01:15 +02009299 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009300 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9301
Daniel Vettereccb1402013-05-22 00:50:22 +02009302 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9303 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9304 enum pipe trans_edp_pipe;
9305 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9306 default:
9307 WARN(1, "unknown pipe linked to edp transcoder\n");
9308 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9309 case TRANS_DDI_EDP_INPUT_A_ON:
9310 trans_edp_pipe = PIPE_A;
9311 break;
9312 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9313 trans_edp_pipe = PIPE_B;
9314 break;
9315 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9316 trans_edp_pipe = PIPE_C;
9317 break;
9318 }
9319
9320 if (trans_edp_pipe == crtc->pipe)
9321 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9322 }
9323
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009324 if (!intel_display_power_is_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02009325 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03009326 return false;
9327
Daniel Vettereccb1402013-05-22 00:50:22 +02009328 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009329 if (!(tmp & PIPECONF_ENABLE))
9330 return false;
9331
Daniel Vetter26804af2014-06-25 22:01:55 +03009332 haswell_get_ddi_port_state(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009333
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009334 intel_get_pipe_timings(crtc, pipe_config);
9335
Chandra Kondurua1b22782015-04-07 15:28:45 -07009336 if (INTEL_INFO(dev)->gen >= 9) {
9337 skl_init_scalers(dev, crtc, pipe_config);
9338 }
9339
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009340 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009341 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009342 if (INTEL_INFO(dev)->gen == 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009343 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009344 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009345 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009346 else
9347 MISSING_CASE(INTEL_INFO(dev)->gen);
9348
Chandra Kondurua1b22782015-04-07 15:28:45 -07009349 } else {
9350 pipe_config->scaler_state.scaler_id = -1;
9351 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009352 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01009353
Jesse Barnese59150d2014-01-07 13:30:45 -08009354 if (IS_HASWELL(dev))
9355 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9356 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009357
Clint Taylorebb69c92014-09-30 10:30:22 -07009358 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
9359 pipe_config->pixel_multiplier =
9360 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9361 } else {
9362 pipe_config->pixel_multiplier = 1;
9363 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009364
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009365 return true;
9366}
9367
Chris Wilson560b85b2010-08-07 11:01:38 +01009368static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
9369{
9370 struct drm_device *dev = crtc->dev;
9371 struct drm_i915_private *dev_priv = dev->dev_private;
9372 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +03009373 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01009374
Ville Syrjälädc41c152014-08-13 11:57:05 +03009375 if (base) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009376 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
9377 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009378 unsigned int stride = roundup_pow_of_two(width) * 4;
9379
9380 switch (stride) {
9381 default:
9382 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9383 width, stride);
9384 stride = 256;
9385 /* fallthrough */
9386 case 256:
9387 case 512:
9388 case 1024:
9389 case 2048:
9390 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009391 }
9392
Ville Syrjälädc41c152014-08-13 11:57:05 +03009393 cntl |= CURSOR_ENABLE |
9394 CURSOR_GAMMA_ENABLE |
9395 CURSOR_FORMAT_ARGB |
9396 CURSOR_STRIDE(stride);
9397
9398 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009399 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009400
Ville Syrjälädc41c152014-08-13 11:57:05 +03009401 if (intel_crtc->cursor_cntl != 0 &&
9402 (intel_crtc->cursor_base != base ||
9403 intel_crtc->cursor_size != size ||
9404 intel_crtc->cursor_cntl != cntl)) {
9405 /* On these chipsets we can only modify the base/size/stride
9406 * whilst the cursor is disabled.
9407 */
9408 I915_WRITE(_CURACNTR, 0);
9409 POSTING_READ(_CURACNTR);
9410 intel_crtc->cursor_cntl = 0;
9411 }
9412
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009413 if (intel_crtc->cursor_base != base) {
Ville Syrjälädc41c152014-08-13 11:57:05 +03009414 I915_WRITE(_CURABASE, base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009415 intel_crtc->cursor_base = base;
9416 }
Ville Syrjälädc41c152014-08-13 11:57:05 +03009417
9418 if (intel_crtc->cursor_size != size) {
9419 I915_WRITE(CURSIZE, size);
9420 intel_crtc->cursor_size = size;
9421 }
9422
Chris Wilson4b0e3332014-05-30 16:35:26 +03009423 if (intel_crtc->cursor_cntl != cntl) {
9424 I915_WRITE(_CURACNTR, cntl);
9425 POSTING_READ(_CURACNTR);
9426 intel_crtc->cursor_cntl = cntl;
9427 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009428}
9429
9430static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
9431{
9432 struct drm_device *dev = crtc->dev;
9433 struct drm_i915_private *dev_priv = dev->dev_private;
9434 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9435 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009436 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +01009437
Chris Wilson4b0e3332014-05-30 16:35:26 +03009438 cntl = 0;
9439 if (base) {
9440 cntl = MCURSOR_GAMMA_ENABLE;
Matt Roper3dd512f2015-02-27 10:12:00 -08009441 switch (intel_crtc->base.cursor->state->crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309442 case 64:
9443 cntl |= CURSOR_MODE_64_ARGB_AX;
9444 break;
9445 case 128:
9446 cntl |= CURSOR_MODE_128_ARGB_AX;
9447 break;
9448 case 256:
9449 cntl |= CURSOR_MODE_256_ARGB_AX;
9450 break;
9451 default:
Matt Roper3dd512f2015-02-27 10:12:00 -08009452 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309453 return;
Chris Wilson560b85b2010-08-07 11:01:38 +01009454 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009455 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +03009456
9457 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
9458 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson560b85b2010-08-07 11:01:38 +01009459 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009460
Matt Roper8e7d6882015-01-21 16:35:41 -08009461 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009462 cntl |= CURSOR_ROTATE_180;
9463
Chris Wilson4b0e3332014-05-30 16:35:26 +03009464 if (intel_crtc->cursor_cntl != cntl) {
9465 I915_WRITE(CURCNTR(pipe), cntl);
9466 POSTING_READ(CURCNTR(pipe));
9467 intel_crtc->cursor_cntl = cntl;
9468 }
9469
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009470 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009471 I915_WRITE(CURBASE(pipe), base);
9472 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009473
9474 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009475}
9476
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009477/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01009478static void intel_crtc_update_cursor(struct drm_crtc *crtc,
9479 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009480{
9481 struct drm_device *dev = crtc->dev;
9482 struct drm_i915_private *dev_priv = dev->dev_private;
9483 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9484 int pipe = intel_crtc->pipe;
Matt Roper3d7d6512014-06-10 08:28:13 -07009485 int x = crtc->cursor_x;
9486 int y = crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009487 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009488
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009489 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009490 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009491
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009492 if (x >= intel_crtc->config->pipe_src_w)
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009493 base = 0;
9494
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009495 if (y >= intel_crtc->config->pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009496 base = 0;
9497
9498 if (x < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009499 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009500 base = 0;
9501
9502 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
9503 x = -x;
9504 }
9505 pos |= x << CURSOR_X_SHIFT;
9506
9507 if (y < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009508 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009509 base = 0;
9510
9511 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
9512 y = -y;
9513 }
9514 pos |= y << CURSOR_Y_SHIFT;
9515
Chris Wilson4b0e3332014-05-30 16:35:26 +03009516 if (base == 0 && intel_crtc->cursor_base == 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009517 return;
9518
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009519 I915_WRITE(CURPOS(pipe), pos);
9520
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009521 /* ILK+ do this automagically */
9522 if (HAS_GMCH_DISPLAY(dev) &&
Matt Roper8e7d6882015-01-21 16:35:41 -08009523 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009524 base += (intel_crtc->base.cursor->state->crtc_h *
9525 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009526 }
9527
Ville Syrjälä8ac54662014-08-12 19:39:54 +03009528 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009529 i845_update_cursor(crtc, base);
9530 else
9531 i9xx_update_cursor(crtc, base);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009532}
9533
Ville Syrjälädc41c152014-08-13 11:57:05 +03009534static bool cursor_size_ok(struct drm_device *dev,
9535 uint32_t width, uint32_t height)
9536{
9537 if (width == 0 || height == 0)
9538 return false;
9539
9540 /*
9541 * 845g/865g are special in that they are only limited by
9542 * the width of their cursors, the height is arbitrary up to
9543 * the precision of the register. Everything else requires
9544 * square cursors, limited to a few power-of-two sizes.
9545 */
9546 if (IS_845G(dev) || IS_I865G(dev)) {
9547 if ((width & 63) != 0)
9548 return false;
9549
9550 if (width > (IS_845G(dev) ? 64 : 512))
9551 return false;
9552
9553 if (height > 1023)
9554 return false;
9555 } else {
9556 switch (width | height) {
9557 case 256:
9558 case 128:
9559 if (IS_GEN2(dev))
9560 return false;
9561 case 64:
9562 break;
9563 default:
9564 return false;
9565 }
9566 }
9567
9568 return true;
9569}
9570
Jesse Barnes79e53942008-11-07 14:24:08 -08009571static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01009572 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08009573{
James Simmons72034252010-08-03 01:33:19 +01009574 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08009575 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08009576
James Simmons72034252010-08-03 01:33:19 +01009577 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009578 intel_crtc->lut_r[i] = red[i] >> 8;
9579 intel_crtc->lut_g[i] = green[i] >> 8;
9580 intel_crtc->lut_b[i] = blue[i] >> 8;
9581 }
9582
9583 intel_crtc_load_lut(crtc);
9584}
9585
Jesse Barnes79e53942008-11-07 14:24:08 -08009586/* VESA 640x480x72Hz mode to set on the pipe */
9587static struct drm_display_mode load_detect_mode = {
9588 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
9589 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
9590};
9591
Daniel Vettera8bb6812014-02-10 18:00:39 +01009592struct drm_framebuffer *
9593__intel_framebuffer_create(struct drm_device *dev,
9594 struct drm_mode_fb_cmd2 *mode_cmd,
9595 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +01009596{
9597 struct intel_framebuffer *intel_fb;
9598 int ret;
9599
9600 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
9601 if (!intel_fb) {
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +03009602 drm_gem_object_unreference(&obj->base);
Chris Wilsond2dff872011-04-19 08:36:26 +01009603 return ERR_PTR(-ENOMEM);
9604 }
9605
9606 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009607 if (ret)
9608 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +01009609
9610 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009611err:
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +03009612 drm_gem_object_unreference(&obj->base);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009613 kfree(intel_fb);
9614
9615 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +01009616}
9617
Daniel Vetterb5ea6422014-03-02 21:18:00 +01009618static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +01009619intel_framebuffer_create(struct drm_device *dev,
9620 struct drm_mode_fb_cmd2 *mode_cmd,
9621 struct drm_i915_gem_object *obj)
9622{
9623 struct drm_framebuffer *fb;
9624 int ret;
9625
9626 ret = i915_mutex_lock_interruptible(dev);
9627 if (ret)
9628 return ERR_PTR(ret);
9629 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
9630 mutex_unlock(&dev->struct_mutex);
9631
9632 return fb;
9633}
9634
Chris Wilsond2dff872011-04-19 08:36:26 +01009635static u32
9636intel_framebuffer_pitch_for_width(int width, int bpp)
9637{
9638 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
9639 return ALIGN(pitch, 64);
9640}
9641
9642static u32
9643intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
9644{
9645 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +02009646 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +01009647}
9648
9649static struct drm_framebuffer *
9650intel_framebuffer_create_for_mode(struct drm_device *dev,
9651 struct drm_display_mode *mode,
9652 int depth, int bpp)
9653{
9654 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00009655 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01009656
9657 obj = i915_gem_alloc_object(dev,
9658 intel_framebuffer_size_for_mode(mode, bpp));
9659 if (obj == NULL)
9660 return ERR_PTR(-ENOMEM);
9661
9662 mode_cmd.width = mode->hdisplay;
9663 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009664 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
9665 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00009666 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01009667
9668 return intel_framebuffer_create(dev, &mode_cmd, obj);
9669}
9670
9671static struct drm_framebuffer *
9672mode_fits_in_fbdev(struct drm_device *dev,
9673 struct drm_display_mode *mode)
9674{
Daniel Vetter4520f532013-10-09 09:18:51 +02009675#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +01009676 struct drm_i915_private *dev_priv = dev->dev_private;
9677 struct drm_i915_gem_object *obj;
9678 struct drm_framebuffer *fb;
9679
Daniel Vetter4c0e5522014-02-14 16:35:54 +01009680 if (!dev_priv->fbdev)
9681 return NULL;
9682
9683 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +01009684 return NULL;
9685
Jesse Barnes8bcd4552014-02-07 12:10:38 -08009686 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +01009687 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +01009688
Jesse Barnes8bcd4552014-02-07 12:10:38 -08009689 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009690 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
9691 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01009692 return NULL;
9693
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009694 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01009695 return NULL;
9696
9697 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +02009698#else
9699 return NULL;
9700#endif
Chris Wilsond2dff872011-04-19 08:36:26 +01009701}
9702
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009703bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01009704 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -05009705 struct intel_load_detect_pipe *old,
9706 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08009707{
9708 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009709 struct intel_encoder *intel_encoder =
9710 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08009711 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01009712 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08009713 struct drm_crtc *crtc = NULL;
9714 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02009715 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -05009716 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009717 struct drm_atomic_state *state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009718 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +03009719 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -05009720 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -08009721
Chris Wilsond2dff872011-04-19 08:36:26 +01009722 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03009723 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03009724 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01009725
Rob Clark51fd3712013-11-19 12:10:12 -05009726retry:
9727 ret = drm_modeset_lock(&config->connection_mutex, ctx);
9728 if (ret)
9729 goto fail_unlock;
Daniel Vetter6e9f7982014-05-29 23:54:47 +02009730
Jesse Barnes79e53942008-11-07 14:24:08 -08009731 /*
9732 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01009733 *
Jesse Barnes79e53942008-11-07 14:24:08 -08009734 * - if the connector already has an assigned crtc, use it (but make
9735 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01009736 *
Jesse Barnes79e53942008-11-07 14:24:08 -08009737 * - try to find the first unused crtc that can drive this connector,
9738 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08009739 */
9740
9741 /* See if we already have a CRTC for this connector */
9742 if (encoder->crtc) {
9743 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01009744
Rob Clark51fd3712013-11-19 12:10:12 -05009745 ret = drm_modeset_lock(&crtc->mutex, ctx);
9746 if (ret)
9747 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01009748 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
9749 if (ret)
9750 goto fail_unlock;
Daniel Vetter7b240562012-12-12 00:35:33 +01009751
Daniel Vetter24218aa2012-08-12 19:27:11 +02009752 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01009753 old->load_detect_temp = false;
9754
9755 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02009756 if (connector->dpms != DRM_MODE_DPMS_ON)
9757 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01009758
Chris Wilson71731882011-04-19 23:10:58 +01009759 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08009760 }
9761
9762 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01009763 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009764 i++;
9765 if (!(encoder->possible_crtcs & (1 << i)))
9766 continue;
Matt Roper83d65732015-02-25 13:12:16 -08009767 if (possible_crtc->state->enable)
Ville Syrjäläa4592492014-08-11 13:15:36 +03009768 continue;
9769 /* This can occur when applying the pipe A quirk on resume. */
9770 if (to_intel_crtc(possible_crtc)->new_enabled)
9771 continue;
9772
9773 crtc = possible_crtc;
9774 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08009775 }
9776
9777 /*
9778 * If we didn't find an unused CRTC, don't use any.
9779 */
9780 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01009781 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Rob Clark51fd3712013-11-19 12:10:12 -05009782 goto fail_unlock;
Jesse Barnes79e53942008-11-07 14:24:08 -08009783 }
9784
Rob Clark51fd3712013-11-19 12:10:12 -05009785 ret = drm_modeset_lock(&crtc->mutex, ctx);
9786 if (ret)
9787 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01009788 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
9789 if (ret)
9790 goto fail_unlock;
Daniel Vetterfc303102012-07-09 10:40:58 +02009791 intel_encoder->new_crtc = to_intel_crtc(crtc);
9792 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08009793
9794 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009795 intel_crtc->new_enabled = true;
Daniel Vetter24218aa2012-08-12 19:27:11 +02009796 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01009797 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01009798 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08009799
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009800 state = drm_atomic_state_alloc(dev);
9801 if (!state)
9802 return false;
9803
9804 state->acquire_ctx = ctx;
9805
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009806 connector_state = drm_atomic_get_connector_state(state, connector);
9807 if (IS_ERR(connector_state)) {
9808 ret = PTR_ERR(connector_state);
9809 goto fail;
9810 }
9811
9812 connector_state->crtc = crtc;
9813 connector_state->best_encoder = &intel_encoder->base;
9814
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +03009815 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
9816 if (IS_ERR(crtc_state)) {
9817 ret = PTR_ERR(crtc_state);
9818 goto fail;
9819 }
9820
9821 crtc_state->base.enable = true;
9822
Chris Wilson64927112011-04-20 07:25:26 +01009823 if (!mode)
9824 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08009825
Chris Wilsond2dff872011-04-19 08:36:26 +01009826 /* We need a framebuffer large enough to accommodate all accesses
9827 * that the plane may generate whilst we perform load detection.
9828 * We can not rely on the fbcon either being present (we get called
9829 * during its initialisation to detect all boot displays, or it may
9830 * not even exist) or that it is large enough to satisfy the
9831 * requested mode.
9832 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02009833 fb = mode_fits_in_fbdev(dev, mode);
9834 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01009835 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02009836 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
9837 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01009838 } else
9839 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02009840 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01009841 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009842 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08009843 }
Chris Wilsond2dff872011-04-19 08:36:26 +01009844
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009845 if (intel_set_mode(crtc, mode, 0, 0, fb, state)) {
Chris Wilson64927112011-04-20 07:25:26 +01009846 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01009847 if (old->release_fb)
9848 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009849 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08009850 }
Daniel Vetter9128b042015-03-03 17:31:21 +01009851 crtc->primary->crtc = crtc;
Chris Wilson71731882011-04-19 23:10:58 +01009852
Jesse Barnes79e53942008-11-07 14:24:08 -08009853 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07009854 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01009855 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009856
9857 fail:
Matt Roper83d65732015-02-25 13:12:16 -08009858 intel_crtc->new_enabled = crtc->state->enable;
Rob Clark51fd3712013-11-19 12:10:12 -05009859fail_unlock:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +03009860 drm_atomic_state_free(state);
9861 state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009862
Rob Clark51fd3712013-11-19 12:10:12 -05009863 if (ret == -EDEADLK) {
9864 drm_modeset_backoff(ctx);
9865 goto retry;
9866 }
9867
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009868 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08009869}
9870
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009871void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +02009872 struct intel_load_detect_pipe *old,
9873 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08009874{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009875 struct drm_device *dev = connector->dev;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009876 struct intel_encoder *intel_encoder =
9877 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01009878 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01009879 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009880 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009881 struct drm_atomic_state *state;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009882 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +03009883 struct intel_crtc_state *crtc_state;
Jesse Barnes79e53942008-11-07 14:24:08 -08009884
Chris Wilsond2dff872011-04-19 08:36:26 +01009885 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03009886 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03009887 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01009888
Chris Wilson8261b192011-04-19 23:18:09 +01009889 if (old->load_detect_temp) {
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009890 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009891 if (!state)
9892 goto fail;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009893
9894 state->acquire_ctx = ctx;
9895
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009896 connector_state = drm_atomic_get_connector_state(state, connector);
9897 if (IS_ERR(connector_state))
9898 goto fail;
9899
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +03009900 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
9901 if (IS_ERR(crtc_state))
9902 goto fail;
9903
Daniel Vetterfc303102012-07-09 10:40:58 +02009904 to_intel_connector(connector)->new_encoder = NULL;
9905 intel_encoder->new_crtc = NULL;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009906 intel_crtc->new_enabled = false;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009907
9908 connector_state->best_encoder = NULL;
9909 connector_state->crtc = NULL;
9910
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +03009911 crtc_state->base.enable = false;
9912
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009913 intel_set_mode(crtc, NULL, 0, 0, NULL, state);
9914
9915 drm_atomic_state_free(state);
Chris Wilsond2dff872011-04-19 08:36:26 +01009916
Daniel Vetter36206362012-12-10 20:42:17 +01009917 if (old->release_fb) {
9918 drm_framebuffer_unregister_private(old->release_fb);
9919 drm_framebuffer_unreference(old->release_fb);
9920 }
Chris Wilsond2dff872011-04-19 08:36:26 +01009921
Chris Wilson0622a532011-04-21 09:32:11 +01009922 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08009923 }
9924
Eric Anholtc751ce42010-03-25 11:48:48 -07009925 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02009926 if (old->dpms_mode != DRM_MODE_DPMS_ON)
9927 connector->funcs->dpms(connector, old->dpms_mode);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009928
9929 return;
9930fail:
9931 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
9932 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -08009933}
9934
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009935static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009936 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009937{
9938 struct drm_i915_private *dev_priv = dev->dev_private;
9939 u32 dpll = pipe_config->dpll_hw_state.dpll;
9940
9941 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +02009942 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009943 else if (HAS_PCH_SPLIT(dev))
9944 return 120000;
9945 else if (!IS_GEN2(dev))
9946 return 96000;
9947 else
9948 return 48000;
9949}
9950
Jesse Barnes79e53942008-11-07 14:24:08 -08009951/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009952static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009953 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08009954{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009955 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08009956 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009957 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +03009958 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08009959 u32 fp;
9960 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009961 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -08009962
9963 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +03009964 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009965 else
Ville Syrjälä293623f2013-09-13 16:18:46 +03009966 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -08009967
9968 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05009969 if (IS_PINEVIEW(dev)) {
9970 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
9971 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08009972 } else {
9973 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
9974 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
9975 }
9976
Chris Wilsona6c45cf2010-09-17 00:32:17 +01009977 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05009978 if (IS_PINEVIEW(dev))
9979 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
9980 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08009981 else
9982 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08009983 DPLL_FPA01_P1_POST_DIV_SHIFT);
9984
9985 switch (dpll & DPLL_MODE_MASK) {
9986 case DPLLB_MODE_DAC_SERIAL:
9987 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
9988 5 : 10;
9989 break;
9990 case DPLLB_MODE_LVDS:
9991 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
9992 7 : 14;
9993 break;
9994 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08009995 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08009996 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009997 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08009998 }
9999
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010000 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010001 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010002 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010003 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010004 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010005 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010006 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010007
10008 if (is_lvds) {
10009 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10010 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010011
10012 if (lvds & LVDS_CLKB_POWER_UP)
10013 clock.p2 = 7;
10014 else
10015 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010016 } else {
10017 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10018 clock.p1 = 2;
10019 else {
10020 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10021 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10022 }
10023 if (dpll & PLL_P2_DIVIDE_BY_4)
10024 clock.p2 = 4;
10025 else
10026 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010027 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010028
10029 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010030 }
10031
Ville Syrjälä18442d02013-09-13 16:00:08 +030010032 /*
10033 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010034 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010035 * encoder's get_config() function.
10036 */
10037 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010038}
10039
Ville Syrjälä6878da02013-09-13 15:59:11 +030010040int intel_dotclock_calculate(int link_freq,
10041 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010042{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010043 /*
10044 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010045 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010046 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010047 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010048 *
10049 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010050 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010051 */
10052
Ville Syrjälä6878da02013-09-13 15:59:11 +030010053 if (!m_n->link_n)
10054 return 0;
10055
10056 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10057}
10058
Ville Syrjälä18442d02013-09-13 16:00:08 +030010059static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010060 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010061{
10062 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +030010063
10064 /* read out port_clock from the DPLL */
10065 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010066
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010067 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +030010068 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +010010069 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +030010070 * agree once we know their relationship in the encoder's
10071 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010072 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010073 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +030010074 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10075 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010076}
10077
10078/** Returns the currently programmed mode of the given pipe. */
10079struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10080 struct drm_crtc *crtc)
10081{
Jesse Barnes548f2452011-02-17 10:40:53 -080010082 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010083 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010084 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010085 struct drm_display_mode *mode;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010086 struct intel_crtc_state pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010087 int htot = I915_READ(HTOTAL(cpu_transcoder));
10088 int hsync = I915_READ(HSYNC(cpu_transcoder));
10089 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10090 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010091 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010092
10093 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10094 if (!mode)
10095 return NULL;
10096
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010097 /*
10098 * Construct a pipe_config sufficient for getting the clock info
10099 * back out of crtc_clock_get.
10100 *
10101 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10102 * to use a real value here instead.
10103 */
Ville Syrjälä293623f2013-09-13 16:18:46 +030010104 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010105 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010106 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10107 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10108 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010109 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10110
Ville Syrjälä773ae032013-09-23 17:48:20 +030010111 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010112 mode->hdisplay = (htot & 0xffff) + 1;
10113 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10114 mode->hsync_start = (hsync & 0xffff) + 1;
10115 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10116 mode->vdisplay = (vtot & 0xffff) + 1;
10117 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10118 mode->vsync_start = (vsync & 0xffff) + 1;
10119 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10120
10121 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010122
10123 return mode;
10124}
10125
Jesse Barnes652c3932009-08-17 13:31:43 -070010126static void intel_decrease_pllclock(struct drm_crtc *crtc)
10127{
10128 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030010129 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -070010130 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -070010131
Sonika Jindalbaff2962014-07-22 11:16:35 +053010132 if (!HAS_GMCH_DISPLAY(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -070010133 return;
10134
10135 if (!dev_priv->lvds_downclock_avail)
10136 return;
10137
10138 /*
10139 * Since this is called by a timer, we should never get here in
10140 * the manual case.
10141 */
10142 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +010010143 int pipe = intel_crtc->pipe;
10144 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +020010145 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +010010146
Zhao Yakui44d98a62009-10-09 11:39:40 +080010147 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -070010148
Sean Paul8ac5a6d2012-02-13 13:14:51 -050010149 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -070010150
Chris Wilson074b5e12012-05-02 12:07:06 +010010151 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -070010152 dpll |= DISPLAY_RATE_SELECT_FPA1;
10153 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010154 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -070010155 dpll = I915_READ(dpll_reg);
10156 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +080010157 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -070010158 }
10159
10160}
10161
Chris Wilsonf047e392012-07-21 12:31:41 +010010162void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010163{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010164 struct drm_i915_private *dev_priv = dev->dev_private;
10165
Chris Wilsonf62a0072014-02-21 17:55:39 +000010166 if (dev_priv->mm.busy)
10167 return;
10168
Paulo Zanoni43694d62014-03-07 20:08:08 -030010169 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010170 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010171 if (INTEL_INFO(dev)->gen >= 6)
10172 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010173 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010174}
10175
10176void intel_mark_idle(struct drm_device *dev)
10177{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010178 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010179 struct drm_crtc *crtc;
10180
Chris Wilsonf62a0072014-02-21 17:55:39 +000010181 if (!dev_priv->mm.busy)
10182 return;
10183
10184 dev_priv->mm.busy = false;
10185
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010186 for_each_crtc(dev, crtc) {
Matt Roperf4510a22014-04-01 15:22:40 -070010187 if (!crtc->primary->fb)
Chris Wilson725a5b52013-01-08 11:02:57 +000010188 continue;
10189
10190 intel_decrease_pllclock(crtc);
10191 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010192
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010193 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010194 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010195
Paulo Zanoni43694d62014-03-07 20:08:08 -030010196 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010197}
10198
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020010199static void intel_crtc_set_state(struct intel_crtc *crtc,
10200 struct intel_crtc_state *crtc_state)
10201{
10202 kfree(crtc->config);
10203 crtc->config = crtc_state;
Ander Conselvan de Oliveira16f3f652015-01-15 14:55:27 +020010204 crtc->base.state = &crtc_state->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020010205}
10206
Jesse Barnes79e53942008-11-07 14:24:08 -080010207static void intel_crtc_destroy(struct drm_crtc *crtc)
10208{
10209 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010210 struct drm_device *dev = crtc->dev;
10211 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010212
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010213 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010214 work = intel_crtc->unpin_work;
10215 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010216 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010217
10218 if (work) {
10219 cancel_work_sync(&work->work);
10220 kfree(work);
10221 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010222
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020010223 intel_crtc_set_state(intel_crtc, NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -080010224 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010225
Jesse Barnes79e53942008-11-07 14:24:08 -080010226 kfree(intel_crtc);
10227}
10228
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010229static void intel_unpin_work_fn(struct work_struct *__work)
10230{
10231 struct intel_unpin_work *work =
10232 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010233 struct drm_device *dev = work->crtc->dev;
Daniel Vetterf99d7062014-06-19 16:01:59 +020010234 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010235
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010236 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000010237 intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
Chris Wilson05394f32010-11-08 19:18:58 +000010238 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010239
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020010240 intel_fbc_update(dev);
John Harrisonf06cc1b2014-11-24 18:49:37 +000010241
10242 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010243 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010244 mutex_unlock(&dev->struct_mutex);
10245
Daniel Vetterf99d7062014-06-19 16:01:59 +020010246 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Chris Wilson89ed88b2015-02-16 14:31:49 +000010247 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010248
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010249 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
10250 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
10251
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010252 kfree(work);
10253}
10254
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010255static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010010256 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010257{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010258 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10259 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010260 unsigned long flags;
10261
10262 /* Ignore early vblank irqs */
10263 if (intel_crtc == NULL)
10264 return;
10265
Daniel Vetterf3260382014-09-15 14:55:23 +020010266 /*
10267 * This is called both by irq handlers and the reset code (to complete
10268 * lost pageflips) so needs the full irqsave spinlocks.
10269 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010270 spin_lock_irqsave(&dev->event_lock, flags);
10271 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000010272
10273 /* Ensure we don't miss a work->pending update ... */
10274 smp_rmb();
10275
10276 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010277 spin_unlock_irqrestore(&dev->event_lock, flags);
10278 return;
10279 }
10280
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010281 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010010282
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010283 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010284}
10285
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010286void intel_finish_page_flip(struct drm_device *dev, int pipe)
10287{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010288 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010289 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10290
Mario Kleiner49b14a52010-12-09 07:00:07 +010010291 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010292}
10293
10294void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10295{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010296 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010297 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10298
Mario Kleiner49b14a52010-12-09 07:00:07 +010010299 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010300}
10301
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010302/* Is 'a' after or equal to 'b'? */
10303static bool g4x_flip_count_after_eq(u32 a, u32 b)
10304{
10305 return !((a - b) & 0x80000000);
10306}
10307
10308static bool page_flip_finished(struct intel_crtc *crtc)
10309{
10310 struct drm_device *dev = crtc->base.dev;
10311 struct drm_i915_private *dev_priv = dev->dev_private;
10312
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030010313 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10314 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10315 return true;
10316
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010317 /*
10318 * The relevant registers doen't exist on pre-ctg.
10319 * As the flip done interrupt doesn't trigger for mmio
10320 * flips on gmch platforms, a flip count check isn't
10321 * really needed there. But since ctg has the registers,
10322 * include it in the check anyway.
10323 */
10324 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10325 return true;
10326
10327 /*
10328 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10329 * used the same base address. In that case the mmio flip might
10330 * have completed, but the CS hasn't even executed the flip yet.
10331 *
10332 * A flip count check isn't enough as the CS might have updated
10333 * the base address just after start of vblank, but before we
10334 * managed to process the interrupt. This means we'd complete the
10335 * CS flip too soon.
10336 *
10337 * Combining both checks should get us a good enough result. It may
10338 * still happen that the CS flip has been executed, but has not
10339 * yet actually completed. But in case the base address is the same
10340 * anyway, we don't really care.
10341 */
10342 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10343 crtc->unpin_work->gtt_offset &&
10344 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
10345 crtc->unpin_work->flip_count);
10346}
10347
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010348void intel_prepare_page_flip(struct drm_device *dev, int plane)
10349{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010350 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010351 struct intel_crtc *intel_crtc =
10352 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10353 unsigned long flags;
10354
Daniel Vetterf3260382014-09-15 14:55:23 +020010355
10356 /*
10357 * This is called both by irq handlers and the reset code (to complete
10358 * lost pageflips) so needs the full irqsave spinlocks.
10359 *
10360 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000010361 * generate a page-flip completion irq, i.e. every modeset
10362 * is also accompanied by a spurious intel_prepare_page_flip().
10363 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010364 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010365 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000010366 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010367 spin_unlock_irqrestore(&dev->event_lock, flags);
10368}
10369
Robin Schroereba905b2014-05-18 02:24:50 +020010370static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
Chris Wilsone7d841c2012-12-03 11:36:30 +000010371{
10372 /* Ensure that the work item is consistent when activating it ... */
10373 smp_wmb();
10374 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
10375 /* and that it is marked active as soon as the irq could fire. */
10376 smp_wmb();
10377}
10378
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010379static int intel_gen2_queue_flip(struct drm_device *dev,
10380 struct drm_crtc *crtc,
10381 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010382 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010383 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010384 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010385{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010386 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010387 u32 flip_mask;
10388 int ret;
10389
Daniel Vetter6d90c952012-04-26 23:28:05 +020010390 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010391 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010392 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010393
10394 /* Can't queue multiple flips, so wait for the previous
10395 * one to finish before executing the next.
10396 */
10397 if (intel_crtc->plane)
10398 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10399 else
10400 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010401 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10402 intel_ring_emit(ring, MI_NOOP);
10403 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10404 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10405 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010406 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010407 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000010408
10409 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010410 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010411 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010412}
10413
10414static int intel_gen3_queue_flip(struct drm_device *dev,
10415 struct drm_crtc *crtc,
10416 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010417 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010418 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010419 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010420{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010421 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010422 u32 flip_mask;
10423 int ret;
10424
Daniel Vetter6d90c952012-04-26 23:28:05 +020010425 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010426 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010427 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010428
10429 if (intel_crtc->plane)
10430 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10431 else
10432 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010433 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10434 intel_ring_emit(ring, MI_NOOP);
10435 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
10436 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10437 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010438 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010439 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010440
Chris Wilsone7d841c2012-12-03 11:36:30 +000010441 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010442 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010443 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010444}
10445
10446static int intel_gen4_queue_flip(struct drm_device *dev,
10447 struct drm_crtc *crtc,
10448 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010449 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010450 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010451 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010452{
10453 struct drm_i915_private *dev_priv = dev->dev_private;
10454 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10455 uint32_t pf, pipesrc;
10456 int ret;
10457
Daniel Vetter6d90c952012-04-26 23:28:05 +020010458 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010459 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010460 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010461
10462 /* i965+ uses the linear or tiled offsets from the
10463 * Display Registers (which do not change across a page-flip)
10464 * so we need only reprogram the base address.
10465 */
Daniel Vetter6d90c952012-04-26 23:28:05 +020010466 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10467 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10468 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010469 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020010470 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010471
10472 /* XXX Enabling the panel-fitter across page-flip is so far
10473 * untested on non-native modes, so ignore it for now.
10474 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
10475 */
10476 pf = 0;
10477 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010478 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010479
10480 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010481 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010482 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010483}
10484
10485static int intel_gen6_queue_flip(struct drm_device *dev,
10486 struct drm_crtc *crtc,
10487 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010488 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010489 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010490 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010491{
10492 struct drm_i915_private *dev_priv = dev->dev_private;
10493 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10494 uint32_t pf, pipesrc;
10495 int ret;
10496
Daniel Vetter6d90c952012-04-26 23:28:05 +020010497 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010498 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010499 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010500
Daniel Vetter6d90c952012-04-26 23:28:05 +020010501 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10502 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10503 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010504 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010505
Chris Wilson99d9acd2012-04-17 20:37:00 +010010506 /* Contrary to the suggestions in the documentation,
10507 * "Enable Panel Fitter" does not seem to be required when page
10508 * flipping with a non-native mode, and worse causes a normal
10509 * modeset to fail.
10510 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
10511 */
10512 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010513 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010514 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010515
10516 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010517 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010518 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010519}
10520
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010521static int intel_gen7_queue_flip(struct drm_device *dev,
10522 struct drm_crtc *crtc,
10523 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010524 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010525 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010526 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010527{
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010528 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010529 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010010530 int len, ret;
10531
Robin Schroereba905b2014-05-18 02:24:50 +020010532 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010533 case PLANE_A:
10534 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
10535 break;
10536 case PLANE_B:
10537 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
10538 break;
10539 case PLANE_C:
10540 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
10541 break;
10542 default:
10543 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010544 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010545 }
10546
Chris Wilsonffe74d72013-08-26 20:58:12 +010010547 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +010010548 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010010549 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010010550 /*
10551 * On Gen 8, SRM is now taking an extra dword to accommodate
10552 * 48bits addresses, and we need a NOOP for the batch size to
10553 * stay even.
10554 */
10555 if (IS_GEN8(dev))
10556 len += 2;
10557 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010010558
Ville Syrjäläf66fab82014-02-11 19:52:06 +020010559 /*
10560 * BSpec MI_DISPLAY_FLIP for IVB:
10561 * "The full packet must be contained within the same cache line."
10562 *
10563 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
10564 * cacheline, if we ever start emitting more commands before
10565 * the MI_DISPLAY_FLIP we may need to first emit everything else,
10566 * then do the cacheline alignment, and finally emit the
10567 * MI_DISPLAY_FLIP.
10568 */
10569 ret = intel_ring_cacheline_align(ring);
10570 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010571 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020010572
Chris Wilsonffe74d72013-08-26 20:58:12 +010010573 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010574 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010575 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010576
Chris Wilsonffe74d72013-08-26 20:58:12 +010010577 /* Unmask the flip-done completion message. Note that the bspec says that
10578 * we should do this for both the BCS and RCS, and that we must not unmask
10579 * more than one flip event at any time (or ensure that one flip message
10580 * can be sent by waiting for flip-done prior to queueing new flips).
10581 * Experimentation says that BCS works despite DERRMR masking all
10582 * flip-done completion events and that unmasking all planes at once
10583 * for the RCS also doesn't appear to drop events. Setting the DERRMR
10584 * to zero does lead to lockups within MI_DISPLAY_FLIP.
10585 */
10586 if (ring->id == RCS) {
10587 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
10588 intel_ring_emit(ring, DERRMR);
10589 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
10590 DERRMR_PIPEB_PRI_FLIP_DONE |
10591 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010010592 if (IS_GEN8(dev))
10593 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
10594 MI_SRM_LRM_GLOBAL_GTT);
10595 else
10596 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
10597 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +010010598 intel_ring_emit(ring, DERRMR);
10599 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010010600 if (IS_GEN8(dev)) {
10601 intel_ring_emit(ring, 0);
10602 intel_ring_emit(ring, MI_NOOP);
10603 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010010604 }
10605
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010606 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010607 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010608 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010609 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000010610
10611 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010612 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010613 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010614}
10615
Sourab Gupta84c33a62014-06-02 16:47:17 +053010616static bool use_mmio_flip(struct intel_engine_cs *ring,
10617 struct drm_i915_gem_object *obj)
10618{
10619 /*
10620 * This is not being used for older platforms, because
10621 * non-availability of flip done interrupt forces us to use
10622 * CS flips. Older platforms derive flip done using some clever
10623 * tricks involving the flip_pending status bits and vblank irqs.
10624 * So using MMIO flips there would disrupt this mechanism.
10625 */
10626
Chris Wilson8e09bf82014-07-08 10:40:30 +010010627 if (ring == NULL)
10628 return true;
10629
Sourab Gupta84c33a62014-06-02 16:47:17 +053010630 if (INTEL_INFO(ring->dev)->gen < 5)
10631 return false;
10632
10633 if (i915.use_mmio_flip < 0)
10634 return false;
10635 else if (i915.use_mmio_flip > 0)
10636 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010010637 else if (i915.enable_execlists)
10638 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053010639 else
John Harrison41c52412014-11-24 18:49:43 +000010640 return ring != i915_gem_request_get_ring(obj->last_read_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010641}
10642
Damien Lespiauff944562014-11-20 14:58:16 +000010643static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
10644{
10645 struct drm_device *dev = intel_crtc->base.dev;
10646 struct drm_i915_private *dev_priv = dev->dev_private;
10647 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000010648 const enum pipe pipe = intel_crtc->pipe;
10649 u32 ctl, stride;
10650
10651 ctl = I915_READ(PLANE_CTL(pipe, 0));
10652 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010010653 switch (fb->modifier[0]) {
10654 case DRM_FORMAT_MOD_NONE:
10655 break;
10656 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000010657 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010010658 break;
10659 case I915_FORMAT_MOD_Y_TILED:
10660 ctl |= PLANE_CTL_TILED_Y;
10661 break;
10662 case I915_FORMAT_MOD_Yf_TILED:
10663 ctl |= PLANE_CTL_TILED_YF;
10664 break;
10665 default:
10666 MISSING_CASE(fb->modifier[0]);
10667 }
Damien Lespiauff944562014-11-20 14:58:16 +000010668
10669 /*
10670 * The stride is either expressed as a multiple of 64 bytes chunks for
10671 * linear buffers or in number of tiles for tiled buffers.
10672 */
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010010673 stride = fb->pitches[0] /
10674 intel_fb_stride_alignment(dev, fb->modifier[0],
10675 fb->pixel_format);
Damien Lespiauff944562014-11-20 14:58:16 +000010676
10677 /*
10678 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
10679 * PLANE_SURF updates, the update is then guaranteed to be atomic.
10680 */
10681 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
10682 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
10683
10684 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
10685 POSTING_READ(PLANE_SURF(pipe, 0));
10686}
10687
10688static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
Sourab Gupta84c33a62014-06-02 16:47:17 +053010689{
10690 struct drm_device *dev = intel_crtc->base.dev;
10691 struct drm_i915_private *dev_priv = dev->dev_private;
10692 struct intel_framebuffer *intel_fb =
10693 to_intel_framebuffer(intel_crtc->base.primary->fb);
10694 struct drm_i915_gem_object *obj = intel_fb->obj;
10695 u32 dspcntr;
10696 u32 reg;
10697
Sourab Gupta84c33a62014-06-02 16:47:17 +053010698 reg = DSPCNTR(intel_crtc->plane);
10699 dspcntr = I915_READ(reg);
10700
Damien Lespiauc5d97472014-10-25 00:11:11 +010010701 if (obj->tiling_mode != I915_TILING_NONE)
10702 dspcntr |= DISPPLANE_TILED;
10703 else
10704 dspcntr &= ~DISPPLANE_TILED;
10705
Sourab Gupta84c33a62014-06-02 16:47:17 +053010706 I915_WRITE(reg, dspcntr);
10707
10708 I915_WRITE(DSPSURF(intel_crtc->plane),
10709 intel_crtc->unpin_work->gtt_offset);
10710 POSTING_READ(DSPSURF(intel_crtc->plane));
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010711
Damien Lespiauff944562014-11-20 14:58:16 +000010712}
10713
10714/*
10715 * XXX: This is the temporary way to update the plane registers until we get
10716 * around to using the usual plane update functions for MMIO flips
10717 */
10718static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
10719{
10720 struct drm_device *dev = intel_crtc->base.dev;
10721 bool atomic_update;
10722 u32 start_vbl_count;
10723
10724 intel_mark_page_flip_active(intel_crtc);
10725
10726 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
10727
10728 if (INTEL_INFO(dev)->gen >= 9)
10729 skl_do_mmio_flip(intel_crtc);
10730 else
10731 /* use_mmio_flip() retricts MMIO flips to ilk+ */
10732 ilk_do_mmio_flip(intel_crtc);
10733
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010734 if (atomic_update)
10735 intel_pipe_update_end(intel_crtc, start_vbl_count);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010736}
10737
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010738static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053010739{
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010740 struct intel_crtc *crtc =
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010741 container_of(work, struct intel_crtc, mmio_flip.work);
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010742 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053010743
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010744 mmio_flip = &crtc->mmio_flip;
10745 if (mmio_flip->req)
John Harrison9c654812014-11-24 18:49:35 +000010746 WARN_ON(__i915_wait_request(mmio_flip->req,
10747 crtc->reset_counter,
10748 false, NULL, NULL) != 0);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010749
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010750 intel_do_mmio_flip(crtc);
10751 if (mmio_flip->req) {
10752 mutex_lock(&crtc->base.dev->struct_mutex);
John Harrison146d84f2014-12-05 13:49:33 +000010753 i915_gem_request_assign(&mmio_flip->req, NULL);
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010754 mutex_unlock(&crtc->base.dev->struct_mutex);
10755 }
Sourab Gupta84c33a62014-06-02 16:47:17 +053010756}
10757
10758static int intel_queue_mmio_flip(struct drm_device *dev,
10759 struct drm_crtc *crtc,
10760 struct drm_framebuffer *fb,
10761 struct drm_i915_gem_object *obj,
10762 struct intel_engine_cs *ring,
10763 uint32_t flags)
10764{
Sourab Gupta84c33a62014-06-02 16:47:17 +053010765 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010766
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010767 i915_gem_request_assign(&intel_crtc->mmio_flip.req,
10768 obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010769
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020010770 schedule_work(&intel_crtc->mmio_flip.work);
10771
Sourab Gupta84c33a62014-06-02 16:47:17 +053010772 return 0;
10773}
10774
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010775static int intel_default_queue_flip(struct drm_device *dev,
10776 struct drm_crtc *crtc,
10777 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010778 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010779 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010780 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010781{
10782 return -ENODEV;
10783}
10784
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010785static bool __intel_pageflip_stall_check(struct drm_device *dev,
10786 struct drm_crtc *crtc)
10787{
10788 struct drm_i915_private *dev_priv = dev->dev_private;
10789 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10790 struct intel_unpin_work *work = intel_crtc->unpin_work;
10791 u32 addr;
10792
10793 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
10794 return true;
10795
10796 if (!work->enable_stall_check)
10797 return false;
10798
10799 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010010800 if (work->flip_queued_req &&
10801 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010802 return false;
10803
Daniel Vetter1e3feef2015-02-13 21:03:45 +010010804 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010805 }
10806
Daniel Vetter1e3feef2015-02-13 21:03:45 +010010807 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010808 return false;
10809
10810 /* Potential stall - if we see that the flip has happened,
10811 * assume a missed interrupt. */
10812 if (INTEL_INFO(dev)->gen >= 4)
10813 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
10814 else
10815 addr = I915_READ(DSPADDR(intel_crtc->plane));
10816
10817 /* There is a potential issue here with a false positive after a flip
10818 * to the same address. We could address this by checking for a
10819 * non-incrementing frame counter.
10820 */
10821 return addr == work->gtt_offset;
10822}
10823
10824void intel_check_page_flip(struct drm_device *dev, int pipe)
10825{
10826 struct drm_i915_private *dev_priv = dev->dev_private;
10827 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10828 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010010829 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020010830
Dave Gordon6c51d462015-03-06 15:34:26 +000010831 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010832
10833 if (crtc == NULL)
10834 return;
10835
Daniel Vetterf3260382014-09-15 14:55:23 +020010836 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010010837 work = intel_crtc->unpin_work;
10838 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010839 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010010840 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010841 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010010842 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010843 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010010844 if (work != NULL &&
10845 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
10846 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020010847 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010848}
10849
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010850static int intel_crtc_page_flip(struct drm_crtc *crtc,
10851 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010852 struct drm_pending_vblank_event *event,
10853 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010854{
10855 struct drm_device *dev = crtc->dev;
10856 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070010857 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070010858 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010859 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080010860 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020010861 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010862 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010863 struct intel_engine_cs *ring;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010010864 bool mmio_flip;
Chris Wilson52e68632010-08-08 10:15:59 +010010865 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010866
Matt Roper2ff8fde2014-07-08 07:50:07 -070010867 /*
10868 * drm_mode_page_flip_ioctl() should already catch this, but double
10869 * check to be safe. In the future we may enable pageflipping from
10870 * a disabled primary plane.
10871 */
10872 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
10873 return -EBUSY;
10874
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030010875 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070010876 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030010877 return -EINVAL;
10878
10879 /*
10880 * TILEOFF/LINOFF registers can't be changed via MI display flips.
10881 * Note that pitch changes could also affect these register.
10882 */
10883 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070010884 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
10885 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030010886 return -EINVAL;
10887
Chris Wilsonf900db42014-02-20 09:26:13 +000010888 if (i915_terminally_wedged(&dev_priv->gpu_error))
10889 goto out_hang;
10890
Daniel Vetterb14c5672013-09-19 12:18:32 +020010891 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010892 if (work == NULL)
10893 return -ENOMEM;
10894
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010895 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010896 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010897 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010898 INIT_WORK(&work->work, intel_unpin_work_fn);
10899
Daniel Vetter87b6b102014-05-15 15:33:46 +020010900 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070010901 if (ret)
10902 goto free_work;
10903
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010904 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010905 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010906 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010907 /* Before declaring the flip queue wedged, check if
10908 * the hardware completed the operation behind our backs.
10909 */
10910 if (__intel_pageflip_stall_check(dev, crtc)) {
10911 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
10912 page_flip_completed(intel_crtc);
10913 } else {
10914 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010915 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010010916
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010917 drm_crtc_vblank_put(crtc);
10918 kfree(work);
10919 return -EBUSY;
10920 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010921 }
10922 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010923 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010924
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010925 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
10926 flush_workqueue(dev_priv->wq);
10927
Jesse Barnes75dfca82010-02-10 15:09:44 -080010928 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010929 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000010930 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010931
Matt Roperf4510a22014-04-01 15:22:40 -070010932 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080010933 update_state_fb(crtc->primary);
Matt Roper1ed1f962015-01-30 16:22:36 -080010934
Chris Wilsone1f99ce2010-10-27 12:45:26 +010010935 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010010936
Chris Wilson89ed88b2015-02-16 14:31:49 +000010937 ret = i915_mutex_lock_interruptible(dev);
10938 if (ret)
10939 goto cleanup;
10940
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010941 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020010942 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010010943
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010944 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Daniel Vettera071fa02014-06-18 23:28:09 +020010945 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010946
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010947 if (IS_VALLEYVIEW(dev)) {
10948 ring = &dev_priv->ring[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010949 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010010950 /* vlv: DISPLAY_FLIP fails to change tiling */
10951 ring = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000010952 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Chris Wilson2a92d5b2014-07-08 10:40:29 +010010953 ring = &dev_priv->ring[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010954 } else if (INTEL_INFO(dev)->gen >= 7) {
John Harrison41c52412014-11-24 18:49:43 +000010955 ring = i915_gem_request_get_ring(obj->last_read_req);
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010956 if (ring == NULL || ring->id != RCS)
10957 ring = &dev_priv->ring[BCS];
10958 } else {
10959 ring = &dev_priv->ring[RCS];
10960 }
10961
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010010962 mmio_flip = use_mmio_flip(ring, obj);
10963
10964 /* When using CS flips, we want to emit semaphores between rings.
10965 * However, when using mmio flips we will create a task to do the
10966 * synchronisation, so all we want here is to pin the framebuffer
10967 * into the display plane and skip any waits.
10968 */
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000010969 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010010970 crtc->primary->state,
10971 mmio_flip ? i915_gem_request_get_ring(obj->last_read_req) : ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010972 if (ret)
10973 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010974
Tvrtko Ursulin121920f2015-03-23 11:10:37 +000010975 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
10976 + intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010977
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010010978 if (mmio_flip) {
Sourab Gupta84c33a62014-06-02 16:47:17 +053010979 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
10980 page_flip_flags);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010981 if (ret)
10982 goto cleanup_unpin;
10983
John Harrisonf06cc1b2014-11-24 18:49:37 +000010984 i915_gem_request_assign(&work->flip_queued_req,
10985 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010986 } else {
Sourab Gupta84c33a62014-06-02 16:47:17 +053010987 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010988 page_flip_flags);
10989 if (ret)
10990 goto cleanup_unpin;
10991
John Harrisonf06cc1b2014-11-24 18:49:37 +000010992 i915_gem_request_assign(&work->flip_queued_req,
10993 intel_ring_get_request(ring));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010994 }
10995
Daniel Vetter1e3feef2015-02-13 21:03:45 +010010996 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010997 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010998
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010999 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Daniel Vettera071fa02014-06-18 23:28:09 +020011000 INTEL_FRONTBUFFER_PRIMARY(pipe));
11001
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020011002 intel_fbc_disable(dev);
Daniel Vetterf99d7062014-06-19 16:01:59 +020011003 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011004 mutex_unlock(&dev->struct_mutex);
11005
Jesse Barnese5510fa2010-07-01 16:48:37 -070011006 trace_i915_flip_request(intel_crtc->plane, obj);
11007
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011008 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010011009
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011010cleanup_unpin:
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011011 intel_unpin_fb_obj(fb, crtc->primary->state);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011012cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011013 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000011014 mutex_unlock(&dev->struct_mutex);
11015cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070011016 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011017 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010011018
Chris Wilson89ed88b2015-02-16 14:31:49 +000011019 drm_gem_object_unreference_unlocked(&obj->base);
11020 drm_framebuffer_unreference(work->old_fb);
11021
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011022 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011023 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011024 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011025
Daniel Vetter87b6b102014-05-15 15:33:46 +020011026 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011027free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010011028 kfree(work);
11029
Chris Wilsonf900db42014-02-20 09:26:13 +000011030 if (ret == -EIO) {
11031out_hang:
Matt Roper53a366b2014-12-23 10:41:53 -080011032 ret = intel_plane_restore(primary);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011033 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011034 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020011035 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011036 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011037 }
Chris Wilsonf900db42014-02-20 09:26:13 +000011038 }
Chris Wilson96b099f2010-06-07 14:03:04 +010011039 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011040}
11041
Jani Nikula65b38e02015-04-13 11:26:56 +030011042static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011043 .mode_set_base_atomic = intel_pipe_set_base_atomic,
11044 .load_lut = intel_crtc_load_lut,
Matt Roperea2c67b2014-12-23 10:41:52 -080011045 .atomic_begin = intel_begin_crtc_commit,
11046 .atomic_flush = intel_finish_crtc_commit,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011047};
11048
Daniel Vetter9a935852012-07-05 22:34:27 +020011049/**
11050 * intel_modeset_update_staged_output_state
11051 *
11052 * Updates the staged output configuration state, e.g. after we've read out the
11053 * current hw state.
11054 */
11055static void intel_modeset_update_staged_output_state(struct drm_device *dev)
11056{
Ville Syrjälä76688512014-01-10 11:28:06 +020011057 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011058 struct intel_encoder *encoder;
11059 struct intel_connector *connector;
11060
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011061 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011062 connector->new_encoder =
11063 to_intel_encoder(connector->base.encoder);
11064 }
11065
Damien Lespiaub2784e12014-08-05 11:29:37 +010011066 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011067 encoder->new_crtc =
11068 to_intel_crtc(encoder->base.crtc);
11069 }
Ville Syrjälä76688512014-01-10 11:28:06 +020011070
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011071 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011072 crtc->new_enabled = crtc->base.state->enable;
Ville Syrjälä76688512014-01-10 11:28:06 +020011073 }
Daniel Vetter9a935852012-07-05 22:34:27 +020011074}
11075
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011076/* Transitional helper to copy current connector/encoder state to
11077 * connector->state. This is needed so that code that is partially
11078 * converted to atomic does the right thing.
11079 */
11080static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11081{
11082 struct intel_connector *connector;
11083
11084 for_each_intel_connector(dev, connector) {
11085 if (connector->base.encoder) {
11086 connector->base.state->best_encoder =
11087 connector->base.encoder;
11088 connector->base.state->crtc =
11089 connector->base.encoder->crtc;
11090 } else {
11091 connector->base.state->best_encoder = NULL;
11092 connector->base.state->crtc = NULL;
11093 }
11094 }
11095}
11096
Daniel Vetter9a935852012-07-05 22:34:27 +020011097/**
11098 * intel_modeset_commit_output_state
11099 *
11100 * This function copies the stage display pipe configuration to the real one.
11101 */
11102static void intel_modeset_commit_output_state(struct drm_device *dev)
11103{
Ville Syrjälä76688512014-01-10 11:28:06 +020011104 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011105 struct intel_encoder *encoder;
11106 struct intel_connector *connector;
11107
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011108 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011109 connector->base.encoder = &connector->new_encoder->base;
11110 }
11111
Damien Lespiaub2784e12014-08-05 11:29:37 +010011112 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011113 encoder->base.crtc = &encoder->new_crtc->base;
11114 }
Ville Syrjälä76688512014-01-10 11:28:06 +020011115
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011116 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011117 crtc->base.state->enable = crtc->new_enabled;
Ville Syrjälä76688512014-01-10 11:28:06 +020011118 crtc->base.enabled = crtc->new_enabled;
11119 }
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011120
11121 intel_modeset_update_connector_atomic_state(dev);
Daniel Vetter9a935852012-07-05 22:34:27 +020011122}
11123
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011124static void
Robin Schroereba905b2014-05-18 02:24:50 +020011125connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011126 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011127{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011128 int bpp = pipe_config->pipe_bpp;
11129
11130 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11131 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011132 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011133
11134 /* Don't use an invalid EDID bpc value */
11135 if (connector->base.display_info.bpc &&
11136 connector->base.display_info.bpc * 3 < bpp) {
11137 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11138 bpp, connector->base.display_info.bpc*3);
11139 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11140 }
11141
11142 /* Clamp bpp to 8 on screens without EDID 1.4 */
11143 if (connector->base.display_info.bpc == 0 && bpp > 24) {
11144 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11145 bpp);
11146 pipe_config->pipe_bpp = 24;
11147 }
11148}
11149
11150static int
11151compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011152 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011153{
11154 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011155 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011156 struct drm_connector *connector;
11157 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011158 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011159
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011160 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011161 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011162 else if (INTEL_INFO(dev)->gen >= 5)
11163 bpp = 12*3;
11164 else
11165 bpp = 8*3;
11166
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011167
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011168 pipe_config->pipe_bpp = bpp;
11169
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011170 state = pipe_config->base.state;
11171
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011172 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011173 for_each_connector_in_state(state, connector, connector_state, i) {
11174 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011175 continue;
11176
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011177 connected_sink_compute_bpp(to_intel_connector(connector),
11178 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011179 }
11180
11181 return bpp;
11182}
11183
Daniel Vetter644db712013-09-19 14:53:58 +020011184static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11185{
11186 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11187 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010011188 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020011189 mode->crtc_hdisplay, mode->crtc_hsync_start,
11190 mode->crtc_hsync_end, mode->crtc_htotal,
11191 mode->crtc_vdisplay, mode->crtc_vsync_start,
11192 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11193}
11194
Daniel Vetterc0b03412013-05-28 12:05:54 +020011195static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011196 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020011197 const char *context)
11198{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011199 struct drm_device *dev = crtc->base.dev;
11200 struct drm_plane *plane;
11201 struct intel_plane *intel_plane;
11202 struct intel_plane_state *state;
11203 struct drm_framebuffer *fb;
11204
11205 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
11206 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020011207
11208 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
11209 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
11210 pipe_config->pipe_bpp, pipe_config->dither);
11211 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11212 pipe_config->has_pch_encoder,
11213 pipe_config->fdi_lanes,
11214 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
11215 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
11216 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011217 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11218 pipe_config->has_dp_encoder,
11219 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
11220 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
11221 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011222
11223 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
11224 pipe_config->has_dp_encoder,
11225 pipe_config->dp_m2_n2.gmch_m,
11226 pipe_config->dp_m2_n2.gmch_n,
11227 pipe_config->dp_m2_n2.link_m,
11228 pipe_config->dp_m2_n2.link_n,
11229 pipe_config->dp_m2_n2.tu);
11230
Daniel Vetter55072d12014-11-20 16:10:28 +010011231 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
11232 pipe_config->has_audio,
11233 pipe_config->has_infoframe);
11234
Daniel Vetterc0b03412013-05-28 12:05:54 +020011235 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011236 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011237 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011238 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11239 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030011240 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030011241 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
11242 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011243 DRM_DEBUG_KMS("num_scalers: %d\n", crtc->num_scalers);
11244 DRM_DEBUG_KMS("scaler_users: 0x%x\n", pipe_config->scaler_state.scaler_users);
11245 DRM_DEBUG_KMS("scaler id: %d\n", pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011246 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11247 pipe_config->gmch_pfit.control,
11248 pipe_config->gmch_pfit.pgm_ratios,
11249 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011250 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020011251 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011252 pipe_config->pch_pfit.size,
11253 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030011254 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030011255 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011256
11257 DRM_DEBUG_KMS("planes on this crtc\n");
11258 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
11259 intel_plane = to_intel_plane(plane);
11260 if (intel_plane->pipe != crtc->pipe)
11261 continue;
11262
11263 state = to_intel_plane_state(plane->state);
11264 fb = state->base.fb;
11265 if (!fb) {
11266 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
11267 "disabled, scaler_id = %d\n",
11268 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11269 plane->base.id, intel_plane->pipe,
11270 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
11271 drm_plane_index(plane), state->scaler_id);
11272 continue;
11273 }
11274
11275 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
11276 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11277 plane->base.id, intel_plane->pipe,
11278 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
11279 drm_plane_index(plane));
11280 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
11281 fb->base.id, fb->width, fb->height, fb->pixel_format);
11282 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
11283 state->scaler_id,
11284 state->src.x1 >> 16, state->src.y1 >> 16,
11285 drm_rect_width(&state->src) >> 16,
11286 drm_rect_height(&state->src) >> 16,
11287 state->dst.x1, state->dst.y1,
11288 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
11289 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020011290}
11291
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011292static bool encoders_cloneable(const struct intel_encoder *a,
11293 const struct intel_encoder *b)
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011294{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011295 /* masks could be asymmetric, so check both ways */
11296 return a == b || (a->cloneable & (1 << b->type) &&
11297 b->cloneable & (1 << a->type));
11298}
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011299
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011300static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11301 struct intel_crtc *crtc,
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011302 struct intel_encoder *encoder)
11303{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011304 struct intel_encoder *source_encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011305 struct drm_connector *connector;
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011306 struct drm_connector_state *connector_state;
11307 int i;
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011308
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011309 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011310 if (connector_state->crtc != &crtc->base)
11311 continue;
11312
11313 source_encoder =
11314 to_intel_encoder(connector_state->best_encoder);
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011315 if (!encoders_cloneable(encoder, source_encoder))
11316 return false;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011317 }
11318
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011319 return true;
11320}
11321
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011322static bool check_encoder_cloning(struct drm_atomic_state *state,
11323 struct intel_crtc *crtc)
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011324{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011325 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011326 struct drm_connector *connector;
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011327 struct drm_connector_state *connector_state;
11328 int i;
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011329
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011330 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011331 if (connector_state->crtc != &crtc->base)
11332 continue;
11333
11334 encoder = to_intel_encoder(connector_state->best_encoder);
11335 if (!check_single_encoder_cloning(state, crtc, encoder))
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011336 return false;
11337 }
11338
11339 return true;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011340}
11341
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011342static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011343{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011344 struct drm_device *dev = state->dev;
11345 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011346 struct drm_connector *connector;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011347 struct drm_connector_state *connector_state;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011348 unsigned int used_ports = 0;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011349 int i;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011350
11351 /*
11352 * Walk the connector list instead of the encoder
11353 * list to detect the problem on ddi platforms
11354 * where there's just one encoder per digital port.
11355 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011356 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011357 if (!connector_state->best_encoder)
11358 continue;
11359
11360 encoder = to_intel_encoder(connector_state->best_encoder);
11361
11362 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011363
11364 switch (encoder->type) {
11365 unsigned int port_mask;
11366 case INTEL_OUTPUT_UNKNOWN:
11367 if (WARN_ON(!HAS_DDI(dev)))
11368 break;
11369 case INTEL_OUTPUT_DISPLAYPORT:
11370 case INTEL_OUTPUT_HDMI:
11371 case INTEL_OUTPUT_EDP:
11372 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
11373
11374 /* the same port mustn't appear more than once */
11375 if (used_ports & port_mask)
11376 return false;
11377
11378 used_ports |= port_mask;
11379 default:
11380 break;
11381 }
11382 }
11383
11384 return true;
11385}
11386
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011387static void
11388clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
11389{
11390 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070011391 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011392
Chandra Konduru663a3642015-04-07 15:28:41 -070011393 /* Clear only the intel specific part of the crtc state excluding scalers */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011394 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070011395 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011396 memset(crtc_state, 0, sizeof *crtc_state);
11397 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070011398 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011399}
11400
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011401static struct intel_crtc_state *
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011402intel_modeset_pipe_config(struct drm_crtc *crtc,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011403 struct drm_display_mode *mode,
11404 struct drm_atomic_state *state)
Daniel Vetter7758a112012-07-08 19:40:39 +020011405{
Daniel Vetter7758a112012-07-08 19:40:39 +020011406 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011407 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011408 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011409 struct intel_crtc_state *pipe_config;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011410 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011411 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010011412 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020011413
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011414 if (!check_encoder_cloning(state, to_intel_crtc(crtc))) {
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011415 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11416 return ERR_PTR(-EINVAL);
11417 }
11418
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011419 if (!check_digital_port_conflicts(state)) {
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011420 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
11421 return ERR_PTR(-EINVAL);
11422 }
11423
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011424 pipe_config = intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));
11425 if (IS_ERR(pipe_config))
11426 return pipe_config;
11427
11428 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020011429
Matt Roper07878242015-02-25 11:43:26 -080011430 pipe_config->base.crtc = crtc;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011431 drm_mode_copy(&pipe_config->base.adjusted_mode, mode);
11432 drm_mode_copy(&pipe_config->base.mode, mode);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030011433
Daniel Vettere143a212013-07-04 12:01:15 +020011434 pipe_config->cpu_transcoder =
11435 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +020011436 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011437
Imre Deak2960bc92013-07-30 13:36:32 +030011438 /*
11439 * Sanitize sync polarity flags based on requested ones. If neither
11440 * positive or negative polarity is requested, treat this as meaning
11441 * negative polarity.
11442 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011443 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030011444 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011445 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030011446
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011447 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030011448 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011449 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030011450
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011451 /* Compute a starting value for pipe_config->pipe_bpp taking the source
11452 * plane pixel format and any sink constraints into account. Returns the
11453 * source plane bpp so that dithering can be selected on mismatches
11454 * after encoders and crtc also have had their say. */
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011455 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
11456 pipe_config);
11457 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011458 goto fail;
11459
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030011460 /*
11461 * Determine the real pipe dimensions. Note that stereo modes can
11462 * increase the actual pipe size due to the frame doubling and
11463 * insertion of additional space for blanks between the frame. This
11464 * is stored in the crtc timings. We use the requested mode to do this
11465 * computation to clearly distinguish it from the adjusted mode, which
11466 * can be changed by the connectors in the below retry loop.
11467 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011468 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080011469 &pipe_config->pipe_src_w,
11470 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030011471
Daniel Vettere29c22c2013-02-21 00:00:16 +010011472encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020011473 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020011474 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020011475 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020011476
Daniel Vetter135c81b2013-07-21 21:37:09 +020011477 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011478 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
11479 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020011480
Daniel Vetter7758a112012-07-08 19:40:39 +020011481 /* Pass our mode to the connectors and the CRTC to give them a chance to
11482 * adjust it according to limitations or connector properties, and also
11483 * a chance to reject the mode entirely.
11484 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011485 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011486 if (connector_state->crtc != crtc)
11487 continue;
11488
11489 encoder = to_intel_encoder(connector_state->best_encoder);
11490
Daniel Vetterefea6e82013-07-21 21:36:59 +020011491 if (!(encoder->compute_config(encoder, pipe_config))) {
11492 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020011493 goto fail;
11494 }
11495 }
11496
Daniel Vetterff9a6752013-06-01 17:16:21 +020011497 /* Set default port clock if not overwritten by the encoder. Needs to be
11498 * done afterwards in case the encoder adjusts the mode. */
11499 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011500 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010011501 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020011502
Daniel Vettera43f6e02013-06-07 23:10:32 +020011503 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010011504 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020011505 DRM_DEBUG_KMS("CRTC fixup failed\n");
11506 goto fail;
11507 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010011508
11509 if (ret == RETRY) {
11510 if (WARN(!retry, "loop in pipe configuration computation\n")) {
11511 ret = -EINVAL;
11512 goto fail;
11513 }
11514
11515 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
11516 retry = false;
11517 goto encoder_retry;
11518 }
11519
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011520 pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011521 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011522 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011523
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011524 return pipe_config;
Daniel Vetter7758a112012-07-08 19:40:39 +020011525fail:
Daniel Vettere29c22c2013-02-21 00:00:16 +010011526 return ERR_PTR(ret);
Daniel Vetter7758a112012-07-08 19:40:39 +020011527}
11528
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011529/* Computes which crtcs are affected and sets the relevant bits in the mask. For
11530 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
11531static void
11532intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
11533 unsigned *prepare_pipes, unsigned *disable_pipes)
11534{
11535 struct intel_crtc *intel_crtc;
11536 struct drm_device *dev = crtc->dev;
11537 struct intel_encoder *encoder;
11538 struct intel_connector *connector;
11539 struct drm_crtc *tmp_crtc;
11540
11541 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
11542
11543 /* Check which crtcs have changed outputs connected to them, these need
11544 * to be part of the prepare_pipes mask. We don't (yet) support global
11545 * modeset across multiple crtcs, so modeset_pipes will only have one
11546 * bit set at most. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011547 for_each_intel_connector(dev, connector) {
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011548 if (connector->base.encoder == &connector->new_encoder->base)
11549 continue;
11550
11551 if (connector->base.encoder) {
11552 tmp_crtc = connector->base.encoder->crtc;
11553
11554 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
11555 }
11556
11557 if (connector->new_encoder)
11558 *prepare_pipes |=
11559 1 << connector->new_encoder->new_crtc->pipe;
11560 }
11561
Damien Lespiaub2784e12014-08-05 11:29:37 +010011562 for_each_intel_encoder(dev, encoder) {
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011563 if (encoder->base.crtc == &encoder->new_crtc->base)
11564 continue;
11565
11566 if (encoder->base.crtc) {
11567 tmp_crtc = encoder->base.crtc;
11568
11569 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
11570 }
11571
11572 if (encoder->new_crtc)
11573 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
11574 }
11575
Ville Syrjälä76688512014-01-10 11:28:06 +020011576 /* Check for pipes that will be enabled/disabled ... */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011577 for_each_intel_crtc(dev, intel_crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011578 if (intel_crtc->base.state->enable == intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011579 continue;
11580
Ville Syrjälä76688512014-01-10 11:28:06 +020011581 if (!intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011582 *disable_pipes |= 1 << intel_crtc->pipe;
Ville Syrjälä76688512014-01-10 11:28:06 +020011583 else
11584 *prepare_pipes |= 1 << intel_crtc->pipe;
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011585 }
11586
11587
11588 /* set_mode is also used to update properties on life display pipes. */
11589 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä76688512014-01-10 11:28:06 +020011590 if (intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011591 *prepare_pipes |= 1 << intel_crtc->pipe;
11592
Daniel Vetterb6c51642013-04-12 18:48:43 +020011593 /*
11594 * For simplicity do a full modeset on any pipe where the output routing
11595 * changed. We could be more clever, but that would require us to be
11596 * more careful with calling the relevant encoder->mode_set functions.
11597 */
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011598 if (*prepare_pipes)
11599 *modeset_pipes = *prepare_pipes;
11600
11601 /* ... and mask these out. */
11602 *modeset_pipes &= ~(*disable_pipes);
11603 *prepare_pipes &= ~(*disable_pipes);
Daniel Vetterb6c51642013-04-12 18:48:43 +020011604
11605 /*
11606 * HACK: We don't (yet) fully support global modesets. intel_set_config
11607 * obies this rule, but the modeset restore mode of
11608 * intel_modeset_setup_hw_state does not.
11609 */
11610 *modeset_pipes &= 1 << intel_crtc->pipe;
11611 *prepare_pipes &= 1 << intel_crtc->pipe;
Daniel Vettere3641d32013-04-11 19:49:07 +020011612
11613 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
11614 *modeset_pipes, *prepare_pipes, *disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011615}
11616
Daniel Vetterea9d7582012-07-10 10:42:52 +020011617static bool intel_crtc_in_use(struct drm_crtc *crtc)
11618{
11619 struct drm_encoder *encoder;
11620 struct drm_device *dev = crtc->dev;
11621
11622 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
11623 if (encoder->crtc == crtc)
11624 return true;
11625
11626 return false;
11627}
11628
11629static void
11630intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
11631{
Daniel Vetterba41c0de2014-11-03 15:04:55 +010011632 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011633 struct intel_encoder *intel_encoder;
11634 struct intel_crtc *intel_crtc;
11635 struct drm_connector *connector;
11636
Daniel Vetterba41c0de2014-11-03 15:04:55 +010011637 intel_shared_dpll_commit(dev_priv);
11638
Damien Lespiaub2784e12014-08-05 11:29:37 +010011639 for_each_intel_encoder(dev, intel_encoder) {
Daniel Vetterea9d7582012-07-10 10:42:52 +020011640 if (!intel_encoder->base.crtc)
11641 continue;
11642
11643 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
11644
11645 if (prepare_pipes & (1 << intel_crtc->pipe))
11646 intel_encoder->connectors_active = false;
11647 }
11648
11649 intel_modeset_commit_output_state(dev);
11650
Ville Syrjälä76688512014-01-10 11:28:06 +020011651 /* Double check state. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011652 for_each_intel_crtc(dev, intel_crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011653 WARN_ON(intel_crtc->base.state->enable != intel_crtc_in_use(&intel_crtc->base));
Daniel Vetterea9d7582012-07-10 10:42:52 +020011654 }
11655
11656 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
11657 if (!connector->encoder || !connector->encoder->crtc)
11658 continue;
11659
11660 intel_crtc = to_intel_crtc(connector->encoder->crtc);
11661
11662 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +020011663 struct drm_property *dpms_property =
11664 dev->mode_config.dpms_property;
11665
Daniel Vetterea9d7582012-07-10 10:42:52 +020011666 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -050011667 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +020011668 dpms_property,
11669 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +020011670
11671 intel_encoder = to_intel_encoder(connector->encoder);
11672 intel_encoder->connectors_active = true;
11673 }
11674 }
11675
11676}
11677
Ville Syrjälä3bd26262013-09-06 23:29:02 +030011678static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011679{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030011680 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011681
11682 if (clock1 == clock2)
11683 return true;
11684
11685 if (!clock1 || !clock2)
11686 return false;
11687
11688 diff = abs(clock1 - clock2);
11689
11690 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
11691 return true;
11692
11693 return false;
11694}
11695
Daniel Vetter25c5b262012-07-08 22:08:04 +020011696#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
11697 list_for_each_entry((intel_crtc), \
11698 &(dev)->mode_config.crtc_list, \
11699 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +020011700 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020011701
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011702static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020011703intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011704 struct intel_crtc_state *current_config,
11705 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011706{
Daniel Vetter66e985c2013-06-05 13:34:20 +020011707#define PIPE_CONF_CHECK_X(name) \
11708 if (current_config->name != pipe_config->name) { \
11709 DRM_ERROR("mismatch in " #name " " \
11710 "(expected 0x%08x, found 0x%08x)\n", \
11711 current_config->name, \
11712 pipe_config->name); \
11713 return false; \
11714 }
11715
Daniel Vetter08a24032013-04-19 11:25:34 +020011716#define PIPE_CONF_CHECK_I(name) \
11717 if (current_config->name != pipe_config->name) { \
11718 DRM_ERROR("mismatch in " #name " " \
11719 "(expected %i, found %i)\n", \
11720 current_config->name, \
11721 pipe_config->name); \
11722 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010011723 }
11724
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011725/* This is required for BDW+ where there is only one set of registers for
11726 * switching between high and low RR.
11727 * This macro can be used whenever a comparison has to be made between one
11728 * hw state and multiple sw state variables.
11729 */
11730#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
11731 if ((current_config->name != pipe_config->name) && \
11732 (current_config->alt_name != pipe_config->name)) { \
11733 DRM_ERROR("mismatch in " #name " " \
11734 "(expected %i or %i, found %i)\n", \
11735 current_config->name, \
11736 current_config->alt_name, \
11737 pipe_config->name); \
11738 return false; \
11739 }
11740
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011741#define PIPE_CONF_CHECK_FLAGS(name, mask) \
11742 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -070011743 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011744 "(expected %i, found %i)\n", \
11745 current_config->name & (mask), \
11746 pipe_config->name & (mask)); \
11747 return false; \
11748 }
11749
Ville Syrjälä5e550652013-09-06 23:29:07 +030011750#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
11751 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
11752 DRM_ERROR("mismatch in " #name " " \
11753 "(expected %i, found %i)\n", \
11754 current_config->name, \
11755 pipe_config->name); \
11756 return false; \
11757 }
11758
Daniel Vetterbb760062013-06-06 14:55:52 +020011759#define PIPE_CONF_QUIRK(quirk) \
11760 ((current_config->quirks | pipe_config->quirks) & (quirk))
11761
Daniel Vettereccb1402013-05-22 00:50:22 +020011762 PIPE_CONF_CHECK_I(cpu_transcoder);
11763
Daniel Vetter08a24032013-04-19 11:25:34 +020011764 PIPE_CONF_CHECK_I(has_pch_encoder);
11765 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +020011766 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
11767 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
11768 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
11769 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
11770 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +020011771
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011772 PIPE_CONF_CHECK_I(has_dp_encoder);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011773
11774 if (INTEL_INFO(dev)->gen < 8) {
11775 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
11776 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
11777 PIPE_CONF_CHECK_I(dp_m_n.link_m);
11778 PIPE_CONF_CHECK_I(dp_m_n.link_n);
11779 PIPE_CONF_CHECK_I(dp_m_n.tu);
11780
11781 if (current_config->has_drrs) {
11782 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
11783 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
11784 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
11785 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
11786 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
11787 }
11788 } else {
11789 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
11790 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
11791 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
11792 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
11793 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
11794 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011795
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011796 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
11797 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
11798 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
11799 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
11800 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
11801 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011802
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011803 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
11804 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
11805 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
11806 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
11807 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
11808 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011809
Daniel Vetterc93f54c2013-06-27 19:47:19 +020011810 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b2014-04-24 23:54:47 +020011811 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020011812 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
11813 IS_VALLEYVIEW(dev))
11814 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080011815 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020011816
Daniel Vetter9ed109a2014-04-24 23:54:52 +020011817 PIPE_CONF_CHECK_I(has_audio);
11818
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011819 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011820 DRM_MODE_FLAG_INTERLACE);
11821
Daniel Vetterbb760062013-06-06 14:55:52 +020011822 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011823 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011824 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011825 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011826 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011827 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011828 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011829 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011830 DRM_MODE_FLAG_NVSYNC);
11831 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070011832
Ville Syrjälä37327ab2013-09-04 18:25:28 +030011833 PIPE_CONF_CHECK_I(pipe_src_w);
11834 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011835
Daniel Vetter99535992014-04-13 12:00:33 +020011836 /*
11837 * FIXME: BIOS likes to set up a cloned config with lvds+external
11838 * screen. Since we don't yet re-compute the pipe config when moving
11839 * just the lvds port away to another pipe the sw tracking won't match.
11840 *
11841 * Proper atomic modesets with recomputed global state will fix this.
11842 * Until then just don't check gmch state for inherited modes.
11843 */
11844 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
11845 PIPE_CONF_CHECK_I(gmch_pfit.control);
11846 /* pfit ratios are autocomputed by the hw on gen4+ */
11847 if (INTEL_INFO(dev)->gen < 4)
11848 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
11849 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
11850 }
11851
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011852 PIPE_CONF_CHECK_I(pch_pfit.enabled);
11853 if (current_config->pch_pfit.enabled) {
11854 PIPE_CONF_CHECK_I(pch_pfit.pos);
11855 PIPE_CONF_CHECK_I(pch_pfit.size);
11856 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020011857
Chandra Kondurua1b22782015-04-07 15:28:45 -070011858 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
11859
Jesse Barnese59150d2014-01-07 13:30:45 -080011860 /* BDW+ don't expose a synchronous way to read the state */
11861 if (IS_HASWELL(dev))
11862 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030011863
Ville Syrjälä282740f2013-09-04 18:30:03 +030011864 PIPE_CONF_CHECK_I(double_wide);
11865
Daniel Vetter26804af2014-06-25 22:01:55 +030011866 PIPE_CONF_CHECK_X(ddi_pll_sel);
11867
Daniel Vetterc0d43d62013-06-07 23:11:08 +020011868 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020011869 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020011870 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020011871 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
11872 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030011873 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000011874 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
11875 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
11876 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020011877
Ville Syrjälä42571ae2013-09-06 23:29:00 +030011878 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
11879 PIPE_CONF_CHECK_I(pipe_bpp);
11880
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011881 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080011882 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030011883
Daniel Vetter66e985c2013-06-05 13:34:20 +020011884#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020011885#undef PIPE_CONF_CHECK_I
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011886#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011887#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030011888#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020011889#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +020011890
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011891 return true;
11892}
11893
Damien Lespiau08db6652014-11-04 17:06:52 +000011894static void check_wm_state(struct drm_device *dev)
11895{
11896 struct drm_i915_private *dev_priv = dev->dev_private;
11897 struct skl_ddb_allocation hw_ddb, *sw_ddb;
11898 struct intel_crtc *intel_crtc;
11899 int plane;
11900
11901 if (INTEL_INFO(dev)->gen < 9)
11902 return;
11903
11904 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
11905 sw_ddb = &dev_priv->wm.skl_hw.ddb;
11906
11907 for_each_intel_crtc(dev, intel_crtc) {
11908 struct skl_ddb_entry *hw_entry, *sw_entry;
11909 const enum pipe pipe = intel_crtc->pipe;
11910
11911 if (!intel_crtc->active)
11912 continue;
11913
11914 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000011915 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000011916 hw_entry = &hw_ddb.plane[pipe][plane];
11917 sw_entry = &sw_ddb->plane[pipe][plane];
11918
11919 if (skl_ddb_entry_equal(hw_entry, sw_entry))
11920 continue;
11921
11922 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
11923 "(expected (%u,%u), found (%u,%u))\n",
11924 pipe_name(pipe), plane + 1,
11925 sw_entry->start, sw_entry->end,
11926 hw_entry->start, hw_entry->end);
11927 }
11928
11929 /* cursor */
11930 hw_entry = &hw_ddb.cursor[pipe];
11931 sw_entry = &sw_ddb->cursor[pipe];
11932
11933 if (skl_ddb_entry_equal(hw_entry, sw_entry))
11934 continue;
11935
11936 DRM_ERROR("mismatch in DDB state pipe %c cursor "
11937 "(expected (%u,%u), found (%u,%u))\n",
11938 pipe_name(pipe),
11939 sw_entry->start, sw_entry->end,
11940 hw_entry->start, hw_entry->end);
11941 }
11942}
11943
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011944static void
11945check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011946{
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011947 struct intel_connector *connector;
11948
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011949 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011950 /* This also checks the encoder/connector hw state with the
11951 * ->get_hw_state callbacks. */
11952 intel_connector_check_state(connector);
11953
Rob Clarke2c719b2014-12-15 13:56:32 -050011954 I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011955 "connector's staged encoder doesn't match current encoder\n");
11956 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011957}
11958
11959static void
11960check_encoder_state(struct drm_device *dev)
11961{
11962 struct intel_encoder *encoder;
11963 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011964
Damien Lespiaub2784e12014-08-05 11:29:37 +010011965 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011966 bool enabled = false;
11967 bool active = false;
11968 enum pipe pipe, tracked_pipe;
11969
11970 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
11971 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030011972 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011973
Rob Clarke2c719b2014-12-15 13:56:32 -050011974 I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011975 "encoder's stage crtc doesn't match current crtc\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050011976 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011977 "encoder's active_connectors set, but no crtc\n");
11978
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011979 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011980 if (connector->base.encoder != &encoder->base)
11981 continue;
11982 enabled = true;
11983 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
11984 active = true;
11985 }
Dave Airlie0e32b392014-05-02 14:02:48 +100011986 /*
11987 * for MST connectors if we unplug the connector is gone
11988 * away but the encoder is still connected to a crtc
11989 * until a modeset happens in response to the hotplug.
11990 */
11991 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
11992 continue;
11993
Rob Clarke2c719b2014-12-15 13:56:32 -050011994 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011995 "encoder's enabled state mismatch "
11996 "(expected %i, found %i)\n",
11997 !!encoder->base.crtc, enabled);
Rob Clarke2c719b2014-12-15 13:56:32 -050011998 I915_STATE_WARN(active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011999 "active encoder with no crtc\n");
12000
Rob Clarke2c719b2014-12-15 13:56:32 -050012001 I915_STATE_WARN(encoder->connectors_active != active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012002 "encoder's computed active state doesn't match tracked active state "
12003 "(expected %i, found %i)\n", active, encoder->connectors_active);
12004
12005 active = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -050012006 I915_STATE_WARN(active != encoder->connectors_active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012007 "encoder's hw state doesn't match sw tracking "
12008 "(expected %i, found %i)\n",
12009 encoder->connectors_active, active);
12010
12011 if (!encoder->base.crtc)
12012 continue;
12013
12014 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
Rob Clarke2c719b2014-12-15 13:56:32 -050012015 I915_STATE_WARN(active && pipe != tracked_pipe,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012016 "active encoder's pipe doesn't match"
12017 "(expected %i, found %i)\n",
12018 tracked_pipe, pipe);
12019
12020 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012021}
12022
12023static void
12024check_crtc_state(struct drm_device *dev)
12025{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012026 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012027 struct intel_crtc *crtc;
12028 struct intel_encoder *encoder;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012029 struct intel_crtc_state pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012030
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012031 for_each_intel_crtc(dev, crtc) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012032 bool enabled = false;
12033 bool active = false;
12034
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012035 memset(&pipe_config, 0, sizeof(pipe_config));
12036
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012037 DRM_DEBUG_KMS("[CRTC:%d]\n",
12038 crtc->base.base.id);
12039
Matt Roper83d65732015-02-25 13:12:16 -080012040 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012041 "active crtc, but not enabled in sw tracking\n");
12042
Damien Lespiaub2784e12014-08-05 11:29:37 +010012043 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012044 if (encoder->base.crtc != &crtc->base)
12045 continue;
12046 enabled = true;
12047 if (encoder->connectors_active)
12048 active = true;
12049 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020012050
Rob Clarke2c719b2014-12-15 13:56:32 -050012051 I915_STATE_WARN(active != crtc->active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012052 "crtc's computed active state doesn't match tracked active state "
12053 "(expected %i, found %i)\n", active, crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080012054 I915_STATE_WARN(enabled != crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012055 "crtc's computed enabled state doesn't match tracked enabled state "
Matt Roper83d65732015-02-25 13:12:16 -080012056 "(expected %i, found %i)\n", enabled,
12057 crtc->base.state->enable);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012058
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012059 active = dev_priv->display.get_pipe_config(crtc,
12060 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +020012061
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030012062 /* hw state is inconsistent with the pipe quirk */
12063 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12064 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetterd62cf622013-05-29 10:41:29 +020012065 active = crtc->active;
12066
Damien Lespiaub2784e12014-08-05 11:29:37 +010012067 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +030012068 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +020012069 if (encoder->base.crtc != &crtc->base)
12070 continue;
Daniel Vetter1d37b682013-11-18 09:00:59 +010012071 if (encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +020012072 encoder->get_config(encoder, &pipe_config);
12073 }
12074
Rob Clarke2c719b2014-12-15 13:56:32 -050012075 I915_STATE_WARN(crtc->active != active,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012076 "crtc active state doesn't match with hw state "
12077 "(expected %i, found %i)\n", crtc->active, active);
12078
Daniel Vetterc0b03412013-05-28 12:05:54 +020012079 if (active &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012080 !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050012081 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Daniel Vetterc0b03412013-05-28 12:05:54 +020012082 intel_dump_pipe_config(crtc, &pipe_config,
12083 "[hw state]");
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012084 intel_dump_pipe_config(crtc, crtc->config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012085 "[sw state]");
12086 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012087 }
12088}
12089
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012090static void
12091check_shared_dpll_state(struct drm_device *dev)
12092{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012093 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012094 struct intel_crtc *crtc;
12095 struct intel_dpll_hw_state dpll_hw_state;
12096 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020012097
12098 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12099 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12100 int enabled_crtcs = 0, active_crtcs = 0;
12101 bool active;
12102
12103 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12104
12105 DRM_DEBUG_KMS("%s\n", pll->name);
12106
12107 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
12108
Rob Clarke2c719b2014-12-15 13:56:32 -050012109 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
Daniel Vetter53589012013-06-05 13:34:16 +020012110 "more active pll users than references: %i vs %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012111 pll->active, hweight32(pll->config.crtc_mask));
Rob Clarke2c719b2014-12-15 13:56:32 -050012112 I915_STATE_WARN(pll->active && !pll->on,
Daniel Vetter53589012013-06-05 13:34:16 +020012113 "pll in active use but not on in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012114 I915_STATE_WARN(pll->on && !pll->active,
Daniel Vetter35c95372013-07-17 06:55:04 +020012115 "pll in on but not on in use in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012116 I915_STATE_WARN(pll->on != active,
Daniel Vetter53589012013-06-05 13:34:16 +020012117 "pll on state mismatch (expected %i, found %i)\n",
12118 pll->on, active);
12119
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012120 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080012121 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
Daniel Vetter53589012013-06-05 13:34:16 +020012122 enabled_crtcs++;
12123 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12124 active_crtcs++;
12125 }
Rob Clarke2c719b2014-12-15 13:56:32 -050012126 I915_STATE_WARN(pll->active != active_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012127 "pll active crtcs mismatch (expected %i, found %i)\n",
12128 pll->active, active_crtcs);
Rob Clarke2c719b2014-12-15 13:56:32 -050012129 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012130 "pll enabled crtcs mismatch (expected %i, found %i)\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012131 hweight32(pll->config.crtc_mask), enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012132
Rob Clarke2c719b2014-12-15 13:56:32 -050012133 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020012134 sizeof(dpll_hw_state)),
12135 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020012136 }
Daniel Vettera6778b32012-07-02 09:56:42 +020012137}
12138
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012139void
12140intel_modeset_check_state(struct drm_device *dev)
12141{
Damien Lespiau08db6652014-11-04 17:06:52 +000012142 check_wm_state(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012143 check_connector_state(dev);
12144 check_encoder_state(dev);
12145 check_crtc_state(dev);
12146 check_shared_dpll_state(dev);
12147}
12148
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012149void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
Ville Syrjälä18442d02013-09-13 16:00:08 +030012150 int dotclock)
12151{
12152 /*
12153 * FDI already provided one idea for the dotclock.
12154 * Yell if the encoder disagrees.
12155 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012156 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030012157 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012158 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030012159}
12160
Ville Syrjälä80715b22014-05-15 20:23:23 +030012161static void update_scanline_offset(struct intel_crtc *crtc)
12162{
12163 struct drm_device *dev = crtc->base.dev;
12164
12165 /*
12166 * The scanline counter increments at the leading edge of hsync.
12167 *
12168 * On most platforms it starts counting from vtotal-1 on the
12169 * first active line. That means the scanline counter value is
12170 * always one less than what we would expect. Ie. just after
12171 * start of vblank, which also occurs at start of hsync (on the
12172 * last active line), the scanline counter will read vblank_start-1.
12173 *
12174 * On gen2 the scanline counter starts counting from 1 instead
12175 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12176 * to keep the value positive), instead of adding one.
12177 *
12178 * On HSW+ the behaviour of the scanline counter depends on the output
12179 * type. For DP ports it behaves like most other platforms, but on HDMI
12180 * there's an extra 1 line difference. So we need to add two instead of
12181 * one to the value.
12182 */
12183 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012184 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012185 int vtotal;
12186
12187 vtotal = mode->crtc_vtotal;
12188 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
12189 vtotal /= 2;
12190
12191 crtc->scanline_offset = vtotal - 1;
12192 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030012193 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030012194 crtc->scanline_offset = 2;
12195 } else
12196 crtc->scanline_offset = 1;
12197}
12198
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012199static struct intel_crtc_state *
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012200intel_modeset_compute_config(struct drm_crtc *crtc,
12201 struct drm_display_mode *mode,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012202 struct drm_atomic_state *state,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012203 unsigned *modeset_pipes,
12204 unsigned *prepare_pipes,
12205 unsigned *disable_pipes)
12206{
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020012207 struct drm_device *dev = crtc->dev;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012208 struct intel_crtc_state *pipe_config = NULL;
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020012209 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012210 int ret = 0;
12211
12212 ret = drm_atomic_add_affected_connectors(state, crtc);
12213 if (ret)
12214 return ERR_PTR(ret);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012215
12216 intel_modeset_affected_pipes(crtc, modeset_pipes,
12217 prepare_pipes, disable_pipes);
12218
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012219 /*
12220 * Note this needs changes when we start tracking multiple modes
12221 * and crtcs. At that point we'll need to compute the whole config
12222 * (i.e. one pipe_config for each crtc) rather than just the one
12223 * for this crtc.
12224 */
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020012225 for_each_intel_crtc_masked(dev, *modeset_pipes, intel_crtc) {
12226 /* FIXME: For now we still expect modeset_pipes has at most
12227 * one bit set. */
12228 if (WARN_ON(&intel_crtc->base != crtc))
12229 continue;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012230
Daniel Vetter8805aa72015-04-10 16:22:38 +020012231 pipe_config = intel_modeset_pipe_config(crtc, mode, state);
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020012232 if (IS_ERR(pipe_config))
12233 return pipe_config;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012234
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020012235 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
12236 "[modeset]");
12237 }
12238
12239 return intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012240}
12241
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012242static int __intel_set_mode_setup_plls(struct drm_atomic_state *state,
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012243 unsigned modeset_pipes,
12244 unsigned disable_pipes)
12245{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012246 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012247 struct drm_i915_private *dev_priv = to_i915(dev);
12248 unsigned clear_pipes = modeset_pipes | disable_pipes;
12249 struct intel_crtc *intel_crtc;
12250 int ret = 0;
12251
12252 if (!dev_priv->display.crtc_compute_clock)
12253 return 0;
12254
12255 ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
12256 if (ret)
12257 goto done;
12258
12259 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012260 struct intel_crtc_state *crtc_state =
12261 intel_atomic_get_crtc_state(state, intel_crtc);
12262
12263 /* Modeset pipes should have a new state by now */
12264 if (WARN_ON(IS_ERR(crtc_state)))
12265 continue;
12266
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012267 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012268 crtc_state);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012269 if (ret) {
12270 intel_shared_dpll_abort_config(dev_priv);
12271 goto done;
12272 }
12273 }
12274
12275done:
12276 return ret;
12277}
12278
Daniel Vetterf30da182013-04-11 20:22:50 +020012279static int __intel_set_mode(struct drm_crtc *crtc,
12280 struct drm_display_mode *mode,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012281 int x, int y, struct drm_framebuffer *fb,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012282 struct intel_crtc_state *pipe_config,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012283 unsigned modeset_pipes,
12284 unsigned prepare_pipes,
12285 unsigned disable_pipes)
Daniel Vettera6778b32012-07-02 09:56:42 +020012286{
12287 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030012288 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030012289 struct drm_display_mode *saved_mode;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030012290 struct drm_atomic_state *state = pipe_config->base.state;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012291 struct intel_crtc_state *crtc_state_copy = NULL;
Daniel Vetter25c5b262012-07-08 22:08:04 +020012292 struct intel_crtc *intel_crtc;
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012293 int ret = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020012294
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030012295 saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012296 if (!saved_mode)
12297 return -ENOMEM;
Daniel Vettera6778b32012-07-02 09:56:42 +020012298
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012299 crtc_state_copy = kmalloc(sizeof(*crtc_state_copy), GFP_KERNEL);
12300 if (!crtc_state_copy) {
12301 ret = -ENOMEM;
12302 goto done;
12303 }
12304
Tim Gardner3ac18232012-12-07 07:54:26 -070012305 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +020012306
Jesse Barnes30a970c2013-11-04 13:48:12 -080012307 /*
12308 * See if the config requires any additional preparation, e.g.
12309 * to adjust global state with pipes off. We need to do this
12310 * here so we can get the modeset_pipe updated config for the new
12311 * mode set on this crtc. For other crtcs we need to use the
12312 * adjusted_mode bits in the crtc directly.
12313 */
Vandana Kannanf8437dd12014-11-24 13:37:39 +053012314 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030012315 ret = valleyview_modeset_global_pipes(state, &prepare_pipes);
12316 if (ret)
12317 goto done;
Jesse Barnes30a970c2013-11-04 13:48:12 -080012318
Ville Syrjäläc164f832013-11-05 22:34:12 +020012319 /* may have added more to prepare_pipes than we should */
12320 prepare_pipes &= ~disable_pipes;
12321 }
12322
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012323 ret = __intel_set_mode_setup_plls(state, modeset_pipes, disable_pipes);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012324 if (ret)
12325 goto done;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +020012326
Daniel Vetter460da9162013-03-27 00:44:51 +010012327 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
12328 intel_crtc_disable(&intel_crtc->base);
12329
Daniel Vetterea9d7582012-07-10 10:42:52 +020012330 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
Maarten Lankhorstce22dba2015-04-21 17:12:56 +030012331 if (intel_crtc->base.state->enable) {
12332 intel_crtc_disable_planes(&intel_crtc->base);
Daniel Vetterea9d7582012-07-10 10:42:52 +020012333 dev_priv->display.crtc_disable(&intel_crtc->base);
Maarten Lankhorstce22dba2015-04-21 17:12:56 +030012334 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012335 }
Daniel Vettera6778b32012-07-02 09:56:42 +020012336
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020012337 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
12338 * to set it here already despite that we pass it down the callchain.
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012339 *
12340 * Note we'll need to fix this up when we start tracking multiple
12341 * pipes; here we assume a single modeset_pipe and only track the
12342 * single crtc and mode.
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020012343 */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012344 if (modeset_pipes) {
Daniel Vetter25c5b262012-07-08 22:08:04 +020012345 crtc->mode = *mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012346 /* mode_set/enable/disable functions rely on a correct pipe
12347 * config. */
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020012348 intel_crtc_set_state(to_intel_crtc(crtc), pipe_config);
Ville Syrjäläc326c0a2013-10-28 12:53:41 +020012349
12350 /*
12351 * Calculate and store various constants which
12352 * are later needed by vblank and swap-completion
12353 * timestamping. They are derived from true hwmode.
12354 */
12355 drm_calc_timestamping_constants(crtc,
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012356 &pipe_config->base.adjusted_mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012357 }
Daniel Vetter7758a112012-07-08 19:40:39 +020012358
Daniel Vetterea9d7582012-07-10 10:42:52 +020012359 /* Only after disabling all output pipelines that will be changed can we
12360 * update the the output configuration. */
12361 intel_modeset_update_state(dev, prepare_pipes);
12362
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030012363 modeset_update_crtc_power_domains(state);
Daniel Vetter47fab732012-10-26 10:58:18 +020012364
Daniel Vetter25c5b262012-07-08 22:08:04 +020012365 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Gustavo Padovan455a6802014-12-01 15:40:11 -080012366 struct drm_plane *primary = intel_crtc->base.primary;
12367 int vdisplay, hdisplay;
Daniel Vetter4c107942014-04-24 23:55:05 +020012368
Gustavo Padovan455a6802014-12-01 15:40:11 -080012369 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
Matt Roper70a101f2015-04-08 18:56:53 -070012370 ret = drm_plane_helper_update(primary, &intel_crtc->base,
12371 fb, 0, 0,
12372 hdisplay, vdisplay,
12373 x << 16, y << 16,
12374 hdisplay << 16, vdisplay << 16);
Daniel Vettera6778b32012-07-02 09:56:42 +020012375 }
12376
12377 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ville Syrjälä80715b22014-05-15 20:23:23 +030012378 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
12379 update_scanline_offset(intel_crtc);
12380
Daniel Vetter25c5b262012-07-08 22:08:04 +020012381 dev_priv->display.crtc_enable(&intel_crtc->base);
Maarten Lankhorstce22dba2015-04-21 17:12:56 +030012382 intel_crtc_enable_planes(&intel_crtc->base);
Ville Syrjälä80715b22014-05-15 20:23:23 +030012383 }
Daniel Vettera6778b32012-07-02 09:56:42 +020012384
Daniel Vettera6778b32012-07-02 09:56:42 +020012385 /* FIXME: add subpixel order */
12386done:
Matt Roper83d65732015-02-25 13:12:16 -080012387 if (ret && crtc->state->enable)
Tim Gardner3ac18232012-12-07 07:54:26 -070012388 crtc->mode = *saved_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +020012389
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012390 if (ret == 0 && pipe_config) {
12391 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12392
12393 /* The pipe_config will be freed with the atomic state, so
12394 * make a copy. */
12395 memcpy(crtc_state_copy, intel_crtc->config,
12396 sizeof *crtc_state_copy);
12397 intel_crtc->config = crtc_state_copy;
12398 intel_crtc->base.state = &crtc_state_copy->base;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012399 } else {
12400 kfree(crtc_state_copy);
12401 }
12402
Tim Gardner3ac18232012-12-07 07:54:26 -070012403 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +020012404 return ret;
12405}
12406
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012407static int intel_set_mode_pipes(struct drm_crtc *crtc,
12408 struct drm_display_mode *mode,
12409 int x, int y, struct drm_framebuffer *fb,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012410 struct intel_crtc_state *pipe_config,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012411 unsigned modeset_pipes,
12412 unsigned prepare_pipes,
12413 unsigned disable_pipes)
12414{
12415 int ret;
12416
12417 ret = __intel_set_mode(crtc, mode, x, y, fb, pipe_config, modeset_pipes,
12418 prepare_pipes, disable_pipes);
12419
12420 if (ret == 0)
12421 intel_modeset_check_state(crtc->dev);
12422
12423 return ret;
12424}
12425
Damien Lespiaue7457a92013-08-08 22:28:59 +010012426static int intel_set_mode(struct drm_crtc *crtc,
12427 struct drm_display_mode *mode,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012428 int x, int y, struct drm_framebuffer *fb,
12429 struct drm_atomic_state *state)
Daniel Vetterf30da182013-04-11 20:22:50 +020012430{
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012431 struct intel_crtc_state *pipe_config;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012432 unsigned modeset_pipes, prepare_pipes, disable_pipes;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012433 int ret = 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020012434
Daniel Vetter8805aa72015-04-10 16:22:38 +020012435 pipe_config = intel_modeset_compute_config(crtc, mode, state,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012436 &modeset_pipes,
12437 &prepare_pipes,
12438 &disable_pipes);
Daniel Vetterf30da182013-04-11 20:22:50 +020012439
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012440 if (IS_ERR(pipe_config)) {
12441 ret = PTR_ERR(pipe_config);
12442 goto out;
12443 }
Daniel Vetterf30da182013-04-11 20:22:50 +020012444
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012445 ret = intel_set_mode_pipes(crtc, mode, x, y, fb, pipe_config,
12446 modeset_pipes, prepare_pipes,
12447 disable_pipes);
12448 if (ret)
12449 goto out;
12450
12451out:
12452 return ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020012453}
12454
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012455void intel_crtc_restore_mode(struct drm_crtc *crtc)
12456{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012457 struct drm_device *dev = crtc->dev;
12458 struct drm_atomic_state *state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030012459 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012460 struct intel_encoder *encoder;
12461 struct intel_connector *connector;
12462 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030012463 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012464
12465 state = drm_atomic_state_alloc(dev);
12466 if (!state) {
12467 DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
12468 crtc->base.id);
12469 return;
12470 }
12471
12472 state->acquire_ctx = dev->mode_config.acquire_ctx;
12473
12474 /* The force restore path in the HW readout code relies on the staged
12475 * config still keeping the user requested config while the actual
12476 * state has been overwritten by the configuration read from HW. We
12477 * need to copy the staged config to the atomic state, otherwise the
12478 * mode set will just reapply the state the HW is already in. */
12479 for_each_intel_encoder(dev, encoder) {
12480 if (&encoder->new_crtc->base != crtc)
12481 continue;
12482
12483 for_each_intel_connector(dev, connector) {
12484 if (connector->new_encoder != encoder)
12485 continue;
12486
12487 connector_state = drm_atomic_get_connector_state(state, &connector->base);
12488 if (IS_ERR(connector_state)) {
12489 DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
12490 connector->base.base.id,
12491 connector->base.name,
12492 PTR_ERR(connector_state));
12493 continue;
12494 }
12495
12496 connector_state->crtc = crtc;
12497 connector_state->best_encoder = &encoder->base;
12498 }
12499 }
12500
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030012501 for_each_intel_crtc(dev, intel_crtc) {
12502 if (intel_crtc->new_enabled == intel_crtc->base.enabled)
12503 continue;
12504
12505 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
12506 if (IS_ERR(crtc_state)) {
12507 DRM_DEBUG_KMS("Failed to add [CRTC:%d] to state: %ld\n",
12508 intel_crtc->base.base.id,
12509 PTR_ERR(crtc_state));
12510 continue;
12511 }
12512
12513 crtc_state->base.enable = intel_crtc->new_enabled;
12514 }
12515
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012516 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb,
12517 state);
12518
12519 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012520}
12521
Daniel Vetter25c5b262012-07-08 22:08:04 +020012522#undef for_each_intel_crtc_masked
12523
Daniel Vetterd9e55602012-07-04 22:16:09 +020012524static void intel_set_config_free(struct intel_set_config *config)
12525{
12526 if (!config)
12527 return;
12528
Daniel Vetter1aa4b622012-07-05 16:20:48 +020012529 kfree(config->save_connector_encoders);
12530 kfree(config->save_encoder_crtcs);
Ville Syrjälä76688512014-01-10 11:28:06 +020012531 kfree(config->save_crtc_enabled);
Daniel Vetterd9e55602012-07-04 22:16:09 +020012532 kfree(config);
12533}
12534
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012535static int intel_set_config_save_state(struct drm_device *dev,
12536 struct intel_set_config *config)
12537{
Ville Syrjälä76688512014-01-10 11:28:06 +020012538 struct drm_crtc *crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012539 struct drm_encoder *encoder;
12540 struct drm_connector *connector;
12541 int count;
12542
Ville Syrjälä76688512014-01-10 11:28:06 +020012543 config->save_crtc_enabled =
12544 kcalloc(dev->mode_config.num_crtc,
12545 sizeof(bool), GFP_KERNEL);
12546 if (!config->save_crtc_enabled)
12547 return -ENOMEM;
12548
Daniel Vetter1aa4b622012-07-05 16:20:48 +020012549 config->save_encoder_crtcs =
12550 kcalloc(dev->mode_config.num_encoder,
12551 sizeof(struct drm_crtc *), GFP_KERNEL);
12552 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012553 return -ENOMEM;
12554
Daniel Vetter1aa4b622012-07-05 16:20:48 +020012555 config->save_connector_encoders =
12556 kcalloc(dev->mode_config.num_connector,
12557 sizeof(struct drm_encoder *), GFP_KERNEL);
12558 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012559 return -ENOMEM;
12560
12561 /* Copy data. Note that driver private data is not affected.
12562 * Should anything bad happen only the expected state is
12563 * restored, not the drivers personal bookkeeping.
12564 */
12565 count = 0;
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010012566 for_each_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080012567 config->save_crtc_enabled[count++] = crtc->state->enable;
Ville Syrjälä76688512014-01-10 11:28:06 +020012568 }
12569
12570 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012571 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +020012572 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012573 }
12574
12575 count = 0;
12576 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +020012577 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012578 }
12579
12580 return 0;
12581}
12582
12583static void intel_set_config_restore_state(struct drm_device *dev,
12584 struct intel_set_config *config)
12585{
Ville Syrjälä76688512014-01-10 11:28:06 +020012586 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020012587 struct intel_encoder *encoder;
12588 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012589 int count;
12590
12591 count = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012592 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020012593 crtc->new_enabled = config->save_crtc_enabled[count++];
12594 }
12595
12596 count = 0;
Damien Lespiaub2784e12014-08-05 11:29:37 +010012597 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012598 encoder->new_crtc =
12599 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012600 }
12601
12602 count = 0;
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012603 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012604 connector->new_encoder =
12605 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012606 }
12607}
12608
Imre Deake3de42b2013-05-03 19:44:07 +020012609static bool
Chris Wilson2e57f472013-07-17 12:14:40 +010012610is_crtc_connector_off(struct drm_mode_set *set)
Imre Deake3de42b2013-05-03 19:44:07 +020012611{
12612 int i;
12613
Chris Wilson2e57f472013-07-17 12:14:40 +010012614 if (set->num_connectors == 0)
12615 return false;
12616
12617 if (WARN_ON(set->connectors == NULL))
12618 return false;
12619
12620 for (i = 0; i < set->num_connectors; i++)
12621 if (set->connectors[i]->encoder &&
12622 set->connectors[i]->encoder->crtc == set->crtc &&
12623 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
Imre Deake3de42b2013-05-03 19:44:07 +020012624 return true;
12625
12626 return false;
12627}
12628
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012629static void
12630intel_set_config_compute_mode_changes(struct drm_mode_set *set,
12631 struct intel_set_config *config)
12632{
Ander Conselvan de Oliveira462a4252015-04-21 17:13:00 +030012633 struct drm_device *dev = set->crtc->dev;
12634 struct intel_connector *connector;
12635 struct intel_encoder *encoder;
12636 struct intel_crtc *crtc;
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012637
12638 /* We should be able to check here if the fb has the same properties
12639 * and then just flip_or_move it */
Chris Wilson2e57f472013-07-17 12:14:40 +010012640 if (is_crtc_connector_off(set)) {
12641 config->mode_changed = true;
Matt Roperf4510a22014-04-01 15:22:40 -070012642 } else if (set->crtc->primary->fb != set->fb) {
Matt Roper3b150f02014-05-29 08:06:53 -070012643 /*
12644 * If we have no fb, we can only flip as long as the crtc is
12645 * active, otherwise we need a full mode set. The crtc may
12646 * be active if we've only disabled the primary plane, or
12647 * in fastboot situations.
12648 */
Matt Roperf4510a22014-04-01 15:22:40 -070012649 if (set->crtc->primary->fb == NULL) {
Jesse Barnes319d9822013-06-26 01:38:19 +030012650 struct intel_crtc *intel_crtc =
12651 to_intel_crtc(set->crtc);
12652
Matt Roper3b150f02014-05-29 08:06:53 -070012653 if (intel_crtc->active) {
Jesse Barnes319d9822013-06-26 01:38:19 +030012654 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
12655 config->fb_changed = true;
12656 } else {
12657 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
12658 config->mode_changed = true;
12659 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012660 } else if (set->fb == NULL) {
12661 config->mode_changed = true;
Daniel Vetter72f49012013-03-28 16:01:35 +010012662 } else if (set->fb->pixel_format !=
Matt Roperf4510a22014-04-01 15:22:40 -070012663 set->crtc->primary->fb->pixel_format) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012664 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +020012665 } else {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012666 config->fb_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +020012667 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012668 }
12669
Daniel Vetter835c5872012-07-10 18:11:08 +020012670 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012671 config->fb_changed = true;
12672
12673 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
12674 DRM_DEBUG_KMS("modes are different, full mode set\n");
12675 drm_mode_debug_printmodeline(&set->crtc->mode);
12676 drm_mode_debug_printmodeline(set->mode);
12677 config->mode_changed = true;
12678 }
Chris Wilsona1d95702013-08-13 18:48:47 +010012679
Ander Conselvan de Oliveira462a4252015-04-21 17:13:00 +030012680 for_each_intel_connector(dev, connector) {
12681 if (&connector->new_encoder->base == connector->base.encoder)
12682 continue;
12683
12684 config->mode_changed = true;
12685 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] encoder changed, full mode switch\n",
12686 connector->base.base.id,
12687 connector->base.name);
12688 }
12689
12690 for_each_intel_encoder(dev, encoder) {
12691 if (&encoder->new_crtc->base == encoder->base.crtc)
12692 continue;
12693
12694 DRM_DEBUG_KMS("[ENCODER:%d:%s] crtc changed, full mode switch\n",
12695 encoder->base.base.id,
12696 encoder->base.name);
12697 config->mode_changed = true;
12698 }
12699
12700 for_each_intel_crtc(dev, crtc) {
12701 if (crtc->new_enabled == crtc->base.state->enable)
12702 continue;
12703
12704 DRM_DEBUG_KMS("[CRTC:%d] %sabled, full mode switch\n",
12705 crtc->base.base.id,
12706 crtc->new_enabled ? "en" : "dis");
12707 config->mode_changed = true;
12708 }
12709
Chris Wilsona1d95702013-08-13 18:48:47 +010012710 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
12711 set->crtc->base.id, config->mode_changed, config->fb_changed);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012712}
12713
Daniel Vetter2e431052012-07-04 22:42:15 +020012714static int
Daniel Vetter9a935852012-07-05 22:34:27 +020012715intel_modeset_stage_output_state(struct drm_device *dev,
12716 struct drm_mode_set *set,
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012717 struct drm_atomic_state *state)
Daniel Vetter50f56112012-07-02 09:35:43 +020012718{
Daniel Vetter9a935852012-07-05 22:34:27 +020012719 struct intel_connector *connector;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012720 struct drm_connector_state *connector_state;
Daniel Vetter9a935852012-07-05 22:34:27 +020012721 struct intel_encoder *encoder;
Ville Syrjälä76688512014-01-10 11:28:06 +020012722 struct intel_crtc *crtc;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030012723 struct intel_crtc_state *crtc_state;
Paulo Zanonif3f08572013-08-12 14:56:53 -030012724 int ro;
Daniel Vetter50f56112012-07-02 09:35:43 +020012725
Damien Lespiau9abdda72013-02-13 13:29:23 +000012726 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +020012727 * of connectors. For paranoia, double-check this. */
12728 WARN_ON(!set->fb && (set->num_connectors != 0));
12729 WARN_ON(set->fb && (set->num_connectors == 0));
12730
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012731 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012732 /* Otherwise traverse passed in connector list and get encoders
12733 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +020012734 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012735 if (set->connectors[ro] == &connector->base) {
Dave Airlie0e32b392014-05-02 14:02:48 +100012736 connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
Daniel Vetter50f56112012-07-02 09:35:43 +020012737 break;
12738 }
12739 }
12740
Daniel Vetter9a935852012-07-05 22:34:27 +020012741 /* If we disable the crtc, disable all its connectors. Also, if
12742 * the connector is on the changing crtc but not on the new
12743 * connector list, disable it. */
12744 if ((!set->fb || ro == set->num_connectors) &&
12745 connector->base.encoder &&
12746 connector->base.encoder->crtc == set->crtc) {
12747 connector->new_encoder = NULL;
12748
12749 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
12750 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012751 connector->base.name);
Daniel Vetter9a935852012-07-05 22:34:27 +020012752 }
Daniel Vetter9a935852012-07-05 22:34:27 +020012753 }
12754 /* connector->new_encoder is now updated for all connectors. */
12755
12756 /* Update crtc of enabled connectors. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012757 for_each_intel_connector(dev, connector) {
Ville Syrjälä76688512014-01-10 11:28:06 +020012758 struct drm_crtc *new_crtc;
12759
Daniel Vetter9a935852012-07-05 22:34:27 +020012760 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +020012761 continue;
12762
Daniel Vetter9a935852012-07-05 22:34:27 +020012763 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +020012764
12765 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012766 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +020012767 new_crtc = set->crtc;
12768 }
12769
12770 /* Make sure the new CRTC will work with the encoder */
Thierry Reding14509912014-01-13 12:00:22 +010012771 if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
12772 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012773 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +020012774 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012775 connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
Daniel Vetter9a935852012-07-05 22:34:27 +020012776
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012777 connector_state =
12778 drm_atomic_get_connector_state(state, &connector->base);
12779 if (IS_ERR(connector_state))
12780 return PTR_ERR(connector_state);
12781
12782 connector_state->crtc = new_crtc;
12783 connector_state->best_encoder = &connector->new_encoder->base;
12784
Daniel Vetter9a935852012-07-05 22:34:27 +020012785 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
12786 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012787 connector->base.name,
Daniel Vetter9a935852012-07-05 22:34:27 +020012788 new_crtc->base.id);
12789 }
12790
12791 /* Check for any encoders that needs to be disabled. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010012792 for_each_intel_encoder(dev, encoder) {
Paulo Zanoni5a65f352014-01-07 14:55:53 -020012793 int num_connectors = 0;
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012794 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012795 if (connector->new_encoder == encoder) {
12796 WARN_ON(!connector->new_encoder->new_crtc);
Paulo Zanoni5a65f352014-01-07 14:55:53 -020012797 num_connectors++;
Daniel Vetter9a935852012-07-05 22:34:27 +020012798 }
12799 }
Paulo Zanoni5a65f352014-01-07 14:55:53 -020012800
12801 if (num_connectors == 0)
12802 encoder->new_crtc = NULL;
12803 else if (num_connectors > 1)
12804 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +020012805 }
Daniel Vetter9a935852012-07-05 22:34:27 +020012806 /* Now we've also updated encoder->new_crtc for all encoders. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012807 for_each_intel_connector(dev, connector) {
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012808 connector_state =
12809 drm_atomic_get_connector_state(state, &connector->base);
Ander Conselvan de Oliveira9d918c12015-03-27 15:33:51 +020012810 if (IS_ERR(connector_state))
12811 return PTR_ERR(connector_state);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012812
12813 if (connector->new_encoder) {
Dave Airlie0e32b392014-05-02 14:02:48 +100012814 if (connector->new_encoder != connector->encoder)
12815 connector->encoder = connector->new_encoder;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012816 } else {
12817 connector_state->crtc = NULL;
Ander Conselvan de Oliveiraf61cccf2015-03-31 11:35:00 +030012818 connector_state->best_encoder = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012819 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012820 }
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012821 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020012822 crtc->new_enabled = false;
12823
Damien Lespiaub2784e12014-08-05 11:29:37 +010012824 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä76688512014-01-10 11:28:06 +020012825 if (encoder->new_crtc == crtc) {
12826 crtc->new_enabled = true;
12827 break;
12828 }
12829 }
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030012830
12831 if (crtc->new_enabled != crtc->base.state->enable) {
12832 crtc_state = intel_atomic_get_crtc_state(state, crtc);
12833 if (IS_ERR(crtc_state))
12834 return PTR_ERR(crtc_state);
12835
12836 crtc_state->base.enable = crtc->new_enabled;
12837 }
Ville Syrjälä76688512014-01-10 11:28:06 +020012838 }
12839
Daniel Vetter2e431052012-07-04 22:42:15 +020012840 return 0;
12841}
12842
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020012843static void disable_crtc_nofb(struct intel_crtc *crtc)
12844{
12845 struct drm_device *dev = crtc->base.dev;
12846 struct intel_encoder *encoder;
12847 struct intel_connector *connector;
12848
12849 DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
12850 pipe_name(crtc->pipe));
12851
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012852 for_each_intel_connector(dev, connector) {
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020012853 if (connector->new_encoder &&
12854 connector->new_encoder->new_crtc == crtc)
12855 connector->new_encoder = NULL;
12856 }
12857
Damien Lespiaub2784e12014-08-05 11:29:37 +010012858 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020012859 if (encoder->new_crtc == crtc)
12860 encoder->new_crtc = NULL;
12861 }
12862
12863 crtc->new_enabled = false;
12864}
12865
Daniel Vetter2e431052012-07-04 22:42:15 +020012866static int intel_crtc_set_config(struct drm_mode_set *set)
12867{
12868 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +020012869 struct drm_mode_set save_set;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012870 struct drm_atomic_state *state = NULL;
Daniel Vetter2e431052012-07-04 22:42:15 +020012871 struct intel_set_config *config;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012872 struct intel_crtc_state *pipe_config;
Jesse Barnes50f52752014-11-07 13:11:00 -080012873 unsigned modeset_pipes, prepare_pipes, disable_pipes;
Daniel Vetter2e431052012-07-04 22:42:15 +020012874 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +020012875
Daniel Vetter8d3e3752012-07-05 16:09:09 +020012876 BUG_ON(!set);
12877 BUG_ON(!set->crtc);
12878 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +020012879
Daniel Vetter7e53f3a2013-01-21 10:52:17 +010012880 /* Enforce sane interface api - has been abused by the fb helper. */
12881 BUG_ON(!set->mode && set->fb);
12882 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +020012883
Daniel Vetter2e431052012-07-04 22:42:15 +020012884 if (set->fb) {
12885 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
12886 set->crtc->base.id, set->fb->base.id,
12887 (int)set->num_connectors, set->x, set->y);
12888 } else {
12889 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +020012890 }
12891
12892 dev = set->crtc->dev;
12893
12894 ret = -ENOMEM;
12895 config = kzalloc(sizeof(*config), GFP_KERNEL);
12896 if (!config)
12897 goto out_config;
12898
12899 ret = intel_set_config_save_state(dev, config);
12900 if (ret)
12901 goto out_config;
12902
12903 save_set.crtc = set->crtc;
12904 save_set.mode = &set->crtc->mode;
12905 save_set.x = set->crtc->x;
12906 save_set.y = set->crtc->y;
Matt Roperf4510a22014-04-01 15:22:40 -070012907 save_set.fb = set->crtc->primary->fb;
Daniel Vetter2e431052012-07-04 22:42:15 +020012908
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012909 state = drm_atomic_state_alloc(dev);
12910 if (!state) {
12911 ret = -ENOMEM;
12912 goto out_config;
12913 }
12914
12915 state->acquire_ctx = dev->mode_config.acquire_ctx;
12916
Ander Conselvan de Oliveira462a4252015-04-21 17:13:00 +030012917 ret = intel_modeset_stage_output_state(dev, set, state);
Daniel Vetter2e431052012-07-04 22:42:15 +020012918 if (ret)
12919 goto fail;
12920
Ander Conselvan de Oliveira462a4252015-04-21 17:13:00 +030012921 /* Compute whether we need a full modeset, only an fb base update or no
12922 * change at all. In the future we might also check whether only the
12923 * mode changed, e.g. for LVDS where we only change the panel fitter in
12924 * such cases. */
12925 intel_set_config_compute_mode_changes(set, config);
12926
Jesse Barnes50f52752014-11-07 13:11:00 -080012927 pipe_config = intel_modeset_compute_config(set->crtc, set->mode,
Daniel Vetter8805aa72015-04-10 16:22:38 +020012928 state,
Jesse Barnes50f52752014-11-07 13:11:00 -080012929 &modeset_pipes,
12930 &prepare_pipes,
12931 &disable_pipes);
Jesse Barnes20664592014-11-05 14:26:09 -080012932 if (IS_ERR(pipe_config)) {
Matt Roper6ac04832014-11-17 09:59:28 -080012933 ret = PTR_ERR(pipe_config);
Jesse Barnes50f52752014-11-07 13:11:00 -080012934 goto fail;
Jesse Barnes20664592014-11-05 14:26:09 -080012935 } else if (pipe_config) {
Ville Syrjäläb9950a12014-11-21 21:00:36 +020012936 if (pipe_config->has_audio !=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012937 to_intel_crtc(set->crtc)->config->has_audio)
Jesse Barnes20664592014-11-05 14:26:09 -080012938 config->mode_changed = true;
12939
Jesse Barnesaf15d2c2014-12-01 09:54:28 -080012940 /*
12941 * Note we have an issue here with infoframes: current code
12942 * only updates them on the full mode set path per hw
12943 * requirements. So here we should be checking for any
12944 * required changes and forcing a mode set.
12945 */
Jesse Barnes20664592014-11-05 14:26:09 -080012946 }
Jesse Barnes50f52752014-11-07 13:11:00 -080012947
Jesse Barnes1f9954d2014-11-05 14:26:10 -080012948 intel_update_pipe_size(to_intel_crtc(set->crtc));
12949
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012950 if (config->mode_changed) {
Jesse Barnes50f52752014-11-07 13:11:00 -080012951 ret = intel_set_mode_pipes(set->crtc, set->mode,
12952 set->x, set->y, set->fb, pipe_config,
12953 modeset_pipes, prepare_pipes,
12954 disable_pipes);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012955 } else if (config->fb_changed) {
Matt Roper3b150f02014-05-29 08:06:53 -070012956 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080012957 struct drm_plane *primary = set->crtc->primary;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030012958 struct intel_plane_state *plane_state =
12959 to_intel_plane_state(primary->state);
12960 bool was_visible = plane_state->visible;
Gustavo Padovan455a6802014-12-01 15:40:11 -080012961 int vdisplay, hdisplay;
Matt Roper3b150f02014-05-29 08:06:53 -070012962
Gustavo Padovan455a6802014-12-01 15:40:11 -080012963 drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
Matt Roper70a101f2015-04-08 18:56:53 -070012964 ret = drm_plane_helper_update(primary, set->crtc, set->fb,
12965 0, 0, hdisplay, vdisplay,
12966 set->x << 16, set->y << 16,
12967 hdisplay << 16, vdisplay << 16);
Matt Roper3b150f02014-05-29 08:06:53 -070012968
12969 /*
12970 * We need to make sure the primary plane is re-enabled if it
12971 * has previously been turned off.
12972 */
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030012973 plane_state = to_intel_plane_state(primary->state);
12974 if (ret == 0 && !was_visible && plane_state->visible) {
Matt Roper3b150f02014-05-29 08:06:53 -070012975 WARN_ON(!intel_crtc->active);
Maarten Lankhorst87d43002015-04-21 17:12:54 +030012976 intel_post_enable_primary(set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070012977 }
12978
Jesse Barnes7ca51a32014-01-07 13:50:49 -080012979 /*
12980 * In the fastboot case this may be our only check of the
12981 * state after boot. It would be better to only do it on
12982 * the first update, but we don't have a nice way of doing that
12983 * (and really, set_config isn't used much for high freq page
12984 * flipping, so increasing its cost here shouldn't be a big
12985 * deal).
12986 */
Jani Nikulad330a952014-01-21 11:24:25 +020012987 if (i915.fastboot && ret == 0)
Jesse Barnes7ca51a32014-01-07 13:50:49 -080012988 intel_modeset_check_state(set->crtc->dev);
Daniel Vetter50f56112012-07-02 09:35:43 +020012989 }
12990
Chris Wilson2d05eae2013-05-03 17:36:25 +010012991 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +020012992 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
12993 set->crtc->base.id, ret);
Daniel Vetter50f56112012-07-02 09:35:43 +020012994fail:
Chris Wilson2d05eae2013-05-03 17:36:25 +010012995 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +020012996
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012997 drm_atomic_state_clear(state);
12998
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020012999 /*
13000 * HACK: if the pipe was on, but we didn't have a framebuffer,
13001 * force the pipe off to avoid oopsing in the modeset code
13002 * due to fb==NULL. This should only happen during boot since
13003 * we don't yet reconstruct the FB from the hardware state.
13004 */
13005 if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
13006 disable_crtc_nofb(to_intel_crtc(save_set.crtc));
13007
Chris Wilson2d05eae2013-05-03 17:36:25 +010013008 /* Try to restore the config */
13009 if (config->mode_changed &&
13010 intel_set_mode(save_set.crtc, save_set.mode,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013011 save_set.x, save_set.y, save_set.fb,
13012 state))
Chris Wilson2d05eae2013-05-03 17:36:25 +010013013 DRM_ERROR("failed to restore config after modeset failure\n");
13014 }
Daniel Vetter50f56112012-07-02 09:35:43 +020013015
Daniel Vetterd9e55602012-07-04 22:16:09 +020013016out_config:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030013017 drm_atomic_state_free(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013018
Daniel Vetterd9e55602012-07-04 22:16:09 +020013019 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +020013020 return ret;
13021}
13022
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013023static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013024 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +020013025 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013026 .destroy = intel_crtc_destroy,
13027 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013028 .atomic_duplicate_state = intel_crtc_duplicate_state,
13029 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013030};
13031
Daniel Vetter53589012013-06-05 13:34:16 +020013032static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
13033 struct intel_shared_dpll *pll,
13034 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013035{
Daniel Vetter53589012013-06-05 13:34:16 +020013036 uint32_t val;
13037
Daniel Vetterf458ebb2014-09-30 10:56:39 +020013038 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030013039 return false;
13040
Daniel Vetter53589012013-06-05 13:34:16 +020013041 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020013042 hw_state->dpll = val;
13043 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
13044 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020013045
13046 return val & DPLL_VCO_ENABLE;
13047}
13048
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013049static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
13050 struct intel_shared_dpll *pll)
13051{
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013052 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
13053 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013054}
13055
Daniel Vettere7b903d2013-06-05 13:34:14 +020013056static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
13057 struct intel_shared_dpll *pll)
13058{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013059 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020013060 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020013061
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013062 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013063
13064 /* Wait for the clocks to stabilize. */
13065 POSTING_READ(PCH_DPLL(pll->id));
13066 udelay(150);
13067
13068 /* The pixel multiplier can only be updated once the
13069 * DPLL is enabled and the clocks are stable.
13070 *
13071 * So write it again.
13072 */
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013073 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013074 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013075 udelay(200);
13076}
13077
13078static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
13079 struct intel_shared_dpll *pll)
13080{
13081 struct drm_device *dev = dev_priv->dev;
13082 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013083
13084 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013085 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020013086 if (intel_crtc_to_shared_dpll(crtc) == pll)
13087 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
13088 }
13089
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013090 I915_WRITE(PCH_DPLL(pll->id), 0);
13091 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013092 udelay(200);
13093}
13094
Daniel Vetter46edb022013-06-05 13:34:12 +020013095static char *ibx_pch_dpll_names[] = {
13096 "PCH DPLL A",
13097 "PCH DPLL B",
13098};
13099
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013100static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013101{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013102 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013103 int i;
13104
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013105 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013106
Daniel Vettere72f9fb2013-06-05 13:34:06 +020013107 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020013108 dev_priv->shared_dplls[i].id = i;
13109 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013110 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013111 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
13112 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020013113 dev_priv->shared_dplls[i].get_hw_state =
13114 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013115 }
13116}
13117
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013118static void intel_shared_dpll_init(struct drm_device *dev)
13119{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013120 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013121
Daniel Vetter9cd86932014-06-25 22:01:57 +030013122 if (HAS_DDI(dev))
13123 intel_ddi_pll_init(dev);
13124 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013125 ibx_pch_dpll_init(dev);
13126 else
13127 dev_priv->num_shared_dpll = 0;
13128
13129 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013130}
13131
Matt Roper6beb8c232014-12-01 15:40:14 -080013132/**
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +000013133 * intel_wm_need_update - Check whether watermarks need updating
13134 * @plane: drm plane
13135 * @state: new plane state
13136 *
13137 * Check current plane state versus the new one to determine whether
13138 * watermarks need to be recalculated.
13139 *
13140 * Returns true or false.
13141 */
13142bool intel_wm_need_update(struct drm_plane *plane,
13143 struct drm_plane_state *state)
13144{
13145 /* Update watermarks on tiling changes. */
13146 if (!plane->state->fb || !state->fb ||
13147 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
13148 plane->state->rotation != state->rotation)
13149 return true;
13150
13151 return false;
13152}
13153
13154/**
Matt Roper6beb8c232014-12-01 15:40:14 -080013155 * intel_prepare_plane_fb - Prepare fb for usage on plane
13156 * @plane: drm plane to prepare for
13157 * @fb: framebuffer to prepare for presentation
13158 *
13159 * Prepares a framebuffer for usage on a display plane. Generally this
13160 * involves pinning the underlying object and updating the frontbuffer tracking
13161 * bits. Some older platforms need special physical address handling for
13162 * cursor planes.
13163 *
13164 * Returns 0 on success, negative error code on failure.
13165 */
13166int
13167intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013168 struct drm_framebuffer *fb,
13169 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013170{
13171 struct drm_device *dev = plane->dev;
Matt Roper6beb8c232014-12-01 15:40:14 -080013172 struct intel_plane *intel_plane = to_intel_plane(plane);
13173 enum pipe pipe = intel_plane->pipe;
13174 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13175 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
13176 unsigned frontbuffer_bits = 0;
13177 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013178
Matt Roperea2c67b2014-12-23 10:41:52 -080013179 if (!obj)
Matt Roper465c1202014-05-29 08:06:54 -070013180 return 0;
13181
Matt Roper6beb8c232014-12-01 15:40:14 -080013182 switch (plane->type) {
13183 case DRM_PLANE_TYPE_PRIMARY:
13184 frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
13185 break;
13186 case DRM_PLANE_TYPE_CURSOR:
13187 frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
13188 break;
13189 case DRM_PLANE_TYPE_OVERLAY:
13190 frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
13191 break;
13192 }
Matt Roper465c1202014-05-29 08:06:54 -070013193
Matt Roper4c345742014-07-09 16:22:10 -070013194 mutex_lock(&dev->struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070013195
Matt Roper6beb8c232014-12-01 15:40:14 -080013196 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13197 INTEL_INFO(dev)->cursor_needs_physical) {
13198 int align = IS_I830(dev) ? 16 * 1024 : 256;
13199 ret = i915_gem_object_attach_phys(obj, align);
13200 if (ret)
13201 DRM_DEBUG_KMS("failed to attach phys object\n");
13202 } else {
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000013203 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
Matt Roper6beb8c232014-12-01 15:40:14 -080013204 }
13205
13206 if (ret == 0)
13207 i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
13208
13209 mutex_unlock(&dev->struct_mutex);
13210
13211 return ret;
13212}
13213
Matt Roper38f3ce32014-12-02 07:45:25 -080013214/**
13215 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13216 * @plane: drm plane to clean up for
13217 * @fb: old framebuffer that was on plane
13218 *
13219 * Cleans up a framebuffer that has just been removed from a plane.
13220 */
13221void
13222intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013223 struct drm_framebuffer *fb,
13224 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013225{
13226 struct drm_device *dev = plane->dev;
13227 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13228
13229 if (WARN_ON(!obj))
13230 return;
13231
13232 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
13233 !INTEL_INFO(dev)->cursor_needs_physical) {
13234 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000013235 intel_unpin_fb_obj(fb, old_state);
Matt Roper38f3ce32014-12-02 07:45:25 -080013236 mutex_unlock(&dev->struct_mutex);
13237 }
Matt Roper465c1202014-05-29 08:06:54 -070013238}
13239
Chandra Konduru6156a452015-04-27 13:48:39 -070013240int
13241skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13242{
13243 int max_scale;
13244 struct drm_device *dev;
13245 struct drm_i915_private *dev_priv;
13246 int crtc_clock, cdclk;
13247
13248 if (!intel_crtc || !crtc_state)
13249 return DRM_PLANE_HELPER_NO_SCALING;
13250
13251 dev = intel_crtc->base.dev;
13252 dev_priv = dev->dev_private;
13253 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
13254 cdclk = dev_priv->display.get_display_clock_speed(dev);
13255
13256 if (!crtc_clock || !cdclk)
13257 return DRM_PLANE_HELPER_NO_SCALING;
13258
13259 /*
13260 * skl max scale is lower of:
13261 * close to 3 but not 3, -1 is for that purpose
13262 * or
13263 * cdclk/crtc_clock
13264 */
13265 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13266
13267 return max_scale;
13268}
13269
Matt Roper465c1202014-05-29 08:06:54 -070013270static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013271intel_check_primary_plane(struct drm_plane *plane,
13272 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013273{
Matt Roper32b7eee2014-12-24 07:59:06 -080013274 struct drm_device *dev = plane->dev;
13275 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roper2b875c22014-12-01 15:40:13 -080013276 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013277 struct intel_crtc *intel_crtc;
Chandra Konduru6156a452015-04-27 13:48:39 -070013278 struct intel_crtc_state *crtc_state;
Matt Roper2b875c22014-12-01 15:40:13 -080013279 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013280 struct drm_rect *dest = &state->dst;
13281 struct drm_rect *src = &state->src;
13282 const struct drm_rect *clip = &state->clip;
Sonika Jindald8106362015-04-10 14:37:28 +053013283 bool can_position = false;
Chandra Konduru6156a452015-04-27 13:48:39 -070013284 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13285 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013286 int ret;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013287
Matt Roperea2c67b2014-12-23 10:41:52 -080013288 crtc = crtc ? crtc : plane->crtc;
13289 intel_crtc = to_intel_crtc(crtc);
Chandra Konduru6156a452015-04-27 13:48:39 -070013290 crtc_state = state->base.state ?
13291 intel_atomic_get_crtc_state(state->base.state, intel_crtc) : NULL;
Matt Roperea2c67b2014-12-23 10:41:52 -080013292
Chandra Konduru6156a452015-04-27 13:48:39 -070013293 if (INTEL_INFO(dev)->gen >= 9) {
13294 min_scale = 1;
13295 max_scale = skl_max_scale(intel_crtc, crtc_state);
Sonika Jindald8106362015-04-10 14:37:28 +053013296 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070013297 }
Sonika Jindald8106362015-04-10 14:37:28 +053013298
Matt Roperc59cb172014-12-01 15:40:16 -080013299 ret = drm_plane_helper_check_update(plane, crtc, fb,
13300 src, dest, clip,
Chandra Konduru6156a452015-04-27 13:48:39 -070013301 min_scale,
13302 max_scale,
Sonika Jindald8106362015-04-10 14:37:28 +053013303 can_position, true,
13304 &state->visible);
Matt Roperc59cb172014-12-01 15:40:16 -080013305 if (ret)
13306 return ret;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013307
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013308 if (intel_crtc->active) {
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013309 struct intel_plane_state *old_state =
13310 to_intel_plane_state(plane->state);
13311
Matt Roper32b7eee2014-12-24 07:59:06 -080013312 intel_crtc->atomic.wait_for_flips = true;
13313
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013314 /*
13315 * FBC does not work on some platforms for rotated
13316 * planes, so disable it when rotation is not 0 and
13317 * update it when rotation is set back to 0.
13318 *
13319 * FIXME: This is redundant with the fbc update done in
13320 * the primary plane enable function except that that
13321 * one is done too late. We eventually need to unify
13322 * this.
13323 */
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013324 if (state->visible &&
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013325 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
Paulo Zanonie35fef22015-02-09 14:46:29 -020013326 dev_priv->fbc.crtc == intel_crtc &&
Matt Roper8e7d6882015-01-21 16:35:41 -080013327 state->base.rotation != BIT(DRM_ROTATE_0)) {
Matt Roper32b7eee2014-12-24 07:59:06 -080013328 intel_crtc->atomic.disable_fbc = true;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013329 }
13330
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013331 if (state->visible && !old_state->visible) {
Matt Roper32b7eee2014-12-24 07:59:06 -080013332 /*
13333 * BDW signals flip done immediately if the plane
13334 * is disabled, even if the plane enable is already
13335 * armed to occur at the next vblank :(
13336 */
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013337 if (IS_BROADWELL(dev))
Matt Roper32b7eee2014-12-24 07:59:06 -080013338 intel_crtc->atomic.wait_vblank = true;
13339 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013340
Matt Roper32b7eee2014-12-24 07:59:06 -080013341 intel_crtc->atomic.fb_bits |=
13342 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
13343
13344 intel_crtc->atomic.update_fbc = true;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +000013345
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +000013346 if (intel_wm_need_update(plane, &state->base))
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +000013347 intel_crtc->atomic.update_wm = true;
Matt Roperc59cb172014-12-01 15:40:16 -080013348 }
13349
Chandra Konduru6156a452015-04-27 13:48:39 -070013350 if (INTEL_INFO(dev)->gen >= 9) {
13351 ret = skl_update_scaler_users(intel_crtc, crtc_state,
13352 to_intel_plane(plane), state, 0);
13353 if (ret)
13354 return ret;
13355 }
13356
Matt Roperc59cb172014-12-01 15:40:16 -080013357 return 0;
Matt Roper465c1202014-05-29 08:06:54 -070013358}
13359
Sonika Jindal48404c12014-08-22 14:06:04 +053013360static void
13361intel_commit_primary_plane(struct drm_plane *plane,
13362 struct intel_plane_state *state)
13363{
Matt Roper2b875c22014-12-01 15:40:13 -080013364 struct drm_crtc *crtc = state->base.crtc;
13365 struct drm_framebuffer *fb = state->base.fb;
13366 struct drm_device *dev = plane->dev;
Sonika Jindal48404c12014-08-22 14:06:04 +053013367 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperea2c67b2014-12-23 10:41:52 -080013368 struct intel_crtc *intel_crtc;
Sonika Jindalce54d852014-08-21 11:44:39 +053013369 struct drm_rect *src = &state->src;
Matt Ropercf4c7c12014-12-04 10:27:42 -080013370
Matt Roperea2c67b2014-12-23 10:41:52 -080013371 crtc = crtc ? crtc : plane->crtc;
13372 intel_crtc = to_intel_crtc(crtc);
13373
Matt Ropercf4c7c12014-12-04 10:27:42 -080013374 plane->fb = fb;
Sonika Jindalce54d852014-08-21 11:44:39 +053013375 crtc->x = src->x1 >> 16;
Matt Roper465c1202014-05-29 08:06:54 -070013376 crtc->y = src->y1 >> 16;
13377
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013378 if (intel_crtc->active) {
Maarten Lankhorst27321ae2015-04-21 17:12:52 +030013379 if (state->visible)
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013380 /* FIXME: kill this fastboot hack */
13381 intel_update_pipe_size(intel_crtc);
13382
Maarten Lankhorst27321ae2015-04-21 17:12:52 +030013383 dev_priv->display.update_primary_plane(crtc, plane->fb,
13384 crtc->x, crtc->y);
Matt Roper32b7eee2014-12-24 07:59:06 -080013385 }
13386}
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013387
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013388static void
13389intel_disable_primary_plane(struct drm_plane *plane,
13390 struct drm_crtc *crtc,
13391 bool force)
13392{
13393 struct drm_device *dev = plane->dev;
13394 struct drm_i915_private *dev_priv = dev->dev_private;
13395
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013396 dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
13397}
13398
Matt Roper32b7eee2014-12-24 07:59:06 -080013399static void intel_begin_crtc_commit(struct drm_crtc *crtc)
13400{
13401 struct drm_device *dev = crtc->dev;
13402 struct drm_i915_private *dev_priv = dev->dev_private;
13403 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080013404 struct intel_plane *intel_plane;
13405 struct drm_plane *p;
13406 unsigned fb_bits = 0;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013407
Matt Roperea2c67b2014-12-23 10:41:52 -080013408 /* Track fb's for any planes being disabled */
13409 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
13410 intel_plane = to_intel_plane(p);
13411
13412 if (intel_crtc->atomic.disabled_planes &
13413 (1 << drm_plane_index(p))) {
13414 switch (p->type) {
13415 case DRM_PLANE_TYPE_PRIMARY:
13416 fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
13417 break;
13418 case DRM_PLANE_TYPE_CURSOR:
13419 fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
13420 break;
13421 case DRM_PLANE_TYPE_OVERLAY:
13422 fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
13423 break;
13424 }
13425
13426 mutex_lock(&dev->struct_mutex);
13427 i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
13428 mutex_unlock(&dev->struct_mutex);
13429 }
13430 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013431
Matt Roper32b7eee2014-12-24 07:59:06 -080013432 if (intel_crtc->atomic.wait_for_flips)
13433 intel_crtc_wait_for_pending_flips(crtc);
13434
13435 if (intel_crtc->atomic.disable_fbc)
13436 intel_fbc_disable(dev);
13437
13438 if (intel_crtc->atomic.pre_disable_primary)
13439 intel_pre_disable_primary(crtc);
13440
13441 if (intel_crtc->atomic.update_wm)
13442 intel_update_watermarks(crtc);
13443
13444 intel_runtime_pm_get(dev_priv);
Matt Roperc34c9ee2014-12-23 10:41:50 -080013445
13446 /* Perform vblank evasion around commit operation */
13447 if (intel_crtc->active)
13448 intel_crtc->atomic.evade =
13449 intel_pipe_update_start(intel_crtc,
13450 &intel_crtc->atomic.start_vbl_count);
Matt Roper32b7eee2014-12-24 07:59:06 -080013451}
13452
13453static void intel_finish_crtc_commit(struct drm_crtc *crtc)
13454{
13455 struct drm_device *dev = crtc->dev;
13456 struct drm_i915_private *dev_priv = dev->dev_private;
13457 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13458 struct drm_plane *p;
13459
Matt Roperc34c9ee2014-12-23 10:41:50 -080013460 if (intel_crtc->atomic.evade)
13461 intel_pipe_update_end(intel_crtc,
13462 intel_crtc->atomic.start_vbl_count);
13463
Matt Roper32b7eee2014-12-24 07:59:06 -080013464 intel_runtime_pm_put(dev_priv);
13465
13466 if (intel_crtc->atomic.wait_vblank)
13467 intel_wait_for_vblank(dev, intel_crtc->pipe);
13468
13469 intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
13470
13471 if (intel_crtc->atomic.update_fbc) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013472 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020013473 intel_fbc_update(dev);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013474 mutex_unlock(&dev->struct_mutex);
13475 }
Matt Roper465c1202014-05-29 08:06:54 -070013476
Matt Roper32b7eee2014-12-24 07:59:06 -080013477 if (intel_crtc->atomic.post_enable_primary)
13478 intel_post_enable_primary(crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013479
Matt Roper32b7eee2014-12-24 07:59:06 -080013480 drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
13481 if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
13482 intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
13483 false, false);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013484
Matt Roper32b7eee2014-12-24 07:59:06 -080013485 memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013486}
13487
Matt Ropercf4c7c12014-12-04 10:27:42 -080013488/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013489 * intel_plane_destroy - destroy a plane
13490 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013491 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013492 * Common destruction function for all types of planes (primary, cursor,
13493 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013494 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013495void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013496{
13497 struct intel_plane *intel_plane = to_intel_plane(plane);
13498 drm_plane_cleanup(plane);
13499 kfree(intel_plane);
13500}
13501
Matt Roper65a3fea2015-01-21 16:35:42 -080013502const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013503 .update_plane = drm_atomic_helper_update_plane,
13504 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013505 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080013506 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080013507 .atomic_get_property = intel_plane_atomic_get_property,
13508 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013509 .atomic_duplicate_state = intel_plane_duplicate_state,
13510 .atomic_destroy_state = intel_plane_destroy_state,
13511
Matt Roper465c1202014-05-29 08:06:54 -070013512};
13513
13514static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13515 int pipe)
13516{
13517 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080013518 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070013519 const uint32_t *intel_primary_formats;
13520 int num_formats;
13521
13522 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13523 if (primary == NULL)
13524 return NULL;
13525
Matt Roper8e7d6882015-01-21 16:35:41 -080013526 state = intel_create_plane_state(&primary->base);
13527 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013528 kfree(primary);
13529 return NULL;
13530 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013531 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013532
Matt Roper465c1202014-05-29 08:06:54 -070013533 primary->can_scale = false;
13534 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013535 if (INTEL_INFO(dev)->gen >= 9) {
13536 primary->can_scale = true;
13537 }
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013538 state->scaler_id = -1;
Matt Roper465c1202014-05-29 08:06:54 -070013539 primary->pipe = pipe;
13540 primary->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080013541 primary->check_plane = intel_check_primary_plane;
13542 primary->commit_plane = intel_commit_primary_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013543 primary->disable_plane = intel_disable_primary_plane;
Chandra Konduru08e221f2015-04-07 15:28:37 -070013544 primary->ckey.flags = I915_SET_COLORKEY_NONE;
Matt Roper465c1202014-05-29 08:06:54 -070013545 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13546 primary->plane = !pipe;
13547
13548 if (INTEL_INFO(dev)->gen <= 3) {
13549 intel_primary_formats = intel_primary_formats_gen2;
13550 num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
13551 } else {
13552 intel_primary_formats = intel_primary_formats_gen4;
13553 num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
13554 }
13555
13556 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013557 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070013558 intel_primary_formats, num_formats,
13559 DRM_PLANE_TYPE_PRIMARY);
Sonika Jindal48404c12014-08-22 14:06:04 +053013560
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013561 if (INTEL_INFO(dev)->gen >= 4)
13562 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053013563
Matt Roperea2c67b2014-12-23 10:41:52 -080013564 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13565
Matt Roper465c1202014-05-29 08:06:54 -070013566 return &primary->base;
13567}
13568
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013569void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13570{
13571 if (!dev->mode_config.rotation_property) {
13572 unsigned long flags = BIT(DRM_ROTATE_0) |
13573 BIT(DRM_ROTATE_180);
13574
13575 if (INTEL_INFO(dev)->gen >= 9)
13576 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13577
13578 dev->mode_config.rotation_property =
13579 drm_mode_create_rotation_property(dev, flags);
13580 }
13581 if (dev->mode_config.rotation_property)
13582 drm_object_attach_property(&plane->base.base,
13583 dev->mode_config.rotation_property,
13584 plane->base.state->rotation);
13585}
13586
Matt Roper3d7d6512014-06-10 08:28:13 -070013587static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030013588intel_check_cursor_plane(struct drm_plane *plane,
13589 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070013590{
Matt Roper2b875c22014-12-01 15:40:13 -080013591 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013592 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080013593 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013594 struct drm_rect *dest = &state->dst;
13595 struct drm_rect *src = &state->src;
13596 const struct drm_rect *clip = &state->clip;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013597 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Matt Roperea2c67b2014-12-23 10:41:52 -080013598 struct intel_crtc *intel_crtc;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013599 unsigned stride;
13600 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013601
Matt Roperea2c67b2014-12-23 10:41:52 -080013602 crtc = crtc ? crtc : plane->crtc;
13603 intel_crtc = to_intel_crtc(crtc);
13604
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013605 ret = drm_plane_helper_check_update(plane, crtc, fb,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013606 src, dest, clip,
13607 DRM_PLANE_HELPER_NO_SCALING,
13608 DRM_PLANE_HELPER_NO_SCALING,
13609 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013610 if (ret)
13611 return ret;
13612
13613
13614 /* if we want to turn off the cursor ignore width and height */
13615 if (!obj)
Matt Roper32b7eee2014-12-24 07:59:06 -080013616 goto finish;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013617
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013618 /* Check for which cursor types we support */
Matt Roperea2c67b2014-12-23 10:41:52 -080013619 if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
13620 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13621 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013622 return -EINVAL;
13623 }
13624
Matt Roperea2c67b2014-12-23 10:41:52 -080013625 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13626 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013627 DRM_DEBUG_KMS("buffer is too small\n");
13628 return -ENOMEM;
13629 }
13630
Ville Syrjälä3a656b52015-03-09 21:08:37 +020013631 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013632 DRM_DEBUG_KMS("cursor cannot be tiled\n");
13633 ret = -EINVAL;
13634 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013635
Matt Roper32b7eee2014-12-24 07:59:06 -080013636finish:
13637 if (intel_crtc->active) {
Ville Syrjälä3749f462015-03-10 13:15:22 +020013638 if (plane->state->crtc_w != state->base.crtc_w)
Matt Roper32b7eee2014-12-24 07:59:06 -080013639 intel_crtc->atomic.update_wm = true;
13640
13641 intel_crtc->atomic.fb_bits |=
13642 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
13643 }
13644
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013645 return ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013646}
13647
Matt Roperf4a2cf22014-12-01 15:40:12 -080013648static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013649intel_disable_cursor_plane(struct drm_plane *plane,
13650 struct drm_crtc *crtc,
13651 bool force)
13652{
13653 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13654
13655 if (!force) {
13656 plane->fb = NULL;
13657 intel_crtc->cursor_bo = NULL;
13658 intel_crtc->cursor_addr = 0;
13659 }
13660
13661 intel_crtc_update_cursor(crtc, false);
13662}
13663
13664static void
Gustavo Padovan852e7872014-09-05 17:22:31 -030013665intel_commit_cursor_plane(struct drm_plane *plane,
13666 struct intel_plane_state *state)
13667{
Matt Roper2b875c22014-12-01 15:40:13 -080013668 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013669 struct drm_device *dev = plane->dev;
13670 struct intel_crtc *intel_crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080013671 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080013672 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070013673
Matt Roperea2c67b2014-12-23 10:41:52 -080013674 crtc = crtc ? crtc : plane->crtc;
13675 intel_crtc = to_intel_crtc(crtc);
Sonika Jindala919db92014-10-23 07:41:33 -070013676
Matt Roperea2c67b2014-12-23 10:41:52 -080013677 plane->fb = state->base.fb;
13678 crtc->cursor_x = state->base.crtc_x;
13679 crtc->cursor_y = state->base.crtc_y;
13680
Gustavo Padovana912f122014-12-01 15:40:10 -080013681 if (intel_crtc->cursor_bo == obj)
13682 goto update;
13683
Matt Roperf4a2cf22014-12-01 15:40:12 -080013684 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080013685 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080013686 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080013687 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080013688 else
Gustavo Padovana912f122014-12-01 15:40:10 -080013689 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080013690
Gustavo Padovana912f122014-12-01 15:40:10 -080013691 intel_crtc->cursor_addr = addr;
13692 intel_crtc->cursor_bo = obj;
13693update:
Gustavo Padovana912f122014-12-01 15:40:10 -080013694
Matt Roper32b7eee2014-12-24 07:59:06 -080013695 if (intel_crtc->active)
Gustavo Padovan852e7872014-09-05 17:22:31 -030013696 intel_crtc_update_cursor(crtc, state->visible);
Matt Roper3d7d6512014-06-10 08:28:13 -070013697}
Gustavo Padovan852e7872014-09-05 17:22:31 -030013698
Matt Roper3d7d6512014-06-10 08:28:13 -070013699static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
13700 int pipe)
13701{
13702 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080013703 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070013704
13705 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
13706 if (cursor == NULL)
13707 return NULL;
13708
Matt Roper8e7d6882015-01-21 16:35:41 -080013709 state = intel_create_plane_state(&cursor->base);
13710 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013711 kfree(cursor);
13712 return NULL;
13713 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013714 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013715
Matt Roper3d7d6512014-06-10 08:28:13 -070013716 cursor->can_scale = false;
13717 cursor->max_downscale = 1;
13718 cursor->pipe = pipe;
13719 cursor->plane = pipe;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013720 state->scaler_id = -1;
Matt Roperc59cb172014-12-01 15:40:16 -080013721 cursor->check_plane = intel_check_cursor_plane;
13722 cursor->commit_plane = intel_commit_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013723 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070013724
13725 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013726 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070013727 intel_cursor_formats,
13728 ARRAY_SIZE(intel_cursor_formats),
13729 DRM_PLANE_TYPE_CURSOR);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013730
13731 if (INTEL_INFO(dev)->gen >= 4) {
13732 if (!dev->mode_config.rotation_property)
13733 dev->mode_config.rotation_property =
13734 drm_mode_create_rotation_property(dev,
13735 BIT(DRM_ROTATE_0) |
13736 BIT(DRM_ROTATE_180));
13737 if (dev->mode_config.rotation_property)
13738 drm_object_attach_property(&cursor->base.base,
13739 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080013740 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013741 }
13742
Matt Roperea2c67b2014-12-23 10:41:52 -080013743 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
13744
Matt Roper3d7d6512014-06-10 08:28:13 -070013745 return &cursor->base;
13746}
13747
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013748static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
13749 struct intel_crtc_state *crtc_state)
13750{
13751 int i;
13752 struct intel_scaler *intel_scaler;
13753 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
13754
13755 for (i = 0; i < intel_crtc->num_scalers; i++) {
13756 intel_scaler = &scaler_state->scalers[i];
13757 intel_scaler->in_use = 0;
13758 intel_scaler->id = i;
13759
13760 intel_scaler->mode = PS_SCALER_MODE_DYN;
13761 }
13762
13763 scaler_state->scaler_id = -1;
13764}
13765
Hannes Ederb358d0a2008-12-18 21:18:47 +010013766static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080013767{
Jani Nikulafbee40d2014-03-31 14:27:18 +030013768 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080013769 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013770 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070013771 struct drm_plane *primary = NULL;
13772 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070013773 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080013774
Daniel Vetter955382f2013-09-19 14:05:45 +020013775 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080013776 if (intel_crtc == NULL)
13777 return;
13778
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013779 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
13780 if (!crtc_state)
13781 goto fail;
13782 intel_crtc_set_state(intel_crtc, crtc_state);
Matt Roper07878242015-02-25 11:43:26 -080013783 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013784
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013785 /* initialize shared scalers */
13786 if (INTEL_INFO(dev)->gen >= 9) {
13787 if (pipe == PIPE_C)
13788 intel_crtc->num_scalers = 1;
13789 else
13790 intel_crtc->num_scalers = SKL_NUM_SCALERS;
13791
13792 skl_init_scalers(dev, intel_crtc, crtc_state);
13793 }
13794
Matt Roper465c1202014-05-29 08:06:54 -070013795 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070013796 if (!primary)
13797 goto fail;
13798
13799 cursor = intel_cursor_plane_create(dev, pipe);
13800 if (!cursor)
13801 goto fail;
13802
Matt Roper465c1202014-05-29 08:06:54 -070013803 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Matt Roper3d7d6512014-06-10 08:28:13 -070013804 cursor, &intel_crtc_funcs);
13805 if (ret)
13806 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080013807
13808 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080013809 for (i = 0; i < 256; i++) {
13810 intel_crtc->lut_r[i] = i;
13811 intel_crtc->lut_g[i] = i;
13812 intel_crtc->lut_b[i] = i;
13813 }
13814
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020013815 /*
13816 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020013817 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020013818 */
Jesse Barnes80824002009-09-10 15:28:06 -070013819 intel_crtc->pipe = pipe;
13820 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010013821 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080013822 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010013823 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070013824 }
13825
Chris Wilson4b0e3332014-05-30 16:35:26 +030013826 intel_crtc->cursor_base = ~0;
13827 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030013828 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030013829
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080013830 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
13831 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
13832 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
13833 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
13834
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020013835 INIT_WORK(&intel_crtc->mmio_flip.work, intel_mmio_flip_work_func);
13836
Jesse Barnes79e53942008-11-07 14:24:08 -080013837 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020013838
13839 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070013840 return;
13841
13842fail:
13843 if (primary)
13844 drm_plane_cleanup(primary);
13845 if (cursor)
13846 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013847 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070013848 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080013849}
13850
Jesse Barnes752aa882013-10-31 18:55:49 +020013851enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
13852{
13853 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020013854 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020013855
Rob Clark51fd3712013-11-19 12:10:12 -050013856 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020013857
Ville Syrjäläd3babd32014-11-07 11:16:01 +020013858 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020013859 return INVALID_PIPE;
13860
13861 return to_intel_crtc(encoder->crtc)->pipe;
13862}
13863
Carl Worth08d7b3d2009-04-29 14:43:54 -070013864int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000013865 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070013866{
Carl Worth08d7b3d2009-04-29 14:43:54 -070013867 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040013868 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020013869 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013870
Rob Clark7707e652014-07-17 23:30:04 -040013871 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070013872
Rob Clark7707e652014-07-17 23:30:04 -040013873 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070013874 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030013875 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013876 }
13877
Rob Clark7707e652014-07-17 23:30:04 -040013878 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020013879 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013880
Daniel Vetterc05422d2009-08-11 16:05:30 +020013881 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013882}
13883
Daniel Vetter66a92782012-07-12 20:08:18 +020013884static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080013885{
Daniel Vetter66a92782012-07-12 20:08:18 +020013886 struct drm_device *dev = encoder->base.dev;
13887 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080013888 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080013889 int entry = 0;
13890
Damien Lespiaub2784e12014-08-05 11:29:37 +010013891 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020013892 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020013893 index_mask |= (1 << entry);
13894
Jesse Barnes79e53942008-11-07 14:24:08 -080013895 entry++;
13896 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010013897
Jesse Barnes79e53942008-11-07 14:24:08 -080013898 return index_mask;
13899}
13900
Chris Wilson4d302442010-12-14 19:21:29 +000013901static bool has_edp_a(struct drm_device *dev)
13902{
13903 struct drm_i915_private *dev_priv = dev->dev_private;
13904
13905 if (!IS_MOBILE(dev))
13906 return false;
13907
13908 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
13909 return false;
13910
Damien Lespiaue3589902014-02-07 19:12:50 +000013911 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000013912 return false;
13913
13914 return true;
13915}
13916
Jesse Barnes84b4e042014-06-25 08:24:29 -070013917static bool intel_crt_present(struct drm_device *dev)
13918{
13919 struct drm_i915_private *dev_priv = dev->dev_private;
13920
Damien Lespiau884497e2013-12-03 13:56:23 +000013921 if (INTEL_INFO(dev)->gen >= 9)
13922 return false;
13923
Damien Lespiaucf404ce2014-10-01 20:04:15 +010013924 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070013925 return false;
13926
13927 if (IS_CHERRYVIEW(dev))
13928 return false;
13929
13930 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
13931 return false;
13932
13933 return true;
13934}
13935
Jesse Barnes79e53942008-11-07 14:24:08 -080013936static void intel_setup_outputs(struct drm_device *dev)
13937{
Eric Anholt725e30a2009-01-22 13:01:02 -080013938 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010013939 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013940 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080013941
Daniel Vetterc9093352013-06-06 22:22:47 +020013942 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080013943
Jesse Barnes84b4e042014-06-25 08:24:29 -070013944 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020013945 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013946
Vandana Kannanc776eb22014-08-19 12:05:01 +053013947 if (IS_BROXTON(dev)) {
13948 /*
13949 * FIXME: Broxton doesn't support port detection via the
13950 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
13951 * detect the ports.
13952 */
13953 intel_ddi_init(dev, PORT_A);
13954 intel_ddi_init(dev, PORT_B);
13955 intel_ddi_init(dev, PORT_C);
13956 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013957 int found;
13958
Jesse Barnesde31fac2015-03-06 15:53:32 -080013959 /*
13960 * Haswell uses DDI functions to detect digital outputs.
13961 * On SKL pre-D0 the strap isn't connected, so we assume
13962 * it's there.
13963 */
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013964 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080013965 /* WaIgnoreDDIAStrap: skl */
13966 if (found ||
13967 (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013968 intel_ddi_init(dev, PORT_A);
13969
13970 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
13971 * register */
13972 found = I915_READ(SFUSE_STRAP);
13973
13974 if (found & SFUSE_STRAP_DDIB_DETECTED)
13975 intel_ddi_init(dev, PORT_B);
13976 if (found & SFUSE_STRAP_DDIC_DETECTED)
13977 intel_ddi_init(dev, PORT_C);
13978 if (found & SFUSE_STRAP_DDID_DETECTED)
13979 intel_ddi_init(dev, PORT_D);
13980 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013981 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020013982 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020013983
13984 if (has_edp_a(dev))
13985 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013986
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013987 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080013988 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010013989 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013990 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013991 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013992 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013993 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013994 }
13995
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013996 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013997 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013998
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013999 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014000 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014001
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014002 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014003 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014004
Daniel Vetter270b3042012-10-27 15:52:05 +020014005 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014006 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070014007 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014008 /*
14009 * The DP_DETECTED bit is the latched state of the DDC
14010 * SDA pin at boot. However since eDP doesn't require DDC
14011 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14012 * eDP ports may have been muxed to an alternate function.
14013 * Thus we can't rely on the DP_DETECTED bit alone to detect
14014 * eDP ports. Consult the VBT as well as DP_DETECTED to
14015 * detect eDP ports.
14016 */
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014017 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
14018 !intel_dp_is_edp(dev, PORT_B))
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014019 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
14020 PORT_B);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014021 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
14022 intel_dp_is_edp(dev, PORT_B))
14023 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014024
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014025 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
14026 !intel_dp_is_edp(dev, PORT_C))
Jesse Barnes6f6005a2013-08-09 09:34:35 -070014027 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
14028 PORT_C);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014029 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
14030 intel_dp_is_edp(dev, PORT_C))
14031 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014032
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014033 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014034 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014035 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
14036 PORT_D);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014037 /* eDP not supported on port D, so don't check VBT */
14038 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
14039 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014040 }
14041
Jani Nikula3cfca972013-08-27 15:12:26 +030014042 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +080014043 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014044 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014045
Paulo Zanonie2debe92013-02-18 19:00:27 -030014046 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014047 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014048 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014049 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
14050 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014051 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014052 }
Ma Ling27185ae2009-08-24 13:50:23 +080014053
Imre Deake7281ea2013-05-08 13:14:08 +030014054 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014055 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014056 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014057
14058 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014059
Paulo Zanonie2debe92013-02-18 19:00:27 -030014060 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014061 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014062 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014063 }
Ma Ling27185ae2009-08-24 13:50:23 +080014064
Paulo Zanonie2debe92013-02-18 19:00:27 -030014065 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014066
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014067 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
14068 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014069 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014070 }
Imre Deake7281ea2013-05-08 13:14:08 +030014071 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014072 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014073 }
Ma Ling27185ae2009-08-24 13:50:23 +080014074
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014075 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014076 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014077 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014078 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014079 intel_dvo_init(dev);
14080
Zhenyu Wang103a1962009-11-27 11:44:36 +080014081 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014082 intel_tv_init(dev);
14083
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014084 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014085
Damien Lespiaub2784e12014-08-05 11:29:37 +010014086 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014087 encoder->base.possible_crtcs = encoder->crtc_mask;
14088 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014089 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014090 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014091
Paulo Zanonidde86e22012-12-01 12:04:25 -020014092 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014093
14094 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014095}
14096
14097static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14098{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014099 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014100 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014101
Daniel Vetteref2d6332014-02-10 18:00:38 +010014102 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014103 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014104 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014105 drm_gem_object_unreference(&intel_fb->obj->base);
14106 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014107 kfree(intel_fb);
14108}
14109
14110static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014111 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014112 unsigned int *handle)
14113{
14114 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014115 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014116
Chris Wilson05394f32010-11-08 19:18:58 +000014117 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014118}
14119
14120static const struct drm_framebuffer_funcs intel_fb_funcs = {
14121 .destroy = intel_user_framebuffer_destroy,
14122 .create_handle = intel_user_framebuffer_create_handle,
14123};
14124
Damien Lespiaub3218032015-02-27 11:15:18 +000014125static
14126u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14127 uint32_t pixel_format)
14128{
14129 u32 gen = INTEL_INFO(dev)->gen;
14130
14131 if (gen >= 9) {
14132 /* "The stride in bytes must not exceed the of the size of 8K
14133 * pixels and 32K bytes."
14134 */
14135 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
14136 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
14137 return 32*1024;
14138 } else if (gen >= 4) {
14139 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14140 return 16*1024;
14141 else
14142 return 32*1024;
14143 } else if (gen >= 3) {
14144 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14145 return 8*1024;
14146 else
14147 return 16*1024;
14148 } else {
14149 /* XXX DSPC is limited to 4k tiled */
14150 return 8*1024;
14151 }
14152}
14153
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014154static int intel_framebuffer_init(struct drm_device *dev,
14155 struct intel_framebuffer *intel_fb,
14156 struct drm_mode_fb_cmd2 *mode_cmd,
14157 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014158{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014159 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014160 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014161 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014162
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014163 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14164
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014165 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14166 /* Enforce that fb modifier and tiling mode match, but only for
14167 * X-tiled. This is needed for FBC. */
14168 if (!!(obj->tiling_mode == I915_TILING_X) !=
14169 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14170 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14171 return -EINVAL;
14172 }
14173 } else {
14174 if (obj->tiling_mode == I915_TILING_X)
14175 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14176 else if (obj->tiling_mode == I915_TILING_Y) {
14177 DRM_DEBUG("No Y tiling for legacy addfb\n");
14178 return -EINVAL;
14179 }
14180 }
14181
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014182 /* Passed in modifier sanity checking. */
14183 switch (mode_cmd->modifier[0]) {
14184 case I915_FORMAT_MOD_Y_TILED:
14185 case I915_FORMAT_MOD_Yf_TILED:
14186 if (INTEL_INFO(dev)->gen < 9) {
14187 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14188 mode_cmd->modifier[0]);
14189 return -EINVAL;
14190 }
14191 case DRM_FORMAT_MOD_NONE:
14192 case I915_FORMAT_MOD_X_TILED:
14193 break;
14194 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014195 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14196 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014197 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014198 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014199
Damien Lespiaub3218032015-02-27 11:15:18 +000014200 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
14201 mode_cmd->pixel_format);
14202 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14203 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14204 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014205 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014206 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014207
Damien Lespiaub3218032015-02-27 11:15:18 +000014208 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14209 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014210 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014211 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14212 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014213 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014214 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014215 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014216 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014217
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014218 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014219 mode_cmd->pitches[0] != obj->stride) {
14220 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14221 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014222 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014223 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014224
Ville Syrjälä57779d02012-10-31 17:50:14 +020014225 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014226 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014227 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014228 case DRM_FORMAT_RGB565:
14229 case DRM_FORMAT_XRGB8888:
14230 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014231 break;
14232 case DRM_FORMAT_XRGB1555:
14233 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014234 if (INTEL_INFO(dev)->gen > 3) {
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 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014239 break;
14240 case DRM_FORMAT_XBGR8888:
14241 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014242 case DRM_FORMAT_XRGB2101010:
14243 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014244 case DRM_FORMAT_XBGR2101010:
14245 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014246 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014247 DRM_DEBUG("unsupported pixel format: %s\n",
14248 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014249 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014250 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014251 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014252 case DRM_FORMAT_YUYV:
14253 case DRM_FORMAT_UYVY:
14254 case DRM_FORMAT_YVYU:
14255 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014256 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014257 DRM_DEBUG("unsupported pixel format: %s\n",
14258 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014259 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014260 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014261 break;
14262 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014263 DRM_DEBUG("unsupported pixel format: %s\n",
14264 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014265 return -EINVAL;
14266 }
14267
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014268 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14269 if (mode_cmd->offsets[0] != 0)
14270 return -EINVAL;
14271
Damien Lespiauec2c9812015-01-20 12:51:45 +000014272 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014273 mode_cmd->pixel_format,
14274 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014275 /* FIXME drm helper for size checks (especially planar formats)? */
14276 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14277 return -EINVAL;
14278
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014279 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14280 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020014281 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014282
Jesse Barnes79e53942008-11-07 14:24:08 -080014283 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14284 if (ret) {
14285 DRM_ERROR("framebuffer init failed %d\n", ret);
14286 return ret;
14287 }
14288
Jesse Barnes79e53942008-11-07 14:24:08 -080014289 return 0;
14290}
14291
Jesse Barnes79e53942008-11-07 14:24:08 -080014292static struct drm_framebuffer *
14293intel_user_framebuffer_create(struct drm_device *dev,
14294 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014295 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014296{
Chris Wilson05394f32010-11-08 19:18:58 +000014297 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014298
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014299 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
14300 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014301 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014302 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014303
Chris Wilsond2dff872011-04-19 08:36:26 +010014304 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080014305}
14306
Daniel Vetter4520f532013-10-09 09:18:51 +020014307#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020014308static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014309{
14310}
14311#endif
14312
Jesse Barnes79e53942008-11-07 14:24:08 -080014313static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014314 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014315 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014316 .atomic_check = intel_atomic_check,
14317 .atomic_commit = intel_atomic_commit,
Jesse Barnes79e53942008-11-07 14:24:08 -080014318};
14319
Jesse Barnese70236a2009-09-21 10:42:27 -070014320/* Set up chip specific display functions */
14321static void intel_init_display(struct drm_device *dev)
14322{
14323 struct drm_i915_private *dev_priv = dev->dev_private;
14324
Daniel Vetteree9300b2013-06-03 22:40:22 +020014325 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14326 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030014327 else if (IS_CHERRYVIEW(dev))
14328 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020014329 else if (IS_VALLEYVIEW(dev))
14330 dev_priv->display.find_dpll = vlv_find_best_dpll;
14331 else if (IS_PINEVIEW(dev))
14332 dev_priv->display.find_dpll = pnv_find_best_dpll;
14333 else
14334 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14335
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014336 if (INTEL_INFO(dev)->gen >= 9) {
14337 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014338 dev_priv->display.get_initial_plane_config =
14339 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014340 dev_priv->display.crtc_compute_clock =
14341 haswell_crtc_compute_clock;
14342 dev_priv->display.crtc_enable = haswell_crtc_enable;
14343 dev_priv->display.crtc_disable = haswell_crtc_disable;
14344 dev_priv->display.off = ironlake_crtc_off;
14345 dev_priv->display.update_primary_plane =
14346 skylake_update_primary_plane;
14347 } else if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014348 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014349 dev_priv->display.get_initial_plane_config =
14350 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014351 dev_priv->display.crtc_compute_clock =
14352 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014353 dev_priv->display.crtc_enable = haswell_crtc_enable;
14354 dev_priv->display.crtc_disable = haswell_crtc_disable;
Daniel Vetterdf8ad702014-06-25 22:02:03 +030014355 dev_priv->display.off = ironlake_crtc_off;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014356 dev_priv->display.update_primary_plane =
14357 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030014358 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014359 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014360 dev_priv->display.get_initial_plane_config =
14361 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014362 dev_priv->display.crtc_compute_clock =
14363 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014364 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14365 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014366 dev_priv->display.off = ironlake_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070014367 dev_priv->display.update_primary_plane =
14368 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014369 } else if (IS_VALLEYVIEW(dev)) {
14370 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014371 dev_priv->display.get_initial_plane_config =
14372 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014373 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014374 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14375 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14376 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070014377 dev_priv->display.update_primary_plane =
14378 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014379 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014380 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014381 dev_priv->display.get_initial_plane_config =
14382 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014383 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014384 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14385 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014386 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070014387 dev_priv->display.update_primary_plane =
14388 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014389 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014390
Jesse Barnese70236a2009-09-21 10:42:27 -070014391 /* Returns the core display clock speed */
Ville Syrjälä1652d192015-03-31 14:12:01 +030014392 if (IS_SKYLAKE(dev))
14393 dev_priv->display.get_display_clock_speed =
14394 skylake_get_display_clock_speed;
14395 else if (IS_BROADWELL(dev))
14396 dev_priv->display.get_display_clock_speed =
14397 broadwell_get_display_clock_speed;
14398 else if (IS_HASWELL(dev))
14399 dev_priv->display.get_display_clock_speed =
14400 haswell_get_display_clock_speed;
14401 else if (IS_VALLEYVIEW(dev))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070014402 dev_priv->display.get_display_clock_speed =
14403 valleyview_get_display_clock_speed;
Ville Syrjäläb37a6432015-03-31 14:11:54 +030014404 else if (IS_GEN5(dev))
14405 dev_priv->display.get_display_clock_speed =
14406 ilk_get_display_clock_speed;
Ville Syrjäläa7c66cd2015-03-31 14:11:56 +030014407 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
14408 IS_GEN6(dev) || IS_IVYBRIDGE(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -070014409 dev_priv->display.get_display_clock_speed =
14410 i945_get_display_clock_speed;
14411 else if (IS_I915G(dev))
14412 dev_priv->display.get_display_clock_speed =
14413 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014414 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014415 dev_priv->display.get_display_clock_speed =
14416 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014417 else if (IS_PINEVIEW(dev))
14418 dev_priv->display.get_display_clock_speed =
14419 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014420 else if (IS_I915GM(dev))
14421 dev_priv->display.get_display_clock_speed =
14422 i915gm_get_display_clock_speed;
14423 else if (IS_I865G(dev))
14424 dev_priv->display.get_display_clock_speed =
14425 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020014426 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014427 dev_priv->display.get_display_clock_speed =
14428 i855_get_display_clock_speed;
14429 else /* 852, 830 */
14430 dev_priv->display.get_display_clock_speed =
14431 i830_get_display_clock_speed;
14432
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014433 if (IS_GEN5(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014434 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014435 } else if (IS_GEN6(dev)) {
14436 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014437 } else if (IS_IVYBRIDGE(dev)) {
14438 /* FIXME: detect B0+ stepping and use auto training */
14439 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Paulo Zanoni059b2fe2014-09-02 16:53:57 -030014440 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014441 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Jesse Barnes30a970c2013-11-04 13:48:12 -080014442 } else if (IS_VALLEYVIEW(dev)) {
14443 dev_priv->display.modeset_global_resources =
14444 valleyview_modeset_global_resources;
Vandana Kannanf8437dd12014-11-24 13:37:39 +053014445 } else if (IS_BROXTON(dev)) {
14446 dev_priv->display.modeset_global_resources =
14447 broxton_modeset_global_resources;
Jesse Barnese70236a2009-09-21 10:42:27 -070014448 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014449
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014450 switch (INTEL_INFO(dev)->gen) {
14451 case 2:
14452 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14453 break;
14454
14455 case 3:
14456 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14457 break;
14458
14459 case 4:
14460 case 5:
14461 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14462 break;
14463
14464 case 6:
14465 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14466 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014467 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070014468 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014469 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14470 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000014471 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000014472 /* Drop through - unsupported since execlist only. */
14473 default:
14474 /* Default just returns -ENODEV to indicate unsupported */
14475 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014476 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020014477
14478 intel_panel_init_backlight_funcs(dev);
Ville Syrjäläe39b9992014-09-04 14:53:14 +030014479
14480 mutex_init(&dev_priv->pps_mutex);
Jesse Barnese70236a2009-09-21 10:42:27 -070014481}
14482
Jesse Barnesb690e962010-07-19 13:53:12 -070014483/*
14484 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14485 * resume, or other times. This quirk makes sure that's the case for
14486 * affected systems.
14487 */
Akshay Joshi0206e352011-08-16 15:34:10 -040014488static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070014489{
14490 struct drm_i915_private *dev_priv = dev->dev_private;
14491
14492 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014493 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014494}
14495
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014496static void quirk_pipeb_force(struct drm_device *dev)
14497{
14498 struct drm_i915_private *dev_priv = dev->dev_private;
14499
14500 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14501 DRM_INFO("applying pipe b force quirk\n");
14502}
14503
Keith Packard435793d2011-07-12 14:56:22 -070014504/*
14505 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14506 */
14507static void quirk_ssc_force_disable(struct drm_device *dev)
14508{
14509 struct drm_i915_private *dev_priv = dev->dev_private;
14510 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014511 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070014512}
14513
Carsten Emde4dca20e2012-03-15 15:56:26 +010014514/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010014515 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14516 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010014517 */
14518static void quirk_invert_brightness(struct drm_device *dev)
14519{
14520 struct drm_i915_private *dev_priv = dev->dev_private;
14521 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014522 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014523}
14524
Scot Doyle9c72cc62014-07-03 23:27:50 +000014525/* Some VBT's incorrectly indicate no backlight is present */
14526static void quirk_backlight_present(struct drm_device *dev)
14527{
14528 struct drm_i915_private *dev_priv = dev->dev_private;
14529 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14530 DRM_INFO("applying backlight present quirk\n");
14531}
14532
Jesse Barnesb690e962010-07-19 13:53:12 -070014533struct intel_quirk {
14534 int device;
14535 int subsystem_vendor;
14536 int subsystem_device;
14537 void (*hook)(struct drm_device *dev);
14538};
14539
Egbert Eich5f85f172012-10-14 15:46:38 +020014540/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14541struct intel_dmi_quirk {
14542 void (*hook)(struct drm_device *dev);
14543 const struct dmi_system_id (*dmi_id_list)[];
14544};
14545
14546static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14547{
14548 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14549 return 1;
14550}
14551
14552static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14553 {
14554 .dmi_id_list = &(const struct dmi_system_id[]) {
14555 {
14556 .callback = intel_dmi_reverse_brightness,
14557 .ident = "NCR Corporation",
14558 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14559 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14560 },
14561 },
14562 { } /* terminating entry */
14563 },
14564 .hook = quirk_invert_brightness,
14565 },
14566};
14567
Ben Widawskyc43b5632012-04-16 14:07:40 -070014568static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070014569 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -040014570 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -070014571
Jesse Barnesb690e962010-07-19 13:53:12 -070014572 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14573 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14574
Jesse Barnesb690e962010-07-19 13:53:12 -070014575 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14576 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14577
Ville Syrjälä5f080c02014-08-15 01:22:06 +030014578 /* 830 needs to leave pipe A & dpll A up */
14579 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14580
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014581 /* 830 needs to leave pipe B & dpll B up */
14582 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14583
Keith Packard435793d2011-07-12 14:56:22 -070014584 /* Lenovo U160 cannot use SSC on LVDS */
14585 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020014586
14587 /* Sony Vaio Y cannot use SSC on LVDS */
14588 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010014589
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010014590 /* Acer Aspire 5734Z must invert backlight brightness */
14591 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14592
14593 /* Acer/eMachines G725 */
14594 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14595
14596 /* Acer/eMachines e725 */
14597 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14598
14599 /* Acer/Packard Bell NCL20 */
14600 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14601
14602 /* Acer Aspire 4736Z */
14603 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020014604
14605 /* Acer Aspire 5336 */
14606 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000014607
14608 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14609 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000014610
Scot Doyledfb3d47b2014-08-21 16:08:02 +000014611 /* Acer C720 Chromebook (Core i3 4005U) */
14612 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14613
jens steinb2a96012014-10-28 20:25:53 +010014614 /* Apple Macbook 2,1 (Core 2 T7400) */
14615 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14616
Scot Doyled4967d82014-07-03 23:27:52 +000014617 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14618 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000014619
14620 /* HP Chromebook 14 (Celeron 2955U) */
14621 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020014622
14623 /* Dell Chromebook 11 */
14624 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070014625};
14626
14627static void intel_init_quirks(struct drm_device *dev)
14628{
14629 struct pci_dev *d = dev->pdev;
14630 int i;
14631
14632 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14633 struct intel_quirk *q = &intel_quirks[i];
14634
14635 if (d->device == q->device &&
14636 (d->subsystem_vendor == q->subsystem_vendor ||
14637 q->subsystem_vendor == PCI_ANY_ID) &&
14638 (d->subsystem_device == q->subsystem_device ||
14639 q->subsystem_device == PCI_ANY_ID))
14640 q->hook(dev);
14641 }
Egbert Eich5f85f172012-10-14 15:46:38 +020014642 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14643 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14644 intel_dmi_quirks[i].hook(dev);
14645 }
Jesse Barnesb690e962010-07-19 13:53:12 -070014646}
14647
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014648/* Disable the VGA plane that we never use */
14649static void i915_disable_vga(struct drm_device *dev)
14650{
14651 struct drm_i915_private *dev_priv = dev->dev_private;
14652 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020014653 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014654
Ville Syrjälä2b37c612014-01-22 21:32:38 +020014655 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014656 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070014657 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014658 sr1 = inb(VGA_SR_DATA);
14659 outb(sr1 | 1<<5, VGA_SR_DATA);
14660 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
14661 udelay(300);
14662
Ville Syrjälä01f5a622014-12-16 18:38:37 +020014663 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014664 POSTING_READ(vga_reg);
14665}
14666
Daniel Vetterf8175862012-04-10 15:50:11 +020014667void intel_modeset_init_hw(struct drm_device *dev)
14668{
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030014669 intel_prepare_ddi(dev);
14670
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +030014671 if (IS_VALLEYVIEW(dev))
14672 vlv_update_cdclk(dev);
14673
Daniel Vetterf8175862012-04-10 15:50:11 +020014674 intel_init_clock_gating(dev);
14675
Daniel Vetter8090c6b2012-06-24 16:42:32 +020014676 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020014677}
14678
Jesse Barnes79e53942008-11-07 14:24:08 -080014679void intel_modeset_init(struct drm_device *dev)
14680{
Jesse Barnes652c3932009-08-17 13:31:43 -070014681 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000014682 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014683 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080014684 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080014685
14686 drm_mode_config_init(dev);
14687
14688 dev->mode_config.min_width = 0;
14689 dev->mode_config.min_height = 0;
14690
Dave Airlie019d96c2011-09-29 16:20:42 +010014691 dev->mode_config.preferred_depth = 24;
14692 dev->mode_config.prefer_shadow = 1;
14693
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000014694 dev->mode_config.allow_fb_modifiers = true;
14695
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020014696 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080014697
Jesse Barnesb690e962010-07-19 13:53:12 -070014698 intel_init_quirks(dev);
14699
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030014700 intel_init_pm(dev);
14701
Ben Widawskye3c74752013-04-05 13:12:39 -070014702 if (INTEL_INFO(dev)->num_pipes == 0)
14703 return;
14704
Jesse Barnese70236a2009-09-21 10:42:27 -070014705 intel_init_display(dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014706 intel_init_audio(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070014707
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014708 if (IS_GEN2(dev)) {
14709 dev->mode_config.max_width = 2048;
14710 dev->mode_config.max_height = 2048;
14711 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070014712 dev->mode_config.max_width = 4096;
14713 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080014714 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014715 dev->mode_config.max_width = 8192;
14716 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080014717 }
Damien Lespiau068be562014-03-28 14:17:49 +000014718
Ville Syrjälädc41c152014-08-13 11:57:05 +030014719 if (IS_845G(dev) || IS_I865G(dev)) {
14720 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
14721 dev->mode_config.cursor_height = 1023;
14722 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000014723 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
14724 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
14725 } else {
14726 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
14727 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
14728 }
14729
Ben Widawsky5d4545a2013-01-17 12:45:15 -080014730 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080014731
Zhao Yakui28c97732009-10-09 11:39:41 +080014732 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014733 INTEL_INFO(dev)->num_pipes,
14734 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080014735
Damien Lespiau055e3932014-08-18 13:49:10 +010014736 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014737 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000014738 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000014739 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070014740 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030014741 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000014742 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070014743 }
Jesse Barnes79e53942008-11-07 14:24:08 -080014744 }
14745
Jesse Barnesf42bb702013-12-16 16:34:23 -080014746 intel_init_dpio(dev);
14747
Daniel Vettere72f9fb2013-06-05 13:34:06 +020014748 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014749
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014750 /* Just disable it once at startup */
14751 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014752 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000014753
14754 /* Just in case the BIOS is doing something questionable. */
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020014755 intel_fbc_disable(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080014756
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014757 drm_modeset_lock_all(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080014758 intel_modeset_setup_hw_state(dev, false);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014759 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014760
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014761 for_each_intel_crtc(dev, crtc) {
Jesse Barnes46f297f2014-03-07 08:57:48 -080014762 if (!crtc->active)
14763 continue;
14764
Jesse Barnes46f297f2014-03-07 08:57:48 -080014765 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080014766 * Note that reserving the BIOS fb up front prevents us
14767 * from stuffing other stolen allocations like the ring
14768 * on top. This prevents some ugliness at boot time, and
14769 * can even allow for smooth boot transitions if the BIOS
14770 * fb is large enough for the active pipe configuration.
14771 */
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014772 if (dev_priv->display.get_initial_plane_config) {
14773 dev_priv->display.get_initial_plane_config(crtc,
Jesse Barnes46f297f2014-03-07 08:57:48 -080014774 &crtc->plane_config);
14775 /*
14776 * If the fb is shared between multiple heads, we'll
14777 * just get the first one.
14778 */
Daniel Vetterf6936e22015-03-26 12:17:05 +010014779 intel_find_initial_plane_obj(crtc, &crtc->plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014780 }
Jesse Barnes46f297f2014-03-07 08:57:48 -080014781 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010014782}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080014783
Daniel Vetter7fad7982012-07-04 17:51:47 +020014784static void intel_enable_pipe_a(struct drm_device *dev)
14785{
14786 struct intel_connector *connector;
14787 struct drm_connector *crt = NULL;
14788 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030014789 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020014790
14791 /* We can't just switch on the pipe A, we need to set things up with a
14792 * proper mode and output configuration. As a gross hack, enable pipe A
14793 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014794 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020014795 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
14796 crt = &connector->base;
14797 break;
14798 }
14799 }
14800
14801 if (!crt)
14802 return;
14803
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030014804 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020014805 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020014806}
14807
Daniel Vetterfa555832012-10-10 23:14:00 +020014808static bool
14809intel_check_plane_mapping(struct intel_crtc *crtc)
14810{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014811 struct drm_device *dev = crtc->base.dev;
14812 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020014813 u32 reg, val;
14814
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014815 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020014816 return true;
14817
14818 reg = DSPCNTR(!crtc->plane);
14819 val = I915_READ(reg);
14820
14821 if ((val & DISPLAY_PLANE_ENABLE) &&
14822 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
14823 return false;
14824
14825 return true;
14826}
14827
Daniel Vetter24929352012-07-02 20:28:59 +020014828static void intel_sanitize_crtc(struct intel_crtc *crtc)
14829{
14830 struct drm_device *dev = crtc->base.dev;
14831 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020014832 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020014833
Daniel Vetter24929352012-07-02 20:28:59 +020014834 /* Clear any frame start delays used for debugging left by the BIOS */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014835 reg = PIPECONF(crtc->config->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020014836 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
14837
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030014838 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010014839 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030014840 if (crtc->active) {
14841 update_scanline_offset(crtc);
Daniel Vetter96256042015-02-13 21:03:42 +010014842 drm_crtc_vblank_on(&crtc->base);
14843 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030014844
Daniel Vetter24929352012-07-02 20:28:59 +020014845 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020014846 * disable the crtc (and hence change the state) if it is wrong. Note
14847 * that gen4+ has a fixed plane -> pipe mapping. */
14848 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020014849 struct intel_connector *connector;
14850 bool plane;
14851
Daniel Vetter24929352012-07-02 20:28:59 +020014852 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
14853 crtc->base.base.id);
14854
14855 /* Pipe has the wrong plane attached and the plane is active.
14856 * Temporarily change the plane mapping and disable everything
14857 * ... */
14858 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030014859 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020014860 crtc->plane = !plane;
Maarten Lankhorstce22dba2015-04-21 17:12:56 +030014861 intel_crtc_disable_planes(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020014862 dev_priv->display.crtc_disable(&crtc->base);
14863 crtc->plane = plane;
14864
14865 /* ... and break all links. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014866 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020014867 if (connector->encoder->base.crtc != &crtc->base)
14868 continue;
14869
Egbert Eich7f1950f2014-04-25 10:56:22 +020014870 connector->base.dpms = DRM_MODE_DPMS_OFF;
14871 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020014872 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020014873 /* multiple connectors may have the same encoder:
14874 * handle them and break crtc link separately */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014875 for_each_intel_connector(dev, connector)
Egbert Eich7f1950f2014-04-25 10:56:22 +020014876 if (connector->encoder->base.crtc == &crtc->base) {
14877 connector->encoder->base.crtc = NULL;
14878 connector->encoder->connectors_active = false;
14879 }
Daniel Vetter24929352012-07-02 20:28:59 +020014880
14881 WARN_ON(crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080014882 crtc->base.state->enable = false;
Daniel Vetter24929352012-07-02 20:28:59 +020014883 crtc->base.enabled = false;
14884 }
Daniel Vetter24929352012-07-02 20:28:59 +020014885
Daniel Vetter7fad7982012-07-04 17:51:47 +020014886 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
14887 crtc->pipe == PIPE_A && !crtc->active) {
14888 /* BIOS forgot to enable pipe A, this mostly happens after
14889 * resume. Force-enable the pipe to fix this, the update_dpms
14890 * call below we restore the pipe to the right state, but leave
14891 * the required bits on. */
14892 intel_enable_pipe_a(dev);
14893 }
14894
Daniel Vetter24929352012-07-02 20:28:59 +020014895 /* Adjust the state of the output pipe according to whether we
14896 * have active connectors/encoders. */
14897 intel_crtc_update_dpms(&crtc->base);
14898
Matt Roper83d65732015-02-25 13:12:16 -080014899 if (crtc->active != crtc->base.state->enable) {
Daniel Vetter24929352012-07-02 20:28:59 +020014900 struct intel_encoder *encoder;
14901
14902 /* This can happen either due to bugs in the get_hw_state
14903 * functions or because the pipe is force-enabled due to the
14904 * pipe A quirk. */
14905 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
14906 crtc->base.base.id,
Matt Roper83d65732015-02-25 13:12:16 -080014907 crtc->base.state->enable ? "enabled" : "disabled",
Daniel Vetter24929352012-07-02 20:28:59 +020014908 crtc->active ? "enabled" : "disabled");
14909
Matt Roper83d65732015-02-25 13:12:16 -080014910 crtc->base.state->enable = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020014911 crtc->base.enabled = crtc->active;
14912
14913 /* Because we only establish the connector -> encoder ->
14914 * crtc links if something is active, this means the
14915 * crtc is now deactivated. Break the links. connector
14916 * -> encoder links are only establish when things are
14917 * actually up, hence no need to break them. */
14918 WARN_ON(crtc->active);
14919
14920 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
14921 WARN_ON(encoder->connectors_active);
14922 encoder->base.crtc = NULL;
14923 }
14924 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020014925
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030014926 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010014927 /*
14928 * We start out with underrun reporting disabled to avoid races.
14929 * For correct bookkeeping mark this on active crtcs.
14930 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020014931 * Also on gmch platforms we dont have any hardware bits to
14932 * disable the underrun reporting. Which means we need to start
14933 * out with underrun reporting disabled also on inactive pipes,
14934 * since otherwise we'll complain about the garbage we read when
14935 * e.g. coming up after runtime pm.
14936 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010014937 * No protection against concurrent access is required - at
14938 * worst a fifo underrun happens which also sets this to false.
14939 */
14940 crtc->cpu_fifo_underrun_disabled = true;
14941 crtc->pch_fifo_underrun_disabled = true;
14942 }
Daniel Vetter24929352012-07-02 20:28:59 +020014943}
14944
14945static void intel_sanitize_encoder(struct intel_encoder *encoder)
14946{
14947 struct intel_connector *connector;
14948 struct drm_device *dev = encoder->base.dev;
14949
14950 /* We need to check both for a crtc link (meaning that the
14951 * encoder is active and trying to read from a pipe) and the
14952 * pipe itself being active. */
14953 bool has_active_crtc = encoder->base.crtc &&
14954 to_intel_crtc(encoder->base.crtc)->active;
14955
14956 if (encoder->connectors_active && !has_active_crtc) {
14957 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
14958 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014959 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020014960
14961 /* Connector is active, but has no active pipe. This is
14962 * fallout from our resume register restoring. Disable
14963 * the encoder manually again. */
14964 if (encoder->base.crtc) {
14965 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
14966 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014967 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020014968 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030014969 if (encoder->post_disable)
14970 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020014971 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020014972 encoder->base.crtc = NULL;
14973 encoder->connectors_active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020014974
14975 /* Inconsistent output/port/pipe state happens presumably due to
14976 * a bug in one of the get_hw_state functions. Or someplace else
14977 * in our code, like the register restore mess on resume. Clamp
14978 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014979 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020014980 if (connector->encoder != encoder)
14981 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020014982 connector->base.dpms = DRM_MODE_DPMS_OFF;
14983 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020014984 }
14985 }
14986 /* Enabled encoders without active connectors will be fixed in
14987 * the crtc fixup. */
14988}
14989
Imre Deak04098752014-02-18 00:02:16 +020014990void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014991{
14992 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020014993 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014994
Imre Deak04098752014-02-18 00:02:16 +020014995 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
14996 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
14997 i915_disable_vga(dev);
14998 }
14999}
15000
15001void i915_redisable_vga(struct drm_device *dev)
15002{
15003 struct drm_i915_private *dev_priv = dev->dev_private;
15004
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015005 /* This function can be called both from intel_modeset_setup_hw_state or
15006 * at a very early point in our resume sequence, where the power well
15007 * structures are not yet restored. Since this function is at a very
15008 * paranoid "someone might have enabled VGA while we were not looking"
15009 * level, just check if the power well is enabled instead of trying to
15010 * follow the "don't touch the power well if we don't need it" policy
15011 * the rest of the driver uses. */
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015012 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015013 return;
15014
Imre Deak04098752014-02-18 00:02:16 +020015015 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015016}
15017
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015018static bool primary_get_hw_state(struct intel_crtc *crtc)
15019{
15020 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
15021
15022 if (!crtc->active)
15023 return false;
15024
15025 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
15026}
15027
Daniel Vetter30e984d2013-06-05 13:34:17 +020015028static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015029{
15030 struct drm_i915_private *dev_priv = dev->dev_private;
15031 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015032 struct intel_crtc *crtc;
15033 struct intel_encoder *encoder;
15034 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020015035 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015036
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015037 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015038 struct drm_plane *primary = crtc->base.primary;
15039 struct intel_plane_state *plane_state;
15040
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015041 memset(crtc->config, 0, sizeof(*crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +020015042
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015043 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
Daniel Vetter99535992014-04-13 12:00:33 +020015044
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015045 crtc->active = dev_priv->display.get_pipe_config(crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015046 crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015047
Matt Roper83d65732015-02-25 13:12:16 -080015048 crtc->base.state->enable = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020015049 crtc->base.enabled = crtc->active;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015050
15051 plane_state = to_intel_plane_state(primary->state);
15052 plane_state->visible = primary_get_hw_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020015053
15054 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15055 crtc->base.base.id,
15056 crtc->active ? "enabled" : "disabled");
15057 }
15058
Daniel Vetter53589012013-06-05 13:34:16 +020015059 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15060 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15061
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015062 pll->on = pll->get_hw_state(dev_priv, pll,
15063 &pll->config.hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020015064 pll->active = 0;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015065 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015066 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015067 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
Daniel Vetter53589012013-06-05 13:34:16 +020015068 pll->active++;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015069 pll->config.crtc_mask |= 1 << crtc->pipe;
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015070 }
Daniel Vetter53589012013-06-05 13:34:16 +020015071 }
Daniel Vetter53589012013-06-05 13:34:16 +020015072
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015073 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015074 pll->name, pll->config.crtc_mask, pll->on);
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015075
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015076 if (pll->config.crtc_mask)
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015077 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
Daniel Vetter53589012013-06-05 13:34:16 +020015078 }
15079
Damien Lespiaub2784e12014-08-05 11:29:37 +010015080 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015081 pipe = 0;
15082
15083 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015084 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15085 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015086 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015087 } else {
15088 encoder->base.crtc = NULL;
15089 }
15090
15091 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015092 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020015093 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015094 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015095 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015096 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015097 }
15098
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015099 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015100 if (connector->get_hw_state(connector)) {
15101 connector->base.dpms = DRM_MODE_DPMS_ON;
15102 connector->encoder->connectors_active = true;
15103 connector->base.encoder = &connector->encoder->base;
15104 } else {
15105 connector->base.dpms = DRM_MODE_DPMS_OFF;
15106 connector->base.encoder = NULL;
15107 }
15108 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15109 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030015110 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015111 connector->base.encoder ? "enabled" : "disabled");
15112 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015113}
15114
15115/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
15116 * and i915 state tracking structures. */
15117void intel_modeset_setup_hw_state(struct drm_device *dev,
15118 bool force_restore)
15119{
15120 struct drm_i915_private *dev_priv = dev->dev_private;
15121 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015122 struct intel_crtc *crtc;
15123 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015124 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015125
15126 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015127
Jesse Barnesbabea612013-06-26 18:57:38 +030015128 /*
15129 * Now that we have the config, copy it to each CRTC struct
15130 * Note that this could go away if we move to using crtc_config
15131 * checking everywhere.
15132 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015133 for_each_intel_crtc(dev, crtc) {
Jani Nikulad330a952014-01-21 11:24:25 +020015134 if (crtc->active && i915.fastboot) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015135 intel_mode_from_pipe_config(&crtc->base.mode,
15136 crtc->config);
Jesse Barnesbabea612013-06-26 18:57:38 +030015137 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
15138 crtc->base.base.id);
15139 drm_mode_debug_printmodeline(&crtc->base.mode);
15140 }
15141 }
15142
Daniel Vetter24929352012-07-02 20:28:59 +020015143 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010015144 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015145 intel_sanitize_encoder(encoder);
15146 }
15147
Damien Lespiau055e3932014-08-18 13:49:10 +010015148 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020015149 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15150 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015151 intel_dump_pipe_config(crtc, crtc->config,
15152 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015153 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015154
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015155 intel_modeset_update_connector_atomic_state(dev);
15156
Daniel Vetter35c95372013-07-17 06:55:04 +020015157 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15158 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15159
15160 if (!pll->on || pll->active)
15161 continue;
15162
15163 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15164
15165 pll->disable(dev_priv, pll);
15166 pll->on = false;
15167 }
15168
Pradeep Bhat30789992014-11-04 17:06:45 +000015169 if (IS_GEN9(dev))
15170 skl_wm_get_hw_state(dev);
15171 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015172 ilk_wm_get_hw_state(dev);
15173
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015174 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015175 i915_redisable_vga(dev);
15176
Daniel Vetterf30da182013-04-11 20:22:50 +020015177 /*
15178 * We need to use raw interfaces for restoring state to avoid
15179 * checking (bogus) intermediate states.
15180 */
Damien Lespiau055e3932014-08-18 13:49:10 +010015181 for_each_pipe(dev_priv, pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070015182 struct drm_crtc *crtc =
15183 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020015184
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020015185 intel_crtc_restore_mode(crtc);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015186 }
15187 } else {
15188 intel_modeset_update_staged_output_state(dev);
15189 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020015190
15191 intel_modeset_check_state(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015192}
15193
15194void intel_modeset_gem_init(struct drm_device *dev)
15195{
Jesse Barnes92122782014-10-09 12:57:42 -070015196 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015197 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070015198 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015199 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015200
Imre Deakae484342014-03-31 15:10:44 +030015201 mutex_lock(&dev->struct_mutex);
15202 intel_init_gt_powersave(dev);
15203 mutex_unlock(&dev->struct_mutex);
15204
Jesse Barnes92122782014-10-09 12:57:42 -070015205 /*
15206 * There may be no VBT; and if the BIOS enabled SSC we can
15207 * just keep using it to avoid unnecessary flicker. Whereas if the
15208 * BIOS isn't using it, don't assume it will work even if the VBT
15209 * indicates as much.
15210 */
15211 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
15212 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15213 DREF_SSC1_ENABLE);
15214
Chris Wilson1833b132012-05-09 11:56:28 +010015215 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020015216
15217 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015218
15219 /*
15220 * Make sure any fbs we allocated at startup are properly
15221 * pinned & fenced. When we do the allocation it's too early
15222 * for this.
15223 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010015224 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070015225 obj = intel_fb_obj(c->primary->fb);
15226 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080015227 continue;
15228
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015229 mutex_lock(&dev->struct_mutex);
15230 ret = intel_pin_and_fence_fb_obj(c->primary,
15231 c->primary->fb,
15232 c->primary->state,
15233 NULL);
15234 mutex_unlock(&dev->struct_mutex);
15235 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080015236 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15237 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100015238 drm_framebuffer_unreference(c->primary->fb);
15239 c->primary->fb = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080015240 update_state_fb(c->primary);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015241 }
15242 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015243
15244 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015245}
15246
Imre Deak4932e2c2014-02-11 17:12:48 +020015247void intel_connector_unregister(struct intel_connector *intel_connector)
15248{
15249 struct drm_connector *connector = &intel_connector->base;
15250
15251 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010015252 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015253}
15254
Jesse Barnes79e53942008-11-07 14:24:08 -080015255void intel_modeset_cleanup(struct drm_device *dev)
15256{
Jesse Barnes652c3932009-08-17 13:31:43 -070015257 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid9255d52013-09-26 20:05:59 -030015258 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070015259
Imre Deak2eb52522014-11-19 15:30:05 +020015260 intel_disable_gt_powersave(dev);
15261
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015262 intel_backlight_unregister(dev);
15263
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015264 /*
15265 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020015266 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015267 * experience fancy races otherwise.
15268 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020015269 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070015270
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015271 /*
15272 * Due to the hpd irq storm handling the hotplug work can re-arm the
15273 * poll handlers. Hence disable polling after hpd handling is shut down.
15274 */
Keith Packardf87ea762010-10-03 19:36:26 -070015275 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015276
Jesse Barnes652c3932009-08-17 13:31:43 -070015277 mutex_lock(&dev->struct_mutex);
15278
Jesse Barnes723bfd72010-10-07 16:01:13 -070015279 intel_unregister_dsm_handler();
15280
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020015281 intel_fbc_disable(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070015282
Kristian Høgsberg69341a52009-11-11 12:19:17 -050015283 mutex_unlock(&dev->struct_mutex);
15284
Chris Wilson1630fe72011-07-08 12:22:42 +010015285 /* flush any delayed tasks or pending work */
15286 flush_scheduled_work();
15287
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015288 /* destroy the backlight and sysfs files before encoders/connectors */
15289 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020015290 struct intel_connector *intel_connector;
15291
15292 intel_connector = to_intel_connector(connector);
15293 intel_connector->unregister(intel_connector);
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015294 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030015295
Jesse Barnes79e53942008-11-07 14:24:08 -080015296 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010015297
15298 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030015299
15300 mutex_lock(&dev->struct_mutex);
15301 intel_cleanup_gt_powersave(dev);
15302 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080015303}
15304
Dave Airlie28d52042009-09-21 14:33:58 +100015305/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080015306 * Return which encoder is currently attached for connector.
15307 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010015308struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080015309{
Chris Wilsondf0e9242010-09-09 16:20:55 +010015310 return &intel_attached_encoder(connector)->base;
15311}
Jesse Barnes79e53942008-11-07 14:24:08 -080015312
Chris Wilsondf0e9242010-09-09 16:20:55 +010015313void intel_connector_attach_encoder(struct intel_connector *connector,
15314 struct intel_encoder *encoder)
15315{
15316 connector->encoder = encoder;
15317 drm_mode_connector_attach_encoder(&connector->base,
15318 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080015319}
Dave Airlie28d52042009-09-21 14:33:58 +100015320
15321/*
15322 * set vga decode state - true == enable VGA decode
15323 */
15324int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15325{
15326 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000015327 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100015328 u16 gmch_ctrl;
15329
Chris Wilson75fa0412014-02-07 18:37:02 -020015330 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15331 DRM_ERROR("failed to read control word\n");
15332 return -EIO;
15333 }
15334
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020015335 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15336 return 0;
15337
Dave Airlie28d52042009-09-21 14:33:58 +100015338 if (state)
15339 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15340 else
15341 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020015342
15343 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15344 DRM_ERROR("failed to write control word\n");
15345 return -EIO;
15346 }
15347
Dave Airlie28d52042009-09-21 14:33:58 +100015348 return 0;
15349}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015350
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015351struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015352
15353 u32 power_well_driver;
15354
Chris Wilson63b66e52013-08-08 15:12:06 +020015355 int num_transcoders;
15356
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015357 struct intel_cursor_error_state {
15358 u32 control;
15359 u32 position;
15360 u32 base;
15361 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010015362 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015363
15364 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015365 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015366 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030015367 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010015368 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015369
15370 struct intel_plane_error_state {
15371 u32 control;
15372 u32 stride;
15373 u32 size;
15374 u32 pos;
15375 u32 addr;
15376 u32 surface;
15377 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010015378 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020015379
15380 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015381 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020015382 enum transcoder cpu_transcoder;
15383
15384 u32 conf;
15385
15386 u32 htotal;
15387 u32 hblank;
15388 u32 hsync;
15389 u32 vtotal;
15390 u32 vblank;
15391 u32 vsync;
15392 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015393};
15394
15395struct intel_display_error_state *
15396intel_display_capture_error_state(struct drm_device *dev)
15397{
Jani Nikulafbee40d2014-03-31 14:27:18 +030015398 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015399 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020015400 int transcoders[] = {
15401 TRANSCODER_A,
15402 TRANSCODER_B,
15403 TRANSCODER_C,
15404 TRANSCODER_EDP,
15405 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015406 int i;
15407
Chris Wilson63b66e52013-08-08 15:12:06 +020015408 if (INTEL_INFO(dev)->num_pipes == 0)
15409 return NULL;
15410
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015411 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015412 if (error == NULL)
15413 return NULL;
15414
Imre Deak190be112013-11-25 17:15:31 +020015415 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015416 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15417
Damien Lespiau055e3932014-08-18 13:49:10 +010015418 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020015419 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015420 __intel_display_power_is_enabled(dev_priv,
15421 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020015422 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015423 continue;
15424
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030015425 error->cursor[i].control = I915_READ(CURCNTR(i));
15426 error->cursor[i].position = I915_READ(CURPOS(i));
15427 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015428
15429 error->plane[i].control = I915_READ(DSPCNTR(i));
15430 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015431 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030015432 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015433 error->plane[i].pos = I915_READ(DSPPOS(i));
15434 }
Paulo Zanonica291362013-03-06 20:03:14 -030015435 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15436 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015437 if (INTEL_INFO(dev)->gen >= 4) {
15438 error->plane[i].surface = I915_READ(DSPSURF(i));
15439 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15440 }
15441
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015442 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030015443
Sonika Jindal3abfce72014-07-21 15:23:43 +053015444 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e2014-04-18 15:55:04 +030015445 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020015446 }
15447
15448 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
15449 if (HAS_DDI(dev_priv->dev))
15450 error->num_transcoders++; /* Account for eDP. */
15451
15452 for (i = 0; i < error->num_transcoders; i++) {
15453 enum transcoder cpu_transcoder = transcoders[i];
15454
Imre Deakddf9c532013-11-27 22:02:02 +020015455 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015456 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020015457 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015458 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015459 continue;
15460
Chris Wilson63b66e52013-08-08 15:12:06 +020015461 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15462
15463 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15464 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15465 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15466 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15467 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15468 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15469 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015470 }
15471
15472 return error;
15473}
15474
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015475#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15476
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015477void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015478intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015479 struct drm_device *dev,
15480 struct intel_display_error_state *error)
15481{
Damien Lespiau055e3932014-08-18 13:49:10 +010015482 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015483 int i;
15484
Chris Wilson63b66e52013-08-08 15:12:06 +020015485 if (!error)
15486 return;
15487
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015488 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020015489 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015490 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015491 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010015492 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015493 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020015494 err_printf(m, " Power: %s\n",
15495 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015496 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030015497 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015498
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015499 err_printf(m, "Plane [%d]:\n", i);
15500 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15501 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015502 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015503 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15504 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015505 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030015506 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015507 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015508 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015509 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15510 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015511 }
15512
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015513 err_printf(m, "Cursor [%d]:\n", i);
15514 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15515 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15516 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015517 }
Chris Wilson63b66e52013-08-08 15:12:06 +020015518
15519 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010015520 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020015521 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015522 err_printf(m, " Power: %s\n",
15523 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020015524 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15525 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15526 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15527 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15528 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15529 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15530 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15531 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015532}
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015533
15534void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
15535{
15536 struct intel_crtc *crtc;
15537
15538 for_each_intel_crtc(dev, crtc) {
15539 struct intel_unpin_work *work;
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015540
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015541 spin_lock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015542
15543 work = crtc->unpin_work;
15544
15545 if (work && work->event &&
15546 work->event->base.file_priv == file) {
15547 kfree(work->event);
15548 work->event = NULL;
15549 }
15550
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015551 spin_unlock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015552 }
15553}