blob: 7c0beef7f3ef0c026c358bc79df7562a980cbdc2 [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);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100110
Dave Airlie0e32b392014-05-02 14:02:48 +1000111static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
112{
113 if (!connector->mst_port)
114 return connector->encoder;
115 else
116 return &connector->mst_port->mst_encoders[pipe]->base;
117}
118
Jesse Barnes79e53942008-11-07 14:24:08 -0800119typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400120 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -0800121} intel_range_t;
122
123typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400124 int dot_limit;
125 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800126} intel_p2_t;
127
Ma Lingd4906092009-03-18 20:13:27 +0800128typedef struct intel_limit intel_limit_t;
129struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -0400130 intel_range_t dot, vco, n, m, m1, m2, p, p1;
131 intel_p2_t p2;
Ma Lingd4906092009-03-18 20:13:27 +0800132};
Jesse Barnes79e53942008-11-07 14:24:08 -0800133
Daniel Vetterd2acd212012-10-20 20:57:43 +0200134int
135intel_pch_rawclk(struct drm_device *dev)
136{
137 struct drm_i915_private *dev_priv = dev->dev_private;
138
139 WARN_ON(!HAS_PCH_SPLIT(dev));
140
141 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
142}
143
Chris Wilson021357a2010-09-07 20:54:59 +0100144static inline u32 /* units of 100MHz */
145intel_fdi_link_freq(struct drm_device *dev)
146{
Chris Wilson8b99e682010-10-13 09:59:17 +0100147 if (IS_GEN5(dev)) {
148 struct drm_i915_private *dev_priv = dev->dev_private;
149 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
150 } else
151 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +0100152}
153
Daniel Vetter5d536e22013-07-06 12:52:06 +0200154static const intel_limit_t intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400155 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200156 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200157 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400158 .m = { .min = 96, .max = 140 },
159 .m1 = { .min = 18, .max = 26 },
160 .m2 = { .min = 6, .max = 16 },
161 .p = { .min = 4, .max = 128 },
162 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700163 .p2 = { .dot_limit = 165000,
164 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700165};
166
Daniel Vetter5d536e22013-07-06 12:52:06 +0200167static const intel_limit_t intel_limits_i8xx_dvo = {
168 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200169 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200170 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200171 .m = { .min = 96, .max = 140 },
172 .m1 = { .min = 18, .max = 26 },
173 .m2 = { .min = 6, .max = 16 },
174 .p = { .min = 4, .max = 128 },
175 .p1 = { .min = 2, .max = 33 },
176 .p2 = { .dot_limit = 165000,
177 .p2_slow = 4, .p2_fast = 4 },
178};
179
Keith Packarde4b36692009-06-05 19:22:17 -0700180static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400181 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200182 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200183 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400184 .m = { .min = 96, .max = 140 },
185 .m1 = { .min = 18, .max = 26 },
186 .m2 = { .min = 6, .max = 16 },
187 .p = { .min = 4, .max = 128 },
188 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700189 .p2 = { .dot_limit = 165000,
190 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700191};
Eric Anholt273e27c2011-03-30 13:01:10 -0700192
Keith Packarde4b36692009-06-05 19:22:17 -0700193static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400194 .dot = { .min = 20000, .max = 400000 },
195 .vco = { .min = 1400000, .max = 2800000 },
196 .n = { .min = 1, .max = 6 },
197 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100198 .m1 = { .min = 8, .max = 18 },
199 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400200 .p = { .min = 5, .max = 80 },
201 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700202 .p2 = { .dot_limit = 200000,
203 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700204};
205
206static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400207 .dot = { .min = 20000, .max = 400000 },
208 .vco = { .min = 1400000, .max = 2800000 },
209 .n = { .min = 1, .max = 6 },
210 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100211 .m1 = { .min = 8, .max = 18 },
212 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400213 .p = { .min = 7, .max = 98 },
214 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700215 .p2 = { .dot_limit = 112000,
216 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700217};
218
Eric Anholt273e27c2011-03-30 13:01:10 -0700219
Keith Packarde4b36692009-06-05 19:22:17 -0700220static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700221 .dot = { .min = 25000, .max = 270000 },
222 .vco = { .min = 1750000, .max = 3500000},
223 .n = { .min = 1, .max = 4 },
224 .m = { .min = 104, .max = 138 },
225 .m1 = { .min = 17, .max = 23 },
226 .m2 = { .min = 5, .max = 11 },
227 .p = { .min = 10, .max = 30 },
228 .p1 = { .min = 1, .max = 3},
229 .p2 = { .dot_limit = 270000,
230 .p2_slow = 10,
231 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800232 },
Keith Packarde4b36692009-06-05 19:22:17 -0700233};
234
235static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700236 .dot = { .min = 22000, .max = 400000 },
237 .vco = { .min = 1750000, .max = 3500000},
238 .n = { .min = 1, .max = 4 },
239 .m = { .min = 104, .max = 138 },
240 .m1 = { .min = 16, .max = 23 },
241 .m2 = { .min = 5, .max = 11 },
242 .p = { .min = 5, .max = 80 },
243 .p1 = { .min = 1, .max = 8},
244 .p2 = { .dot_limit = 165000,
245 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700246};
247
248static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700249 .dot = { .min = 20000, .max = 115000 },
250 .vco = { .min = 1750000, .max = 3500000 },
251 .n = { .min = 1, .max = 3 },
252 .m = { .min = 104, .max = 138 },
253 .m1 = { .min = 17, .max = 23 },
254 .m2 = { .min = 5, .max = 11 },
255 .p = { .min = 28, .max = 112 },
256 .p1 = { .min = 2, .max = 8 },
257 .p2 = { .dot_limit = 0,
258 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800259 },
Keith Packarde4b36692009-06-05 19:22:17 -0700260};
261
262static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700263 .dot = { .min = 80000, .max = 224000 },
264 .vco = { .min = 1750000, .max = 3500000 },
265 .n = { .min = 1, .max = 3 },
266 .m = { .min = 104, .max = 138 },
267 .m1 = { .min = 17, .max = 23 },
268 .m2 = { .min = 5, .max = 11 },
269 .p = { .min = 14, .max = 42 },
270 .p1 = { .min = 2, .max = 6 },
271 .p2 = { .dot_limit = 0,
272 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800273 },
Keith Packarde4b36692009-06-05 19:22:17 -0700274};
275
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500276static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400277 .dot = { .min = 20000, .max = 400000},
278 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700279 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400280 .n = { .min = 3, .max = 6 },
281 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700282 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400283 .m1 = { .min = 0, .max = 0 },
284 .m2 = { .min = 0, .max = 254 },
285 .p = { .min = 5, .max = 80 },
286 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700287 .p2 = { .dot_limit = 200000,
288 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700289};
290
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500291static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400292 .dot = { .min = 20000, .max = 400000 },
293 .vco = { .min = 1700000, .max = 3500000 },
294 .n = { .min = 3, .max = 6 },
295 .m = { .min = 2, .max = 256 },
296 .m1 = { .min = 0, .max = 0 },
297 .m2 = { .min = 0, .max = 254 },
298 .p = { .min = 7, .max = 112 },
299 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700300 .p2 = { .dot_limit = 112000,
301 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700302};
303
Eric Anholt273e27c2011-03-30 13:01:10 -0700304/* Ironlake / Sandybridge
305 *
306 * We calculate clock using (register_value + 2) for N/M1/M2, so here
307 * the range value for them is (actual_value - 2).
308 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800309static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700310 .dot = { .min = 25000, .max = 350000 },
311 .vco = { .min = 1760000, .max = 3510000 },
312 .n = { .min = 1, .max = 5 },
313 .m = { .min = 79, .max = 127 },
314 .m1 = { .min = 12, .max = 22 },
315 .m2 = { .min = 5, .max = 9 },
316 .p = { .min = 5, .max = 80 },
317 .p1 = { .min = 1, .max = 8 },
318 .p2 = { .dot_limit = 225000,
319 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700320};
321
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800322static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700323 .dot = { .min = 25000, .max = 350000 },
324 .vco = { .min = 1760000, .max = 3510000 },
325 .n = { .min = 1, .max = 3 },
326 .m = { .min = 79, .max = 118 },
327 .m1 = { .min = 12, .max = 22 },
328 .m2 = { .min = 5, .max = 9 },
329 .p = { .min = 28, .max = 112 },
330 .p1 = { .min = 2, .max = 8 },
331 .p2 = { .dot_limit = 225000,
332 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800333};
334
335static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700336 .dot = { .min = 25000, .max = 350000 },
337 .vco = { .min = 1760000, .max = 3510000 },
338 .n = { .min = 1, .max = 3 },
339 .m = { .min = 79, .max = 127 },
340 .m1 = { .min = 12, .max = 22 },
341 .m2 = { .min = 5, .max = 9 },
342 .p = { .min = 14, .max = 56 },
343 .p1 = { .min = 2, .max = 8 },
344 .p2 = { .dot_limit = 225000,
345 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800346};
347
Eric Anholt273e27c2011-03-30 13:01:10 -0700348/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800349static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700350 .dot = { .min = 25000, .max = 350000 },
351 .vco = { .min = 1760000, .max = 3510000 },
352 .n = { .min = 1, .max = 2 },
353 .m = { .min = 79, .max = 126 },
354 .m1 = { .min = 12, .max = 22 },
355 .m2 = { .min = 5, .max = 9 },
356 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400357 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700358 .p2 = { .dot_limit = 225000,
359 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800360};
361
362static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700363 .dot = { .min = 25000, .max = 350000 },
364 .vco = { .min = 1760000, .max = 3510000 },
365 .n = { .min = 1, .max = 3 },
366 .m = { .min = 79, .max = 126 },
367 .m1 = { .min = 12, .max = 22 },
368 .m2 = { .min = 5, .max = 9 },
369 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400370 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700371 .p2 = { .dot_limit = 225000,
372 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800373};
374
Ville Syrjälädc730512013-09-24 21:26:30 +0300375static const intel_limit_t intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300376 /*
377 * These are the data rate limits (measured in fast clocks)
378 * since those are the strictest limits we have. The fast
379 * clock and actual rate limits are more relaxed, so checking
380 * them would make no difference.
381 */
382 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200383 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700384 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700385 .m1 = { .min = 2, .max = 3 },
386 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300387 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300388 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700389};
390
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300391static const intel_limit_t intel_limits_chv = {
392 /*
393 * These are the data rate limits (measured in fast clocks)
394 * since those are the strictest limits we have. The fast
395 * clock and actual rate limits are more relaxed, so checking
396 * them would make no difference.
397 */
398 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200399 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300400 .n = { .min = 1, .max = 1 },
401 .m1 = { .min = 2, .max = 2 },
402 .m2 = { .min = 24 << 22, .max = 175 << 22 },
403 .p1 = { .min = 2, .max = 4 },
404 .p2 = { .p2_slow = 1, .p2_fast = 14 },
405};
406
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200407static const intel_limit_t intel_limits_bxt = {
408 /* FIXME: find real dot limits */
409 .dot = { .min = 0, .max = INT_MAX },
410 .vco = { .min = 4800000, .max = 6480000 },
411 .n = { .min = 1, .max = 1 },
412 .m1 = { .min = 2, .max = 2 },
413 /* FIXME: find real m2 limits */
414 .m2 = { .min = 2 << 22, .max = 255 << 22 },
415 .p1 = { .min = 2, .max = 4 },
416 .p2 = { .p2_slow = 1, .p2_fast = 20 },
417};
418
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300419static void vlv_clock(int refclk, intel_clock_t *clock)
420{
421 clock->m = clock->m1 * clock->m2;
422 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200423 if (WARN_ON(clock->n == 0 || clock->p == 0))
424 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300425 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
426 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300427}
428
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300429/**
430 * Returns whether any output on the specified pipe is of the specified type
431 */
Damien Lespiau40935612014-10-29 11:16:59 +0000432bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300433{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300434 struct drm_device *dev = crtc->base.dev;
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300435 struct intel_encoder *encoder;
436
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300437 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300438 if (encoder->type == type)
439 return true;
440
441 return false;
442}
443
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200444/**
445 * Returns whether any output on the specified pipe will have the specified
446 * type after a staged modeset is complete, i.e., the same as
447 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
448 * encoder->crtc.
449 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200450static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
451 int type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200452{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200453 struct drm_atomic_state *state = crtc_state->base.state;
454 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200455 struct intel_encoder *encoder;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200456 int i, num_connectors = 0;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200457
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200458 for (i = 0; i < state->num_connector; i++) {
459 if (!state->connectors[i])
460 continue;
461
462 connector_state = state->connector_states[i];
463 if (connector_state->crtc != crtc_state->base.crtc)
464 continue;
465
466 num_connectors++;
467
468 encoder = to_intel_encoder(connector_state->best_encoder);
469 if (encoder->type == type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200470 return true;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200471 }
472
473 WARN_ON(num_connectors == 0);
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200474
475 return false;
476}
477
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200478static const intel_limit_t *
479intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800480{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200481 struct drm_device *dev = crtc_state->base.crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800482 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800483
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200484 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100485 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000486 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800487 limit = &intel_limits_ironlake_dual_lvds_100m;
488 else
489 limit = &intel_limits_ironlake_dual_lvds;
490 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000491 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800492 limit = &intel_limits_ironlake_single_lvds_100m;
493 else
494 limit = &intel_limits_ironlake_single_lvds;
495 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200496 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800497 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800498
499 return limit;
500}
501
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200502static const intel_limit_t *
503intel_g4x_limit(struct intel_crtc_state *crtc_state)
Ma Ling044c7c42009-03-18 20:13:23 +0800504{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200505 struct drm_device *dev = crtc_state->base.crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800506 const intel_limit_t *limit;
507
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200508 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100509 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700510 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800511 else
Keith Packarde4b36692009-06-05 19:22:17 -0700512 limit = &intel_limits_g4x_single_channel_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200513 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
514 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700515 limit = &intel_limits_g4x_hdmi;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200516 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700517 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800518 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700519 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800520
521 return limit;
522}
523
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200524static const intel_limit_t *
525intel_limit(struct intel_crtc_state *crtc_state, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800526{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200527 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800528 const intel_limit_t *limit;
529
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200530 if (IS_BROXTON(dev))
531 limit = &intel_limits_bxt;
532 else if (HAS_PCH_SPLIT(dev))
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200533 limit = intel_ironlake_limit(crtc_state, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800534 else if (IS_G4X(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200535 limit = intel_g4x_limit(crtc_state);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500536 } else if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200537 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500538 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800539 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500540 limit = &intel_limits_pineview_sdvo;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300541 } else if (IS_CHERRYVIEW(dev)) {
542 limit = &intel_limits_chv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700543 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjälädc730512013-09-24 21:26:30 +0300544 limit = &intel_limits_vlv;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100545 } else if (!IS_GEN2(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200546 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100547 limit = &intel_limits_i9xx_lvds;
548 else
549 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800550 } else {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200551 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700552 limit = &intel_limits_i8xx_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200553 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Keith Packarde4b36692009-06-05 19:22:17 -0700554 limit = &intel_limits_i8xx_dvo;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200555 else
556 limit = &intel_limits_i8xx_dac;
Jesse Barnes79e53942008-11-07 14:24:08 -0800557 }
558 return limit;
559}
560
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500561/* m1 is reserved as 0 in Pineview, n is a ring counter */
562static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800563{
Shaohua Li21778322009-02-23 15:19:16 +0800564 clock->m = clock->m2 + 2;
565 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200566 if (WARN_ON(clock->n == 0 || clock->p == 0))
567 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300568 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
569 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Shaohua Li21778322009-02-23 15:19:16 +0800570}
571
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200572static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
573{
574 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
575}
576
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200577static void i9xx_clock(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800578{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200579 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800580 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200581 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
582 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300583 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
584 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Jesse Barnes79e53942008-11-07 14:24:08 -0800585}
586
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300587static void chv_clock(int refclk, intel_clock_t *clock)
588{
589 clock->m = clock->m1 * clock->m2;
590 clock->p = clock->p1 * clock->p2;
591 if (WARN_ON(clock->n == 0 || clock->p == 0))
592 return;
593 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
594 clock->n << 22);
595 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
596}
597
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800598#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800599/**
600 * Returns whether the given set of divisors are valid for a given refclk with
601 * the given connectors.
602 */
603
Chris Wilson1b894b52010-12-14 20:04:54 +0000604static bool intel_PLL_is_valid(struct drm_device *dev,
605 const intel_limit_t *limit,
606 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800607{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300608 if (clock->n < limit->n.min || limit->n.max < clock->n)
609 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800610 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400611 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800612 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400613 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800614 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400615 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300616
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200617 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300618 if (clock->m1 <= clock->m2)
619 INTELPllInvalid("m1 <= m2\n");
620
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200621 if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300622 if (clock->p < limit->p.min || limit->p.max < clock->p)
623 INTELPllInvalid("p out of range\n");
624 if (clock->m < limit->m.min || limit->m.max < clock->m)
625 INTELPllInvalid("m out of range\n");
626 }
627
Jesse Barnes79e53942008-11-07 14:24:08 -0800628 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400629 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800630 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
631 * connector, etc., rather than just a single range.
632 */
633 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400634 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800635
636 return true;
637}
638
Ma Lingd4906092009-03-18 20:13:27 +0800639static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200640i9xx_find_best_dpll(const intel_limit_t *limit,
641 struct intel_crtc_state *crtc_state,
Sean Paulcec2f352012-01-10 15:09:36 -0800642 int target, int refclk, intel_clock_t *match_clock,
643 intel_clock_t *best_clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800644{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200645 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300646 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800647 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800648 int err = target;
649
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200650 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800651 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100652 * For LVDS just rely on its current settings for dual-channel.
653 * We haven't figured out how to reliably set up different
654 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800655 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100656 if (intel_is_dual_link_lvds(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800657 clock.p2 = limit->p2.p2_fast;
658 else
659 clock.p2 = limit->p2.p2_slow;
660 } else {
661 if (target < limit->p2.dot_limit)
662 clock.p2 = limit->p2.p2_slow;
663 else
664 clock.p2 = limit->p2.p2_fast;
665 }
666
Akshay Joshi0206e352011-08-16 15:34:10 -0400667 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800668
Zhao Yakui42158662009-11-20 11:24:18 +0800669 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
670 clock.m1++) {
671 for (clock.m2 = limit->m2.min;
672 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200673 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800674 break;
675 for (clock.n = limit->n.min;
676 clock.n <= limit->n.max; clock.n++) {
677 for (clock.p1 = limit->p1.min;
678 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800679 int this_err;
680
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200681 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000682 if (!intel_PLL_is_valid(dev, limit,
683 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800684 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800685 if (match_clock &&
686 clock.p != match_clock->p)
687 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800688
689 this_err = abs(clock.dot - target);
690 if (this_err < err) {
691 *best_clock = clock;
692 err = this_err;
693 }
694 }
695 }
696 }
697 }
698
699 return (err != target);
700}
701
Ma Lingd4906092009-03-18 20:13:27 +0800702static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200703pnv_find_best_dpll(const intel_limit_t *limit,
704 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200705 int target, int refclk, intel_clock_t *match_clock,
706 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200707{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200708 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300709 struct drm_device *dev = crtc->base.dev;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200710 intel_clock_t clock;
711 int err = target;
712
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200713 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200714 /*
715 * For LVDS just rely on its current settings for dual-channel.
716 * We haven't figured out how to reliably set up different
717 * single/dual channel state, if we even can.
718 */
719 if (intel_is_dual_link_lvds(dev))
720 clock.p2 = limit->p2.p2_fast;
721 else
722 clock.p2 = limit->p2.p2_slow;
723 } else {
724 if (target < limit->p2.dot_limit)
725 clock.p2 = limit->p2.p2_slow;
726 else
727 clock.p2 = limit->p2.p2_fast;
728 }
729
730 memset(best_clock, 0, sizeof(*best_clock));
731
732 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
733 clock.m1++) {
734 for (clock.m2 = limit->m2.min;
735 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200736 for (clock.n = limit->n.min;
737 clock.n <= limit->n.max; clock.n++) {
738 for (clock.p1 = limit->p1.min;
739 clock.p1 <= limit->p1.max; clock.p1++) {
740 int this_err;
741
742 pineview_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800743 if (!intel_PLL_is_valid(dev, limit,
744 &clock))
745 continue;
746 if (match_clock &&
747 clock.p != match_clock->p)
748 continue;
749
750 this_err = abs(clock.dot - target);
751 if (this_err < err) {
752 *best_clock = clock;
753 err = this_err;
754 }
755 }
756 }
757 }
758 }
759
760 return (err != target);
761}
762
Ma Lingd4906092009-03-18 20:13:27 +0800763static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200764g4x_find_best_dpll(const intel_limit_t *limit,
765 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200766 int target, int refclk, intel_clock_t *match_clock,
767 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800768{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200769 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300770 struct drm_device *dev = crtc->base.dev;
Ma Lingd4906092009-03-18 20:13:27 +0800771 intel_clock_t clock;
772 int max_n;
773 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400774 /* approximately equals target * 0.00585 */
775 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800776 found = false;
777
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200778 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100779 if (intel_is_dual_link_lvds(dev))
Ma Lingd4906092009-03-18 20:13:27 +0800780 clock.p2 = limit->p2.p2_fast;
781 else
782 clock.p2 = limit->p2.p2_slow;
783 } else {
784 if (target < limit->p2.dot_limit)
785 clock.p2 = limit->p2.p2_slow;
786 else
787 clock.p2 = limit->p2.p2_fast;
788 }
789
790 memset(best_clock, 0, sizeof(*best_clock));
791 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200792 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800793 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200794 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800795 for (clock.m1 = limit->m1.max;
796 clock.m1 >= limit->m1.min; clock.m1--) {
797 for (clock.m2 = limit->m2.max;
798 clock.m2 >= limit->m2.min; clock.m2--) {
799 for (clock.p1 = limit->p1.max;
800 clock.p1 >= limit->p1.min; clock.p1--) {
801 int this_err;
802
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200803 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000804 if (!intel_PLL_is_valid(dev, limit,
805 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800806 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000807
808 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800809 if (this_err < err_most) {
810 *best_clock = clock;
811 err_most = this_err;
812 max_n = clock.n;
813 found = true;
814 }
815 }
816 }
817 }
818 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800819 return found;
820}
Ma Lingd4906092009-03-18 20:13:27 +0800821
Imre Deakd5dd62b2015-03-17 11:40:03 +0200822/*
823 * Check if the calculated PLL configuration is more optimal compared to the
824 * best configuration and error found so far. Return the calculated error.
825 */
826static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
827 const intel_clock_t *calculated_clock,
828 const intel_clock_t *best_clock,
829 unsigned int best_error_ppm,
830 unsigned int *error_ppm)
831{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200832 /*
833 * For CHV ignore the error and consider only the P value.
834 * Prefer a bigger P value based on HW requirements.
835 */
836 if (IS_CHERRYVIEW(dev)) {
837 *error_ppm = 0;
838
839 return calculated_clock->p > best_clock->p;
840 }
841
Imre Deak24be4e42015-03-17 11:40:04 +0200842 if (WARN_ON_ONCE(!target_freq))
843 return false;
844
Imre Deakd5dd62b2015-03-17 11:40:03 +0200845 *error_ppm = div_u64(1000000ULL *
846 abs(target_freq - calculated_clock->dot),
847 target_freq);
848 /*
849 * Prefer a better P value over a better (smaller) error if the error
850 * is small. Ensure this preference for future configurations too by
851 * setting the error to 0.
852 */
853 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
854 *error_ppm = 0;
855
856 return true;
857 }
858
859 return *error_ppm + 10 < best_error_ppm;
860}
861
Zhenyu Wang2c072452009-06-05 15:38:42 +0800862static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200863vlv_find_best_dpll(const intel_limit_t *limit,
864 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200865 int target, int refclk, intel_clock_t *match_clock,
866 intel_clock_t *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700867{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200868 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300869 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300870 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300871 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300872 /* min update 19.2 MHz */
873 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300874 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700875
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300876 target *= 5; /* fast clock */
877
878 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700879
880 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300881 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300882 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300883 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300884 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300885 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700886 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300887 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200888 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300889
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300890 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
891 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300892
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300893 vlv_clock(refclk, &clock);
894
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300895 if (!intel_PLL_is_valid(dev, limit,
896 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300897 continue;
898
Imre Deakd5dd62b2015-03-17 11:40:03 +0200899 if (!vlv_PLL_is_optimal(dev, target,
900 &clock,
901 best_clock,
902 bestppm, &ppm))
903 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300904
Imre Deakd5dd62b2015-03-17 11:40:03 +0200905 *best_clock = clock;
906 bestppm = ppm;
907 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700908 }
909 }
910 }
911 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700912
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300913 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700914}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700915
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300916static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200917chv_find_best_dpll(const intel_limit_t *limit,
918 struct intel_crtc_state *crtc_state,
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300919 int target, int refclk, intel_clock_t *match_clock,
920 intel_clock_t *best_clock)
921{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200922 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300923 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200924 unsigned int best_error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300925 intel_clock_t clock;
926 uint64_t m2;
927 int found = false;
928
929 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200930 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300931
932 /*
933 * Based on hardware doc, the n always set to 1, and m1 always
934 * set to 2. If requires to support 200Mhz refclk, we need to
935 * revisit this because n may not 1 anymore.
936 */
937 clock.n = 1, clock.m1 = 2;
938 target *= 5; /* fast clock */
939
940 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
941 for (clock.p2 = limit->p2.p2_fast;
942 clock.p2 >= limit->p2.p2_slow;
943 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +0200944 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300945
946 clock.p = clock.p1 * clock.p2;
947
948 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
949 clock.n) << 22, refclk * clock.m1);
950
951 if (m2 > INT_MAX/clock.m1)
952 continue;
953
954 clock.m2 = m2;
955
956 chv_clock(refclk, &clock);
957
958 if (!intel_PLL_is_valid(dev, limit, &clock))
959 continue;
960
Imre Deak9ca3ba02015-03-17 11:40:05 +0200961 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
962 best_error_ppm, &error_ppm))
963 continue;
964
965 *best_clock = clock;
966 best_error_ppm = error_ppm;
967 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300968 }
969 }
970
971 return found;
972}
973
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200974bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
975 intel_clock_t *best_clock)
976{
977 int refclk = i9xx_get_refclk(crtc_state, 0);
978
979 return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
980 target_clock, refclk, NULL, best_clock);
981}
982
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300983bool intel_crtc_active(struct drm_crtc *crtc)
984{
985 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
986
987 /* Be paranoid as we can arrive here with only partial
988 * state retrieved from the hardware during setup.
989 *
Damien Lespiau241bfc32013-09-25 16:45:37 +0100990 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300991 * as Haswell has gained clock readout/fastboot support.
992 *
Dave Airlie66e514c2014-04-03 07:51:54 +1000993 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300994 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -0700995 *
996 * FIXME: The intel_crtc->active here should be switched to
997 * crtc->state->active once we have proper CRTC states wired up
998 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300999 */
Matt Roperc3d1f432015-03-09 10:19:23 -07001000 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001001 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001002}
1003
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001004enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1005 enum pipe pipe)
1006{
1007 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1008 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1009
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001010 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001011}
1012
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001013static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1014{
1015 struct drm_i915_private *dev_priv = dev->dev_private;
1016 u32 reg = PIPEDSL(pipe);
1017 u32 line1, line2;
1018 u32 line_mask;
1019
1020 if (IS_GEN2(dev))
1021 line_mask = DSL_LINEMASK_GEN2;
1022 else
1023 line_mask = DSL_LINEMASK_GEN3;
1024
1025 line1 = I915_READ(reg) & line_mask;
1026 mdelay(5);
1027 line2 = I915_READ(reg) & line_mask;
1028
1029 return line1 == line2;
1030}
1031
Keith Packardab7ad7f2010-10-03 00:33:06 -07001032/*
1033 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001034 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001035 *
1036 * After disabling a pipe, we can't wait for vblank in the usual way,
1037 * spinning on the vblank interrupt status bit, since we won't actually
1038 * see an interrupt when the pipe is disabled.
1039 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001040 * On Gen4 and above:
1041 * wait for the pipe register state bit to turn off
1042 *
1043 * Otherwise:
1044 * wait for the display line value to settle (it usually
1045 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001046 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001047 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001048static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001049{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001050 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001051 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001052 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001053 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001054
Keith Packardab7ad7f2010-10-03 00:33:06 -07001055 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001056 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001057
Keith Packardab7ad7f2010-10-03 00:33:06 -07001058 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +01001059 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1060 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001061 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001062 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001063 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001064 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001065 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001066 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001067}
1068
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001069/*
1070 * ibx_digital_port_connected - is the specified port connected?
1071 * @dev_priv: i915 private structure
1072 * @port: the port to test
1073 *
1074 * Returns true if @port is connected, false otherwise.
1075 */
1076bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
1077 struct intel_digital_port *port)
1078{
1079 u32 bit;
1080
Damien Lespiauc36346e2012-12-13 16:09:03 +00001081 if (HAS_PCH_IBX(dev_priv->dev)) {
Robin Schroereba905b2014-05-18 02:24:50 +02001082 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +00001083 case PORT_B:
1084 bit = SDE_PORTB_HOTPLUG;
1085 break;
1086 case PORT_C:
1087 bit = SDE_PORTC_HOTPLUG;
1088 break;
1089 case PORT_D:
1090 bit = SDE_PORTD_HOTPLUG;
1091 break;
1092 default:
1093 return true;
1094 }
1095 } else {
Robin Schroereba905b2014-05-18 02:24:50 +02001096 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +00001097 case PORT_B:
1098 bit = SDE_PORTB_HOTPLUG_CPT;
1099 break;
1100 case PORT_C:
1101 bit = SDE_PORTC_HOTPLUG_CPT;
1102 break;
1103 case PORT_D:
1104 bit = SDE_PORTD_HOTPLUG_CPT;
1105 break;
1106 default:
1107 return true;
1108 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001109 }
1110
1111 return I915_READ(SDEISR) & bit;
1112}
1113
Jesse Barnesb24e7172011-01-04 15:09:30 -08001114static const char *state_string(bool enabled)
1115{
1116 return enabled ? "on" : "off";
1117}
1118
1119/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001120void assert_pll(struct drm_i915_private *dev_priv,
1121 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001122{
1123 int reg;
1124 u32 val;
1125 bool cur_state;
1126
1127 reg = DPLL(pipe);
1128 val = I915_READ(reg);
1129 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001130 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001131 "PLL state assertion failure (expected %s, current %s)\n",
1132 state_string(state), state_string(cur_state));
1133}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001134
Jani Nikula23538ef2013-08-27 15:12:22 +03001135/* XXX: the dsi pll is shared between MIPI DSI ports */
1136static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1137{
1138 u32 val;
1139 bool cur_state;
1140
1141 mutex_lock(&dev_priv->dpio_lock);
1142 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1143 mutex_unlock(&dev_priv->dpio_lock);
1144
1145 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001146 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001147 "DSI PLL state assertion failure (expected %s, current %s)\n",
1148 state_string(state), state_string(cur_state));
1149}
1150#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1151#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1152
Daniel Vetter55607e82013-06-16 21:42:39 +02001153struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +02001154intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -08001155{
Daniel Vettere2b78262013-06-07 23:10:03 +02001156 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1157
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001158 if (crtc->config->shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +02001159 return NULL;
1160
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001161 return &dev_priv->shared_dplls[crtc->config->shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +02001162}
1163
Jesse Barnesb24e7172011-01-04 15:09:30 -08001164/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +02001165void assert_shared_dpll(struct drm_i915_private *dev_priv,
1166 struct intel_shared_dpll *pll,
1167 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001168{
Jesse Barnes040484a2011-01-03 12:14:26 -08001169 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +02001170 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001171
Chris Wilson92b27b02012-05-20 18:10:50 +01001172 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +02001173 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001174 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001175
Daniel Vetter53589012013-06-05 13:34:16 +02001176 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Rob Clarke2c719b2014-12-15 13:56:32 -05001177 I915_STATE_WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +02001178 "%s assertion failure (expected %s, current %s)\n",
1179 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001180}
Jesse Barnes040484a2011-01-03 12:14:26 -08001181
1182static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1183 enum pipe pipe, bool state)
1184{
1185 int reg;
1186 u32 val;
1187 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001188 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1189 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001190
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001191 if (HAS_DDI(dev_priv->dev)) {
1192 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -02001193 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001194 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -02001195 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001196 } else {
1197 reg = FDI_TX_CTL(pipe);
1198 val = I915_READ(reg);
1199 cur_state = !!(val & FDI_TX_ENABLE);
1200 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001201 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001202 "FDI TX state assertion failure (expected %s, current %s)\n",
1203 state_string(state), state_string(cur_state));
1204}
1205#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1206#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1207
1208static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1209 enum pipe pipe, bool state)
1210{
1211 int reg;
1212 u32 val;
1213 bool cur_state;
1214
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001215 reg = FDI_RX_CTL(pipe);
1216 val = I915_READ(reg);
1217 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001218 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001219 "FDI RX state assertion failure (expected %s, current %s)\n",
1220 state_string(state), state_string(cur_state));
1221}
1222#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1223#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1224
1225static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1226 enum pipe pipe)
1227{
1228 int reg;
1229 u32 val;
1230
1231 /* ILK FDI PLL is always enabled */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001232 if (INTEL_INFO(dev_priv->dev)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001233 return;
1234
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001235 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001236 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001237 return;
1238
Jesse Barnes040484a2011-01-03 12:14:26 -08001239 reg = FDI_TX_CTL(pipe);
1240 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001241 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 -08001242}
1243
Daniel Vetter55607e82013-06-16 21:42:39 +02001244void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1245 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001246{
1247 int reg;
1248 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001249 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001250
1251 reg = FDI_RX_CTL(pipe);
1252 val = I915_READ(reg);
Daniel Vetter55607e82013-06-16 21:42:39 +02001253 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001254 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001255 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1256 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001257}
1258
Daniel Vetterb680c372014-09-19 18:27:27 +02001259void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1260 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001261{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001262 struct drm_device *dev = dev_priv->dev;
1263 int pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001264 u32 val;
1265 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001266 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001267
Jani Nikulabedd4db2014-08-22 15:04:13 +03001268 if (WARN_ON(HAS_DDI(dev)))
1269 return;
1270
1271 if (HAS_PCH_SPLIT(dev)) {
1272 u32 port_sel;
1273
Jesse Barnesea0760c2011-01-04 15:09:32 -08001274 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001275 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1276
1277 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1278 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1279 panel_pipe = PIPE_B;
1280 /* XXX: else fix for eDP */
1281 } else if (IS_VALLEYVIEW(dev)) {
1282 /* presumably write lock depends on pipe, not port select */
1283 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1284 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001285 } else {
1286 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001287 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1288 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001289 }
1290
1291 val = I915_READ(pp_reg);
1292 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001293 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001294 locked = false;
1295
Rob Clarke2c719b2014-12-15 13:56:32 -05001296 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001297 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001298 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001299}
1300
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001301static void assert_cursor(struct drm_i915_private *dev_priv,
1302 enum pipe pipe, bool state)
1303{
1304 struct drm_device *dev = dev_priv->dev;
1305 bool cur_state;
1306
Paulo Zanonid9d82082014-02-27 16:30:56 -03001307 if (IS_845G(dev) || IS_I865G(dev))
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001308 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001309 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001310 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001311
Rob Clarke2c719b2014-12-15 13:56:32 -05001312 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001313 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1314 pipe_name(pipe), state_string(state), state_string(cur_state));
1315}
1316#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1317#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1318
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001319void assert_pipe(struct drm_i915_private *dev_priv,
1320 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001321{
1322 int reg;
1323 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001324 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001325 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1326 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001327
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001328 /* if we need the pipe quirk it must be always on */
1329 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1330 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001331 state = true;
1332
Daniel Vetterf458ebb2014-09-30 10:56:39 +02001333 if (!intel_display_power_is_enabled(dev_priv,
Paulo Zanonib97186f2013-05-03 12:15:36 -03001334 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001335 cur_state = false;
1336 } else {
1337 reg = PIPECONF(cpu_transcoder);
1338 val = I915_READ(reg);
1339 cur_state = !!(val & PIPECONF_ENABLE);
1340 }
1341
Rob Clarke2c719b2014-12-15 13:56:32 -05001342 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001343 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001344 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001345}
1346
Chris Wilson931872f2012-01-16 23:01:13 +00001347static void assert_plane(struct drm_i915_private *dev_priv,
1348 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001349{
1350 int reg;
1351 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001352 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001353
1354 reg = DSPCNTR(plane);
1355 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001356 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001357 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001358 "plane %c assertion failure (expected %s, current %s)\n",
1359 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001360}
1361
Chris Wilson931872f2012-01-16 23:01:13 +00001362#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1363#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1364
Jesse Barnesb24e7172011-01-04 15:09:30 -08001365static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1366 enum pipe pipe)
1367{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001368 struct drm_device *dev = dev_priv->dev;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001369 int reg, i;
1370 u32 val;
1371 int cur_pipe;
1372
Ville Syrjälä653e1022013-06-04 13:49:05 +03001373 /* Primary planes are fixed to pipes on gen4+ */
1374 if (INTEL_INFO(dev)->gen >= 4) {
Adam Jackson28c057942011-10-07 14:38:42 -04001375 reg = DSPCNTR(pipe);
1376 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001377 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001378 "plane %c assertion failure, should be disabled but not\n",
1379 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001380 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001381 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001382
Jesse Barnesb24e7172011-01-04 15:09:30 -08001383 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001384 for_each_pipe(dev_priv, i) {
Jesse Barnesb24e7172011-01-04 15:09:30 -08001385 reg = DSPCNTR(i);
1386 val = I915_READ(reg);
1387 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1388 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001389 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001390 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1391 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001392 }
1393}
1394
Jesse Barnes19332d72013-03-28 09:55:38 -07001395static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1396 enum pipe pipe)
1397{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001398 struct drm_device *dev = dev_priv->dev;
Damien Lespiau1fe47782014-03-03 17:31:47 +00001399 int reg, sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001400 u32 val;
1401
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001402 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001403 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001404 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001405 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001406 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1407 sprite, pipe_name(pipe));
1408 }
1409 } else if (IS_VALLEYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001410 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +00001411 reg = SPCNTR(pipe, sprite);
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001412 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001413 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001414 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001415 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001416 }
1417 } else if (INTEL_INFO(dev)->gen >= 7) {
1418 reg = SPRCTL(pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001419 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001420 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001421 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001422 plane_name(pipe), pipe_name(pipe));
1423 } else if (INTEL_INFO(dev)->gen >= 5) {
1424 reg = DVSCNTR(pipe);
1425 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001426 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001427 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1428 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001429 }
1430}
1431
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001432static void assert_vblank_disabled(struct drm_crtc *crtc)
1433{
Rob Clarke2c719b2014-12-15 13:56:32 -05001434 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001435 drm_crtc_vblank_put(crtc);
1436}
1437
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001438static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
Jesse Barnes92f25842011-01-04 15:09:34 -08001439{
1440 u32 val;
1441 bool enabled;
1442
Rob Clarke2c719b2014-12-15 13:56:32 -05001443 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001444
Jesse Barnes92f25842011-01-04 15:09:34 -08001445 val = I915_READ(PCH_DREF_CONTROL);
1446 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1447 DREF_SUPERSPREAD_SOURCE_MASK));
Rob Clarke2c719b2014-12-15 13:56:32 -05001448 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
Jesse Barnes92f25842011-01-04 15:09:34 -08001449}
1450
Daniel Vetterab9412b2013-05-03 11:49:46 +02001451static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1452 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001453{
1454 int reg;
1455 u32 val;
1456 bool enabled;
1457
Daniel Vetterab9412b2013-05-03 11:49:46 +02001458 reg = PCH_TRANSCONF(pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001459 val = I915_READ(reg);
1460 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001461 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001462 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1463 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001464}
1465
Keith Packard4e634382011-08-06 10:39:45 -07001466static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1467 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001468{
1469 if ((val & DP_PORT_EN) == 0)
1470 return false;
1471
1472 if (HAS_PCH_CPT(dev_priv->dev)) {
1473 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1474 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1475 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1476 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001477 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1478 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1479 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001480 } else {
1481 if ((val & DP_PIPE_MASK) != (pipe << 30))
1482 return false;
1483 }
1484 return true;
1485}
1486
Keith Packard1519b992011-08-06 10:35:34 -07001487static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1488 enum pipe pipe, u32 val)
1489{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001490 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001491 return false;
1492
1493 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001494 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001495 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001496 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1497 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1498 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001499 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001500 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001501 return false;
1502 }
1503 return true;
1504}
1505
1506static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1507 enum pipe pipe, u32 val)
1508{
1509 if ((val & LVDS_PORT_EN) == 0)
1510 return false;
1511
1512 if (HAS_PCH_CPT(dev_priv->dev)) {
1513 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1514 return false;
1515 } else {
1516 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1517 return false;
1518 }
1519 return true;
1520}
1521
1522static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1523 enum pipe pipe, u32 val)
1524{
1525 if ((val & ADPA_DAC_ENABLE) == 0)
1526 return false;
1527 if (HAS_PCH_CPT(dev_priv->dev)) {
1528 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1529 return false;
1530 } else {
1531 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1532 return false;
1533 }
1534 return true;
1535}
1536
Jesse Barnes291906f2011-02-02 12:28:03 -08001537static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001538 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001539{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001540 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001541 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001542 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001543 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001544
Rob Clarke2c719b2014-12-15 13:56:32 -05001545 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001546 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001547 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001548}
1549
1550static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1551 enum pipe pipe, int reg)
1552{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001553 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001554 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001555 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001556 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001557
Rob Clarke2c719b2014-12-15 13:56:32 -05001558 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001559 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001560 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001561}
1562
1563static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1564 enum pipe pipe)
1565{
1566 int reg;
1567 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001568
Keith Packardf0575e92011-07-25 22:12:43 -07001569 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1570 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1571 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001572
1573 reg = PCH_ADPA;
1574 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001575 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001576 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001577 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001578
1579 reg = PCH_LVDS;
1580 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001581 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001582 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001583 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001584
Paulo Zanonie2debe92013-02-18 19:00:27 -03001585 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1586 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1587 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001588}
1589
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001590static void intel_init_dpio(struct drm_device *dev)
1591{
1592 struct drm_i915_private *dev_priv = dev->dev_private;
1593
1594 if (!IS_VALLEYVIEW(dev))
1595 return;
1596
Chon Ming Leea09cadd2014-04-09 13:28:14 +03001597 /*
1598 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1599 * CHV x1 PHY (DP/HDMI D)
1600 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1601 */
1602 if (IS_CHERRYVIEW(dev)) {
1603 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1604 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1605 } else {
1606 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1607 }
Jesse Barnes5382f5f352013-12-16 16:34:24 -08001608}
1609
Ville Syrjäläd288f652014-10-28 13:20:22 +02001610static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001611 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001612{
Daniel Vetter426115c2013-07-11 22:13:42 +02001613 struct drm_device *dev = crtc->base.dev;
1614 struct drm_i915_private *dev_priv = dev->dev_private;
1615 int reg = DPLL(crtc->pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02001616 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001617
Daniel Vetter426115c2013-07-11 22:13:42 +02001618 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001619
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001620 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001621 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1622
1623 /* PLL is protected by panel, make sure we can write it */
Jani Nikula6a9e7362014-08-22 15:06:35 +03001624 if (IS_MOBILE(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001625 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001626
Daniel Vetter426115c2013-07-11 22:13:42 +02001627 I915_WRITE(reg, dpll);
1628 POSTING_READ(reg);
1629 udelay(150);
1630
1631 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1632 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1633
Ville Syrjäläd288f652014-10-28 13:20:22 +02001634 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
Daniel Vetter426115c2013-07-11 22:13:42 +02001635 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001636
1637 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001638 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001639 POSTING_READ(reg);
1640 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001641 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001642 POSTING_READ(reg);
1643 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001644 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001645 POSTING_READ(reg);
1646 udelay(150); /* wait for warmup */
1647}
1648
Ville Syrjäläd288f652014-10-28 13:20:22 +02001649static void chv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001650 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001651{
1652 struct drm_device *dev = crtc->base.dev;
1653 struct drm_i915_private *dev_priv = dev->dev_private;
1654 int pipe = crtc->pipe;
1655 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001656 u32 tmp;
1657
1658 assert_pipe_disabled(dev_priv, crtc->pipe);
1659
1660 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1661
1662 mutex_lock(&dev_priv->dpio_lock);
1663
1664 /* Enable back the 10bit clock to display controller */
1665 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1666 tmp |= DPIO_DCLKP_EN;
1667 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1668
1669 /*
1670 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1671 */
1672 udelay(1);
1673
1674 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001675 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001676
1677 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001678 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001679 DRM_ERROR("PLL %d failed to lock\n", pipe);
1680
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001681 /* not sure when this should be written */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001682 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001683 POSTING_READ(DPLL_MD(pipe));
1684
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001685 mutex_unlock(&dev_priv->dpio_lock);
1686}
1687
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001688static int intel_num_dvo_pipes(struct drm_device *dev)
1689{
1690 struct intel_crtc *crtc;
1691 int count = 0;
1692
1693 for_each_intel_crtc(dev, crtc)
1694 count += crtc->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001695 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001696
1697 return count;
1698}
1699
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001700static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001701{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001702 struct drm_device *dev = crtc->base.dev;
1703 struct drm_i915_private *dev_priv = dev->dev_private;
1704 int reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001705 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001706
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001707 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001708
1709 /* No really, not for ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001710 BUG_ON(INTEL_INFO(dev)->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001711
1712 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001713 if (IS_MOBILE(dev) && !IS_I830(dev))
1714 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001715
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001716 /* Enable DVO 2x clock on both PLLs if necessary */
1717 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1718 /*
1719 * It appears to be important that we don't enable this
1720 * for the current pipe before otherwise configuring the
1721 * PLL. No idea how this should be handled if multiple
1722 * DVO outputs are enabled simultaneosly.
1723 */
1724 dpll |= DPLL_DVO_2X_MODE;
1725 I915_WRITE(DPLL(!crtc->pipe),
1726 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1727 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001728
1729 /* Wait for the clocks to stabilize. */
1730 POSTING_READ(reg);
1731 udelay(150);
1732
1733 if (INTEL_INFO(dev)->gen >= 4) {
1734 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001735 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001736 } else {
1737 /* The pixel multiplier can only be updated once the
1738 * DPLL is enabled and the clocks are stable.
1739 *
1740 * So write it again.
1741 */
1742 I915_WRITE(reg, dpll);
1743 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001744
1745 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001746 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001747 POSTING_READ(reg);
1748 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001749 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001750 POSTING_READ(reg);
1751 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001752 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001753 POSTING_READ(reg);
1754 udelay(150); /* wait for warmup */
1755}
1756
1757/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001758 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001759 * @dev_priv: i915 private structure
1760 * @pipe: pipe PLL to disable
1761 *
1762 * Disable the PLL for @pipe, making sure the pipe is off first.
1763 *
1764 * Note! This is for pre-ILK only.
1765 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001766static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001767{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001768 struct drm_device *dev = crtc->base.dev;
1769 struct drm_i915_private *dev_priv = dev->dev_private;
1770 enum pipe pipe = crtc->pipe;
1771
1772 /* Disable DVO 2x clock on both PLLs if necessary */
1773 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001774 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001775 intel_num_dvo_pipes(dev) == 1) {
1776 I915_WRITE(DPLL(PIPE_B),
1777 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1778 I915_WRITE(DPLL(PIPE_A),
1779 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1780 }
1781
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001782 /* Don't disable pipe or pipe PLLs if needed */
1783 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1784 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001785 return;
1786
1787 /* Make sure the pipe isn't still relying on us */
1788 assert_pipe_disabled(dev_priv, pipe);
1789
Daniel Vetter50b44a42013-06-05 13:34:33 +02001790 I915_WRITE(DPLL(pipe), 0);
1791 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001792}
1793
Jesse Barnesf6071162013-10-01 10:41:38 -07001794static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1795{
1796 u32 val = 0;
1797
1798 /* Make sure the pipe isn't still relying on us */
1799 assert_pipe_disabled(dev_priv, pipe);
1800
Imre Deake5cbfbf2014-01-09 17:08:16 +02001801 /*
1802 * Leave integrated clock source and reference clock enabled for pipe B.
1803 * The latter is needed for VGA hotplug / manual detection.
1804 */
Jesse Barnesf6071162013-10-01 10:41:38 -07001805 if (pipe == PIPE_B)
Imre Deake5cbfbf2014-01-09 17:08:16 +02001806 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07001807 I915_WRITE(DPLL(pipe), val);
1808 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001809
1810}
1811
1812static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1813{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001814 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001815 u32 val;
1816
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001817 /* Make sure the pipe isn't still relying on us */
1818 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001819
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001820 /* Set PLL en = 0 */
Ville Syrjäläd17ec4c2014-06-28 02:03:59 +03001821 val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001822 if (pipe != PIPE_A)
1823 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1824 I915_WRITE(DPLL(pipe), val);
1825 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001826
1827 mutex_lock(&dev_priv->dpio_lock);
1828
1829 /* Disable 10bit clock to display controller */
1830 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1831 val &= ~DPIO_DCLKP_EN;
1832 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1833
Ville Syrjälä61407f62014-05-27 16:32:55 +03001834 /* disable left/right clock distribution */
1835 if (pipe != PIPE_B) {
1836 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1837 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1838 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1839 } else {
1840 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1841 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1842 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1843 }
1844
Ville Syrjäläd7520482014-04-09 13:28:59 +03001845 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001846}
1847
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001848void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1849 struct intel_digital_port *dport)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001850{
1851 u32 port_mask;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001852 int dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001853
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001854 switch (dport->port) {
1855 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001856 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001857 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001858 break;
1859 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001860 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001861 dpll_reg = DPLL(0);
1862 break;
1863 case PORT_D:
1864 port_mask = DPLL_PORTD_READY_MASK;
1865 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001866 break;
1867 default:
1868 BUG();
1869 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001870
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001871 if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
Jesse Barnes89b667f2013-04-18 14:51:36 -07001872 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001873 port_name(dport->port), I915_READ(dpll_reg));
Jesse Barnes89b667f2013-04-18 14:51:36 -07001874}
1875
Daniel Vetterb14b1052014-04-24 23:55:13 +02001876static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1877{
1878 struct drm_device *dev = crtc->base.dev;
1879 struct drm_i915_private *dev_priv = dev->dev_private;
1880 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1881
Chris Wilsonbe19f0f2014-05-28 16:16:42 +01001882 if (WARN_ON(pll == NULL))
1883 return;
1884
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001885 WARN_ON(!pll->config.crtc_mask);
Daniel Vetterb14b1052014-04-24 23:55:13 +02001886 if (pll->active == 0) {
1887 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1888 WARN_ON(pll->on);
1889 assert_shared_dpll_disabled(dev_priv, pll);
1890
1891 pll->mode_set(dev_priv, pll);
1892 }
1893}
1894
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001895/**
Daniel Vetter85b38942014-04-24 23:55:14 +02001896 * intel_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001897 * @dev_priv: i915 private structure
1898 * @pipe: pipe PLL to enable
1899 *
1900 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1901 * drives the transcoder clock.
1902 */
Daniel Vetter85b38942014-04-24 23:55:14 +02001903static void intel_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001904{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001905 struct drm_device *dev = crtc->base.dev;
1906 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001907 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001908
Daniel Vetter87a875b2013-06-05 13:34:19 +02001909 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001910 return;
1911
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001912 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001913 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001914
Damien Lespiau74dd6922014-07-29 18:06:17 +01001915 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
Daniel Vetter46edb022013-06-05 13:34:12 +02001916 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001917 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001918
Daniel Vettercdbd2312013-06-05 13:34:03 +02001919 if (pll->active++) {
1920 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001921 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001922 return;
1923 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001924 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001925
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001926 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1927
Daniel Vetter46edb022013-06-05 13:34:12 +02001928 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001929 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001930 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001931}
1932
Damien Lespiauf6daaec2014-08-09 23:00:56 +01001933static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001934{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001935 struct drm_device *dev = crtc->base.dev;
1936 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001937 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001938
Jesse Barnes92f25842011-01-04 15:09:34 -08001939 /* PCH only available on ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001940 BUG_ON(INTEL_INFO(dev)->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001941 if (WARN_ON(pll == NULL))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001942 return;
1943
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001944 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001945 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001946
Daniel Vetter46edb022013-06-05 13:34:12 +02001947 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1948 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001949 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001950
Chris Wilson48da64a2012-05-13 20:16:12 +01001951 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001952 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001953 return;
1954 }
1955
Daniel Vettere9d69442013-06-05 13:34:15 +02001956 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001957 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001958 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001959 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001960
Daniel Vetter46edb022013-06-05 13:34:12 +02001961 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001962 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001963 pll->on = false;
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001964
1965 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
Jesse Barnes92f25842011-01-04 15:09:34 -08001966}
1967
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001968static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1969 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001970{
Daniel Vetter23670b322012-11-01 09:15:30 +01001971 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001972 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001973 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001974 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001975
1976 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001977 BUG_ON(!HAS_PCH_SPLIT(dev));
Jesse Barnes040484a2011-01-03 12:14:26 -08001978
1979 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001980 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001981 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001982
1983 /* FDI must be feeding us bits for PCH ports */
1984 assert_fdi_tx_enabled(dev_priv, pipe);
1985 assert_fdi_rx_enabled(dev_priv, pipe);
1986
Daniel Vetter23670b322012-11-01 09:15:30 +01001987 if (HAS_PCH_CPT(dev)) {
1988 /* Workaround: Set the timing override bit before enabling the
1989 * pch transcoder. */
1990 reg = TRANS_CHICKEN2(pipe);
1991 val = I915_READ(reg);
1992 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1993 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001994 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001995
Daniel Vetterab9412b2013-05-03 11:49:46 +02001996 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001997 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001998 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001999
2000 if (HAS_PCH_IBX(dev_priv->dev)) {
2001 /*
2002 * make the BPC in transcoder be consistent with
2003 * that in pipeconf reg.
2004 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002005 val &= ~PIPECONF_BPC_MASK;
2006 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07002007 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002008
2009 val &= ~TRANS_INTERLACE_MASK;
2010 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002011 if (HAS_PCH_IBX(dev_priv->dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002012 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002013 val |= TRANS_LEGACY_INTERLACED_ILK;
2014 else
2015 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002016 else
2017 val |= TRANS_PROGRESSIVE;
2018
Jesse Barnes040484a2011-01-03 12:14:26 -08002019 I915_WRITE(reg, val | TRANS_ENABLE);
2020 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002021 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08002022}
2023
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002024static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02002025 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08002026{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002027 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002028
2029 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03002030 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002031
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002032 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01002033 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02002034 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002035
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002036 /* Workaround: set timing override bit. */
2037 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002038 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002039 I915_WRITE(_TRANSA_CHICKEN2, val);
2040
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02002041 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02002042 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002043
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02002044 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
2045 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02002046 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002047 else
2048 val |= TRANS_PROGRESSIVE;
2049
Daniel Vetterab9412b2013-05-03 11:49:46 +02002050 I915_WRITE(LPT_TRANSCONF, val);
2051 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02002052 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002053}
2054
Paulo Zanonib8a4f402012-10-31 18:12:42 -02002055static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
2056 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08002057{
Daniel Vetter23670b322012-11-01 09:15:30 +01002058 struct drm_device *dev = dev_priv->dev;
2059 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08002060
2061 /* FDI relies on the transcoder */
2062 assert_fdi_tx_disabled(dev_priv, pipe);
2063 assert_fdi_rx_disabled(dev_priv, pipe);
2064
Jesse Barnes291906f2011-02-02 12:28:03 -08002065 /* Ports must be off as well */
2066 assert_pch_ports_disabled(dev_priv, pipe);
2067
Daniel Vetterab9412b2013-05-03 11:49:46 +02002068 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002069 val = I915_READ(reg);
2070 val &= ~TRANS_ENABLE;
2071 I915_WRITE(reg, val);
2072 /* wait for PCH transcoder off, transcoder state */
2073 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002074 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01002075
2076 if (!HAS_PCH_IBX(dev)) {
2077 /* Workaround: Clear the timing override chicken bit again. */
2078 reg = TRANS_CHICKEN2(pipe);
2079 val = I915_READ(reg);
2080 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2081 I915_WRITE(reg, val);
2082 }
Jesse Barnes040484a2011-01-03 12:14:26 -08002083}
2084
Paulo Zanoniab4d9662012-10-31 18:12:55 -02002085static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002086{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002087 u32 val;
2088
Daniel Vetterab9412b2013-05-03 11:49:46 +02002089 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002090 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02002091 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002092 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02002093 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02002094 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002095
2096 /* Workaround: clear timing override bit. */
2097 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002098 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002099 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08002100}
2101
2102/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002103 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02002104 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08002105 *
Paulo Zanoni03722642014-01-17 13:51:09 -02002106 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08002107 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002108 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02002109static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002110{
Paulo Zanoni03722642014-01-17 13:51:09 -02002111 struct drm_device *dev = crtc->base.dev;
2112 struct drm_i915_private *dev_priv = dev->dev_private;
2113 enum pipe pipe = crtc->pipe;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002114 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2115 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002116 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002117 int reg;
2118 u32 val;
2119
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002120 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002121 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002122 assert_sprites_disabled(dev_priv, pipe);
2123
Paulo Zanoni681e5812012-12-06 11:12:38 -02002124 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002125 pch_transcoder = TRANSCODER_A;
2126 else
2127 pch_transcoder = pipe;
2128
Jesse Barnesb24e7172011-01-04 15:09:30 -08002129 /*
2130 * A pipe without a PLL won't actually be able to drive bits from
2131 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2132 * need the check.
2133 */
Imre Deak50360402015-01-16 00:55:16 -08002134 if (HAS_GMCH_DISPLAY(dev_priv->dev))
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002135 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03002136 assert_dsi_pll_enabled(dev_priv);
2137 else
2138 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002139 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002140 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002141 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002142 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002143 assert_fdi_tx_pll_enabled(dev_priv,
2144 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08002145 }
2146 /* FIXME: assert CPU port conditions for SNB+ */
2147 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08002148
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002149 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002150 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002151 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002152 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2153 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002154 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002155 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002156
2157 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002158 POSTING_READ(reg);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002159}
2160
2161/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002162 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002163 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002164 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002165 * Disable the pipe of @crtc, making sure that various hardware
2166 * specific requirements are met, if applicable, e.g. plane
2167 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002168 *
2169 * Will wait until the pipe has shut down before returning.
2170 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002171static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002172{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002173 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002174 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002175 enum pipe pipe = crtc->pipe;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002176 int reg;
2177 u32 val;
2178
2179 /*
2180 * Make sure planes won't keep trying to pump pixels to us,
2181 * or we might hang the display.
2182 */
2183 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002184 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002185 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002186
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002187 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002188 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002189 if ((val & PIPECONF_ENABLE) == 0)
2190 return;
2191
Ville Syrjälä67adc642014-08-15 01:21:57 +03002192 /*
2193 * Double wide has implications for planes
2194 * so best keep it disabled when not needed.
2195 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002196 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002197 val &= ~PIPECONF_DOUBLE_WIDE;
2198
2199 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002200 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2201 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002202 val &= ~PIPECONF_ENABLE;
2203
2204 I915_WRITE(reg, val);
2205 if ((val & PIPECONF_ENABLE) == 0)
2206 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002207}
2208
Keith Packardd74362c2011-07-28 14:47:14 -07002209/*
2210 * Plane regs are double buffered, going from enabled->disabled needs a
2211 * trigger in order to latch. The display address reg provides this.
2212 */
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002213void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
2214 enum plane plane)
Keith Packardd74362c2011-07-28 14:47:14 -07002215{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00002216 struct drm_device *dev = dev_priv->dev;
2217 u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002218
2219 I915_WRITE(reg, I915_READ(reg));
2220 POSTING_READ(reg);
Keith Packardd74362c2011-07-28 14:47:14 -07002221}
2222
Jesse Barnesb24e7172011-01-04 15:09:30 -08002223/**
Matt Roper262ca2b2014-03-18 17:22:55 -07002224 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002225 * @plane: plane to be enabled
2226 * @crtc: crtc for the plane
Jesse Barnesb24e7172011-01-04 15:09:30 -08002227 *
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002228 * Enable @plane on @crtc, making sure that the pipe is running first.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002229 */
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002230static void intel_enable_primary_hw_plane(struct drm_plane *plane,
2231 struct drm_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002232{
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002233 struct drm_device *dev = plane->dev;
2234 struct drm_i915_private *dev_priv = dev->dev_private;
2235 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002236
2237 /* If the pipe isn't enabled, we can't pump pixels and may hang */
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002238 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002239
Ville Syrjälä98ec7732014-04-30 17:43:01 +03002240 if (intel_crtc->primary_enabled)
2241 return;
Ville Syrjälä0037f712013-10-01 18:02:20 +03002242
Ville Syrjälä4c445e02013-10-09 17:24:58 +03002243 intel_crtc->primary_enabled = true;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002244
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002245 dev_priv->display.update_primary_plane(crtc, plane->fb,
2246 crtc->x, crtc->y);
Ville Syrjälä33c3b0d2014-06-24 13:59:28 +03002247
2248 /*
2249 * BDW signals flip done immediately if the plane
2250 * is disabled, even if the plane enable is already
2251 * armed to occur at the next vblank :(
2252 */
2253 if (IS_BROADWELL(dev))
2254 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002255}
2256
Jesse Barnesb24e7172011-01-04 15:09:30 -08002257/**
Matt Roper262ca2b2014-03-18 17:22:55 -07002258 * intel_disable_primary_hw_plane - disable the primary hardware plane
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002259 * @plane: plane to be disabled
2260 * @crtc: crtc for the plane
Jesse Barnesb24e7172011-01-04 15:09:30 -08002261 *
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002262 * Disable @plane on @crtc, making sure that the pipe is running first.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002263 */
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002264static void intel_disable_primary_hw_plane(struct drm_plane *plane,
2265 struct drm_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002266{
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002267 struct drm_device *dev = plane->dev;
2268 struct drm_i915_private *dev_priv = dev->dev_private;
2269 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2270
Matt Roper32b7eee2014-12-24 07:59:06 -08002271 if (WARN_ON(!intel_crtc->active))
2272 return;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002273
Ville Syrjälä98ec7732014-04-30 17:43:01 +03002274 if (!intel_crtc->primary_enabled)
2275 return;
Ville Syrjälä0037f712013-10-01 18:02:20 +03002276
Ville Syrjälä4c445e02013-10-09 17:24:58 +03002277 intel_crtc->primary_enabled = false;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002278
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002279 dev_priv->display.update_primary_plane(crtc, plane->fb,
2280 crtc->x, crtc->y);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002281}
2282
Chris Wilson693db182013-03-05 14:52:39 +00002283static bool need_vtd_wa(struct drm_device *dev)
2284{
2285#ifdef CONFIG_INTEL_IOMMU
2286 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2287 return true;
2288#endif
2289 return false;
2290}
2291
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002292unsigned int
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002293intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
2294 uint64_t fb_format_modifier)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002295{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002296 unsigned int tile_height;
2297 uint32_t pixel_bytes;
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002298
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002299 switch (fb_format_modifier) {
2300 case DRM_FORMAT_MOD_NONE:
2301 tile_height = 1;
2302 break;
2303 case I915_FORMAT_MOD_X_TILED:
2304 tile_height = IS_GEN2(dev) ? 16 : 8;
2305 break;
2306 case I915_FORMAT_MOD_Y_TILED:
2307 tile_height = 32;
2308 break;
2309 case I915_FORMAT_MOD_Yf_TILED:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002310 pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
2311 switch (pixel_bytes) {
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002312 default:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002313 case 1:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002314 tile_height = 64;
2315 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002316 case 2:
2317 case 4:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002318 tile_height = 32;
2319 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002320 case 8:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002321 tile_height = 16;
2322 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002323 case 16:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002324 WARN_ONCE(1,
2325 "128-bit pixels are not supported for display!");
2326 tile_height = 16;
2327 break;
2328 }
2329 break;
2330 default:
2331 MISSING_CASE(fb_format_modifier);
2332 tile_height = 1;
2333 break;
2334 }
Daniel Vetter091df6c2015-02-10 17:16:10 +00002335
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002336 return tile_height;
2337}
2338
2339unsigned int
2340intel_fb_align_height(struct drm_device *dev, unsigned int height,
2341 uint32_t pixel_format, uint64_t fb_format_modifier)
2342{
2343 return ALIGN(height, intel_tile_height(dev, pixel_format,
2344 fb_format_modifier));
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002345}
2346
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002347static int
2348intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
2349 const struct drm_plane_state *plane_state)
2350{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002351 struct intel_rotation_info *info = &view->rotation_info;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002352
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002353 *view = i915_ggtt_view_normal;
2354
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002355 if (!plane_state)
2356 return 0;
2357
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002358 if (!intel_rotation_90_or_270(plane_state->rotation))
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002359 return 0;
2360
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002361 *view = i915_ggtt_view_rotated;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002362
2363 info->height = fb->height;
2364 info->pixel_format = fb->pixel_format;
2365 info->pitch = fb->pitches[0];
2366 info->fb_modifier = fb->modifier[0];
2367
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002368 return 0;
2369}
2370
Chris Wilson127bd2a2010-07-23 23:32:05 +01002371int
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002372intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2373 struct drm_framebuffer *fb,
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002374 const struct drm_plane_state *plane_state,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002375 struct intel_engine_cs *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002376{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002377 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002378 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002379 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002380 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002381 u32 alignment;
2382 int ret;
2383
Matt Roperebcdd392014-07-09 16:22:11 -07002384 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2385
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002386 switch (fb->modifier[0]) {
2387 case DRM_FORMAT_MOD_NONE:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002388 if (INTEL_INFO(dev)->gen >= 9)
2389 alignment = 256 * 1024;
2390 else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
Chris Wilson534843d2010-07-05 18:01:46 +01002391 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002392 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01002393 alignment = 4 * 1024;
2394 else
2395 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002396 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002397 case I915_FORMAT_MOD_X_TILED:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002398 if (INTEL_INFO(dev)->gen >= 9)
2399 alignment = 256 * 1024;
2400 else {
2401 /* pin() will align the object as required by fence */
2402 alignment = 0;
2403 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002404 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002405 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiau1327b9a2015-02-27 11:15:20 +00002406 case I915_FORMAT_MOD_Yf_TILED:
2407 if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2408 "Y tiling bo slipped through, driver bug!\n"))
2409 return -EINVAL;
2410 alignment = 1 * 1024 * 1024;
2411 break;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002412 default:
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002413 MISSING_CASE(fb->modifier[0]);
2414 return -EINVAL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002415 }
2416
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002417 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2418 if (ret)
2419 return ret;
2420
Chris Wilson693db182013-03-05 14:52:39 +00002421 /* Note that the w/a also requires 64 PTE of padding following the
2422 * bo. We currently fill all unused PTE with the shadow page and so
2423 * we should always have valid PTE following the scanout preventing
2424 * the VT-d warning.
2425 */
2426 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2427 alignment = 256 * 1024;
2428
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002429 /*
2430 * Global gtt pte registers are special registers which actually forward
2431 * writes to a chunk of system memory. Which means that there is no risk
2432 * that the register values disappear as soon as we call
2433 * intel_runtime_pm_put(), so it is correct to wrap only the
2434 * pin/unpin/fence and not more.
2435 */
2436 intel_runtime_pm_get(dev_priv);
2437
Chris Wilsonce453d82011-02-21 14:43:56 +00002438 dev_priv->mm.interruptible = false;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00002439 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002440 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002441 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00002442 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002443
2444 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2445 * fence, whereas 965+ only requires a fence if using
2446 * framebuffer compression. For simplicity, we always install
2447 * a fence as the cost is not that onerous.
2448 */
Chris Wilson06d98132012-04-17 15:31:24 +01002449 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002450 if (ret)
2451 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002452
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002453 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002454
Chris Wilsonce453d82011-02-21 14:43:56 +00002455 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002456 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002457 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002458
2459err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002460 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilsonce453d82011-02-21 14:43:56 +00002461err_interruptible:
2462 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002463 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002464 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002465}
2466
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002467static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
2468 const struct drm_plane_state *plane_state)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002469{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002470 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002471 struct i915_ggtt_view view;
2472 int ret;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002473
Matt Roperebcdd392014-07-09 16:22:11 -07002474 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2475
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002476 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2477 WARN_ONCE(ret, "Couldn't get view from plane state!");
2478
Chris Wilson1690e1e2011-12-14 13:57:08 +01002479 i915_gem_object_unpin_fence(obj);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002480 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002481}
2482
Daniel Vetterc2c75132012-07-05 12:17:30 +02002483/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2484 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00002485unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2486 unsigned int tiling_mode,
2487 unsigned int cpp,
2488 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002489{
Chris Wilsonbc752862013-02-21 20:04:31 +00002490 if (tiling_mode != I915_TILING_NONE) {
2491 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002492
Chris Wilsonbc752862013-02-21 20:04:31 +00002493 tile_rows = *y / 8;
2494 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002495
Chris Wilsonbc752862013-02-21 20:04:31 +00002496 tiles = *x / (512/cpp);
2497 *x %= 512/cpp;
2498
2499 return tile_rows * pitch * 8 + tiles * 4096;
2500 } else {
2501 unsigned int offset;
2502
2503 offset = *y * pitch + *x * cpp;
2504 *y = 0;
2505 *x = (offset & 4095) / cpp;
2506 return offset & -4096;
2507 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002508}
2509
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002510static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002511{
2512 switch (format) {
2513 case DISPPLANE_8BPP:
2514 return DRM_FORMAT_C8;
2515 case DISPPLANE_BGRX555:
2516 return DRM_FORMAT_XRGB1555;
2517 case DISPPLANE_BGRX565:
2518 return DRM_FORMAT_RGB565;
2519 default:
2520 case DISPPLANE_BGRX888:
2521 return DRM_FORMAT_XRGB8888;
2522 case DISPPLANE_RGBX888:
2523 return DRM_FORMAT_XBGR8888;
2524 case DISPPLANE_BGRX101010:
2525 return DRM_FORMAT_XRGB2101010;
2526 case DISPPLANE_RGBX101010:
2527 return DRM_FORMAT_XBGR2101010;
2528 }
2529}
2530
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002531static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2532{
2533 switch (format) {
2534 case PLANE_CTL_FORMAT_RGB_565:
2535 return DRM_FORMAT_RGB565;
2536 default:
2537 case PLANE_CTL_FORMAT_XRGB_8888:
2538 if (rgb_order) {
2539 if (alpha)
2540 return DRM_FORMAT_ABGR8888;
2541 else
2542 return DRM_FORMAT_XBGR8888;
2543 } else {
2544 if (alpha)
2545 return DRM_FORMAT_ARGB8888;
2546 else
2547 return DRM_FORMAT_XRGB8888;
2548 }
2549 case PLANE_CTL_FORMAT_XRGB_2101010:
2550 if (rgb_order)
2551 return DRM_FORMAT_XBGR2101010;
2552 else
2553 return DRM_FORMAT_XRGB2101010;
2554 }
2555}
2556
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002557static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002558intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2559 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002560{
2561 struct drm_device *dev = crtc->base.dev;
2562 struct drm_i915_gem_object *obj = NULL;
2563 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002564 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002565 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2566 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2567 PAGE_SIZE);
2568
2569 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002570
Chris Wilsonff2652e2014-03-10 08:07:02 +00002571 if (plane_config->size == 0)
2572 return false;
2573
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002574 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2575 base_aligned,
2576 base_aligned,
2577 size_aligned);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002578 if (!obj)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002579 return false;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002580
Damien Lespiau49af4492015-01-20 12:51:44 +00002581 obj->tiling_mode = plane_config->tiling;
2582 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002583 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002584
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002585 mode_cmd.pixel_format = fb->pixel_format;
2586 mode_cmd.width = fb->width;
2587 mode_cmd.height = fb->height;
2588 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002589 mode_cmd.modifier[0] = fb->modifier[0];
2590 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002591
2592 mutex_lock(&dev->struct_mutex);
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002593 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002594 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002595 DRM_DEBUG_KMS("intel fb init failed\n");
2596 goto out_unref_obj;
2597 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002598 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002599
Daniel Vetterf6936e22015-03-26 12:17:05 +01002600 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002601 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002602
2603out_unref_obj:
2604 drm_gem_object_unreference(&obj->base);
2605 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002606 return false;
2607}
2608
Matt Roperafd65eb2015-02-03 13:10:04 -08002609/* Update plane->state->fb to match plane->fb after driver-internal updates */
2610static void
2611update_state_fb(struct drm_plane *plane)
2612{
2613 if (plane->fb == plane->state->fb)
2614 return;
2615
2616 if (plane->state->fb)
2617 drm_framebuffer_unreference(plane->state->fb);
2618 plane->state->fb = plane->fb;
2619 if (plane->state->fb)
2620 drm_framebuffer_reference(plane->state->fb);
2621}
2622
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002623static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002624intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2625 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002626{
2627 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002628 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002629 struct drm_crtc *c;
2630 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002631 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002632 struct drm_plane *primary = intel_crtc->base.primary;
2633 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002634
Damien Lespiau2d140302015-02-05 17:22:18 +00002635 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002636 return;
2637
Daniel Vetterf6936e22015-03-26 12:17:05 +01002638 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002639 fb = &plane_config->fb->base;
2640 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002641 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002642
Damien Lespiau2d140302015-02-05 17:22:18 +00002643 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002644
2645 /*
2646 * Failed to alloc the obj, check to see if we should share
2647 * an fb with another CRTC instead
2648 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002649 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002650 i = to_intel_crtc(c);
2651
2652 if (c == &intel_crtc->base)
2653 continue;
2654
Matt Roper2ff8fde2014-07-08 07:50:07 -07002655 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002656 continue;
2657
Daniel Vetter88595ac2015-03-26 12:42:24 +01002658 fb = c->primary->fb;
2659 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002660 continue;
2661
Daniel Vetter88595ac2015-03-26 12:42:24 +01002662 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002663 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002664 drm_framebuffer_reference(fb);
2665 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002666 }
2667 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002668
2669 return;
2670
2671valid_fb:
2672 obj = intel_fb_obj(fb);
2673 if (obj->tiling_mode != I915_TILING_NONE)
2674 dev_priv->preserve_bios_swizzle = true;
2675
2676 primary->fb = fb;
2677 primary->state->crtc = &intel_crtc->base;
2678 primary->crtc = &intel_crtc->base;
2679 update_state_fb(primary);
2680 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002681}
2682
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002683static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2684 struct drm_framebuffer *fb,
2685 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002686{
2687 struct drm_device *dev = crtc->dev;
2688 struct drm_i915_private *dev_priv = dev->dev_private;
2689 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002690 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002691 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002692 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002693 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002694 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302695 int pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002696
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +03002697 if (!intel_crtc->primary_enabled || !fb) {
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002698 I915_WRITE(reg, 0);
2699 if (INTEL_INFO(dev)->gen >= 4)
2700 I915_WRITE(DSPSURF(plane), 0);
2701 else
2702 I915_WRITE(DSPADDR(plane), 0);
2703 POSTING_READ(reg);
2704 return;
2705 }
2706
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002707 obj = intel_fb_obj(fb);
2708 if (WARN_ON(obj == NULL))
2709 return;
2710
2711 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2712
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002713 dspcntr = DISPPLANE_GAMMA_ENABLE;
2714
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002715 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002716
2717 if (INTEL_INFO(dev)->gen < 4) {
2718 if (intel_crtc->pipe == PIPE_B)
2719 dspcntr |= DISPPLANE_SEL_PIPE_B;
2720
2721 /* pipesrc and dspsize control the size that is scaled from,
2722 * which should always be the user's requested size.
2723 */
2724 I915_WRITE(DSPSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002725 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2726 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002727 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002728 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2729 I915_WRITE(PRIMSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002730 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2731 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002732 I915_WRITE(PRIMPOS(plane), 0);
2733 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002734 }
2735
Ville Syrjälä57779d02012-10-31 17:50:14 +02002736 switch (fb->pixel_format) {
2737 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002738 dspcntr |= DISPPLANE_8BPP;
2739 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002740 case DRM_FORMAT_XRGB1555:
2741 case DRM_FORMAT_ARGB1555:
2742 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002743 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002744 case DRM_FORMAT_RGB565:
2745 dspcntr |= DISPPLANE_BGRX565;
2746 break;
2747 case DRM_FORMAT_XRGB8888:
2748 case DRM_FORMAT_ARGB8888:
2749 dspcntr |= DISPPLANE_BGRX888;
2750 break;
2751 case DRM_FORMAT_XBGR8888:
2752 case DRM_FORMAT_ABGR8888:
2753 dspcntr |= DISPPLANE_RGBX888;
2754 break;
2755 case DRM_FORMAT_XRGB2101010:
2756 case DRM_FORMAT_ARGB2101010:
2757 dspcntr |= DISPPLANE_BGRX101010;
2758 break;
2759 case DRM_FORMAT_XBGR2101010:
2760 case DRM_FORMAT_ABGR2101010:
2761 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002762 break;
2763 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002764 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002765 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002766
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002767 if (INTEL_INFO(dev)->gen >= 4 &&
2768 obj->tiling_mode != I915_TILING_NONE)
2769 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002770
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002771 if (IS_G4X(dev))
2772 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2773
Ville Syrjäläb98971272014-08-27 16:51:22 +03002774 linear_offset = y * fb->pitches[0] + x * pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002775
Daniel Vetterc2c75132012-07-05 12:17:30 +02002776 if (INTEL_INFO(dev)->gen >= 4) {
2777 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002778 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002779 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002780 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002781 linear_offset -= intel_crtc->dspaddr_offset;
2782 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002783 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002784 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002785
Matt Roper8e7d6882015-01-21 16:35:41 -08002786 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302787 dspcntr |= DISPPLANE_ROTATE_180;
2788
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002789 x += (intel_crtc->config->pipe_src_w - 1);
2790 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302791
2792 /* Finding the last pixel of the last line of the display
2793 data and adding to linear_offset*/
2794 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002795 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2796 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302797 }
2798
2799 I915_WRITE(reg, dspcntr);
2800
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002801 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002802 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002803 I915_WRITE(DSPSURF(plane),
2804 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002805 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002806 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002807 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002808 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002809 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002810}
2811
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002812static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2813 struct drm_framebuffer *fb,
2814 int x, int y)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002815{
2816 struct drm_device *dev = crtc->dev;
2817 struct drm_i915_private *dev_priv = dev->dev_private;
2818 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002819 struct drm_i915_gem_object *obj;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002820 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002821 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002822 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002823 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302824 int pixel_size;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002825
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +03002826 if (!intel_crtc->primary_enabled || !fb) {
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002827 I915_WRITE(reg, 0);
2828 I915_WRITE(DSPSURF(plane), 0);
2829 POSTING_READ(reg);
2830 return;
2831 }
2832
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002833 obj = intel_fb_obj(fb);
2834 if (WARN_ON(obj == NULL))
2835 return;
2836
2837 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2838
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002839 dspcntr = DISPPLANE_GAMMA_ENABLE;
2840
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002841 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002842
2843 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2844 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2845
Ville Syrjälä57779d02012-10-31 17:50:14 +02002846 switch (fb->pixel_format) {
2847 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002848 dspcntr |= DISPPLANE_8BPP;
2849 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002850 case DRM_FORMAT_RGB565:
2851 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002852 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002853 case DRM_FORMAT_XRGB8888:
2854 case DRM_FORMAT_ARGB8888:
2855 dspcntr |= DISPPLANE_BGRX888;
2856 break;
2857 case DRM_FORMAT_XBGR8888:
2858 case DRM_FORMAT_ABGR8888:
2859 dspcntr |= DISPPLANE_RGBX888;
2860 break;
2861 case DRM_FORMAT_XRGB2101010:
2862 case DRM_FORMAT_ARGB2101010:
2863 dspcntr |= DISPPLANE_BGRX101010;
2864 break;
2865 case DRM_FORMAT_XBGR2101010:
2866 case DRM_FORMAT_ABGR2101010:
2867 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002868 break;
2869 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002870 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002871 }
2872
2873 if (obj->tiling_mode != I915_TILING_NONE)
2874 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002875
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002876 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002877 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002878
Ville Syrjäläb98971272014-08-27 16:51:22 +03002879 linear_offset = y * fb->pitches[0] + x * pixel_size;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002880 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002881 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002882 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002883 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002884 linear_offset -= intel_crtc->dspaddr_offset;
Matt Roper8e7d6882015-01-21 16:35:41 -08002885 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302886 dspcntr |= DISPPLANE_ROTATE_180;
2887
2888 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002889 x += (intel_crtc->config->pipe_src_w - 1);
2890 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302891
2892 /* Finding the last pixel of the last line of the display
2893 data and adding to linear_offset*/
2894 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002895 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2896 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302897 }
2898 }
2899
2900 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002901
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002902 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002903 I915_WRITE(DSPSURF(plane),
2904 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002905 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002906 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2907 } else {
2908 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2909 I915_WRITE(DSPLINOFF(plane), linear_offset);
2910 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002911 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002912}
2913
Damien Lespiaub3218032015-02-27 11:15:18 +00002914u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2915 uint32_t pixel_format)
2916{
2917 u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2918
2919 /*
2920 * The stride is either expressed as a multiple of 64 bytes
2921 * chunks for linear buffers or in number of tiles for tiled
2922 * buffers.
2923 */
2924 switch (fb_modifier) {
2925 case DRM_FORMAT_MOD_NONE:
2926 return 64;
2927 case I915_FORMAT_MOD_X_TILED:
2928 if (INTEL_INFO(dev)->gen == 2)
2929 return 128;
2930 return 512;
2931 case I915_FORMAT_MOD_Y_TILED:
2932 /* No need to check for old gens and Y tiling since this is
2933 * about the display engine and those will be blocked before
2934 * we get here.
2935 */
2936 return 128;
2937 case I915_FORMAT_MOD_Yf_TILED:
2938 if (bits_per_pixel == 8)
2939 return 64;
2940 else
2941 return 128;
2942 default:
2943 MISSING_CASE(fb_modifier);
2944 return 64;
2945 }
2946}
2947
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002948unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
2949 struct drm_i915_gem_object *obj)
2950{
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002951 const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002952
2953 if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002954 view = &i915_ggtt_view_rotated;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002955
2956 return i915_gem_obj_ggtt_offset_view(obj, view);
2957}
2958
Chandra Kondurua1b22782015-04-07 15:28:45 -07002959/*
2960 * This function detaches (aka. unbinds) unused scalers in hardware
2961 */
2962void skl_detach_scalers(struct intel_crtc *intel_crtc)
2963{
2964 struct drm_device *dev;
2965 struct drm_i915_private *dev_priv;
2966 struct intel_crtc_scaler_state *scaler_state;
2967 int i;
2968
2969 if (!intel_crtc || !intel_crtc->config)
2970 return;
2971
2972 dev = intel_crtc->base.dev;
2973 dev_priv = dev->dev_private;
2974 scaler_state = &intel_crtc->config->scaler_state;
2975
2976 /* loop through and disable scalers that aren't in use */
2977 for (i = 0; i < intel_crtc->num_scalers; i++) {
2978 if (!scaler_state->scalers[i].in_use) {
2979 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, i), 0);
2980 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, i), 0);
2981 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, i), 0);
2982 DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
2983 intel_crtc->base.base.id, intel_crtc->pipe, i);
2984 }
2985 }
2986}
2987
Chandra Konduru6156a452015-04-27 13:48:39 -07002988u32 skl_plane_ctl_format(uint32_t pixel_format)
2989{
2990 u32 plane_ctl_format = 0;
2991 switch (pixel_format) {
2992 case DRM_FORMAT_RGB565:
2993 plane_ctl_format = PLANE_CTL_FORMAT_RGB_565;
2994 break;
2995 case DRM_FORMAT_XBGR8888:
2996 plane_ctl_format = PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
2997 break;
2998 case DRM_FORMAT_XRGB8888:
2999 plane_ctl_format = PLANE_CTL_FORMAT_XRGB_8888;
3000 break;
3001 /*
3002 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
3003 * to be already pre-multiplied. We need to add a knob (or a different
3004 * DRM_FORMAT) for user-space to configure that.
3005 */
3006 case DRM_FORMAT_ABGR8888:
3007 plane_ctl_format = PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
3008 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
3009 break;
3010 case DRM_FORMAT_ARGB8888:
3011 plane_ctl_format = PLANE_CTL_FORMAT_XRGB_8888 |
3012 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
3013 break;
3014 case DRM_FORMAT_XRGB2101010:
3015 plane_ctl_format = PLANE_CTL_FORMAT_XRGB_2101010;
3016 break;
3017 case DRM_FORMAT_XBGR2101010:
3018 plane_ctl_format = PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
3019 break;
3020 case DRM_FORMAT_YUYV:
3021 plane_ctl_format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
3022 break;
3023 case DRM_FORMAT_YVYU:
3024 plane_ctl_format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
3025 break;
3026 case DRM_FORMAT_UYVY:
3027 plane_ctl_format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
3028 break;
3029 case DRM_FORMAT_VYUY:
3030 plane_ctl_format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
3031 break;
3032 default:
3033 BUG();
3034 }
3035 return plane_ctl_format;
3036}
3037
3038u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
3039{
3040 u32 plane_ctl_tiling = 0;
3041 switch (fb_modifier) {
3042 case DRM_FORMAT_MOD_NONE:
3043 break;
3044 case I915_FORMAT_MOD_X_TILED:
3045 plane_ctl_tiling = PLANE_CTL_TILED_X;
3046 break;
3047 case I915_FORMAT_MOD_Y_TILED:
3048 plane_ctl_tiling = PLANE_CTL_TILED_Y;
3049 break;
3050 case I915_FORMAT_MOD_Yf_TILED:
3051 plane_ctl_tiling = PLANE_CTL_TILED_YF;
3052 break;
3053 default:
3054 MISSING_CASE(fb_modifier);
3055 }
3056 return plane_ctl_tiling;
3057}
3058
3059u32 skl_plane_ctl_rotation(unsigned int rotation)
3060{
3061 u32 plane_ctl_rotation = 0;
3062 switch (rotation) {
3063 case BIT(DRM_ROTATE_0):
3064 break;
3065 case BIT(DRM_ROTATE_90):
3066 plane_ctl_rotation = PLANE_CTL_ROTATE_90;
3067 break;
3068 case BIT(DRM_ROTATE_180):
3069 plane_ctl_rotation = PLANE_CTL_ROTATE_180;
3070 break;
3071 case BIT(DRM_ROTATE_270):
3072 plane_ctl_rotation = PLANE_CTL_ROTATE_270;
3073 break;
3074 default:
3075 MISSING_CASE(rotation);
3076 }
3077
3078 return plane_ctl_rotation;
3079}
3080
Damien Lespiau70d21f02013-07-03 21:06:04 +01003081static void skylake_update_primary_plane(struct drm_crtc *crtc,
3082 struct drm_framebuffer *fb,
3083 int x, int y)
3084{
3085 struct drm_device *dev = crtc->dev;
3086 struct drm_i915_private *dev_priv = dev->dev_private;
3087 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003088 struct drm_i915_gem_object *obj;
3089 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303090 u32 plane_ctl, stride_div, stride;
3091 u32 tile_height, plane_offset, plane_size;
3092 unsigned int rotation;
3093 int x_offset, y_offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003094 unsigned long surf_addr;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303095 struct drm_plane *plane;
Chandra Konduru6156a452015-04-27 13:48:39 -07003096 struct intel_crtc_state *crtc_state = intel_crtc->config;
3097 struct intel_plane_state *plane_state;
3098 int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
3099 int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
3100 int scaler_id = -1;
3101
3102 plane = crtc->primary;
3103 plane_state = to_intel_plane_state(plane->state);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003104
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +03003105 if (!intel_crtc->primary_enabled || !fb) {
Damien Lespiau70d21f02013-07-03 21:06:04 +01003106 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3107 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3108 POSTING_READ(PLANE_CTL(pipe, 0));
3109 return;
3110 }
3111
3112 plane_ctl = PLANE_CTL_ENABLE |
3113 PLANE_CTL_PIPE_GAMMA_ENABLE |
3114 PLANE_CTL_PIPE_CSC_ENABLE;
3115
Chandra Konduru6156a452015-04-27 13:48:39 -07003116 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3117 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003118 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303119
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303120 rotation = plane->state->rotation;
Chandra Konduru6156a452015-04-27 13:48:39 -07003121 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003122
Damien Lespiaub3218032015-02-27 11:15:18 +00003123 obj = intel_fb_obj(fb);
3124 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3125 fb->pixel_format);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303126 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
3127
Chandra Konduru6156a452015-04-27 13:48:39 -07003128 /*
3129 * FIXME: intel_plane_state->src, dst aren't set when transitional
3130 * update_plane helpers are called from legacy paths.
3131 * Once full atomic crtc is available, below check can be avoided.
3132 */
3133 if (drm_rect_width(&plane_state->src)) {
3134 scaler_id = plane_state->scaler_id;
3135 src_x = plane_state->src.x1 >> 16;
3136 src_y = plane_state->src.y1 >> 16;
3137 src_w = drm_rect_width(&plane_state->src) >> 16;
3138 src_h = drm_rect_height(&plane_state->src) >> 16;
3139 dst_x = plane_state->dst.x1;
3140 dst_y = plane_state->dst.y1;
3141 dst_w = drm_rect_width(&plane_state->dst);
3142 dst_h = drm_rect_height(&plane_state->dst);
3143
3144 WARN_ON(x != src_x || y != src_y);
3145 } else {
3146 src_w = intel_crtc->config->pipe_src_w;
3147 src_h = intel_crtc->config->pipe_src_h;
3148 }
3149
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303150 if (intel_rotation_90_or_270(rotation)) {
3151 /* stride = Surface height in tiles */
3152 tile_height = intel_tile_height(dev, fb->bits_per_pixel,
3153 fb->modifier[0]);
3154 stride = DIV_ROUND_UP(fb->height, tile_height);
Chandra Konduru6156a452015-04-27 13:48:39 -07003155 x_offset = stride * tile_height - y - src_h;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303156 y_offset = x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003157 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303158 } else {
3159 stride = fb->pitches[0] / stride_div;
3160 x_offset = x;
3161 y_offset = y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003162 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303163 }
3164 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003165
Damien Lespiau70d21f02013-07-03 21:06:04 +01003166 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303167 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3168 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3169 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003170
3171 if (scaler_id >= 0) {
3172 uint32_t ps_ctrl = 0;
3173
3174 WARN_ON(!dst_w || !dst_h);
3175 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3176 crtc_state->scaler_state.scalers[scaler_id].mode;
3177 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3178 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3179 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3180 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3181 I915_WRITE(PLANE_POS(pipe, 0), 0);
3182 } else {
3183 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3184 }
3185
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003186 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003187
3188 POSTING_READ(PLANE_SURF(pipe, 0));
3189}
3190
Jesse Barnes17638cd2011-06-24 12:19:23 -07003191/* Assume fb object is pinned & idle & fenced and just update base pointers */
3192static int
3193intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3194 int x, int y, enum mode_set_atomic state)
3195{
3196 struct drm_device *dev = crtc->dev;
3197 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003198
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01003199 if (dev_priv->display.disable_fbc)
3200 dev_priv->display.disable_fbc(dev);
Jesse Barnes81255562010-08-02 12:07:50 -07003201
Daniel Vetter29b9bde2014-04-24 23:55:01 +02003202 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3203
3204 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07003205}
3206
Ville Syrjälä75147472014-11-24 18:28:11 +02003207static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003208{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003209 struct drm_crtc *crtc;
3210
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003211 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003212 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3213 enum plane plane = intel_crtc->plane;
3214
3215 intel_prepare_page_flip(dev, plane);
3216 intel_finish_page_flip_plane(dev, plane);
3217 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003218}
3219
3220static void intel_update_primary_planes(struct drm_device *dev)
3221{
3222 struct drm_i915_private *dev_priv = dev->dev_private;
3223 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003224
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003225 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003226 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3227
Rob Clark51fd3712013-11-19 12:10:12 -05003228 drm_modeset_lock(&crtc->mutex, NULL);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003229 /*
3230 * FIXME: Once we have proper support for primary planes (and
3231 * disabling them without disabling the entire crtc) allow again
Dave Airlie66e514c2014-04-03 07:51:54 +10003232 * a NULL crtc->primary->fb.
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003233 */
Matt Roperf4510a22014-04-01 15:22:40 -07003234 if (intel_crtc->active && crtc->primary->fb)
Matt Roper262ca2b2014-03-18 17:22:55 -07003235 dev_priv->display.update_primary_plane(crtc,
Dave Airlie66e514c2014-04-03 07:51:54 +10003236 crtc->primary->fb,
Matt Roper262ca2b2014-03-18 17:22:55 -07003237 crtc->x,
3238 crtc->y);
Rob Clark51fd3712013-11-19 12:10:12 -05003239 drm_modeset_unlock(&crtc->mutex);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003240 }
3241}
3242
Ville Syrjälä75147472014-11-24 18:28:11 +02003243void intel_prepare_reset(struct drm_device *dev)
3244{
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003245 struct drm_i915_private *dev_priv = to_i915(dev);
3246 struct intel_crtc *crtc;
3247
Ville Syrjälä75147472014-11-24 18:28:11 +02003248 /* no reset support for gen2 */
3249 if (IS_GEN2(dev))
3250 return;
3251
3252 /* reset doesn't touch the display */
3253 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3254 return;
3255
3256 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003257
3258 /*
3259 * Disabling the crtcs gracefully seems nicer. Also the
3260 * g33 docs say we should at least disable all the planes.
3261 */
3262 for_each_intel_crtc(dev, crtc) {
3263 if (crtc->active)
3264 dev_priv->display.crtc_disable(&crtc->base);
3265 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003266}
3267
3268void intel_finish_reset(struct drm_device *dev)
3269{
3270 struct drm_i915_private *dev_priv = to_i915(dev);
3271
3272 /*
3273 * Flips in the rings will be nuked by the reset,
3274 * so complete all pending flips so that user space
3275 * will get its events and not get stuck.
3276 */
3277 intel_complete_page_flips(dev);
3278
3279 /* no reset support for gen2 */
3280 if (IS_GEN2(dev))
3281 return;
3282
3283 /* reset doesn't touch the display */
3284 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3285 /*
3286 * Flips in the rings have been nuked by the reset,
3287 * so update the base address of all primary
3288 * planes to the the last fb to make sure we're
3289 * showing the correct fb after a reset.
3290 */
3291 intel_update_primary_planes(dev);
3292 return;
3293 }
3294
3295 /*
3296 * The display has been reset as well,
3297 * so need a full re-initialization.
3298 */
3299 intel_runtime_pm_disable_interrupts(dev_priv);
3300 intel_runtime_pm_enable_interrupts(dev_priv);
3301
3302 intel_modeset_init_hw(dev);
3303
3304 spin_lock_irq(&dev_priv->irq_lock);
3305 if (dev_priv->display.hpd_irq_setup)
3306 dev_priv->display.hpd_irq_setup(dev);
3307 spin_unlock_irq(&dev_priv->irq_lock);
3308
3309 intel_modeset_setup_hw_state(dev, true);
3310
3311 intel_hpd_init(dev_priv);
3312
3313 drm_modeset_unlock_all(dev);
3314}
3315
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003316static int
Chris Wilson14667a42012-04-03 17:58:35 +01003317intel_finish_fb(struct drm_framebuffer *old_fb)
3318{
Matt Roper2ff8fde2014-07-08 07:50:07 -07003319 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
Chris Wilson14667a42012-04-03 17:58:35 +01003320 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
3321 bool was_interruptible = dev_priv->mm.interruptible;
3322 int ret;
3323
Chris Wilson14667a42012-04-03 17:58:35 +01003324 /* Big Hammer, we also need to ensure that any pending
3325 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3326 * current scanout is retired before unpinning the old
3327 * framebuffer.
3328 *
3329 * This should only fail upon a hung GPU, in which case we
3330 * can safely continue.
3331 */
3332 dev_priv->mm.interruptible = false;
3333 ret = i915_gem_object_finish_gpu(obj);
3334 dev_priv->mm.interruptible = was_interruptible;
3335
3336 return ret;
3337}
3338
Chris Wilson7d5e3792014-03-04 13:15:08 +00003339static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3340{
3341 struct drm_device *dev = crtc->dev;
3342 struct drm_i915_private *dev_priv = dev->dev_private;
3343 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003344 bool pending;
3345
3346 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3347 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3348 return false;
3349
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003350 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003351 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003352 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003353
3354 return pending;
3355}
3356
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003357static void intel_update_pipe_size(struct intel_crtc *crtc)
3358{
3359 struct drm_device *dev = crtc->base.dev;
3360 struct drm_i915_private *dev_priv = dev->dev_private;
3361 const struct drm_display_mode *adjusted_mode;
3362
3363 if (!i915.fastboot)
3364 return;
3365
3366 /*
3367 * Update pipe size and adjust fitter if needed: the reason for this is
3368 * that in compute_mode_changes we check the native mode (not the pfit
3369 * mode) to see if we can flip rather than do a full mode set. In the
3370 * fastboot case, we'll flip, but if we don't update the pipesrc and
3371 * pfit state, we'll end up with a big fb scanned out into the wrong
3372 * sized surface.
3373 *
3374 * To fix this properly, we need to hoist the checks up into
3375 * compute_mode_changes (or above), check the actual pfit state and
3376 * whether the platform allows pfit disable with pipe active, and only
3377 * then update the pipesrc and pfit state, even on the flip path.
3378 */
3379
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003380 adjusted_mode = &crtc->config->base.adjusted_mode;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003381
3382 I915_WRITE(PIPESRC(crtc->pipe),
3383 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3384 (adjusted_mode->crtc_vdisplay - 1));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003385 if (!crtc->config->pch_pfit.enabled &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03003386 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3387 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003388 I915_WRITE(PF_CTL(crtc->pipe), 0);
3389 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3390 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3391 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003392 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3393 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003394}
3395
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003396static void intel_fdi_normal_train(struct drm_crtc *crtc)
3397{
3398 struct drm_device *dev = crtc->dev;
3399 struct drm_i915_private *dev_priv = dev->dev_private;
3400 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3401 int pipe = intel_crtc->pipe;
3402 u32 reg, temp;
3403
3404 /* enable normal train */
3405 reg = FDI_TX_CTL(pipe);
3406 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003407 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003408 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3409 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003410 } else {
3411 temp &= ~FDI_LINK_TRAIN_NONE;
3412 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003413 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003414 I915_WRITE(reg, temp);
3415
3416 reg = FDI_RX_CTL(pipe);
3417 temp = I915_READ(reg);
3418 if (HAS_PCH_CPT(dev)) {
3419 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3420 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3421 } else {
3422 temp &= ~FDI_LINK_TRAIN_NONE;
3423 temp |= FDI_LINK_TRAIN_NONE;
3424 }
3425 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3426
3427 /* wait one idle pattern time */
3428 POSTING_READ(reg);
3429 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003430
3431 /* IVB wants error correction enabled */
3432 if (IS_IVYBRIDGE(dev))
3433 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3434 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003435}
3436
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003437/* The FDI link training functions for ILK/Ibexpeak. */
3438static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3439{
3440 struct drm_device *dev = crtc->dev;
3441 struct drm_i915_private *dev_priv = dev->dev_private;
3442 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3443 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003444 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003445
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003446 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003447 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003448
Adam Jacksone1a44742010-06-25 15:32:14 -04003449 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3450 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003451 reg = FDI_RX_IMR(pipe);
3452 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003453 temp &= ~FDI_RX_SYMBOL_LOCK;
3454 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003455 I915_WRITE(reg, temp);
3456 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003457 udelay(150);
3458
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003459 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003460 reg = FDI_TX_CTL(pipe);
3461 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003462 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003463 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003464 temp &= ~FDI_LINK_TRAIN_NONE;
3465 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003466 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003467
Chris Wilson5eddb702010-09-11 13:48:45 +01003468 reg = FDI_RX_CTL(pipe);
3469 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003470 temp &= ~FDI_LINK_TRAIN_NONE;
3471 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003472 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3473
3474 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003475 udelay(150);
3476
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003477 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003478 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3479 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3480 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003481
Chris Wilson5eddb702010-09-11 13:48:45 +01003482 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003483 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003484 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003485 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3486
3487 if ((temp & FDI_RX_BIT_LOCK)) {
3488 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003489 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003490 break;
3491 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003492 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003493 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003494 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003495
3496 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003497 reg = FDI_TX_CTL(pipe);
3498 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003499 temp &= ~FDI_LINK_TRAIN_NONE;
3500 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003501 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003502
Chris Wilson5eddb702010-09-11 13:48:45 +01003503 reg = FDI_RX_CTL(pipe);
3504 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003505 temp &= ~FDI_LINK_TRAIN_NONE;
3506 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003507 I915_WRITE(reg, temp);
3508
3509 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003510 udelay(150);
3511
Chris Wilson5eddb702010-09-11 13:48:45 +01003512 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003513 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003514 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003515 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3516
3517 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003518 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003519 DRM_DEBUG_KMS("FDI train 2 done.\n");
3520 break;
3521 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003522 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003523 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003524 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003525
3526 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003527
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003528}
3529
Akshay Joshi0206e352011-08-16 15:34:10 -04003530static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003531 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3532 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3533 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3534 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3535};
3536
3537/* The FDI link training functions for SNB/Cougarpoint. */
3538static void gen6_fdi_link_train(struct drm_crtc *crtc)
3539{
3540 struct drm_device *dev = crtc->dev;
3541 struct drm_i915_private *dev_priv = dev->dev_private;
3542 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3543 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05003544 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003545
Adam Jacksone1a44742010-06-25 15:32:14 -04003546 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3547 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003548 reg = FDI_RX_IMR(pipe);
3549 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003550 temp &= ~FDI_RX_SYMBOL_LOCK;
3551 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003552 I915_WRITE(reg, temp);
3553
3554 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003555 udelay(150);
3556
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003557 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003558 reg = FDI_TX_CTL(pipe);
3559 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003560 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003561 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003562 temp &= ~FDI_LINK_TRAIN_NONE;
3563 temp |= FDI_LINK_TRAIN_PATTERN_1;
3564 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3565 /* SNB-B */
3566 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003567 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003568
Daniel Vetterd74cf322012-10-26 10:58:13 +02003569 I915_WRITE(FDI_RX_MISC(pipe),
3570 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3571
Chris Wilson5eddb702010-09-11 13:48:45 +01003572 reg = FDI_RX_CTL(pipe);
3573 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003574 if (HAS_PCH_CPT(dev)) {
3575 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3576 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3577 } else {
3578 temp &= ~FDI_LINK_TRAIN_NONE;
3579 temp |= FDI_LINK_TRAIN_PATTERN_1;
3580 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003581 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3582
3583 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003584 udelay(150);
3585
Akshay Joshi0206e352011-08-16 15:34:10 -04003586 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003587 reg = FDI_TX_CTL(pipe);
3588 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003589 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3590 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003591 I915_WRITE(reg, temp);
3592
3593 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003594 udelay(500);
3595
Sean Paulfa37d392012-03-02 12:53:39 -05003596 for (retry = 0; retry < 5; retry++) {
3597 reg = FDI_RX_IIR(pipe);
3598 temp = I915_READ(reg);
3599 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3600 if (temp & FDI_RX_BIT_LOCK) {
3601 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3602 DRM_DEBUG_KMS("FDI train 1 done.\n");
3603 break;
3604 }
3605 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003606 }
Sean Paulfa37d392012-03-02 12:53:39 -05003607 if (retry < 5)
3608 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003609 }
3610 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003611 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003612
3613 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003614 reg = FDI_TX_CTL(pipe);
3615 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003616 temp &= ~FDI_LINK_TRAIN_NONE;
3617 temp |= FDI_LINK_TRAIN_PATTERN_2;
3618 if (IS_GEN6(dev)) {
3619 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3620 /* SNB-B */
3621 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3622 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003623 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003624
Chris Wilson5eddb702010-09-11 13:48:45 +01003625 reg = FDI_RX_CTL(pipe);
3626 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003627 if (HAS_PCH_CPT(dev)) {
3628 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3629 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3630 } else {
3631 temp &= ~FDI_LINK_TRAIN_NONE;
3632 temp |= FDI_LINK_TRAIN_PATTERN_2;
3633 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003634 I915_WRITE(reg, temp);
3635
3636 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003637 udelay(150);
3638
Akshay Joshi0206e352011-08-16 15:34:10 -04003639 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003640 reg = FDI_TX_CTL(pipe);
3641 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003642 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3643 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003644 I915_WRITE(reg, temp);
3645
3646 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003647 udelay(500);
3648
Sean Paulfa37d392012-03-02 12:53:39 -05003649 for (retry = 0; retry < 5; retry++) {
3650 reg = FDI_RX_IIR(pipe);
3651 temp = I915_READ(reg);
3652 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3653 if (temp & FDI_RX_SYMBOL_LOCK) {
3654 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3655 DRM_DEBUG_KMS("FDI train 2 done.\n");
3656 break;
3657 }
3658 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003659 }
Sean Paulfa37d392012-03-02 12:53:39 -05003660 if (retry < 5)
3661 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003662 }
3663 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003664 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003665
3666 DRM_DEBUG_KMS("FDI train done.\n");
3667}
3668
Jesse Barnes357555c2011-04-28 15:09:55 -07003669/* Manual link training for Ivy Bridge A0 parts */
3670static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3671{
3672 struct drm_device *dev = crtc->dev;
3673 struct drm_i915_private *dev_priv = dev->dev_private;
3674 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3675 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003676 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003677
3678 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3679 for train result */
3680 reg = FDI_RX_IMR(pipe);
3681 temp = I915_READ(reg);
3682 temp &= ~FDI_RX_SYMBOL_LOCK;
3683 temp &= ~FDI_RX_BIT_LOCK;
3684 I915_WRITE(reg, temp);
3685
3686 POSTING_READ(reg);
3687 udelay(150);
3688
Daniel Vetter01a415f2012-10-27 15:58:40 +02003689 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3690 I915_READ(FDI_RX_IIR(pipe)));
3691
Jesse Barnes139ccd32013-08-19 11:04:55 -07003692 /* Try each vswing and preemphasis setting twice before moving on */
3693 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3694 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003695 reg = FDI_TX_CTL(pipe);
3696 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003697 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3698 temp &= ~FDI_TX_ENABLE;
3699 I915_WRITE(reg, temp);
3700
3701 reg = FDI_RX_CTL(pipe);
3702 temp = I915_READ(reg);
3703 temp &= ~FDI_LINK_TRAIN_AUTO;
3704 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3705 temp &= ~FDI_RX_ENABLE;
3706 I915_WRITE(reg, temp);
3707
3708 /* enable CPU FDI TX and PCH FDI RX */
3709 reg = FDI_TX_CTL(pipe);
3710 temp = I915_READ(reg);
3711 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003712 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003713 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003714 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003715 temp |= snb_b_fdi_train_param[j/2];
3716 temp |= FDI_COMPOSITE_SYNC;
3717 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3718
3719 I915_WRITE(FDI_RX_MISC(pipe),
3720 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3721
3722 reg = FDI_RX_CTL(pipe);
3723 temp = I915_READ(reg);
3724 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3725 temp |= FDI_COMPOSITE_SYNC;
3726 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3727
3728 POSTING_READ(reg);
3729 udelay(1); /* should be 0.5us */
3730
3731 for (i = 0; i < 4; i++) {
3732 reg = FDI_RX_IIR(pipe);
3733 temp = I915_READ(reg);
3734 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3735
3736 if (temp & FDI_RX_BIT_LOCK ||
3737 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3738 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3739 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3740 i);
3741 break;
3742 }
3743 udelay(1); /* should be 0.5us */
3744 }
3745 if (i == 4) {
3746 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3747 continue;
3748 }
3749
3750 /* Train 2 */
3751 reg = FDI_TX_CTL(pipe);
3752 temp = I915_READ(reg);
3753 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3754 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3755 I915_WRITE(reg, temp);
3756
3757 reg = FDI_RX_CTL(pipe);
3758 temp = I915_READ(reg);
3759 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3760 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003761 I915_WRITE(reg, temp);
3762
3763 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003764 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003765
Jesse Barnes139ccd32013-08-19 11:04:55 -07003766 for (i = 0; i < 4; i++) {
3767 reg = FDI_RX_IIR(pipe);
3768 temp = I915_READ(reg);
3769 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003770
Jesse Barnes139ccd32013-08-19 11:04:55 -07003771 if (temp & FDI_RX_SYMBOL_LOCK ||
3772 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3773 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3774 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3775 i);
3776 goto train_done;
3777 }
3778 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003779 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003780 if (i == 4)
3781 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003782 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003783
Jesse Barnes139ccd32013-08-19 11:04:55 -07003784train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003785 DRM_DEBUG_KMS("FDI train done.\n");
3786}
3787
Daniel Vetter88cefb62012-08-12 19:27:14 +02003788static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003789{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003790 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003791 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003792 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003793 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003794
Jesse Barnesc64e3112010-09-10 11:27:03 -07003795
Jesse Barnes0e23b992010-09-10 11:10:00 -07003796 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003797 reg = FDI_RX_CTL(pipe);
3798 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003799 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003800 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003801 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003802 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3803
3804 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003805 udelay(200);
3806
3807 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003808 temp = I915_READ(reg);
3809 I915_WRITE(reg, temp | FDI_PCDCLK);
3810
3811 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003812 udelay(200);
3813
Paulo Zanoni20749732012-11-23 15:30:38 -02003814 /* Enable CPU FDI TX PLL, always on for Ironlake */
3815 reg = FDI_TX_CTL(pipe);
3816 temp = I915_READ(reg);
3817 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3818 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003819
Paulo Zanoni20749732012-11-23 15:30:38 -02003820 POSTING_READ(reg);
3821 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003822 }
3823}
3824
Daniel Vetter88cefb62012-08-12 19:27:14 +02003825static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3826{
3827 struct drm_device *dev = intel_crtc->base.dev;
3828 struct drm_i915_private *dev_priv = dev->dev_private;
3829 int pipe = intel_crtc->pipe;
3830 u32 reg, temp;
3831
3832 /* Switch from PCDclk to Rawclk */
3833 reg = FDI_RX_CTL(pipe);
3834 temp = I915_READ(reg);
3835 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3836
3837 /* Disable CPU FDI TX PLL */
3838 reg = FDI_TX_CTL(pipe);
3839 temp = I915_READ(reg);
3840 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3841
3842 POSTING_READ(reg);
3843 udelay(100);
3844
3845 reg = FDI_RX_CTL(pipe);
3846 temp = I915_READ(reg);
3847 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3848
3849 /* Wait for the clocks to turn off. */
3850 POSTING_READ(reg);
3851 udelay(100);
3852}
3853
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003854static void ironlake_fdi_disable(struct drm_crtc *crtc)
3855{
3856 struct drm_device *dev = crtc->dev;
3857 struct drm_i915_private *dev_priv = dev->dev_private;
3858 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3859 int pipe = intel_crtc->pipe;
3860 u32 reg, temp;
3861
3862 /* disable CPU FDI tx and PCH FDI rx */
3863 reg = FDI_TX_CTL(pipe);
3864 temp = I915_READ(reg);
3865 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3866 POSTING_READ(reg);
3867
3868 reg = FDI_RX_CTL(pipe);
3869 temp = I915_READ(reg);
3870 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003871 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003872 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3873
3874 POSTING_READ(reg);
3875 udelay(100);
3876
3877 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003878 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003879 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003880
3881 /* still set train pattern 1 */
3882 reg = FDI_TX_CTL(pipe);
3883 temp = I915_READ(reg);
3884 temp &= ~FDI_LINK_TRAIN_NONE;
3885 temp |= FDI_LINK_TRAIN_PATTERN_1;
3886 I915_WRITE(reg, temp);
3887
3888 reg = FDI_RX_CTL(pipe);
3889 temp = I915_READ(reg);
3890 if (HAS_PCH_CPT(dev)) {
3891 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3892 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3893 } else {
3894 temp &= ~FDI_LINK_TRAIN_NONE;
3895 temp |= FDI_LINK_TRAIN_PATTERN_1;
3896 }
3897 /* BPC in FDI rx is consistent with that in PIPECONF */
3898 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003899 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003900 I915_WRITE(reg, temp);
3901
3902 POSTING_READ(reg);
3903 udelay(100);
3904}
3905
Chris Wilson5dce5b932014-01-20 10:17:36 +00003906bool intel_has_pending_fb_unpin(struct drm_device *dev)
3907{
3908 struct intel_crtc *crtc;
3909
3910 /* Note that we don't need to be called with mode_config.lock here
3911 * as our list of CRTC objects is static for the lifetime of the
3912 * device and so cannot disappear as we iterate. Similarly, we can
3913 * happily treat the predicates as racy, atomic checks as userspace
3914 * cannot claim and pin a new fb without at least acquring the
3915 * struct_mutex and so serialising with us.
3916 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003917 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003918 if (atomic_read(&crtc->unpin_work_count) == 0)
3919 continue;
3920
3921 if (crtc->unpin_work)
3922 intel_wait_for_vblank(dev, crtc->pipe);
3923
3924 return true;
3925 }
3926
3927 return false;
3928}
3929
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003930static void page_flip_completed(struct intel_crtc *intel_crtc)
3931{
3932 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3933 struct intel_unpin_work *work = intel_crtc->unpin_work;
3934
3935 /* ensure that the unpin work is consistent wrt ->pending. */
3936 smp_rmb();
3937 intel_crtc->unpin_work = NULL;
3938
3939 if (work->event)
3940 drm_send_vblank_event(intel_crtc->base.dev,
3941 intel_crtc->pipe,
3942 work->event);
3943
3944 drm_crtc_vblank_put(&intel_crtc->base);
3945
3946 wake_up_all(&dev_priv->pending_flip_queue);
3947 queue_work(dev_priv->wq, &work->work);
3948
3949 trace_i915_flip_complete(intel_crtc->plane,
3950 work->pending_flip_obj);
3951}
3952
Ville Syrjälä46a55d32014-05-21 14:04:46 +03003953void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003954{
Chris Wilson0f911282012-04-17 10:05:38 +01003955 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003956 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003957
Daniel Vetter2c10d572012-12-20 21:24:07 +01003958 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Chris Wilson9c787942014-09-05 07:13:25 +01003959 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3960 !intel_crtc_has_pending_flip(crtc),
3961 60*HZ) == 0)) {
3962 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003963
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003964 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003965 if (intel_crtc->unpin_work) {
3966 WARN_ONCE(1, "Removing stuck page flip\n");
3967 page_flip_completed(intel_crtc);
3968 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003969 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003970 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003971
Chris Wilson975d5682014-08-20 13:13:34 +01003972 if (crtc->primary->fb) {
3973 mutex_lock(&dev->struct_mutex);
3974 intel_finish_fb(crtc->primary->fb);
3975 mutex_unlock(&dev->struct_mutex);
3976 }
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003977}
3978
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003979/* Program iCLKIP clock to the desired frequency */
3980static void lpt_program_iclkip(struct drm_crtc *crtc)
3981{
3982 struct drm_device *dev = crtc->dev;
3983 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003984 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003985 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3986 u32 temp;
3987
Daniel Vetter09153002012-12-12 14:06:44 +01003988 mutex_lock(&dev_priv->dpio_lock);
3989
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003990 /* It is necessary to ungate the pixclk gate prior to programming
3991 * the divisors, and gate it back when it is done.
3992 */
3993 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3994
3995 /* Disable SSCCTL */
3996 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003997 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3998 SBI_SSCCTL_DISABLE,
3999 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004000
4001 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03004002 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004003 auxdiv = 1;
4004 divsel = 0x41;
4005 phaseinc = 0x20;
4006 } else {
4007 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01004008 * but the adjusted_mode->crtc_clock in in KHz. To get the
4009 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004010 * convert the virtual clock precision to KHz here for higher
4011 * precision.
4012 */
4013 u32 iclk_virtual_root_freq = 172800 * 1000;
4014 u32 iclk_pi_range = 64;
4015 u32 desired_divisor, msb_divisor_value, pi_value;
4016
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03004017 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004018 msb_divisor_value = desired_divisor / iclk_pi_range;
4019 pi_value = desired_divisor % iclk_pi_range;
4020
4021 auxdiv = 0;
4022 divsel = msb_divisor_value - 2;
4023 phaseinc = pi_value;
4024 }
4025
4026 /* This should not happen with any sane values */
4027 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
4028 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
4029 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
4030 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
4031
4032 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 +03004033 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004034 auxdiv,
4035 divsel,
4036 phasedir,
4037 phaseinc);
4038
4039 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004040 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004041 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4042 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4043 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4044 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4045 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4046 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004047 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004048
4049 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004050 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004051 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4052 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004053 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004054
4055 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004056 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004057 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004058 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004059
4060 /* Wait for initialization time */
4061 udelay(24);
4062
4063 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01004064
4065 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004066}
4067
Daniel Vetter275f01b22013-05-03 11:49:47 +02004068static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4069 enum pipe pch_transcoder)
4070{
4071 struct drm_device *dev = crtc->base.dev;
4072 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004073 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02004074
4075 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4076 I915_READ(HTOTAL(cpu_transcoder)));
4077 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4078 I915_READ(HBLANK(cpu_transcoder)));
4079 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4080 I915_READ(HSYNC(cpu_transcoder)));
4081
4082 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4083 I915_READ(VTOTAL(cpu_transcoder)));
4084 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4085 I915_READ(VBLANK(cpu_transcoder)));
4086 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4087 I915_READ(VSYNC(cpu_transcoder)));
4088 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4089 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4090}
4091
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004092static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004093{
4094 struct drm_i915_private *dev_priv = dev->dev_private;
4095 uint32_t temp;
4096
4097 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004098 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004099 return;
4100
4101 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4102 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4103
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004104 temp &= ~FDI_BC_BIFURCATION_SELECT;
4105 if (enable)
4106 temp |= FDI_BC_BIFURCATION_SELECT;
4107
4108 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004109 I915_WRITE(SOUTH_CHICKEN1, temp);
4110 POSTING_READ(SOUTH_CHICKEN1);
4111}
4112
4113static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4114{
4115 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004116
4117 switch (intel_crtc->pipe) {
4118 case PIPE_A:
4119 break;
4120 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004121 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004122 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004123 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004124 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004125
4126 break;
4127 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004128 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004129
4130 break;
4131 default:
4132 BUG();
4133 }
4134}
4135
Jesse Barnesf67a5592011-01-05 10:31:48 -08004136/*
4137 * Enable PCH resources required for PCH ports:
4138 * - PCH PLLs
4139 * - FDI training & RX/TX
4140 * - update transcoder timings
4141 * - DP transcoding bits
4142 * - transcoder
4143 */
4144static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004145{
4146 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004147 struct drm_i915_private *dev_priv = dev->dev_private;
4148 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4149 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004150 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004151
Daniel Vetterab9412b2013-05-03 11:49:46 +02004152 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004153
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004154 if (IS_IVYBRIDGE(dev))
4155 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4156
Daniel Vettercd986ab2012-10-26 10:58:12 +02004157 /* Write the TU size bits before fdi link training, so that error
4158 * detection works. */
4159 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4160 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4161
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004162 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004163 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004164
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004165 /* We need to program the right clock selection before writing the pixel
4166 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004167 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004168 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004169
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004170 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004171 temp |= TRANS_DPLL_ENABLE(pipe);
4172 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004173 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004174 temp |= sel;
4175 else
4176 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004177 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004178 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004179
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004180 /* XXX: pch pll's can be enabled any time before we enable the PCH
4181 * transcoder, and we actually should do this to not upset any PCH
4182 * transcoder that already use the clock when we share it.
4183 *
4184 * Note that enable_shared_dpll tries to do the right thing, but
4185 * get_shared_dpll unconditionally resets the pll - we need that to have
4186 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004187 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004188
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004189 /* set transcoder timing, panel must allow it */
4190 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004191 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004192
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004193 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004194
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004195 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004196 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004197 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01004198 reg = TRANS_DP_CTL(pipe);
4199 temp = I915_READ(reg);
4200 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004201 TRANS_DP_SYNC_MASK |
4202 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01004203 temp |= (TRANS_DP_OUTPUT_ENABLE |
4204 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004205 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004206
4207 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004208 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004209 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004210 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004211
4212 switch (intel_trans_dp_port_sel(crtc)) {
4213 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004214 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004215 break;
4216 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004217 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004218 break;
4219 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004220 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004221 break;
4222 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004223 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004224 }
4225
Chris Wilson5eddb702010-09-11 13:48:45 +01004226 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004227 }
4228
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004229 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004230}
4231
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004232static void lpt_pch_enable(struct drm_crtc *crtc)
4233{
4234 struct drm_device *dev = crtc->dev;
4235 struct drm_i915_private *dev_priv = dev->dev_private;
4236 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004237 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004238
Daniel Vetterab9412b2013-05-03 11:49:46 +02004239 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004240
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004241 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004242
Paulo Zanoni0540e482012-10-31 18:12:40 -02004243 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004244 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004245
Paulo Zanoni937bb612012-10-31 18:12:47 -02004246 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004247}
4248
Daniel Vetter716c2e52014-06-25 22:02:02 +03004249void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004250{
Daniel Vettere2b78262013-06-07 23:10:03 +02004251 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004252
4253 if (pll == NULL)
4254 return;
4255
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02004256 if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004257 WARN(1, "bad %s crtc mask\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004258 return;
4259 }
4260
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02004261 pll->config.crtc_mask &= ~(1 << crtc->pipe);
4262 if (pll->config.crtc_mask == 0) {
Daniel Vetterf4a091c2013-06-10 17:28:22 +02004263 WARN_ON(pll->on);
4264 WARN_ON(pll->active);
4265 }
4266
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004267 crtc->config->shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004268}
4269
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004270struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4271 struct intel_crtc_state *crtc_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004272{
Daniel Vettere2b78262013-06-07 23:10:03 +02004273 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004274 struct intel_shared_dpll *pll;
Daniel Vettere2b78262013-06-07 23:10:03 +02004275 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004276
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004277 if (HAS_PCH_IBX(dev_priv->dev)) {
4278 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02004279 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004280 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004281
Daniel Vetter46edb022013-06-05 13:34:12 +02004282 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4283 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004284
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004285 WARN_ON(pll->new_config->crtc_mask);
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004286
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004287 goto found;
4288 }
4289
Satheeshakrishna Mbcddf612014-08-22 09:49:10 +05304290 if (IS_BROXTON(dev_priv->dev)) {
4291 /* PLL is attached to port in bxt */
4292 struct intel_encoder *encoder;
4293 struct intel_digital_port *intel_dig_port;
4294
4295 encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
4296 if (WARN_ON(!encoder))
4297 return NULL;
4298
4299 intel_dig_port = enc_to_dig_port(&encoder->base);
4300 /* 1:1 mapping between ports and PLLs */
4301 i = (enum intel_dpll_id)intel_dig_port->port;
4302 pll = &dev_priv->shared_dplls[i];
4303 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4304 crtc->base.base.id, pll->name);
4305 WARN_ON(pll->new_config->crtc_mask);
4306
4307 goto found;
4308 }
4309
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];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004312
4313 /* Only want to check enabled timings first */
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004314 if (pll->new_config->crtc_mask == 0)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004315 continue;
4316
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004317 if (memcmp(&crtc_state->dpll_hw_state,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004318 &pll->new_config->hw_state,
4319 sizeof(pll->new_config->hw_state)) == 0) {
4320 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004321 crtc->base.base.id, pll->name,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004322 pll->new_config->crtc_mask,
4323 pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004324 goto found;
4325 }
4326 }
4327
4328 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004329 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4330 pll = &dev_priv->shared_dplls[i];
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004331 if (pll->new_config->crtc_mask == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02004332 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4333 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004334 goto found;
4335 }
4336 }
4337
4338 return NULL;
4339
4340found:
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004341 if (pll->new_config->crtc_mask == 0)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004342 pll->new_config->hw_state = crtc_state->dpll_hw_state;
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004343
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004344 crtc_state->shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02004345 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4346 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02004347
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004348 pll->new_config->crtc_mask |= 1 << crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004349
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004350 return pll;
4351}
4352
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004353/**
4354 * intel_shared_dpll_start_config - start a new PLL staged config
4355 * @dev_priv: DRM device
4356 * @clear_pipes: mask of pipes that will have their PLLs freed
4357 *
4358 * Starts a new PLL staged config, copying the current config but
4359 * releasing the references of pipes specified in clear_pipes.
4360 */
4361static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
4362 unsigned clear_pipes)
4363{
4364 struct intel_shared_dpll *pll;
4365 enum intel_dpll_id i;
4366
4367 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4368 pll = &dev_priv->shared_dplls[i];
4369
4370 pll->new_config = kmemdup(&pll->config, sizeof pll->config,
4371 GFP_KERNEL);
4372 if (!pll->new_config)
4373 goto cleanup;
4374
4375 pll->new_config->crtc_mask &= ~clear_pipes;
4376 }
4377
4378 return 0;
4379
4380cleanup:
4381 while (--i >= 0) {
4382 pll = &dev_priv->shared_dplls[i];
Ander Conselvan de Oliveiraf354d732014-11-07 14:07:41 +02004383 kfree(pll->new_config);
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004384 pll->new_config = NULL;
4385 }
4386
4387 return -ENOMEM;
4388}
4389
4390static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
4391{
4392 struct intel_shared_dpll *pll;
4393 enum intel_dpll_id i;
4394
4395 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4396 pll = &dev_priv->shared_dplls[i];
4397
4398 WARN_ON(pll->new_config == &pll->config);
4399
4400 pll->config = *pll->new_config;
4401 kfree(pll->new_config);
4402 pll->new_config = NULL;
4403 }
4404}
4405
4406static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
4407{
4408 struct intel_shared_dpll *pll;
4409 enum intel_dpll_id i;
4410
4411 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4412 pll = &dev_priv->shared_dplls[i];
4413
4414 WARN_ON(pll->new_config == &pll->config);
4415
4416 kfree(pll->new_config);
4417 pll->new_config = NULL;
4418 }
4419}
4420
Daniel Vettera1520312013-05-03 11:49:50 +02004421static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004422{
4423 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01004424 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004425 u32 temp;
4426
4427 temp = I915_READ(dslreg);
4428 udelay(500);
4429 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004430 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004431 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004432 }
4433}
4434
Chandra Kondurua1b22782015-04-07 15:28:45 -07004435/**
4436 * skl_update_scaler_users - Stages update to crtc's scaler state
4437 * @intel_crtc: crtc
4438 * @crtc_state: crtc_state
4439 * @plane: plane (NULL indicates crtc is requesting update)
4440 * @plane_state: plane's state
4441 * @force_detach: request unconditional detachment of scaler
4442 *
4443 * This function updates scaler state for requested plane or crtc.
4444 * To request scaler usage update for a plane, caller shall pass plane pointer.
4445 * To request scaler usage update for crtc, caller shall pass plane pointer
4446 * as NULL.
4447 *
4448 * Return
4449 * 0 - scaler_usage updated successfully
4450 * error - requested scaling cannot be supported or other error condition
4451 */
4452int
4453skl_update_scaler_users(
4454 struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state,
4455 struct intel_plane *intel_plane, struct intel_plane_state *plane_state,
4456 int force_detach)
4457{
4458 int need_scaling;
4459 int idx;
4460 int src_w, src_h, dst_w, dst_h;
4461 int *scaler_id;
4462 struct drm_framebuffer *fb;
4463 struct intel_crtc_scaler_state *scaler_state;
Chandra Konduru6156a452015-04-27 13:48:39 -07004464 unsigned int rotation;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004465
4466 if (!intel_crtc || !crtc_state)
4467 return 0;
4468
4469 scaler_state = &crtc_state->scaler_state;
4470
4471 idx = intel_plane ? drm_plane_index(&intel_plane->base) : SKL_CRTC_INDEX;
4472 fb = intel_plane ? plane_state->base.fb : NULL;
4473
4474 if (intel_plane) {
4475 src_w = drm_rect_width(&plane_state->src) >> 16;
4476 src_h = drm_rect_height(&plane_state->src) >> 16;
4477 dst_w = drm_rect_width(&plane_state->dst);
4478 dst_h = drm_rect_height(&plane_state->dst);
4479 scaler_id = &plane_state->scaler_id;
Chandra Konduru6156a452015-04-27 13:48:39 -07004480 rotation = plane_state->base.rotation;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004481 } else {
4482 struct drm_display_mode *adjusted_mode =
4483 &crtc_state->base.adjusted_mode;
4484 src_w = crtc_state->pipe_src_w;
4485 src_h = crtc_state->pipe_src_h;
4486 dst_w = adjusted_mode->hdisplay;
4487 dst_h = adjusted_mode->vdisplay;
4488 scaler_id = &scaler_state->scaler_id;
Chandra Konduru6156a452015-04-27 13:48:39 -07004489 rotation = DRM_ROTATE_0;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004490 }
Chandra Konduru6156a452015-04-27 13:48:39 -07004491
4492 need_scaling = intel_rotation_90_or_270(rotation) ?
4493 (src_h != dst_w || src_w != dst_h):
4494 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004495
4496 /*
4497 * if plane is being disabled or scaler is no more required or force detach
4498 * - free scaler binded to this plane/crtc
4499 * - in order to do this, update crtc->scaler_usage
4500 *
4501 * Here scaler state in crtc_state is set free so that
4502 * scaler can be assigned to other user. Actual register
4503 * update to free the scaler is done in plane/panel-fit programming.
4504 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4505 */
4506 if (force_detach || !need_scaling || (intel_plane &&
4507 (!fb || !plane_state->visible))) {
4508 if (*scaler_id >= 0) {
4509 scaler_state->scaler_users &= ~(1 << idx);
4510 scaler_state->scalers[*scaler_id].in_use = 0;
4511
4512 DRM_DEBUG_KMS("Staged freeing scaler id %d.%d from %s:%d "
4513 "crtc_state = %p scaler_users = 0x%x\n",
4514 intel_crtc->pipe, *scaler_id, intel_plane ? "PLANE" : "CRTC",
4515 intel_plane ? intel_plane->base.base.id :
4516 intel_crtc->base.base.id, crtc_state,
4517 scaler_state->scaler_users);
4518 *scaler_id = -1;
4519 }
4520 return 0;
4521 }
4522
4523 /* range checks */
4524 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4525 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4526
4527 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4528 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
4529 DRM_DEBUG_KMS("%s:%d scaler_user index %u.%u: src %ux%u dst %ux%u "
4530 "size is out of scaler range\n",
4531 intel_plane ? "PLANE" : "CRTC",
4532 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4533 intel_crtc->pipe, idx, src_w, src_h, dst_w, dst_h);
4534 return -EINVAL;
4535 }
4536
4537 /* check colorkey */
4538 if (intel_plane && intel_plane->ckey.flags != I915_SET_COLORKEY_NONE) {
4539 DRM_DEBUG_KMS("PLANE:%d scaling with color key not allowed",
4540 intel_plane->base.base.id);
4541 return -EINVAL;
4542 }
4543
4544 /* Check src format */
4545 if (intel_plane) {
4546 switch (fb->pixel_format) {
4547 case DRM_FORMAT_RGB565:
4548 case DRM_FORMAT_XBGR8888:
4549 case DRM_FORMAT_XRGB8888:
4550 case DRM_FORMAT_ABGR8888:
4551 case DRM_FORMAT_ARGB8888:
4552 case DRM_FORMAT_XRGB2101010:
4553 case DRM_FORMAT_ARGB2101010:
4554 case DRM_FORMAT_XBGR2101010:
4555 case DRM_FORMAT_ABGR2101010:
4556 case DRM_FORMAT_YUYV:
4557 case DRM_FORMAT_YVYU:
4558 case DRM_FORMAT_UYVY:
4559 case DRM_FORMAT_VYUY:
4560 break;
4561 default:
4562 DRM_DEBUG_KMS("PLANE:%d FB:%d unsupported scaling format 0x%x\n",
4563 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4564 return -EINVAL;
4565 }
4566 }
4567
4568 /* mark this plane as a scaler user in crtc_state */
4569 scaler_state->scaler_users |= (1 << idx);
4570 DRM_DEBUG_KMS("%s:%d staged scaling request for %ux%u->%ux%u "
4571 "crtc_state = %p scaler_users = 0x%x\n",
4572 intel_plane ? "PLANE" : "CRTC",
4573 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4574 src_w, src_h, dst_w, dst_h, crtc_state, scaler_state->scaler_users);
4575 return 0;
4576}
4577
4578static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004579{
4580 struct drm_device *dev = crtc->base.dev;
4581 struct drm_i915_private *dev_priv = dev->dev_private;
4582 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004583 struct intel_crtc_scaler_state *scaler_state =
4584 &crtc->config->scaler_state;
4585
4586 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4587
4588 /* To update pfit, first update scaler state */
4589 skl_update_scaler_users(crtc, crtc->config, NULL, NULL, !enable);
4590 intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
4591 skl_detach_scalers(crtc);
4592 if (!enable)
4593 return;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004594
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004595 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004596 int id;
4597
4598 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4599 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4600 return;
4601 }
4602
4603 id = scaler_state->scaler_id;
4604 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4605 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4606 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4607 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4608
4609 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004610 }
4611}
4612
Jesse Barnesb074cec2013-04-25 12:55:02 -07004613static void ironlake_pfit_enable(struct intel_crtc *crtc)
4614{
4615 struct drm_device *dev = crtc->base.dev;
4616 struct drm_i915_private *dev_priv = dev->dev_private;
4617 int pipe = crtc->pipe;
4618
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004619 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004620 /* Force use of hard-coded filter coefficients
4621 * as some pre-programmed values are broken,
4622 * e.g. x201.
4623 */
4624 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4625 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4626 PF_PIPE_SEL_IVB(pipe));
4627 else
4628 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004629 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4630 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004631 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004632}
4633
Matt Roper4a3b8762014-12-23 10:41:51 -08004634static void intel_enable_sprite_planes(struct drm_crtc *crtc)
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004635{
4636 struct drm_device *dev = crtc->dev;
4637 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07004638 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004639 struct intel_plane *intel_plane;
4640
Matt Roperaf2b6532014-04-01 15:22:32 -07004641 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4642 intel_plane = to_intel_plane(plane);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004643 if (intel_plane->pipe == pipe)
4644 intel_plane_restore(&intel_plane->base);
Matt Roperaf2b6532014-04-01 15:22:32 -07004645 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004646}
4647
Matt Roper0d703d42015-03-04 10:49:04 -08004648/*
4649 * Disable a plane internally without actually modifying the plane's state.
4650 * This will allow us to easily restore the plane later by just reprogramming
4651 * its state.
4652 */
4653static void disable_plane_internal(struct drm_plane *plane)
4654{
4655 struct intel_plane *intel_plane = to_intel_plane(plane);
4656 struct drm_plane_state *state =
4657 plane->funcs->atomic_duplicate_state(plane);
4658 struct intel_plane_state *intel_state = to_intel_plane_state(state);
4659
4660 intel_state->visible = false;
4661 intel_plane->commit_plane(plane, intel_state);
4662
4663 intel_plane_destroy_state(plane, state);
4664}
4665
Matt Roper4a3b8762014-12-23 10:41:51 -08004666static void intel_disable_sprite_planes(struct drm_crtc *crtc)
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004667{
4668 struct drm_device *dev = crtc->dev;
4669 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07004670 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004671 struct intel_plane *intel_plane;
4672
Matt Roperaf2b6532014-04-01 15:22:32 -07004673 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4674 intel_plane = to_intel_plane(plane);
Matt Roper0d703d42015-03-04 10:49:04 -08004675 if (plane->fb && intel_plane->pipe == pipe)
4676 disable_plane_internal(plane);
Matt Roperaf2b6532014-04-01 15:22:32 -07004677 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004678}
4679
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004680void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004681{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004682 struct drm_device *dev = crtc->base.dev;
4683 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004684
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004685 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004686 return;
4687
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004688 /* We can only enable IPS after we enable a plane and wait for a vblank */
4689 intel_wait_for_vblank(dev, crtc->pipe);
4690
Paulo Zanonid77e4532013-09-24 13:52:55 -03004691 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004692 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004693 mutex_lock(&dev_priv->rps.hw_lock);
4694 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4695 mutex_unlock(&dev_priv->rps.hw_lock);
4696 /* Quoting Art Runyan: "its not safe to expect any particular
4697 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004698 * mailbox." Moreover, the mailbox may return a bogus state,
4699 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004700 */
4701 } else {
4702 I915_WRITE(IPS_CTL, IPS_ENABLE);
4703 /* The bit only becomes 1 in the next vblank, so this wait here
4704 * is essentially intel_wait_for_vblank. If we don't have this
4705 * and don't wait for vblanks until the end of crtc_enable, then
4706 * the HW state readout code will complain that the expected
4707 * IPS_CTL value is not the one we read. */
4708 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4709 DRM_ERROR("Timed out waiting for IPS enable\n");
4710 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004711}
4712
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004713void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004714{
4715 struct drm_device *dev = crtc->base.dev;
4716 struct drm_i915_private *dev_priv = dev->dev_private;
4717
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004718 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004719 return;
4720
4721 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004722 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004723 mutex_lock(&dev_priv->rps.hw_lock);
4724 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4725 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004726 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4727 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4728 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004729 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004730 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004731 POSTING_READ(IPS_CTL);
4732 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004733
4734 /* We need to wait for a vblank before we can disable the plane. */
4735 intel_wait_for_vblank(dev, crtc->pipe);
4736}
4737
4738/** Loads the palette/gamma unit for the CRTC with the prepared values */
4739static void intel_crtc_load_lut(struct drm_crtc *crtc)
4740{
4741 struct drm_device *dev = crtc->dev;
4742 struct drm_i915_private *dev_priv = dev->dev_private;
4743 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4744 enum pipe pipe = intel_crtc->pipe;
4745 int palreg = PALETTE(pipe);
4746 int i;
4747 bool reenable_ips = false;
4748
4749 /* The clocks have to be on to load the palette. */
Matt Roper83d65732015-02-25 13:12:16 -08004750 if (!crtc->state->enable || !intel_crtc->active)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004751 return;
4752
Imre Deak50360402015-01-16 00:55:16 -08004753 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03004754 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004755 assert_dsi_pll_enabled(dev_priv);
4756 else
4757 assert_pll_enabled(dev_priv, pipe);
4758 }
4759
4760 /* use legacy palette for Ironlake */
Sonika Jindal7a1db492014-07-22 11:18:27 +05304761 if (!HAS_GMCH_DISPLAY(dev))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004762 palreg = LGC_PALETTE(pipe);
4763
4764 /* Workaround : Do not read or write the pipe palette/gamma data while
4765 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4766 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004767 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03004768 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4769 GAMMA_MODE_MODE_SPLIT)) {
4770 hsw_disable_ips(intel_crtc);
4771 reenable_ips = true;
4772 }
4773
4774 for (i = 0; i < 256; i++) {
4775 I915_WRITE(palreg + 4 * i,
4776 (intel_crtc->lut_r[i] << 16) |
4777 (intel_crtc->lut_g[i] << 8) |
4778 intel_crtc->lut_b[i]);
4779 }
4780
4781 if (reenable_ips)
4782 hsw_enable_ips(intel_crtc);
4783}
4784
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004785static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
4786{
4787 if (!enable && intel_crtc->overlay) {
4788 struct drm_device *dev = intel_crtc->base.dev;
4789 struct drm_i915_private *dev_priv = dev->dev_private;
4790
4791 mutex_lock(&dev->struct_mutex);
4792 dev_priv->mm.interruptible = false;
4793 (void) intel_overlay_switch_off(intel_crtc->overlay);
4794 dev_priv->mm.interruptible = true;
4795 mutex_unlock(&dev->struct_mutex);
4796 }
4797
4798 /* Let userspace switch the overlay on again. In most cases userspace
4799 * has to recompute where to put it anyway.
4800 */
4801}
4802
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004803static void intel_crtc_enable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004804{
4805 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004806 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4807 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004808
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03004809 intel_enable_primary_hw_plane(crtc->primary, crtc);
Matt Roper4a3b8762014-12-23 10:41:51 -08004810 intel_enable_sprite_planes(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004811 intel_crtc_update_cursor(crtc, true);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004812 intel_crtc_dpms_overlay(intel_crtc, true);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004813
4814 hsw_enable_ips(intel_crtc);
4815
4816 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004817 intel_fbc_update(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004818 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf99d7062014-06-19 16:01:59 +02004819
4820 /*
4821 * FIXME: Once we grow proper nuclear flip support out of this we need
4822 * to compute the mask of flip planes precisely. For the time being
4823 * consider this a flip from a NULL plane.
4824 */
4825 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004826}
4827
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004828static void intel_crtc_disable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004829{
4830 struct drm_device *dev = crtc->dev;
4831 struct drm_i915_private *dev_priv = dev->dev_private;
4832 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4833 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004834
4835 intel_crtc_wait_for_pending_flips(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004836
Paulo Zanonie35fef22015-02-09 14:46:29 -02004837 if (dev_priv->fbc.crtc == intel_crtc)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004838 intel_fbc_disable(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004839
4840 hsw_disable_ips(intel_crtc);
4841
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004842 intel_crtc_dpms_overlay(intel_crtc, false);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004843 intel_crtc_update_cursor(crtc, false);
Matt Roper4a3b8762014-12-23 10:41:51 -08004844 intel_disable_sprite_planes(crtc);
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03004845 intel_disable_primary_hw_plane(crtc->primary, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004846
Daniel Vetterf99d7062014-06-19 16:01:59 +02004847 /*
4848 * FIXME: Once we grow proper nuclear flip support out of this we need
4849 * to compute the mask of flip planes precisely. For the time being
4850 * consider this a flip to a NULL plane.
4851 */
4852 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004853}
4854
Jesse Barnesf67a5592011-01-05 10:31:48 -08004855static void ironlake_crtc_enable(struct drm_crtc *crtc)
4856{
4857 struct drm_device *dev = crtc->dev;
4858 struct drm_i915_private *dev_priv = dev->dev_private;
4859 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004860 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004861 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004862
Matt Roper83d65732015-02-25 13:12:16 -08004863 WARN_ON(!crtc->state->enable);
Daniel Vetter08a48462012-07-02 11:43:47 +02004864
Jesse Barnesf67a5592011-01-05 10:31:48 -08004865 if (intel_crtc->active)
4866 return;
4867
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004868 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004869 intel_prepare_shared_dpll(intel_crtc);
4870
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004871 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304872 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004873
4874 intel_set_pipe_timings(intel_crtc);
4875
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004876 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004877 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004878 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004879 }
4880
4881 ironlake_set_pipeconf(crtc);
4882
Jesse Barnesf67a5592011-01-05 10:31:48 -08004883 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004884
Daniel Vettera72e4c92014-09-30 10:56:47 +02004885 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4886 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004887
Daniel Vetterf6736a12013-06-05 13:34:30 +02004888 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004889 if (encoder->pre_enable)
4890 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004891
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004892 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004893 /* Note: FDI PLL enabling _must_ be done before we enable the
4894 * cpu pipes, hence this is separate from all the other fdi/pch
4895 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004896 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004897 } else {
4898 assert_fdi_tx_disabled(dev_priv, pipe);
4899 assert_fdi_rx_disabled(dev_priv, pipe);
4900 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004901
Jesse Barnesb074cec2013-04-25 12:55:02 -07004902 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004903
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004904 /*
4905 * On ILK+ LUT must be loaded before the pipe is running but with
4906 * clocks enabled
4907 */
4908 intel_crtc_load_lut(crtc);
4909
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004910 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004911 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004912
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004913 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004914 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004915
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004916 assert_vblank_disabled(crtc);
4917 drm_crtc_vblank_on(crtc);
4918
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004919 for_each_encoder_on_crtc(dev, crtc, encoder)
4920 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004921
4922 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004923 cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02004924
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004925 intel_crtc_enable_planes(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004926}
4927
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004928/* IPS only exists on ULT machines and is tied to pipe A. */
4929static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4930{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004931 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004932}
4933
Paulo Zanonie4916942013-09-20 16:21:19 -03004934/*
4935 * This implements the workaround described in the "notes" section of the mode
4936 * set sequence documentation. When going from no pipes or single pipe to
4937 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4938 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4939 */
4940static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4941{
4942 struct drm_device *dev = crtc->base.dev;
4943 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4944
4945 /* We want to get the other_active_crtc only if there's only 1 other
4946 * active crtc. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004947 for_each_intel_crtc(dev, crtc_it) {
Paulo Zanonie4916942013-09-20 16:21:19 -03004948 if (!crtc_it->active || crtc_it == crtc)
4949 continue;
4950
4951 if (other_active_crtc)
4952 return;
4953
4954 other_active_crtc = crtc_it;
4955 }
4956 if (!other_active_crtc)
4957 return;
4958
4959 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4960 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4961}
4962
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004963static void haswell_crtc_enable(struct drm_crtc *crtc)
4964{
4965 struct drm_device *dev = crtc->dev;
4966 struct drm_i915_private *dev_priv = dev->dev_private;
4967 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4968 struct intel_encoder *encoder;
4969 int pipe = intel_crtc->pipe;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004970
Matt Roper83d65732015-02-25 13:12:16 -08004971 WARN_ON(!crtc->state->enable);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004972
4973 if (intel_crtc->active)
4974 return;
4975
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004976 if (intel_crtc_to_shared_dpll(intel_crtc))
4977 intel_enable_shared_dpll(intel_crtc);
4978
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004979 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304980 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004981
4982 intel_set_pipe_timings(intel_crtc);
4983
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004984 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
4985 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
4986 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004987 }
4988
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004989 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004990 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004991 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004992 }
4993
4994 haswell_set_pipeconf(crtc);
4995
4996 intel_set_pipe_csc(crtc);
4997
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004998 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004999
Daniel Vettera72e4c92014-09-30 10:56:47 +02005000 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005001 for_each_encoder_on_crtc(dev, crtc, encoder)
5002 if (encoder->pre_enable)
5003 encoder->pre_enable(encoder);
5004
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005005 if (intel_crtc->config->has_pch_encoder) {
Daniel Vettera72e4c92014-09-30 10:56:47 +02005006 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5007 true);
Imre Deak4fe94672014-06-25 22:01:49 +03005008 dev_priv->display.fdi_link_train(crtc);
5009 }
5010
Paulo Zanoni1f544382012-10-24 11:32:00 -02005011 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005012
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005013 if (INTEL_INFO(dev)->gen == 9)
Chandra Kondurua1b22782015-04-07 15:28:45 -07005014 skylake_pfit_update(intel_crtc, 1);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005015 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00005016 ironlake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005017 else
5018 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005019
5020 /*
5021 * On ILK+ LUT must be loaded before the pipe is running but with
5022 * clocks enabled
5023 */
5024 intel_crtc_load_lut(crtc);
5025
Paulo Zanoni1f544382012-10-24 11:32:00 -02005026 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00005027 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005028
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005029 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005030 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005031
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005032 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02005033 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005034
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005035 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10005036 intel_ddi_set_vc_payload_alloc(crtc, true);
5037
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005038 assert_vblank_disabled(crtc);
5039 drm_crtc_vblank_on(crtc);
5040
Jani Nikula8807e552013-08-30 19:40:32 +03005041 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005042 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005043 intel_opregion_notify_encoder(encoder, true);
5044 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005045
Paulo Zanonie4916942013-09-20 16:21:19 -03005046 /* If we change the relative order between pipe/planes enabling, we need
5047 * to change the workaround. */
5048 haswell_mode_set_planes_workaround(intel_crtc);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03005049 intel_crtc_enable_planes(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005050}
5051
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005052static void ironlake_pfit_disable(struct intel_crtc *crtc)
5053{
5054 struct drm_device *dev = crtc->base.dev;
5055 struct drm_i915_private *dev_priv = dev->dev_private;
5056 int pipe = crtc->pipe;
5057
5058 /* To avoid upsetting the power well on haswell only disable the pfit if
5059 * it's in use. The hw state code will make sure we get this right. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005060 if (crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005061 I915_WRITE(PF_CTL(pipe), 0);
5062 I915_WRITE(PF_WIN_POS(pipe), 0);
5063 I915_WRITE(PF_WIN_SZ(pipe), 0);
5064 }
5065}
5066
Jesse Barnes6be4a602010-09-10 10:26:01 -07005067static void ironlake_crtc_disable(struct drm_crtc *crtc)
5068{
5069 struct drm_device *dev = crtc->dev;
5070 struct drm_i915_private *dev_priv = dev->dev_private;
5071 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005072 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005073 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01005074 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005075
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005076 if (!intel_crtc->active)
5077 return;
5078
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03005079 intel_crtc_disable_planes(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005080
Daniel Vetterea9d7582012-07-10 10:42:52 +02005081 for_each_encoder_on_crtc(dev, crtc, encoder)
5082 encoder->disable(encoder);
5083
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005084 drm_crtc_vblank_off(crtc);
5085 assert_vblank_disabled(crtc);
5086
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005087 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005088 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Daniel Vetterd925c592013-06-05 13:34:04 +02005089
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005090 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005091
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005092 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005093
Daniel Vetterbf49ec82012-09-06 22:15:40 +02005094 for_each_encoder_on_crtc(dev, crtc, encoder)
5095 if (encoder->post_disable)
5096 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005097
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005098 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005099 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005100
Daniel Vetterd925c592013-06-05 13:34:04 +02005101 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005102
Daniel Vetterd925c592013-06-05 13:34:04 +02005103 if (HAS_PCH_CPT(dev)) {
5104 /* disable TRANS_DP_CTL */
5105 reg = TRANS_DP_CTL(pipe);
5106 temp = I915_READ(reg);
5107 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5108 TRANS_DP_PORT_SEL_MASK);
5109 temp |= TRANS_DP_PORT_SEL_NONE;
5110 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005111
Daniel Vetterd925c592013-06-05 13:34:04 +02005112 /* disable DPLL_SEL */
5113 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005114 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005115 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005116 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005117
5118 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005119 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02005120
5121 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005122 }
5123
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005124 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005125 intel_update_watermarks(crtc);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01005126
5127 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005128 intel_fbc_update(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01005129 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005130}
5131
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005132static void haswell_crtc_disable(struct drm_crtc *crtc)
5133{
5134 struct drm_device *dev = crtc->dev;
5135 struct drm_i915_private *dev_priv = dev->dev_private;
5136 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5137 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005138 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005139
5140 if (!intel_crtc->active)
5141 return;
5142
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03005143 intel_crtc_disable_planes(crtc);
Ville Syrjälädda9a662013-09-19 17:00:37 -03005144
Jani Nikula8807e552013-08-30 19:40:32 +03005145 for_each_encoder_on_crtc(dev, crtc, encoder) {
5146 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005147 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005148 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005149
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005150 drm_crtc_vblank_off(crtc);
5151 assert_vblank_disabled(crtc);
5152
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005153 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005154 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5155 false);
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005156 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005157
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005158 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005159 intel_ddi_set_vc_payload_alloc(crtc, false);
5160
Paulo Zanoniad80a812012-10-24 16:06:19 -02005161 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005162
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005163 if (INTEL_INFO(dev)->gen == 9)
Chandra Kondurua1b22782015-04-07 15:28:45 -07005164 skylake_pfit_update(intel_crtc, 0);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005165 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00005166 ironlake_pfit_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005167 else
5168 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005169
Paulo Zanoni1f544382012-10-24 11:32:00 -02005170 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005171
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005172 if (intel_crtc->config->has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02005173 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02005174 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02005175 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005176
Imre Deak97b040a2014-06-25 22:01:50 +03005177 for_each_encoder_on_crtc(dev, crtc, encoder)
5178 if (encoder->post_disable)
5179 encoder->post_disable(encoder);
5180
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005181 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005182 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005183
5184 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005185 intel_fbc_update(dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005186 mutex_unlock(&dev->struct_mutex);
Daniel Vetterdf8ad702014-06-25 22:02:03 +03005187
5188 if (intel_crtc_to_shared_dpll(intel_crtc))
5189 intel_disable_shared_dpll(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005190}
5191
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005192static void ironlake_crtc_off(struct drm_crtc *crtc)
5193{
5194 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005195 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005196}
5197
Paulo Zanoni6441ab52012-10-05 12:05:58 -03005198
Jesse Barnes2dd24552013-04-25 12:55:01 -07005199static void i9xx_pfit_enable(struct intel_crtc *crtc)
5200{
5201 struct drm_device *dev = crtc->base.dev;
5202 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005203 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005204
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005205 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005206 return;
5207
Daniel Vetterc0b03412013-05-28 12:05:54 +02005208 /*
5209 * The panel fitter should only be adjusted whilst the pipe is disabled,
5210 * according to register description and PRM.
5211 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005212 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5213 assert_pipe_disabled(dev_priv, crtc->pipe);
5214
Jesse Barnesb074cec2013-04-25 12:55:02 -07005215 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5216 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005217
5218 /* Border color in case we don't scale up to the full screen. Black by
5219 * default, change to something else for debugging. */
5220 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005221}
5222
Dave Airlied05410f2014-06-05 13:22:59 +10005223static enum intel_display_power_domain port_to_power_domain(enum port port)
5224{
5225 switch (port) {
5226 case PORT_A:
5227 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
5228 case PORT_B:
5229 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
5230 case PORT_C:
5231 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
5232 case PORT_D:
5233 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
5234 default:
5235 WARN_ON_ONCE(1);
5236 return POWER_DOMAIN_PORT_OTHER;
5237 }
5238}
5239
Imre Deak77d22dc2014-03-05 16:20:52 +02005240#define for_each_power_domain(domain, mask) \
5241 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
5242 if ((1 << (domain)) & (mask))
5243
Imre Deak319be8a2014-03-04 19:22:57 +02005244enum intel_display_power_domain
5245intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005246{
Imre Deak319be8a2014-03-04 19:22:57 +02005247 struct drm_device *dev = intel_encoder->base.dev;
5248 struct intel_digital_port *intel_dig_port;
5249
5250 switch (intel_encoder->type) {
5251 case INTEL_OUTPUT_UNKNOWN:
5252 /* Only DDI platforms should ever use this output type */
5253 WARN_ON_ONCE(!HAS_DDI(dev));
5254 case INTEL_OUTPUT_DISPLAYPORT:
5255 case INTEL_OUTPUT_HDMI:
5256 case INTEL_OUTPUT_EDP:
5257 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005258 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005259 case INTEL_OUTPUT_DP_MST:
5260 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5261 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005262 case INTEL_OUTPUT_ANALOG:
5263 return POWER_DOMAIN_PORT_CRT;
5264 case INTEL_OUTPUT_DSI:
5265 return POWER_DOMAIN_PORT_DSI;
5266 default:
5267 return POWER_DOMAIN_PORT_OTHER;
5268 }
5269}
5270
5271static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
5272{
5273 struct drm_device *dev = crtc->dev;
5274 struct intel_encoder *intel_encoder;
5275 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5276 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005277 unsigned long mask;
5278 enum transcoder transcoder;
5279
5280 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
5281
5282 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5283 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005284 if (intel_crtc->config->pch_pfit.enabled ||
5285 intel_crtc->config->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005286 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5287
Imre Deak319be8a2014-03-04 19:22:57 +02005288 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5289 mask |= BIT(intel_display_port_power_domain(intel_encoder));
5290
Imre Deak77d22dc2014-03-05 16:20:52 +02005291 return mask;
5292}
5293
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005294static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
Imre Deak77d22dc2014-03-05 16:20:52 +02005295{
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005296 struct drm_device *dev = state->dev;
Imre Deak77d22dc2014-03-05 16:20:52 +02005297 struct drm_i915_private *dev_priv = dev->dev_private;
5298 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
5299 struct intel_crtc *crtc;
5300
5301 /*
5302 * First get all needed power domains, then put all unneeded, to avoid
5303 * any unnecessary toggling of the power wells.
5304 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005305 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005306 enum intel_display_power_domain domain;
5307
Matt Roper83d65732015-02-25 13:12:16 -08005308 if (!crtc->base.state->enable)
Imre Deak77d22dc2014-03-05 16:20:52 +02005309 continue;
5310
Imre Deak319be8a2014-03-04 19:22:57 +02005311 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
Imre Deak77d22dc2014-03-05 16:20:52 +02005312
5313 for_each_power_domain(domain, pipe_domains[crtc->pipe])
5314 intel_display_power_get(dev_priv, domain);
5315 }
5316
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005317 if (dev_priv->display.modeset_global_resources)
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005318 dev_priv->display.modeset_global_resources(state);
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005319
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005320 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005321 enum intel_display_power_domain domain;
5322
5323 for_each_power_domain(domain, crtc->enabled_power_domains)
5324 intel_display_power_put(dev_priv, domain);
5325
5326 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
5327 }
5328
5329 intel_display_set_init_power(dev_priv, false);
5330}
5331
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305332void broxton_set_cdclk(struct drm_device *dev, int frequency)
5333{
5334 struct drm_i915_private *dev_priv = dev->dev_private;
5335 uint32_t divider;
5336 uint32_t ratio;
5337 uint32_t current_freq;
5338 int ret;
5339
5340 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5341 switch (frequency) {
5342 case 144000:
5343 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5344 ratio = BXT_DE_PLL_RATIO(60);
5345 break;
5346 case 288000:
5347 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5348 ratio = BXT_DE_PLL_RATIO(60);
5349 break;
5350 case 384000:
5351 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5352 ratio = BXT_DE_PLL_RATIO(60);
5353 break;
5354 case 576000:
5355 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5356 ratio = BXT_DE_PLL_RATIO(60);
5357 break;
5358 case 624000:
5359 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5360 ratio = BXT_DE_PLL_RATIO(65);
5361 break;
5362 case 19200:
5363 /*
5364 * Bypass frequency with DE PLL disabled. Init ratio, divider
5365 * to suppress GCC warning.
5366 */
5367 ratio = 0;
5368 divider = 0;
5369 break;
5370 default:
5371 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5372
5373 return;
5374 }
5375
5376 mutex_lock(&dev_priv->rps.hw_lock);
5377 /* Inform power controller of upcoming frequency change */
5378 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5379 0x80000000);
5380 mutex_unlock(&dev_priv->rps.hw_lock);
5381
5382 if (ret) {
5383 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5384 ret, frequency);
5385 return;
5386 }
5387
5388 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5389 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5390 current_freq = current_freq * 500 + 1000;
5391
5392 /*
5393 * DE PLL has to be disabled when
5394 * - setting to 19.2MHz (bypass, PLL isn't used)
5395 * - before setting to 624MHz (PLL needs toggling)
5396 * - before setting to any frequency from 624MHz (PLL needs toggling)
5397 */
5398 if (frequency == 19200 || frequency == 624000 ||
5399 current_freq == 624000) {
5400 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5401 /* Timeout 200us */
5402 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5403 1))
5404 DRM_ERROR("timout waiting for DE PLL unlock\n");
5405 }
5406
5407 if (frequency != 19200) {
5408 uint32_t val;
5409
5410 val = I915_READ(BXT_DE_PLL_CTL);
5411 val &= ~BXT_DE_PLL_RATIO_MASK;
5412 val |= ratio;
5413 I915_WRITE(BXT_DE_PLL_CTL, val);
5414
5415 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5416 /* Timeout 200us */
5417 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5418 DRM_ERROR("timeout waiting for DE PLL lock\n");
5419
5420 val = I915_READ(CDCLK_CTL);
5421 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5422 val |= divider;
5423 /*
5424 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5425 * enable otherwise.
5426 */
5427 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5428 if (frequency >= 500000)
5429 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5430
5431 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5432 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5433 val |= (frequency - 1000) / 500;
5434 I915_WRITE(CDCLK_CTL, val);
5435 }
5436
5437 mutex_lock(&dev_priv->rps.hw_lock);
5438 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5439 DIV_ROUND_UP(frequency, 25000));
5440 mutex_unlock(&dev_priv->rps.hw_lock);
5441
5442 if (ret) {
5443 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5444 ret, frequency);
5445 return;
5446 }
5447
5448 dev_priv->cdclk_freq = frequency;
5449}
5450
5451void broxton_init_cdclk(struct drm_device *dev)
5452{
5453 struct drm_i915_private *dev_priv = dev->dev_private;
5454 uint32_t val;
5455
5456 /*
5457 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5458 * or else the reset will hang because there is no PCH to respond.
5459 * Move the handshake programming to initialization sequence.
5460 * Previously was left up to BIOS.
5461 */
5462 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5463 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5464 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5465
5466 /* Enable PG1 for cdclk */
5467 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5468
5469 /* check if cd clock is enabled */
5470 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5471 DRM_DEBUG_KMS("Display already initialized\n");
5472 return;
5473 }
5474
5475 /*
5476 * FIXME:
5477 * - The initial CDCLK needs to be read from VBT.
5478 * Need to make this change after VBT has changes for BXT.
5479 * - check if setting the max (or any) cdclk freq is really necessary
5480 * here, it belongs to modeset time
5481 */
5482 broxton_set_cdclk(dev, 624000);
5483
5484 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005485 POSTING_READ(DBUF_CTL);
5486
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305487 udelay(10);
5488
5489 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5490 DRM_ERROR("DBuf power enable timeout!\n");
5491}
5492
5493void broxton_uninit_cdclk(struct drm_device *dev)
5494{
5495 struct drm_i915_private *dev_priv = dev->dev_private;
5496
5497 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005498 POSTING_READ(DBUF_CTL);
5499
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305500 udelay(10);
5501
5502 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5503 DRM_ERROR("DBuf power disable timeout!\n");
5504
5505 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5506 broxton_set_cdclk(dev, 19200);
5507
5508 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5509}
5510
Ville Syrjälädfcab172014-06-13 13:37:47 +03005511/* returns HPLL frequency in kHz */
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005512static int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005513{
Jesse Barnes586f49d2013-11-04 16:06:59 -08005514 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08005515
Jesse Barnes586f49d2013-11-04 16:06:59 -08005516 /* Obtain SKU information */
5517 mutex_lock(&dev_priv->dpio_lock);
5518 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
5519 CCK_FUSE_HPLL_FREQ_MASK;
5520 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005521
Ville Syrjälädfcab172014-06-13 13:37:47 +03005522 return vco_freq[hpll_freq] * 1000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005523}
5524
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005525static void vlv_update_cdclk(struct drm_device *dev)
5526{
5527 struct drm_i915_private *dev_priv = dev->dev_private;
5528
Vandana Kannan164dfd22014-11-24 13:37:41 +05305529 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
Ville Syrjälä43dc52c2014-10-07 17:41:20 +03005530 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
Vandana Kannan164dfd22014-11-24 13:37:41 +05305531 dev_priv->cdclk_freq);
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005532
5533 /*
5534 * Program the gmbus_freq based on the cdclk frequency.
5535 * BSpec erroneously claims we should aim for 4MHz, but
5536 * in fact 1MHz is the correct frequency.
5537 */
Vandana Kannan164dfd22014-11-24 13:37:41 +05305538 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005539}
5540
Jesse Barnes30a970c2013-11-04 13:48:12 -08005541/* Adjust CDclk dividers to allow high res or save power if possible */
5542static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5543{
5544 struct drm_i915_private *dev_priv = dev->dev_private;
5545 u32 val, cmd;
5546
Vandana Kannan164dfd22014-11-24 13:37:41 +05305547 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5548 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005549
Ville Syrjälädfcab172014-06-13 13:37:47 +03005550 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005551 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005552 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005553 cmd = 1;
5554 else
5555 cmd = 0;
5556
5557 mutex_lock(&dev_priv->rps.hw_lock);
5558 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5559 val &= ~DSPFREQGUAR_MASK;
5560 val |= (cmd << DSPFREQGUAR_SHIFT);
5561 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5562 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5563 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5564 50)) {
5565 DRM_ERROR("timed out waiting for CDclk change\n");
5566 }
5567 mutex_unlock(&dev_priv->rps.hw_lock);
5568
Ville Syrjälädfcab172014-06-13 13:37:47 +03005569 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005570 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005571
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005572 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005573
5574 mutex_lock(&dev_priv->dpio_lock);
5575 /* adjust cdclk divider */
5576 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjälä9cf33db2014-06-13 13:37:48 +03005577 val &= ~DISPLAY_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005578 val |= divider;
5579 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005580
5581 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5582 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5583 50))
5584 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005585 mutex_unlock(&dev_priv->dpio_lock);
5586 }
5587
5588 mutex_lock(&dev_priv->dpio_lock);
5589 /* adjust self-refresh exit latency value */
5590 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5591 val &= ~0x7f;
5592
5593 /*
5594 * For high bandwidth configs, we set a higher latency in the bunit
5595 * so that the core display fetch happens in time to avoid underruns.
5596 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005597 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005598 val |= 4500 / 250; /* 4.5 usec */
5599 else
5600 val |= 3000 / 250; /* 3.0 usec */
5601 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
5602 mutex_unlock(&dev_priv->dpio_lock);
5603
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005604 vlv_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005605}
5606
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005607static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5608{
5609 struct drm_i915_private *dev_priv = dev->dev_private;
5610 u32 val, cmd;
5611
Vandana Kannan164dfd22014-11-24 13:37:41 +05305612 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5613 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005614
5615 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005616 case 333333:
5617 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005618 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005619 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005620 break;
5621 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005622 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005623 return;
5624 }
5625
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005626 /*
5627 * Specs are full of misinformation, but testing on actual
5628 * hardware has shown that we just need to write the desired
5629 * CCK divider into the Punit register.
5630 */
5631 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5632
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005633 mutex_lock(&dev_priv->rps.hw_lock);
5634 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5635 val &= ~DSPFREQGUAR_MASK_CHV;
5636 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5637 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5638 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5639 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5640 50)) {
5641 DRM_ERROR("timed out waiting for CDclk change\n");
5642 }
5643 mutex_unlock(&dev_priv->rps.hw_lock);
5644
5645 vlv_update_cdclk(dev);
5646}
5647
Jesse Barnes30a970c2013-11-04 13:48:12 -08005648static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5649 int max_pixclk)
5650{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005651 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005652 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005653
Jesse Barnes30a970c2013-11-04 13:48:12 -08005654 /*
5655 * Really only a few cases to deal with, as only 4 CDclks are supported:
5656 * 200MHz
5657 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005658 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005659 * 400MHz (VLV only)
5660 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5661 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005662 *
5663 * We seem to get an unstable or solid color picture at 200MHz.
5664 * Not sure what's wrong. For now use 200MHz only when all pipes
5665 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005666 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005667 if (!IS_CHERRYVIEW(dev_priv) &&
5668 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005669 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005670 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005671 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005672 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005673 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005674 else
5675 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005676}
5677
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305678static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5679 int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005680{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305681 /*
5682 * FIXME:
5683 * - remove the guardband, it's not needed on BXT
5684 * - set 19.2MHz bypass frequency if there are no active pipes
5685 */
5686 if (max_pixclk > 576000*9/10)
5687 return 624000;
5688 else if (max_pixclk > 384000*9/10)
5689 return 576000;
5690 else if (max_pixclk > 288000*9/10)
5691 return 384000;
5692 else if (max_pixclk > 144000*9/10)
5693 return 288000;
5694 else
5695 return 144000;
5696}
5697
Jesse Barnes30a970c2013-11-04 13:48:12 -08005698/* compute the max pixel clock for new configuration */
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005699static int intel_mode_max_pixclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005700{
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005701 struct drm_device *dev = state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005702 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005703 struct intel_crtc_state *crtc_state;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005704 int max_pixclk = 0;
5705
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005706 for_each_intel_crtc(dev, intel_crtc) {
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005707 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
5708 if (IS_ERR(crtc_state))
5709 return PTR_ERR(crtc_state);
5710
5711 if (!crtc_state->base.enable)
5712 continue;
5713
5714 max_pixclk = max(max_pixclk,
5715 crtc_state->base.adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005716 }
5717
5718 return max_pixclk;
5719}
5720
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005721static int valleyview_modeset_global_pipes(struct drm_atomic_state *state,
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005722 unsigned *prepare_pipes)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005723{
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005724 struct drm_i915_private *dev_priv = to_i915(state->dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005725 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005726 int max_pixclk = intel_mode_max_pixclk(state);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305727 int cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005728
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005729 if (max_pixclk < 0)
5730 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005731
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305732 if (IS_VALLEYVIEW(dev_priv))
5733 cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
5734 else
5735 cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
5736
5737 if (cdclk == dev_priv->cdclk_freq)
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005738 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005739
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005740 /* disable/enable all currently active pipes while we change cdclk */
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005741 for_each_intel_crtc(state->dev, intel_crtc)
Matt Roper83d65732015-02-25 13:12:16 -08005742 if (intel_crtc->base.state->enable)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005743 *prepare_pipes |= (1 << intel_crtc->pipe);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005744
5745 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005746}
5747
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005748static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5749{
5750 unsigned int credits, default_credits;
5751
5752 if (IS_CHERRYVIEW(dev_priv))
5753 default_credits = PFI_CREDIT(12);
5754 else
5755 default_credits = PFI_CREDIT(8);
5756
Vandana Kannan164dfd22014-11-24 13:37:41 +05305757 if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005758 /* CHV suggested value is 31 or 63 */
5759 if (IS_CHERRYVIEW(dev_priv))
5760 credits = PFI_CREDIT_31;
5761 else
5762 credits = PFI_CREDIT(15);
5763 } else {
5764 credits = default_credits;
5765 }
5766
5767 /*
5768 * WA - write default credits before re-programming
5769 * FIXME: should we also set the resend bit here?
5770 */
5771 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5772 default_credits);
5773
5774 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5775 credits | PFI_CREDIT_RESEND);
5776
5777 /*
5778 * FIXME is this guaranteed to clear
5779 * immediately or should we poll for it?
5780 */
5781 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
5782}
5783
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005784static void valleyview_modeset_global_resources(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005785{
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005786 struct drm_device *dev = state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005787 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005788 int max_pixclk = intel_mode_max_pixclk(state);
5789 int req_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005790
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005791 /* The only reason this can fail is if we fail to add the crtc_state
5792 * to the atomic state. But that can't happen since the call to
5793 * intel_mode_max_pixclk() in valleyview_modeset_global_pipes() (which
5794 * can't have failed otherwise the mode set would be aborted) added all
5795 * the states already. */
5796 if (WARN_ON(max_pixclk < 0))
5797 return;
5798
5799 req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005800
Vandana Kannan164dfd22014-11-24 13:37:41 +05305801 if (req_cdclk != dev_priv->cdclk_freq) {
Imre Deak738c05c2014-11-19 16:25:37 +02005802 /*
5803 * FIXME: We can end up here with all power domains off, yet
5804 * with a CDCLK frequency other than the minimum. To account
5805 * for this take the PIPE-A power domain, which covers the HW
5806 * blocks needed for the following programming. This can be
5807 * removed once it's guaranteed that we get here either with
5808 * the minimum CDCLK set, or the required power domains
5809 * enabled.
5810 */
5811 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
5812
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005813 if (IS_CHERRYVIEW(dev))
5814 cherryview_set_cdclk(dev, req_cdclk);
5815 else
5816 valleyview_set_cdclk(dev, req_cdclk);
Imre Deak738c05c2014-11-19 16:25:37 +02005817
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005818 vlv_program_pfi_credits(dev_priv);
5819
Imre Deak738c05c2014-11-19 16:25:37 +02005820 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005821 }
Jesse Barnes30a970c2013-11-04 13:48:12 -08005822}
5823
Jesse Barnes89b667f2013-04-18 14:51:36 -07005824static void valleyview_crtc_enable(struct drm_crtc *crtc)
5825{
5826 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02005827 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005828 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5829 struct intel_encoder *encoder;
5830 int pipe = intel_crtc->pipe;
Jani Nikula23538ef2013-08-27 15:12:22 +03005831 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005832
Matt Roper83d65732015-02-25 13:12:16 -08005833 WARN_ON(!crtc->state->enable);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005834
5835 if (intel_crtc->active)
5836 return;
5837
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005838 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
Shobhit Kumar8525a232014-06-25 12:20:39 +05305839
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005840 if (!is_dsi) {
5841 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005842 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005843 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005844 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005845 }
Daniel Vetter5b18e572014-04-24 23:55:06 +02005846
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005847 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05305848 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005849
5850 intel_set_pipe_timings(intel_crtc);
5851
Ville Syrjäläc14b0482014-10-16 20:52:34 +03005852 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
5853 struct drm_i915_private *dev_priv = dev->dev_private;
5854
5855 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
5856 I915_WRITE(CHV_CANVAS(pipe), 0);
5857 }
5858
Daniel Vetter5b18e572014-04-24 23:55:06 +02005859 i9xx_set_pipeconf(intel_crtc);
5860
Jesse Barnes89b667f2013-04-18 14:51:36 -07005861 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005862
Daniel Vettera72e4c92014-09-30 10:56:47 +02005863 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005864
Jesse Barnes89b667f2013-04-18 14:51:36 -07005865 for_each_encoder_on_crtc(dev, crtc, encoder)
5866 if (encoder->pre_pll_enable)
5867 encoder->pre_pll_enable(encoder);
5868
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005869 if (!is_dsi) {
5870 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005871 chv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005872 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005873 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005874 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07005875
5876 for_each_encoder_on_crtc(dev, crtc, encoder)
5877 if (encoder->pre_enable)
5878 encoder->pre_enable(encoder);
5879
Jesse Barnes2dd24552013-04-25 12:55:01 -07005880 i9xx_pfit_enable(intel_crtc);
5881
Ville Syrjälä63cbb072013-06-04 13:48:59 +03005882 intel_crtc_load_lut(crtc);
5883
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005884 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005885 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02005886
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005887 assert_vblank_disabled(crtc);
5888 drm_crtc_vblank_on(crtc);
5889
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005890 for_each_encoder_on_crtc(dev, crtc, encoder)
5891 encoder->enable(encoder);
5892
Ville Syrjälä9ab04602014-05-08 19:23:14 +03005893 intel_crtc_enable_planes(crtc);
Daniel Vetterd40d9182014-05-21 11:45:40 +02005894
Ville Syrjälä56b80e12014-05-16 19:40:22 +03005895 /* Underruns don't raise interrupts, so check manually. */
Daniel Vettera72e4c92014-09-30 10:56:47 +02005896 i9xx_check_fifo_underruns(dev_priv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005897}
5898
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005899static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
5900{
5901 struct drm_device *dev = crtc->base.dev;
5902 struct drm_i915_private *dev_priv = dev->dev_private;
5903
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005904 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
5905 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005906}
5907
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005908static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005909{
5910 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02005911 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08005912 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005913 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08005914 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08005915
Matt Roper83d65732015-02-25 13:12:16 -08005916 WARN_ON(!crtc->state->enable);
Daniel Vetter08a48462012-07-02 11:43:47 +02005917
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005918 if (intel_crtc->active)
5919 return;
5920
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005921 i9xx_set_pll_dividers(intel_crtc);
5922
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005923 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05305924 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005925
5926 intel_set_pipe_timings(intel_crtc);
5927
Daniel Vetter5b18e572014-04-24 23:55:06 +02005928 i9xx_set_pipeconf(intel_crtc);
5929
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005930 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01005931
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005932 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005933 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005934
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02005935 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02005936 if (encoder->pre_enable)
5937 encoder->pre_enable(encoder);
5938
Daniel Vetterf6736a12013-06-05 13:34:30 +02005939 i9xx_enable_pll(intel_crtc);
5940
Jesse Barnes2dd24552013-04-25 12:55:01 -07005941 i9xx_pfit_enable(intel_crtc);
5942
Ville Syrjälä63cbb072013-06-04 13:48:59 +03005943 intel_crtc_load_lut(crtc);
5944
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005945 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005946 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02005947
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005948 assert_vblank_disabled(crtc);
5949 drm_crtc_vblank_on(crtc);
5950
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005951 for_each_encoder_on_crtc(dev, crtc, encoder)
5952 encoder->enable(encoder);
5953
Ville Syrjälä9ab04602014-05-08 19:23:14 +03005954 intel_crtc_enable_planes(crtc);
Daniel Vetterd40d9182014-05-21 11:45:40 +02005955
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005956 /*
5957 * Gen2 reports pipe underruns whenever all planes are disabled.
5958 * So don't enable underrun reporting before at least some planes
5959 * are enabled.
5960 * FIXME: Need to fix the logic to work when we turn off all planes
5961 * but leave the pipe running.
5962 */
5963 if (IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005964 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005965
Ville Syrjälä56b80e12014-05-16 19:40:22 +03005966 /* Underruns don't raise interrupts, so check manually. */
Daniel Vettera72e4c92014-09-30 10:56:47 +02005967 i9xx_check_fifo_underruns(dev_priv);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005968}
5969
Daniel Vetter87476d62013-04-11 16:29:06 +02005970static void i9xx_pfit_disable(struct intel_crtc *crtc)
5971{
5972 struct drm_device *dev = crtc->base.dev;
5973 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02005974
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005975 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02005976 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02005977
5978 assert_pipe_disabled(dev_priv, crtc->pipe);
5979
Daniel Vetter328d8e82013-05-08 10:36:31 +02005980 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
5981 I915_READ(PFIT_CONTROL));
5982 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02005983}
5984
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005985static void i9xx_crtc_disable(struct drm_crtc *crtc)
5986{
5987 struct drm_device *dev = crtc->dev;
5988 struct drm_i915_private *dev_priv = dev->dev_private;
5989 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005990 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005991 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005992
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005993 if (!intel_crtc->active)
5994 return;
5995
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005996 /*
5997 * Gen2 reports pipe underruns whenever all planes are disabled.
5998 * So diasble underrun reporting before all the planes get disabled.
5999 * FIXME: Need to fix the logic to work when we turn off all planes
6000 * but leave the pipe running.
6001 */
6002 if (IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006003 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006004
Imre Deak564ed192014-06-13 14:54:21 +03006005 /*
6006 * Vblank time updates from the shadow to live plane control register
6007 * are blocked if the memory self-refresh mode is active at that
6008 * moment. So to make sure the plane gets truly disabled, disable
6009 * first the self-refresh mode. The self-refresh enable bit in turn
6010 * will be checked/applied by the HW only at the next frame start
6011 * event which is after the vblank start event, so we need to have a
6012 * wait-for-vblank between disabling the plane and the pipe.
6013 */
6014 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä9ab04602014-05-08 19:23:14 +03006015 intel_crtc_disable_planes(crtc);
6016
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006017 /*
6018 * On gen2 planes are double buffered but the pipe isn't, so we must
6019 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03006020 * We also need to wait on all gmch platforms because of the
6021 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006022 */
Imre Deak564ed192014-06-13 14:54:21 +03006023 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006024
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006025 for_each_encoder_on_crtc(dev, crtc, encoder)
6026 encoder->disable(encoder);
6027
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006028 drm_crtc_vblank_off(crtc);
6029 assert_vblank_disabled(crtc);
6030
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006031 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006032
Daniel Vetter87476d62013-04-11 16:29:06 +02006033 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006034
Jesse Barnes89b667f2013-04-18 14:51:36 -07006035 for_each_encoder_on_crtc(dev, crtc, encoder)
6036 if (encoder->post_disable)
6037 encoder->post_disable(encoder);
6038
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006039 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006040 if (IS_CHERRYVIEW(dev))
6041 chv_disable_pll(dev_priv, pipe);
6042 else if (IS_VALLEYVIEW(dev))
6043 vlv_disable_pll(dev_priv, pipe);
6044 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006045 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006046 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006047
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006048 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006049 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006050
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006051 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03006052 intel_update_watermarks(crtc);
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006053
Daniel Vetterefa96242014-04-24 23:55:02 +02006054 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02006055 intel_fbc_update(dev);
Daniel Vetterefa96242014-04-24 23:55:02 +02006056 mutex_unlock(&dev->struct_mutex);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006057}
6058
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006059static void i9xx_crtc_off(struct drm_crtc *crtc)
6060{
6061}
6062
Borun Fub04c5bd2014-07-12 10:02:27 +05306063/* Master function to enable/disable CRTC and corresponding power wells */
6064void intel_crtc_control(struct drm_crtc *crtc, bool enable)
Chris Wilsoncdd59982010-09-08 16:30:16 +01006065{
Chris Wilsoncdd59982010-09-08 16:30:16 +01006066 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006067 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006068 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006069 enum intel_display_power_domain domain;
6070 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006071
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006072 if (enable) {
6073 if (!intel_crtc->active) {
Daniel Vettere1e9fb82014-06-25 22:02:04 +03006074 domains = get_crtc_power_domains(crtc);
6075 for_each_power_domain(domain, domains)
6076 intel_display_power_get(dev_priv, domain);
6077 intel_crtc->enabled_power_domains = domains;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006078
6079 dev_priv->display.crtc_enable(crtc);
6080 }
6081 } else {
6082 if (intel_crtc->active) {
6083 dev_priv->display.crtc_disable(crtc);
6084
Daniel Vettere1e9fb82014-06-25 22:02:04 +03006085 domains = intel_crtc->enabled_power_domains;
6086 for_each_power_domain(domain, domains)
6087 intel_display_power_put(dev_priv, domain);
6088 intel_crtc->enabled_power_domains = 0;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006089 }
6090 }
Borun Fub04c5bd2014-07-12 10:02:27 +05306091}
6092
6093/**
6094 * Sets the power management mode of the pipe and plane.
6095 */
6096void intel_crtc_update_dpms(struct drm_crtc *crtc)
6097{
6098 struct drm_device *dev = crtc->dev;
6099 struct intel_encoder *intel_encoder;
6100 bool enable = false;
6101
6102 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
6103 enable |= intel_encoder->connectors_active;
6104
6105 intel_crtc_control(crtc, enable);
Daniel Vetter976f8a22012-07-08 22:34:21 +02006106}
6107
Daniel Vetter976f8a22012-07-08 22:34:21 +02006108static void intel_crtc_disable(struct drm_crtc *crtc)
6109{
6110 struct drm_device *dev = crtc->dev;
6111 struct drm_connector *connector;
6112 struct drm_i915_private *dev_priv = dev->dev_private;
6113
6114 /* crtc should still be enabled when we disable it. */
Matt Roper83d65732015-02-25 13:12:16 -08006115 WARN_ON(!crtc->state->enable);
Daniel Vetter976f8a22012-07-08 22:34:21 +02006116
6117 dev_priv->display.crtc_disable(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006118 dev_priv->display.off(crtc);
6119
Matt Roper70a101f2015-04-08 18:56:53 -07006120 drm_plane_helper_disable(crtc->primary);
Daniel Vetter976f8a22012-07-08 22:34:21 +02006121
6122 /* Update computed state. */
6123 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
6124 if (!connector->encoder || !connector->encoder->crtc)
6125 continue;
6126
6127 if (connector->encoder->crtc != crtc)
6128 continue;
6129
6130 connector->dpms = DRM_MODE_DPMS_OFF;
6131 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01006132 }
6133}
6134
Chris Wilsonea5b2132010-08-04 13:50:23 +01006135void intel_encoder_destroy(struct drm_encoder *encoder)
6136{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006137 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006138
Chris Wilsonea5b2132010-08-04 13:50:23 +01006139 drm_encoder_cleanup(encoder);
6140 kfree(intel_encoder);
6141}
6142
Damien Lespiau92373292013-08-08 22:28:57 +01006143/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006144 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
6145 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01006146static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006147{
6148 if (mode == DRM_MODE_DPMS_ON) {
6149 encoder->connectors_active = true;
6150
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006151 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006152 } else {
6153 encoder->connectors_active = false;
6154
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006155 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006156 }
6157}
6158
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006159/* Cross check the actual hw state with our own modeset state tracking (and it's
6160 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02006161static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006162{
6163 if (connector->get_hw_state(connector)) {
6164 struct intel_encoder *encoder = connector->encoder;
6165 struct drm_crtc *crtc;
6166 bool encoder_enabled;
6167 enum pipe pipe;
6168
6169 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6170 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03006171 connector->base.name);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006172
Dave Airlie0e32b392014-05-02 14:02:48 +10006173 /* there is no real hw state for MST connectors */
6174 if (connector->mst_port)
6175 return;
6176
Rob Clarke2c719b2014-12-15 13:56:32 -05006177 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006178 "wrong connector dpms state\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006179 I915_STATE_WARN(connector->base.encoder != &encoder->base,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006180 "active connector not linked to encoder\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006181
Dave Airlie36cd7442014-05-02 13:44:18 +10006182 if (encoder) {
Rob Clarke2c719b2014-12-15 13:56:32 -05006183 I915_STATE_WARN(!encoder->connectors_active,
Dave Airlie36cd7442014-05-02 13:44:18 +10006184 "encoder->connectors_active not set\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006185
Dave Airlie36cd7442014-05-02 13:44:18 +10006186 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -05006187 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
6188 if (I915_STATE_WARN_ON(!encoder->base.crtc))
Dave Airlie36cd7442014-05-02 13:44:18 +10006189 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006190
Dave Airlie36cd7442014-05-02 13:44:18 +10006191 crtc = encoder->base.crtc;
6192
Matt Roper83d65732015-02-25 13:12:16 -08006193 I915_STATE_WARN(!crtc->state->enable,
6194 "crtc not enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006195 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
6196 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
Dave Airlie36cd7442014-05-02 13:44:18 +10006197 "encoder active on the wrong pipe\n");
6198 }
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006199 }
6200}
6201
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006202int intel_connector_init(struct intel_connector *connector)
6203{
6204 struct drm_connector_state *connector_state;
6205
6206 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6207 if (!connector_state)
6208 return -ENOMEM;
6209
6210 connector->base.state = connector_state;
6211 return 0;
6212}
6213
6214struct intel_connector *intel_connector_alloc(void)
6215{
6216 struct intel_connector *connector;
6217
6218 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6219 if (!connector)
6220 return NULL;
6221
6222 if (intel_connector_init(connector) < 0) {
6223 kfree(connector);
6224 return NULL;
6225 }
6226
6227 return connector;
6228}
6229
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006230/* Even simpler default implementation, if there's really no special case to
6231 * consider. */
6232void intel_connector_dpms(struct drm_connector *connector, int mode)
6233{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006234 /* All the simple cases only support two dpms states. */
6235 if (mode != DRM_MODE_DPMS_ON)
6236 mode = DRM_MODE_DPMS_OFF;
6237
6238 if (mode == connector->dpms)
6239 return;
6240
6241 connector->dpms = mode;
6242
6243 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dcf2013-09-29 19:15:07 +01006244 if (connector->encoder)
6245 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006246
Daniel Vetterb9805142012-08-31 17:37:33 +02006247 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006248}
6249
Daniel Vetterf0947c32012-07-02 13:10:34 +02006250/* Simple connector->get_hw_state implementation for encoders that support only
6251 * one connector and no cloning and hence the encoder state determines the state
6252 * of the connector. */
6253bool intel_connector_get_hw_state(struct intel_connector *connector)
6254{
Daniel Vetter24929352012-07-02 20:28:59 +02006255 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006256 struct intel_encoder *encoder = connector->encoder;
6257
6258 return encoder->get_hw_state(encoder, &pipe);
6259}
6260
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006261static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006262{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006263 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6264 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006265
6266 return 0;
6267}
6268
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006269static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006270 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006271{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006272 struct drm_atomic_state *state = pipe_config->base.state;
6273 struct intel_crtc *other_crtc;
6274 struct intel_crtc_state *other_crtc_state;
6275
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006276 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6277 pipe_name(pipe), pipe_config->fdi_lanes);
6278 if (pipe_config->fdi_lanes > 4) {
6279 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6280 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006281 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006282 }
6283
Paulo Zanonibafb6552013-11-02 21:07:44 -07006284 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006285 if (pipe_config->fdi_lanes > 2) {
6286 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6287 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006288 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006289 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006290 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006291 }
6292 }
6293
6294 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006295 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006296
6297 /* Ivybridge 3 pipe is really complicated */
6298 switch (pipe) {
6299 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006300 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006301 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006302 if (pipe_config->fdi_lanes <= 2)
6303 return 0;
6304
6305 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6306 other_crtc_state =
6307 intel_atomic_get_crtc_state(state, other_crtc);
6308 if (IS_ERR(other_crtc_state))
6309 return PTR_ERR(other_crtc_state);
6310
6311 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006312 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6313 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006314 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006315 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006316 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006317 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006318 if (pipe_config->fdi_lanes > 2) {
6319 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6320 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006321 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006322 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006323
6324 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6325 other_crtc_state =
6326 intel_atomic_get_crtc_state(state, other_crtc);
6327 if (IS_ERR(other_crtc_state))
6328 return PTR_ERR(other_crtc_state);
6329
6330 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006331 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006332 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006333 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006334 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006335 default:
6336 BUG();
6337 }
6338}
6339
Daniel Vettere29c22c2013-02-21 00:00:16 +01006340#define RETRY 1
6341static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006342 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006343{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006344 struct drm_device *dev = intel_crtc->base.dev;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006345 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006346 int lane, link_bw, fdi_dotclock, ret;
6347 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006348
Daniel Vettere29c22c2013-02-21 00:00:16 +01006349retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006350 /* FDI is a binary signal running at ~2.7GHz, encoding
6351 * each output octet as 10 bits. The actual frequency
6352 * is stored as a divider into a 100MHz clock, and the
6353 * mode pixel clock is stored in units of 1KHz.
6354 * Hence the bw of each lane in terms of the mode signal
6355 * is:
6356 */
6357 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6358
Damien Lespiau241bfc32013-09-25 16:45:37 +01006359 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006360
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006361 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006362 pipe_config->pipe_bpp);
6363
6364 pipe_config->fdi_lanes = lane;
6365
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006366 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006367 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006368
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006369 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6370 intel_crtc->pipe, pipe_config);
6371 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006372 pipe_config->pipe_bpp -= 2*3;
6373 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6374 pipe_config->pipe_bpp);
6375 needs_recompute = true;
6376 pipe_config->bw_constrained = true;
6377
6378 goto retry;
6379 }
6380
6381 if (needs_recompute)
6382 return RETRY;
6383
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006384 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006385}
6386
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006387static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006388 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006389{
Jani Nikulad330a952014-01-21 11:24:25 +02006390 pipe_config->ips_enabled = i915.enable_ips &&
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03006391 hsw_crtc_supports_ips(crtc) &&
Jesse Barnesb6dfdc92013-07-25 10:06:50 -07006392 pipe_config->pipe_bpp <= 24;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006393}
6394
Daniel Vettera43f6e02013-06-07 23:10:32 +02006395static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006396 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006397{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006398 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006399 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006400 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chandra Kondurud03c93d2015-04-09 16:42:46 -07006401 int ret;
Chris Wilson89749352010-09-12 18:25:19 +01006402
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006403 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006404 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006405 int clock_limit =
6406 dev_priv->display.get_display_clock_speed(dev);
6407
6408 /*
6409 * Enable pixel doubling when the dot clock
6410 * is > 90% of the (display) core speed.
6411 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03006412 * GDG double wide on either pipe,
6413 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006414 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03006415 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01006416 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006417 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006418 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006419 }
6420
Damien Lespiau241bfc32013-09-25 16:45:37 +01006421 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006422 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006423 }
Chris Wilson89749352010-09-12 18:25:19 +01006424
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006425 /*
6426 * Pipe horizontal size must be even in:
6427 * - DVO ganged mode
6428 * - LVDS dual channel mode
6429 * - Double wide pipe
6430 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006431 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006432 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6433 pipe_config->pipe_src_w &= ~1;
6434
Damien Lespiau8693a822013-05-03 18:48:11 +01006435 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6436 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006437 */
6438 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
6439 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006440 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006441
Damien Lespiauf5adf942013-06-24 18:29:34 +01006442 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006443 hsw_compute_ips_config(crtc, pipe_config);
6444
Daniel Vetter877d48d2013-04-19 11:24:43 +02006445 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006446 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006447
Chandra Kondurud03c93d2015-04-09 16:42:46 -07006448 /* FIXME: remove below call once atomic mode set is place and all crtc
6449 * related checks called from atomic_crtc_check function */
6450 ret = 0;
6451 DRM_DEBUG_KMS("intel_crtc = %p drm_state (pipe_config->base.state) = %p\n",
6452 crtc, pipe_config->base.state);
6453 ret = intel_atomic_setup_scalers(dev, crtc, pipe_config);
6454
6455 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006456}
6457
Ville Syrjälä1652d192015-03-31 14:12:01 +03006458static int skylake_get_display_clock_speed(struct drm_device *dev)
6459{
6460 struct drm_i915_private *dev_priv = to_i915(dev);
6461 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6462 uint32_t cdctl = I915_READ(CDCLK_CTL);
6463 uint32_t linkrate;
6464
6465 if (!(lcpll1 & LCPLL_PLL_ENABLE)) {
6466 WARN(1, "LCPLL1 not enabled\n");
6467 return 24000; /* 24MHz is the cd freq with NSSC ref */
6468 }
6469
6470 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6471 return 540000;
6472
6473 linkrate = (I915_READ(DPLL_CTRL1) &
Damien Lespiau71cd8422015-04-30 16:39:17 +01006474 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006475
Damien Lespiau71cd8422015-04-30 16:39:17 +01006476 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6477 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006478 /* vco 8640 */
6479 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6480 case CDCLK_FREQ_450_432:
6481 return 432000;
6482 case CDCLK_FREQ_337_308:
6483 return 308570;
6484 case CDCLK_FREQ_675_617:
6485 return 617140;
6486 default:
6487 WARN(1, "Unknown cd freq selection\n");
6488 }
6489 } else {
6490 /* vco 8100 */
6491 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6492 case CDCLK_FREQ_450_432:
6493 return 450000;
6494 case CDCLK_FREQ_337_308:
6495 return 337500;
6496 case CDCLK_FREQ_675_617:
6497 return 675000;
6498 default:
6499 WARN(1, "Unknown cd freq selection\n");
6500 }
6501 }
6502
6503 /* error case, do as if DPLL0 isn't enabled */
6504 return 24000;
6505}
6506
6507static int broadwell_get_display_clock_speed(struct drm_device *dev)
6508{
6509 struct drm_i915_private *dev_priv = dev->dev_private;
6510 uint32_t lcpll = I915_READ(LCPLL_CTL);
6511 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6512
6513 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6514 return 800000;
6515 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6516 return 450000;
6517 else if (freq == LCPLL_CLK_FREQ_450)
6518 return 450000;
6519 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6520 return 540000;
6521 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6522 return 337500;
6523 else
6524 return 675000;
6525}
6526
6527static int haswell_get_display_clock_speed(struct drm_device *dev)
6528{
6529 struct drm_i915_private *dev_priv = dev->dev_private;
6530 uint32_t lcpll = I915_READ(LCPLL_CTL);
6531 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6532
6533 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6534 return 800000;
6535 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6536 return 450000;
6537 else if (freq == LCPLL_CLK_FREQ_450)
6538 return 450000;
6539 else if (IS_HSW_ULT(dev))
6540 return 337500;
6541 else
6542 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006543}
6544
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006545static int valleyview_get_display_clock_speed(struct drm_device *dev)
6546{
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006547 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006548 u32 val;
6549 int divider;
6550
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006551 if (dev_priv->hpll_freq == 0)
6552 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
6553
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006554 mutex_lock(&dev_priv->dpio_lock);
6555 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
6556 mutex_unlock(&dev_priv->dpio_lock);
6557
6558 divider = val & DISPLAY_FREQUENCY_VALUES;
6559
Ville Syrjälä7d007f42014-06-13 13:37:53 +03006560 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
6561 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
6562 "cdclk change in progress\n");
6563
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006564 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006565}
6566
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006567static int ilk_get_display_clock_speed(struct drm_device *dev)
6568{
6569 return 450000;
6570}
6571
Jesse Barnese70236a2009-09-21 10:42:27 -07006572static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006573{
Jesse Barnese70236a2009-09-21 10:42:27 -07006574 return 400000;
6575}
Jesse Barnes79e53942008-11-07 14:24:08 -08006576
Jesse Barnese70236a2009-09-21 10:42:27 -07006577static int i915_get_display_clock_speed(struct drm_device *dev)
6578{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006579 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006580}
Jesse Barnes79e53942008-11-07 14:24:08 -08006581
Jesse Barnese70236a2009-09-21 10:42:27 -07006582static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6583{
6584 return 200000;
6585}
Jesse Barnes79e53942008-11-07 14:24:08 -08006586
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006587static int pnv_get_display_clock_speed(struct drm_device *dev)
6588{
6589 u16 gcfgc = 0;
6590
6591 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6592
6593 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6594 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006595 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006596 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006597 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006598 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006599 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006600 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6601 return 200000;
6602 default:
6603 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6604 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006605 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006606 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006607 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006608 }
6609}
6610
Jesse Barnese70236a2009-09-21 10:42:27 -07006611static int i915gm_get_display_clock_speed(struct drm_device *dev)
6612{
6613 u16 gcfgc = 0;
6614
6615 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6616
6617 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006618 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006619 else {
6620 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6621 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006622 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006623 default:
6624 case GC_DISPLAY_CLOCK_190_200_MHZ:
6625 return 190000;
6626 }
6627 }
6628}
Jesse Barnes79e53942008-11-07 14:24:08 -08006629
Jesse Barnese70236a2009-09-21 10:42:27 -07006630static int i865_get_display_clock_speed(struct drm_device *dev)
6631{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006632 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006633}
6634
6635static int i855_get_display_clock_speed(struct drm_device *dev)
6636{
6637 u16 hpllcc = 0;
6638 /* Assume that the hardware is in the high speed state. This
6639 * should be the default.
6640 */
6641 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6642 case GC_CLOCK_133_200:
6643 case GC_CLOCK_100_200:
6644 return 200000;
6645 case GC_CLOCK_166_250:
6646 return 250000;
6647 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006648 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006649 }
6650
6651 /* Shouldn't happen */
6652 return 0;
6653}
6654
6655static int i830_get_display_clock_speed(struct drm_device *dev)
6656{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006657 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006658}
6659
Zhenyu Wang2c072452009-06-05 15:38:42 +08006660static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006661intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006662{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006663 while (*num > DATA_LINK_M_N_MASK ||
6664 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08006665 *num >>= 1;
6666 *den >>= 1;
6667 }
6668}
6669
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006670static void compute_m_n(unsigned int m, unsigned int n,
6671 uint32_t *ret_m, uint32_t *ret_n)
6672{
6673 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
6674 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6675 intel_reduce_m_n_ratio(ret_m, ret_n);
6676}
6677
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006678void
6679intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6680 int pixel_clock, int link_clock,
6681 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006682{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006683 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006684
6685 compute_m_n(bits_per_pixel * pixel_clock,
6686 link_clock * nlanes * 8,
6687 &m_n->gmch_m, &m_n->gmch_n);
6688
6689 compute_m_n(pixel_clock, link_clock,
6690 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08006691}
6692
Chris Wilsona7615032011-01-12 17:04:08 +00006693static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
6694{
Jani Nikulad330a952014-01-21 11:24:25 +02006695 if (i915.panel_use_ssc >= 0)
6696 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006697 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07006698 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00006699}
6700
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006701static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
6702 int num_connectors)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006703{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006704 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006705 struct drm_i915_private *dev_priv = dev->dev_private;
6706 int refclk;
6707
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006708 WARN_ON(!crtc_state->base.state);
6709
Imre Deak5ab7b0b2015-03-06 03:29:25 +02006710 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02006711 refclk = 100000;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006712 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006713 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02006714 refclk = dev_priv->vbt.lvds_ssc_freq;
6715 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006716 } else if (!IS_GEN2(dev)) {
6717 refclk = 96000;
6718 } else {
6719 refclk = 48000;
6720 }
6721
6722 return refclk;
6723}
6724
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006725static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006726{
Daniel Vetter7df00d72013-05-21 21:54:55 +02006727 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006728}
Daniel Vetterf47709a2013-03-28 10:42:02 +01006729
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006730static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
6731{
6732 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006733}
6734
Daniel Vetterf47709a2013-03-28 10:42:02 +01006735static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006736 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08006737 intel_clock_t *reduced_clock)
6738{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006739 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006740 u32 fp, fp2 = 0;
6741
6742 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006743 fp = pnv_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 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006746 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006747 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006748 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006749 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006750 }
6751
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006752 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006753
Daniel Vetterf47709a2013-03-28 10:42:02 +01006754 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006755 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07006756 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006757 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01006758 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006759 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006760 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006761 }
6762}
6763
Chon Ming Lee5e69f972013-09-05 20:41:49 +08006764static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
6765 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07006766{
6767 u32 reg_val;
6768
6769 /*
6770 * PLLB opamp always calibrates to max value of 0x3f, force enable it
6771 * and set it to a reasonable value instead.
6772 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006773 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006774 reg_val &= 0xffffff00;
6775 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006776 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006777
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006778 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006779 reg_val &= 0x8cffffff;
6780 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006781 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, 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_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006784 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006785 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006786
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006787 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006788 reg_val &= 0x00ffffff;
6789 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006790 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006791}
6792
Daniel Vetterb5518422013-05-03 11:49:48 +02006793static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
6794 struct intel_link_m_n *m_n)
6795{
6796 struct drm_device *dev = crtc->base.dev;
6797 struct drm_i915_private *dev_priv = dev->dev_private;
6798 int pipe = crtc->pipe;
6799
Daniel Vettere3b95f12013-05-03 11:49:49 +02006800 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6801 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
6802 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
6803 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02006804}
6805
6806static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07006807 struct intel_link_m_n *m_n,
6808 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02006809{
6810 struct drm_device *dev = crtc->base.dev;
6811 struct drm_i915_private *dev_priv = dev->dev_private;
6812 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006813 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02006814
6815 if (INTEL_INFO(dev)->gen >= 5) {
6816 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
6817 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
6818 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
6819 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07006820 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
6821 * for gen < 8) and if DRRS is supported (to make sure the
6822 * registers are not unnecessarily accessed).
6823 */
Durgadoss R44395bf2015-02-13 15:33:02 +05306824 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006825 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07006826 I915_WRITE(PIPE_DATA_M2(transcoder),
6827 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
6828 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
6829 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
6830 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
6831 }
Daniel Vetterb5518422013-05-03 11:49:48 +02006832 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02006833 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6834 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
6835 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
6836 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02006837 }
6838}
6839
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306840void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006841{
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306842 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
6843
6844 if (m_n == M1_N1) {
6845 dp_m_n = &crtc->config->dp_m_n;
6846 dp_m2_n2 = &crtc->config->dp_m2_n2;
6847 } else if (m_n == M2_N2) {
6848
6849 /*
6850 * M2_N2 registers are not supported. Hence m2_n2 divider value
6851 * needs to be programmed into M1_N1.
6852 */
6853 dp_m_n = &crtc->config->dp_m2_n2;
6854 } else {
6855 DRM_ERROR("Unsupported divider value\n");
6856 return;
6857 }
6858
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006859 if (crtc->config->has_pch_encoder)
6860 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006861 else
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306862 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006863}
6864
Ville Syrjäläd288f652014-10-28 13:20:22 +02006865static void vlv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006866 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006867{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006868 u32 dpll, dpll_md;
6869
6870 /*
6871 * Enable DPIO clock input. We should never disable the reference
6872 * clock for pipe B, since VGA hotplug / manual detection depends
6873 * on it.
6874 */
6875 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
6876 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
6877 /* We should never disable this, set it here for state tracking */
6878 if (crtc->pipe == PIPE_B)
6879 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
6880 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02006881 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006882
Ville Syrjäläd288f652014-10-28 13:20:22 +02006883 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006884 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02006885 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006886}
6887
Ville Syrjäläd288f652014-10-28 13:20:22 +02006888static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006889 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006890{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006891 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006892 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01006893 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006894 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006895 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006896 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006897
Daniel Vetter09153002012-12-12 14:06:44 +01006898 mutex_lock(&dev_priv->dpio_lock);
6899
Ville Syrjäläd288f652014-10-28 13:20:22 +02006900 bestn = pipe_config->dpll.n;
6901 bestm1 = pipe_config->dpll.m1;
6902 bestm2 = pipe_config->dpll.m2;
6903 bestp1 = pipe_config->dpll.p1;
6904 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006905
Jesse Barnes89b667f2013-04-18 14:51:36 -07006906 /* See eDP HDMI DPIO driver vbios notes doc */
6907
6908 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006909 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08006910 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006911
6912 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006913 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006914
6915 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006916 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006917 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006918 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006919
6920 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006921 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006922
6923 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006924 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
6925 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
6926 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006927 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07006928
6929 /*
6930 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
6931 * but we don't support that).
6932 * Note: don't use the DAC post divider as it seems unstable.
6933 */
6934 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006935 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006936
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006937 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006938 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006939
Jesse Barnes89b667f2013-04-18 14:51:36 -07006940 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02006941 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006942 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
6943 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006944 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03006945 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006946 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006947 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006948 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006949
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02006950 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07006951 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006952 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006953 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006954 0x0df40000);
6955 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006956 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006957 0x0df70000);
6958 } else { /* HDMI or VGA */
6959 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006960 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006961 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006962 0x0df70000);
6963 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006964 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006965 0x0df40000);
6966 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006967
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006968 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006969 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006970 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
6971 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006972 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006973 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006974
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006975 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Daniel Vetter09153002012-12-12 14:06:44 +01006976 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006977}
6978
Ville Syrjäläd288f652014-10-28 13:20:22 +02006979static void chv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006980 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006981{
Ville Syrjäläd288f652014-10-28 13:20:22 +02006982 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006983 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
6984 DPLL_VCO_ENABLE;
6985 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02006986 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006987
Ville Syrjäläd288f652014-10-28 13:20:22 +02006988 pipe_config->dpll_hw_state.dpll_md =
6989 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006990}
6991
Ville Syrjäläd288f652014-10-28 13:20:22 +02006992static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006993 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006994{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006995 struct drm_device *dev = crtc->base.dev;
6996 struct drm_i915_private *dev_priv = dev->dev_private;
6997 int pipe = crtc->pipe;
6998 int dpll_reg = DPLL(crtc->pipe);
6999 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307000 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007001 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307002 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307003 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007004
Ville Syrjäläd288f652014-10-28 13:20:22 +02007005 bestn = pipe_config->dpll.n;
7006 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7007 bestm1 = pipe_config->dpll.m1;
7008 bestm2 = pipe_config->dpll.m2 >> 22;
7009 bestp1 = pipe_config->dpll.p1;
7010 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307011 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307012 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307013 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007014
7015 /*
7016 * Enable Refclk and SSC
7017 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007018 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007019 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007020
7021 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007022
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007023 /* p1 and p2 divider */
7024 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7025 5 << DPIO_CHV_S1_DIV_SHIFT |
7026 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7027 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7028 1 << DPIO_CHV_K_DIV_SHIFT);
7029
7030 /* Feedback post-divider - m2 */
7031 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7032
7033 /* Feedback refclk divider - n and m1 */
7034 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7035 DPIO_CHV_M1_DIV_BY_2 |
7036 1 << DPIO_CHV_N_DIV_SHIFT);
7037
7038 /* M2 fraction division */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307039 if (bestm2_frac)
7040 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007041
7042 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307043 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7044 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7045 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7046 if (bestm2_frac)
7047 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7048 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007049
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307050 /* Program digital lock detect threshold */
7051 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7052 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7053 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7054 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7055 if (!bestm2_frac)
7056 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7057 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7058
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007059 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307060 if (vco == 5400000) {
7061 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7062 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7063 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7064 tribuf_calcntr = 0x9;
7065 } else if (vco <= 6200000) {
7066 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7067 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7068 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7069 tribuf_calcntr = 0x9;
7070 } else if (vco <= 6480000) {
7071 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7072 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7073 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7074 tribuf_calcntr = 0x8;
7075 } else {
7076 /* Not supported. Apply the same limits as in the max case */
7077 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7078 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7079 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7080 tribuf_calcntr = 0;
7081 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007082 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7083
Ville Syrjälä968040b2015-03-11 22:52:08 +02007084 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307085 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7086 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7087 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7088
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007089 /* AFC Recal */
7090 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7091 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7092 DPIO_AFC_RECAL);
7093
7094 mutex_unlock(&dev_priv->dpio_lock);
7095}
7096
Ville Syrjäläd288f652014-10-28 13:20:22 +02007097/**
7098 * vlv_force_pll_on - forcibly enable just the PLL
7099 * @dev_priv: i915 private structure
7100 * @pipe: pipe PLL to enable
7101 * @dpll: PLL configuration
7102 *
7103 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7104 * in cases where we need the PLL enabled even when @pipe is not going to
7105 * be enabled.
7106 */
7107void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7108 const struct dpll *dpll)
7109{
7110 struct intel_crtc *crtc =
7111 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007112 struct intel_crtc_state pipe_config = {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007113 .base.crtc = &crtc->base,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007114 .pixel_multiplier = 1,
7115 .dpll = *dpll,
7116 };
7117
7118 if (IS_CHERRYVIEW(dev)) {
7119 chv_update_pll(crtc, &pipe_config);
7120 chv_prepare_pll(crtc, &pipe_config);
7121 chv_enable_pll(crtc, &pipe_config);
7122 } else {
7123 vlv_update_pll(crtc, &pipe_config);
7124 vlv_prepare_pll(crtc, &pipe_config);
7125 vlv_enable_pll(crtc, &pipe_config);
7126 }
7127}
7128
7129/**
7130 * vlv_force_pll_off - forcibly disable just the PLL
7131 * @dev_priv: i915 private structure
7132 * @pipe: pipe PLL to disable
7133 *
7134 * Disable the PLL for @pipe. To be used in cases where we need
7135 * the PLL enabled even when @pipe is not going to be enabled.
7136 */
7137void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7138{
7139 if (IS_CHERRYVIEW(dev))
7140 chv_disable_pll(to_i915(dev), pipe);
7141 else
7142 vlv_disable_pll(to_i915(dev), pipe);
7143}
7144
Daniel Vetterf47709a2013-03-28 10:42:02 +01007145static void i9xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007146 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01007147 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007148 int num_connectors)
7149{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007150 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007151 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007152 u32 dpll;
7153 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007154 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007155
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007156 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307157
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007158 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7159 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007160
7161 dpll = DPLL_VGA_MODE_DIS;
7162
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007163 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007164 dpll |= DPLLB_MODE_LVDS;
7165 else
7166 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007167
Daniel Vetteref1b4602013-06-01 17:17:04 +02007168 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007169 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007170 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007171 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007172
7173 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007174 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007175
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007176 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007177 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007178
7179 /* compute bitmask from p1 value */
7180 if (IS_PINEVIEW(dev))
7181 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7182 else {
7183 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7184 if (IS_G4X(dev) && reduced_clock)
7185 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7186 }
7187 switch (clock->p2) {
7188 case 5:
7189 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7190 break;
7191 case 7:
7192 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7193 break;
7194 case 10:
7195 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7196 break;
7197 case 14:
7198 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7199 break;
7200 }
7201 if (INTEL_INFO(dev)->gen >= 4)
7202 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7203
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007204 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007205 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007206 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007207 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7208 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7209 else
7210 dpll |= PLL_REF_INPUT_DREFCLK;
7211
7212 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007213 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007214
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007215 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007216 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007217 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007218 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007219 }
7220}
7221
Daniel Vetterf47709a2013-03-28 10:42:02 +01007222static void i8xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007223 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01007224 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007225 int num_connectors)
7226{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007227 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007228 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007229 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007230 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007231
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007232 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307233
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007234 dpll = DPLL_VGA_MODE_DIS;
7235
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007236 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007237 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7238 } else {
7239 if (clock->p1 == 2)
7240 dpll |= PLL_P1_DIVIDE_BY_TWO;
7241 else
7242 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7243 if (clock->p2 == 4)
7244 dpll |= PLL_P2_DIVIDE_BY_4;
7245 }
7246
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007247 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007248 dpll |= DPLL_DVO_2X_MODE;
7249
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007250 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007251 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7252 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7253 else
7254 dpll |= PLL_REF_INPUT_DREFCLK;
7255
7256 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007257 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007258}
7259
Daniel Vetter8a654f32013-06-01 17:16:22 +02007260static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007261{
7262 struct drm_device *dev = intel_crtc->base.dev;
7263 struct drm_i915_private *dev_priv = dev->dev_private;
7264 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007265 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02007266 struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007267 &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007268 uint32_t crtc_vtotal, crtc_vblank_end;
7269 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007270
7271 /* We need to be careful not to changed the adjusted mode, for otherwise
7272 * the hw state checker will get angry at the mismatch. */
7273 crtc_vtotal = adjusted_mode->crtc_vtotal;
7274 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007275
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007276 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007277 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007278 crtc_vtotal -= 1;
7279 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007280
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007281 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007282 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7283 else
7284 vsyncshift = adjusted_mode->crtc_hsync_start -
7285 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007286 if (vsyncshift < 0)
7287 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007288 }
7289
7290 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007291 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007292
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007293 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007294 (adjusted_mode->crtc_hdisplay - 1) |
7295 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007296 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007297 (adjusted_mode->crtc_hblank_start - 1) |
7298 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007299 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007300 (adjusted_mode->crtc_hsync_start - 1) |
7301 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7302
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007303 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007304 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007305 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007306 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007307 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007308 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007309 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007310 (adjusted_mode->crtc_vsync_start - 1) |
7311 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7312
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007313 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7314 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7315 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7316 * bits. */
7317 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7318 (pipe == PIPE_B || pipe == PIPE_C))
7319 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7320
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007321 /* pipesrc controls the size that is scaled from, which should
7322 * always be the user's requested size.
7323 */
7324 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007325 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7326 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007327}
7328
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007329static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007330 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007331{
7332 struct drm_device *dev = crtc->base.dev;
7333 struct drm_i915_private *dev_priv = dev->dev_private;
7334 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7335 uint32_t tmp;
7336
7337 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007338 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7339 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007340 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007341 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7342 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007343 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007344 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7345 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007346
7347 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007348 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7349 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007350 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007351 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7352 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007353 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007354 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7355 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007356
7357 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007358 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7359 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7360 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007361 }
7362
7363 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007364 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7365 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7366
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007367 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7368 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007369}
7370
Daniel Vetterf6a83282014-02-11 15:28:57 -08007371void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007372 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007373{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007374 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7375 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7376 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7377 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007378
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007379 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7380 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7381 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7382 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007383
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007384 mode->flags = pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007385
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007386 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7387 mode->flags |= pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007388}
7389
Daniel Vetter84b046f2013-02-19 18:48:54 +01007390static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7391{
7392 struct drm_device *dev = intel_crtc->base.dev;
7393 struct drm_i915_private *dev_priv = dev->dev_private;
7394 uint32_t pipeconf;
7395
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007396 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007397
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007398 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7399 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7400 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007401
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007402 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007403 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007404
Daniel Vetterff9ce462013-04-24 14:57:17 +02007405 /* only g4x and later have fancy bpc/dither controls */
7406 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007407 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007408 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007409 pipeconf |= PIPECONF_DITHER_EN |
7410 PIPECONF_DITHER_TYPE_SP;
7411
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007412 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007413 case 18:
7414 pipeconf |= PIPECONF_6BPC;
7415 break;
7416 case 24:
7417 pipeconf |= PIPECONF_8BPC;
7418 break;
7419 case 30:
7420 pipeconf |= PIPECONF_10BPC;
7421 break;
7422 default:
7423 /* Case prevented by intel_choose_pipe_bpp_dither. */
7424 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007425 }
7426 }
7427
7428 if (HAS_PIPE_CXSR(dev)) {
7429 if (intel_crtc->lowfreq_avail) {
7430 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7431 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7432 } else {
7433 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007434 }
7435 }
7436
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007437 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007438 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007439 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007440 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7441 else
7442 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7443 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007444 pipeconf |= PIPECONF_PROGRESSIVE;
7445
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007446 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007447 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007448
Daniel Vetter84b046f2013-02-19 18:48:54 +01007449 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7450 POSTING_READ(PIPECONF(intel_crtc->pipe));
7451}
7452
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007453static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7454 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007455{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007456 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007457 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc751ce42010-03-25 11:48:48 -07007458 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07007459 intel_clock_t clock, reduced_clock;
Daniel Vettera16af722013-04-30 14:01:44 +02007460 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007461 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01007462 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08007463 const intel_limit_t *limit;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007464 struct drm_atomic_state *state = crtc_state->base.state;
7465 struct drm_connector_state *connector_state;
7466 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -08007467
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007468 for (i = 0; i < state->num_connector; i++) {
7469 if (!state->connectors[i])
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02007470 continue;
7471
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007472 connector_state = state->connector_states[i];
7473 if (connector_state->crtc != &crtc->base)
7474 continue;
7475
7476 encoder = to_intel_encoder(connector_state->best_encoder);
7477
Chris Wilson5eddb702010-09-11 13:48:45 +01007478 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007479 case INTEL_OUTPUT_LVDS:
7480 is_lvds = true;
7481 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007482 case INTEL_OUTPUT_DSI:
7483 is_dsi = true;
7484 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007485 default:
7486 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007487 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05007488
Eric Anholtc751ce42010-03-25 11:48:48 -07007489 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08007490 }
7491
Jani Nikulaf2335332013-09-13 11:03:09 +03007492 if (is_dsi)
Daniel Vetter5b18e572014-04-24 23:55:06 +02007493 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007494
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007495 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007496 refclk = i9xx_get_refclk(crtc_state, num_connectors);
Jani Nikulaf2335332013-09-13 11:03:09 +03007497
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007498 /*
7499 * Returns a set of divisors for the desired target clock with
7500 * the given refclk, or FALSE. The returned values represent
7501 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7502 * 2) / p1 / p2.
7503 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007504 limit = intel_limit(crtc_state, refclk);
7505 ok = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007506 crtc_state->port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007507 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03007508 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007509 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7510 return -EINVAL;
7511 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007512
Jani Nikulaf2335332013-09-13 11:03:09 +03007513 if (is_lvds && dev_priv->lvds_downclock_avail) {
7514 /*
7515 * Ensure we match the reduced clock's P to the target
7516 * clock. If the clocks don't match, we can't switch
7517 * the display clock by using the FP0/FP1. In such case
7518 * we will disable the LVDS downclock feature.
7519 */
7520 has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007521 dev_priv->display.find_dpll(limit, crtc_state,
Jani Nikulaf2335332013-09-13 11:03:09 +03007522 dev_priv->lvds_downclock,
7523 refclk, &clock,
7524 &reduced_clock);
7525 }
7526 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007527 crtc_state->dpll.n = clock.n;
7528 crtc_state->dpll.m1 = clock.m1;
7529 crtc_state->dpll.m2 = clock.m2;
7530 crtc_state->dpll.p1 = clock.p1;
7531 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007532 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007533
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007534 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007535 i8xx_update_pll(crtc, crtc_state,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307536 has_reduced_clock ? &reduced_clock : NULL,
7537 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007538 } else if (IS_CHERRYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007539 chv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007540 } else if (IS_VALLEYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007541 vlv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007542 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007543 i9xx_update_pll(crtc, crtc_state,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007544 has_reduced_clock ? &reduced_clock : NULL,
Robin Schroereba905b2014-05-18 02:24:50 +02007545 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007546 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007547
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007548 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007549}
7550
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007551static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007552 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007553{
7554 struct drm_device *dev = crtc->base.dev;
7555 struct drm_i915_private *dev_priv = dev->dev_private;
7556 uint32_t tmp;
7557
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007558 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7559 return;
7560
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007561 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007562 if (!(tmp & PFIT_ENABLE))
7563 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007564
Daniel Vetter06922822013-07-11 13:35:40 +02007565 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007566 if (INTEL_INFO(dev)->gen < 4) {
7567 if (crtc->pipe != PIPE_B)
7568 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007569 } else {
7570 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7571 return;
7572 }
7573
Daniel Vetter06922822013-07-11 13:35:40 +02007574 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007575 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7576 if (INTEL_INFO(dev)->gen < 5)
7577 pipe_config->gmch_pfit.lvds_border_bits =
7578 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7579}
7580
Jesse Barnesacbec812013-09-20 11:29:32 -07007581static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007582 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07007583{
7584 struct drm_device *dev = crtc->base.dev;
7585 struct drm_i915_private *dev_priv = dev->dev_private;
7586 int pipe = pipe_config->cpu_transcoder;
7587 intel_clock_t clock;
7588 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07007589 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07007590
Shobhit Kumarf573de52014-07-30 20:32:37 +05307591 /* In case of MIPI DPLL will not even be used */
7592 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7593 return;
7594
Jesse Barnesacbec812013-09-20 11:29:32 -07007595 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007596 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Jesse Barnesacbec812013-09-20 11:29:32 -07007597 mutex_unlock(&dev_priv->dpio_lock);
7598
7599 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7600 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7601 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7602 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7603 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7604
Ville Syrjäläf6466282013-10-14 14:50:31 +03007605 vlv_clock(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007606
Ville Syrjäläf6466282013-10-14 14:50:31 +03007607 /* clock.dot is the fast clock */
7608 pipe_config->port_clock = clock.dot / 5;
Jesse Barnesacbec812013-09-20 11:29:32 -07007609}
7610
Damien Lespiau5724dbd2015-01-20 12:51:52 +00007611static void
7612i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7613 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007614{
7615 struct drm_device *dev = crtc->base.dev;
7616 struct drm_i915_private *dev_priv = dev->dev_private;
7617 u32 val, base, offset;
7618 int pipe = crtc->pipe, plane = crtc->plane;
7619 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00007620 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007621 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00007622 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007623
Damien Lespiau42a7b082015-02-05 19:35:13 +00007624 val = I915_READ(DSPCNTR(plane));
7625 if (!(val & DISPLAY_PLANE_ENABLE))
7626 return;
7627
Damien Lespiaud9806c92015-01-21 14:07:19 +00007628 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00007629 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007630 DRM_DEBUG_KMS("failed to alloc fb\n");
7631 return;
7632 }
7633
Damien Lespiau1b842c82015-01-21 13:50:54 +00007634 fb = &intel_fb->base;
7635
Daniel Vetter18c52472015-02-10 17:16:09 +00007636 if (INTEL_INFO(dev)->gen >= 4) {
7637 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007638 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00007639 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
7640 }
7641 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007642
7643 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00007644 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007645 fb->pixel_format = fourcc;
7646 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007647
7648 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007649 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007650 offset = I915_READ(DSPTILEOFF(plane));
7651 else
7652 offset = I915_READ(DSPLINOFF(plane));
7653 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7654 } else {
7655 base = I915_READ(DSPADDR(plane));
7656 }
7657 plane_config->base = base;
7658
7659 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007660 fb->width = ((val >> 16) & 0xfff) + 1;
7661 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007662
7663 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007664 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007665
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007666 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00007667 fb->pixel_format,
7668 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007669
Daniel Vetterf37b5c22015-02-10 23:12:27 +01007670 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007671
Damien Lespiau2844a922015-01-20 12:51:48 +00007672 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7673 pipe_name(pipe), plane, fb->width, fb->height,
7674 fb->bits_per_pixel, base, fb->pitches[0],
7675 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007676
Damien Lespiau2d140302015-02-05 17:22:18 +00007677 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007678}
7679
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007680static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007681 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007682{
7683 struct drm_device *dev = crtc->base.dev;
7684 struct drm_i915_private *dev_priv = dev->dev_private;
7685 int pipe = pipe_config->cpu_transcoder;
7686 enum dpio_channel port = vlv_pipe_to_channel(pipe);
7687 intel_clock_t clock;
7688 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
7689 int refclk = 100000;
7690
7691 mutex_lock(&dev_priv->dpio_lock);
7692 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
7693 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
7694 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
7695 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
7696 mutex_unlock(&dev_priv->dpio_lock);
7697
7698 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
7699 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
7700 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
7701 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
7702 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
7703
7704 chv_clock(refclk, &clock);
7705
7706 /* clock.dot is the fast clock */
7707 pipe_config->port_clock = clock.dot / 5;
7708}
7709
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007710static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007711 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007712{
7713 struct drm_device *dev = crtc->base.dev;
7714 struct drm_i915_private *dev_priv = dev->dev_private;
7715 uint32_t tmp;
7716
Daniel Vetterf458ebb2014-09-30 10:56:39 +02007717 if (!intel_display_power_is_enabled(dev_priv,
7718 POWER_DOMAIN_PIPE(crtc->pipe)))
Imre Deakb5482bd2014-03-05 16:20:55 +02007719 return false;
7720
Daniel Vettere143a212013-07-04 12:01:15 +02007721 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007722 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02007723
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007724 tmp = I915_READ(PIPECONF(crtc->pipe));
7725 if (!(tmp & PIPECONF_ENABLE))
7726 return false;
7727
Ville Syrjälä42571ae2013-09-06 23:29:00 +03007728 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
7729 switch (tmp & PIPECONF_BPC_MASK) {
7730 case PIPECONF_6BPC:
7731 pipe_config->pipe_bpp = 18;
7732 break;
7733 case PIPECONF_8BPC:
7734 pipe_config->pipe_bpp = 24;
7735 break;
7736 case PIPECONF_10BPC:
7737 pipe_config->pipe_bpp = 30;
7738 break;
7739 default:
7740 break;
7741 }
7742 }
7743
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02007744 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
7745 pipe_config->limited_color_range = true;
7746
Ville Syrjälä282740f2013-09-04 18:30:03 +03007747 if (INTEL_INFO(dev)->gen < 4)
7748 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
7749
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007750 intel_get_pipe_timings(crtc, pipe_config);
7751
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007752 i9xx_get_pfit_config(crtc, pipe_config);
7753
Daniel Vetter6c49f242013-06-06 12:45:25 +02007754 if (INTEL_INFO(dev)->gen >= 4) {
7755 tmp = I915_READ(DPLL_MD(crtc->pipe));
7756 pipe_config->pixel_multiplier =
7757 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
7758 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007759 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02007760 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
7761 tmp = I915_READ(DPLL(crtc->pipe));
7762 pipe_config->pixel_multiplier =
7763 ((tmp & SDVO_MULTIPLIER_MASK)
7764 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
7765 } else {
7766 /* Note that on i915G/GM the pixel multiplier is in the sdvo
7767 * port and will be fixed up in the encoder->get_config
7768 * function. */
7769 pipe_config->pixel_multiplier = 1;
7770 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007771 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
7772 if (!IS_VALLEYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03007773 /*
7774 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
7775 * on 830. Filter it out here so that we don't
7776 * report errors due to that.
7777 */
7778 if (IS_I830(dev))
7779 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
7780
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007781 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
7782 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03007783 } else {
7784 /* Mask out read-only status bits. */
7785 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
7786 DPLL_PORTC_READY_MASK |
7787 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007788 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02007789
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007790 if (IS_CHERRYVIEW(dev))
7791 chv_crtc_clock_get(crtc, pipe_config);
7792 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07007793 vlv_crtc_clock_get(crtc, pipe_config);
7794 else
7795 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03007796
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007797 return true;
7798}
7799
Paulo Zanonidde86e22012-12-01 12:04:25 -02007800static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07007801{
7802 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007803 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007804 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007805 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07007806 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07007807 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07007808 bool has_ck505 = false;
7809 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007810
7811 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01007812 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07007813 switch (encoder->type) {
7814 case INTEL_OUTPUT_LVDS:
7815 has_panel = true;
7816 has_lvds = true;
7817 break;
7818 case INTEL_OUTPUT_EDP:
7819 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03007820 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07007821 has_cpu_edp = true;
7822 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007823 default:
7824 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007825 }
7826 }
7827
Keith Packard99eb6a02011-09-26 14:29:12 -07007828 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007829 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07007830 can_ssc = has_ck505;
7831 } else {
7832 has_ck505 = false;
7833 can_ssc = true;
7834 }
7835
Imre Deak2de69052013-05-08 13:14:04 +03007836 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
7837 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007838
7839 /* Ironlake: try to setup display ref clock before DPLL
7840 * enabling. This is only under driver's control after
7841 * PCH B stepping, previous chipset stepping should be
7842 * ignoring this setting.
7843 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007844 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007845
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007846 /* As we must carefully and slowly disable/enable each source in turn,
7847 * compute the final state we want first and check if we need to
7848 * make any changes at all.
7849 */
7850 final = val;
7851 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07007852 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007853 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07007854 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007855 final |= DREF_NONSPREAD_SOURCE_ENABLE;
7856
7857 final &= ~DREF_SSC_SOURCE_MASK;
7858 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
7859 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007860
Keith Packard199e5d72011-09-22 12:01:57 -07007861 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007862 final |= DREF_SSC_SOURCE_ENABLE;
7863
7864 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7865 final |= DREF_SSC1_ENABLE;
7866
7867 if (has_cpu_edp) {
7868 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7869 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
7870 else
7871 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
7872 } else
7873 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
7874 } else {
7875 final |= DREF_SSC_SOURCE_DISABLE;
7876 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
7877 }
7878
7879 if (final == val)
7880 return;
7881
7882 /* Always enable nonspread source */
7883 val &= ~DREF_NONSPREAD_SOURCE_MASK;
7884
7885 if (has_ck505)
7886 val |= DREF_NONSPREAD_CK505_ENABLE;
7887 else
7888 val |= DREF_NONSPREAD_SOURCE_ENABLE;
7889
7890 if (has_panel) {
7891 val &= ~DREF_SSC_SOURCE_MASK;
7892 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007893
Keith Packard199e5d72011-09-22 12:01:57 -07007894 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07007895 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07007896 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007897 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02007898 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007899 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007900
7901 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007902 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007903 POSTING_READ(PCH_DREF_CONTROL);
7904 udelay(200);
7905
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007906 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007907
7908 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07007909 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07007910 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07007911 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007912 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02007913 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007914 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07007915 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007916 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007917
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007918 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007919 POSTING_READ(PCH_DREF_CONTROL);
7920 udelay(200);
7921 } else {
7922 DRM_DEBUG_KMS("Disabling SSC entirely\n");
7923
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007924 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07007925
7926 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007927 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007928
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007929 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007930 POSTING_READ(PCH_DREF_CONTROL);
7931 udelay(200);
7932
7933 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007934 val &= ~DREF_SSC_SOURCE_MASK;
7935 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007936
7937 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007938 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007939
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007940 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007941 POSTING_READ(PCH_DREF_CONTROL);
7942 udelay(200);
7943 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007944
7945 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007946}
7947
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007948static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02007949{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007950 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02007951
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007952 tmp = I915_READ(SOUTH_CHICKEN2);
7953 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
7954 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007955
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007956 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
7957 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
7958 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02007959
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007960 tmp = I915_READ(SOUTH_CHICKEN2);
7961 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
7962 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007963
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007964 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
7965 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
7966 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007967}
7968
7969/* WaMPhyProgramming:hsw */
7970static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
7971{
7972 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02007973
7974 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
7975 tmp &= ~(0xFF << 24);
7976 tmp |= (0x12 << 24);
7977 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
7978
Paulo Zanonidde86e22012-12-01 12:04:25 -02007979 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
7980 tmp |= (1 << 11);
7981 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
7982
7983 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
7984 tmp |= (1 << 11);
7985 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
7986
Paulo Zanonidde86e22012-12-01 12:04:25 -02007987 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
7988 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7989 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
7990
7991 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
7992 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7993 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
7994
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007995 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
7996 tmp &= ~(7 << 13);
7997 tmp |= (5 << 13);
7998 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007999
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008000 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8001 tmp &= ~(7 << 13);
8002 tmp |= (5 << 13);
8003 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008004
8005 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8006 tmp &= ~0xFF;
8007 tmp |= 0x1C;
8008 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8009
8010 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8011 tmp &= ~0xFF;
8012 tmp |= 0x1C;
8013 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8014
8015 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8016 tmp &= ~(0xFF << 16);
8017 tmp |= (0x1C << 16);
8018 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8019
8020 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8021 tmp &= ~(0xFF << 16);
8022 tmp |= (0x1C << 16);
8023 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8024
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008025 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8026 tmp |= (1 << 27);
8027 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008028
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008029 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8030 tmp |= (1 << 27);
8031 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008032
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008033 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8034 tmp &= ~(0xF << 28);
8035 tmp |= (4 << 28);
8036 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008037
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008038 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8039 tmp &= ~(0xF << 28);
8040 tmp |= (4 << 28);
8041 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008042}
8043
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008044/* Implements 3 different sequences from BSpec chapter "Display iCLK
8045 * Programming" based on the parameters passed:
8046 * - Sequence to enable CLKOUT_DP
8047 * - Sequence to enable CLKOUT_DP without spread
8048 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8049 */
8050static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8051 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008052{
8053 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008054 uint32_t reg, tmp;
8055
8056 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8057 with_spread = true;
8058 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
8059 with_fdi, "LP PCH doesn't have FDI\n"))
8060 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008061
8062 mutex_lock(&dev_priv->dpio_lock);
8063
8064 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8065 tmp &= ~SBI_SSCCTL_DISABLE;
8066 tmp |= SBI_SSCCTL_PATHALT;
8067 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8068
8069 udelay(24);
8070
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008071 if (with_spread) {
8072 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8073 tmp &= ~SBI_SSCCTL_PATHALT;
8074 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008075
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008076 if (with_fdi) {
8077 lpt_reset_fdi_mphy(dev_priv);
8078 lpt_program_fdi_mphy(dev_priv);
8079 }
8080 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008081
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008082 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8083 SBI_GEN0 : SBI_DBUFF0;
8084 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8085 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8086 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008087
8088 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008089}
8090
Paulo Zanoni47701c32013-07-23 11:19:25 -03008091/* Sequence to disable CLKOUT_DP */
8092static void lpt_disable_clkout_dp(struct drm_device *dev)
8093{
8094 struct drm_i915_private *dev_priv = dev->dev_private;
8095 uint32_t reg, tmp;
8096
8097 mutex_lock(&dev_priv->dpio_lock);
8098
8099 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8100 SBI_GEN0 : SBI_DBUFF0;
8101 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8102 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8103 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8104
8105 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8106 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8107 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8108 tmp |= SBI_SSCCTL_PATHALT;
8109 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8110 udelay(32);
8111 }
8112 tmp |= SBI_SSCCTL_DISABLE;
8113 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8114 }
8115
8116 mutex_unlock(&dev_priv->dpio_lock);
8117}
8118
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008119static void lpt_init_pch_refclk(struct drm_device *dev)
8120{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008121 struct intel_encoder *encoder;
8122 bool has_vga = false;
8123
Damien Lespiaub2784e12014-08-05 11:29:37 +01008124 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008125 switch (encoder->type) {
8126 case INTEL_OUTPUT_ANALOG:
8127 has_vga = true;
8128 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008129 default:
8130 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008131 }
8132 }
8133
Paulo Zanoni47701c32013-07-23 11:19:25 -03008134 if (has_vga)
8135 lpt_enable_clkout_dp(dev, true, true);
8136 else
8137 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008138}
8139
Paulo Zanonidde86e22012-12-01 12:04:25 -02008140/*
8141 * Initialize reference clocks when the driver loads
8142 */
8143void intel_init_pch_refclk(struct drm_device *dev)
8144{
8145 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8146 ironlake_init_pch_refclk(dev);
8147 else if (HAS_PCH_LPT(dev))
8148 lpt_init_pch_refclk(dev);
8149}
8150
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008151static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008152{
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008153 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008154 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008155 struct drm_atomic_state *state = crtc_state->base.state;
8156 struct drm_connector_state *connector_state;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008157 struct intel_encoder *encoder;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008158 int num_connectors = 0, i;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008159 bool is_lvds = false;
8160
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008161 for (i = 0; i < state->num_connector; i++) {
8162 if (!state->connectors[i])
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02008163 continue;
8164
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008165 connector_state = state->connector_states[i];
8166 if (connector_state->crtc != crtc_state->base.crtc)
8167 continue;
8168
8169 encoder = to_intel_encoder(connector_state->best_encoder);
8170
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008171 switch (encoder->type) {
8172 case INTEL_OUTPUT_LVDS:
8173 is_lvds = true;
8174 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008175 default:
8176 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008177 }
8178 num_connectors++;
8179 }
8180
8181 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008182 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008183 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008184 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008185 }
8186
8187 return 120000;
8188}
8189
Daniel Vetter6ff93602013-04-19 11:24:36 +02008190static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008191{
8192 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8193 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8194 int pipe = intel_crtc->pipe;
8195 uint32_t val;
8196
Daniel Vetter78114072013-06-13 00:54:57 +02008197 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008198
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008199 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008200 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008201 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008202 break;
8203 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008204 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008205 break;
8206 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008207 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008208 break;
8209 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008210 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008211 break;
8212 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008213 /* Case prevented by intel_choose_pipe_bpp_dither. */
8214 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008215 }
8216
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008217 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008218 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8219
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008220 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008221 val |= PIPECONF_INTERLACED_ILK;
8222 else
8223 val |= PIPECONF_PROGRESSIVE;
8224
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008225 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008226 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008227
Paulo Zanonic8203562012-09-12 10:06:29 -03008228 I915_WRITE(PIPECONF(pipe), val);
8229 POSTING_READ(PIPECONF(pipe));
8230}
8231
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008232/*
8233 * Set up the pipe CSC unit.
8234 *
8235 * Currently only full range RGB to limited range RGB conversion
8236 * is supported, but eventually this should handle various
8237 * RGB<->YCbCr scenarios as well.
8238 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01008239static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008240{
8241 struct drm_device *dev = crtc->dev;
8242 struct drm_i915_private *dev_priv = dev->dev_private;
8243 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8244 int pipe = intel_crtc->pipe;
8245 uint16_t coeff = 0x7800; /* 1.0 */
8246
8247 /*
8248 * TODO: Check what kind of values actually come out of the pipe
8249 * with these coeff/postoff values and adjust to get the best
8250 * accuracy. Perhaps we even need to take the bpc value into
8251 * consideration.
8252 */
8253
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008254 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008255 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8256
8257 /*
8258 * GY/GU and RY/RU should be the other way around according
8259 * to BSpec, but reality doesn't agree. Just set them up in
8260 * a way that results in the correct picture.
8261 */
8262 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8263 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8264
8265 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8266 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8267
8268 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8269 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8270
8271 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8272 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8273 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8274
8275 if (INTEL_INFO(dev)->gen > 6) {
8276 uint16_t postoff = 0;
8277
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008278 if (intel_crtc->config->limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02008279 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008280
8281 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8282 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8283 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8284
8285 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8286 } else {
8287 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8288
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008289 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008290 mode |= CSC_BLACK_SCREEN_OFFSET;
8291
8292 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8293 }
8294}
8295
Daniel Vetter6ff93602013-04-19 11:24:36 +02008296static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008297{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008298 struct drm_device *dev = crtc->dev;
8299 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008300 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008301 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008302 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008303 uint32_t val;
8304
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008305 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008306
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008307 if (IS_HASWELL(dev) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008308 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8309
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008310 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008311 val |= PIPECONF_INTERLACED_ILK;
8312 else
8313 val |= PIPECONF_PROGRESSIVE;
8314
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008315 I915_WRITE(PIPECONF(cpu_transcoder), val);
8316 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008317
8318 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8319 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008320
Satheeshakrishna M3cdf1222014-04-08 15:46:53 +05308321 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008322 val = 0;
8323
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008324 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008325 case 18:
8326 val |= PIPEMISC_DITHER_6_BPC;
8327 break;
8328 case 24:
8329 val |= PIPEMISC_DITHER_8_BPC;
8330 break;
8331 case 30:
8332 val |= PIPEMISC_DITHER_10_BPC;
8333 break;
8334 case 36:
8335 val |= PIPEMISC_DITHER_12_BPC;
8336 break;
8337 default:
8338 /* Case prevented by pipe_config_set_bpp. */
8339 BUG();
8340 }
8341
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008342 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008343 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8344
8345 I915_WRITE(PIPEMISC(pipe), val);
8346 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008347}
8348
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008349static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008350 struct intel_crtc_state *crtc_state,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008351 intel_clock_t *clock,
8352 bool *has_reduced_clock,
8353 intel_clock_t *reduced_clock)
8354{
8355 struct drm_device *dev = crtc->dev;
8356 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008357 int refclk;
8358 const intel_limit_t *limit;
Daniel Vettera16af722013-04-30 14:01:44 +02008359 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008360
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008361 is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008362
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008363 refclk = ironlake_get_refclk(crtc_state);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008364
8365 /*
8366 * Returns a set of divisors for the desired target clock with the given
8367 * refclk, or FALSE. The returned values represent the clock equation:
8368 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8369 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008370 limit = intel_limit(crtc_state, refclk);
8371 ret = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008372 crtc_state->port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008373 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008374 if (!ret)
8375 return false;
8376
8377 if (is_lvds && dev_priv->lvds_downclock_avail) {
8378 /*
8379 * Ensure we match the reduced clock's P to the target clock.
8380 * If the clocks don't match, we can't switch the display clock
8381 * by using the FP0/FP1. In such case we will disable the LVDS
8382 * downclock feature.
8383 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02008384 *has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008385 dev_priv->display.find_dpll(limit, crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008386 dev_priv->lvds_downclock,
8387 refclk, clock,
8388 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008389 }
8390
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008391 return true;
8392}
8393
Paulo Zanonid4b19312012-11-29 11:29:32 -02008394int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8395{
8396 /*
8397 * Account for spread spectrum to avoid
8398 * oversubscribing the link. Max center spread
8399 * is 2.5%; use 5% for safety's sake.
8400 */
8401 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008402 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008403}
8404
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008405static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008406{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008407 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008408}
8409
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008410static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008411 struct intel_crtc_state *crtc_state,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008412 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008413 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008414{
8415 struct drm_crtc *crtc = &intel_crtc->base;
8416 struct drm_device *dev = crtc->dev;
8417 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008418 struct drm_atomic_state *state = crtc_state->base.state;
8419 struct drm_connector_state *connector_state;
8420 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008421 uint32_t dpll;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008422 int factor, num_connectors = 0, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008423 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008424
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008425 for (i = 0; i < state->num_connector; i++) {
8426 if (!state->connectors[i])
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02008427 continue;
8428
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008429 connector_state = state->connector_states[i];
8430 if (connector_state->crtc != crtc_state->base.crtc)
8431 continue;
8432
8433 encoder = to_intel_encoder(connector_state->best_encoder);
8434
8435 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008436 case INTEL_OUTPUT_LVDS:
8437 is_lvds = true;
8438 break;
8439 case INTEL_OUTPUT_SDVO:
8440 case INTEL_OUTPUT_HDMI:
8441 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008442 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008443 default:
8444 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008445 }
8446
8447 num_connectors++;
8448 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008449
Chris Wilsonc1858122010-12-03 21:35:48 +00008450 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008451 factor = 21;
8452 if (is_lvds) {
8453 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008454 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008455 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008456 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008457 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008458 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008459
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008460 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02008461 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00008462
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008463 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8464 *fp2 |= FP_CB_TUNE;
8465
Chris Wilson5eddb702010-09-11 13:48:45 +01008466 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008467
Eric Anholta07d6782011-03-30 13:01:08 -07008468 if (is_lvds)
8469 dpll |= DPLLB_MODE_LVDS;
8470 else
8471 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008472
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008473 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008474 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008475
8476 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008477 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008478 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008479 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008480
Eric Anholta07d6782011-03-30 13:01:08 -07008481 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008482 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008483 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008484 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008485
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008486 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008487 case 5:
8488 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8489 break;
8490 case 7:
8491 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8492 break;
8493 case 10:
8494 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8495 break;
8496 case 14:
8497 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8498 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008499 }
8500
Daniel Vetterb4c09f32013-04-30 14:01:42 +02008501 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008502 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008503 else
8504 dpll |= PLL_REF_INPUT_DREFCLK;
8505
Daniel Vetter959e16d2013-06-05 13:34:21 +02008506 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008507}
8508
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008509static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8510 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008511{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008512 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008513 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008514 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03008515 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01008516 bool is_lvds = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008517 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008518
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03008519 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
Jesse Barnes79e53942008-11-07 14:24:08 -08008520
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008521 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8522 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
8523
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008524 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008525 &has_reduced_clock, &reduced_clock);
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008526 if (!ok && !crtc_state->clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008527 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8528 return -EINVAL;
8529 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01008530 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008531 if (!crtc_state->clock_set) {
8532 crtc_state->dpll.n = clock.n;
8533 crtc_state->dpll.m1 = clock.m1;
8534 crtc_state->dpll.m2 = clock.m2;
8535 crtc_state->dpll.p1 = clock.p1;
8536 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008537 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008538
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008539 /* 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 +02008540 if (crtc_state->has_pch_encoder) {
8541 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008542 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008543 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008544
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008545 dpll = ironlake_compute_dpll(crtc, crtc_state,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008546 &fp, &reduced_clock,
8547 has_reduced_clock ? &fp2 : NULL);
8548
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008549 crtc_state->dpll_hw_state.dpll = dpll;
8550 crtc_state->dpll_hw_state.fp0 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008551 if (has_reduced_clock)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008552 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008553 else
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008554 crtc_state->dpll_hw_state.fp1 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008555
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008556 pll = intel_get_shared_dpll(crtc, crtc_state);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008557 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03008558 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008559 pipe_name(crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07008560 return -EINVAL;
8561 }
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008562 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008563
Rodrigo Viviab585de2015-03-24 12:40:09 -07008564 if (is_lvds && has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008565 crtc->lowfreq_avail = true;
Daniel Vetterbcd644e2013-06-05 13:34:22 +02008566 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008567 crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008568
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008569 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008570}
8571
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008572static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8573 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008574{
8575 struct drm_device *dev = crtc->base.dev;
8576 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008577 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008578
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008579 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8580 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8581 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8582 & ~TU_SIZE_MASK;
8583 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8584 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8585 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8586}
8587
8588static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8589 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008590 struct intel_link_m_n *m_n,
8591 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008592{
8593 struct drm_device *dev = crtc->base.dev;
8594 struct drm_i915_private *dev_priv = dev->dev_private;
8595 enum pipe pipe = crtc->pipe;
8596
8597 if (INTEL_INFO(dev)->gen >= 5) {
8598 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8599 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8600 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8601 & ~TU_SIZE_MASK;
8602 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8603 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8604 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008605 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8606 * gen < 8) and if DRRS is supported (to make sure the
8607 * registers are not unnecessarily read).
8608 */
8609 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008610 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008611 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8612 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8613 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8614 & ~TU_SIZE_MASK;
8615 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8616 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8617 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8618 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008619 } else {
8620 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8621 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8622 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8623 & ~TU_SIZE_MASK;
8624 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8625 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8626 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8627 }
8628}
8629
8630void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008631 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008632{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02008633 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008634 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8635 else
8636 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008637 &pipe_config->dp_m_n,
8638 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008639}
8640
Daniel Vetter72419202013-04-04 13:28:53 +02008641static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008642 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02008643{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008644 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008645 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02008646}
8647
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008648static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008649 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008650{
8651 struct drm_device *dev = crtc->base.dev;
8652 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07008653 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8654 uint32_t ps_ctrl = 0;
8655 int id = -1;
8656 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008657
Chandra Kondurua1b22782015-04-07 15:28:45 -07008658 /* find scaler attached to this pipe */
8659 for (i = 0; i < crtc->num_scalers; i++) {
8660 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8661 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8662 id = i;
8663 pipe_config->pch_pfit.enabled = true;
8664 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8665 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8666 break;
8667 }
8668 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008669
Chandra Kondurua1b22782015-04-07 15:28:45 -07008670 scaler_state->scaler_id = id;
8671 if (id >= 0) {
8672 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8673 } else {
8674 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008675 }
8676}
8677
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008678static void
8679skylake_get_initial_plane_config(struct intel_crtc *crtc,
8680 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008681{
8682 struct drm_device *dev = crtc->base.dev;
8683 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00008684 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008685 int pipe = crtc->pipe;
8686 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008687 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008688 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008689 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008690
Damien Lespiaud9806c92015-01-21 14:07:19 +00008691 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008692 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008693 DRM_DEBUG_KMS("failed to alloc fb\n");
8694 return;
8695 }
8696
Damien Lespiau1b842c82015-01-21 13:50:54 +00008697 fb = &intel_fb->base;
8698
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008699 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00008700 if (!(val & PLANE_CTL_ENABLE))
8701 goto error;
8702
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008703 pixel_format = val & PLANE_CTL_FORMAT_MASK;
8704 fourcc = skl_format_to_fourcc(pixel_format,
8705 val & PLANE_CTL_ORDER_RGBX,
8706 val & PLANE_CTL_ALPHA_MASK);
8707 fb->pixel_format = fourcc;
8708 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
8709
Damien Lespiau40f46282015-02-27 11:15:21 +00008710 tiling = val & PLANE_CTL_TILED_MASK;
8711 switch (tiling) {
8712 case PLANE_CTL_TILED_LINEAR:
8713 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
8714 break;
8715 case PLANE_CTL_TILED_X:
8716 plane_config->tiling = I915_TILING_X;
8717 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8718 break;
8719 case PLANE_CTL_TILED_Y:
8720 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
8721 break;
8722 case PLANE_CTL_TILED_YF:
8723 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
8724 break;
8725 default:
8726 MISSING_CASE(tiling);
8727 goto error;
8728 }
8729
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008730 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
8731 plane_config->base = base;
8732
8733 offset = I915_READ(PLANE_OFFSET(pipe, 0));
8734
8735 val = I915_READ(PLANE_SIZE(pipe, 0));
8736 fb->height = ((val >> 16) & 0xfff) + 1;
8737 fb->width = ((val >> 0) & 0x1fff) + 1;
8738
8739 val = I915_READ(PLANE_STRIDE(pipe, 0));
Damien Lespiau40f46282015-02-27 11:15:21 +00008740 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
8741 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008742 fb->pitches[0] = (val & 0x3ff) * stride_mult;
8743
8744 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008745 fb->pixel_format,
8746 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008747
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008748 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008749
8750 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8751 pipe_name(pipe), fb->width, fb->height,
8752 fb->bits_per_pixel, base, fb->pitches[0],
8753 plane_config->size);
8754
Damien Lespiau2d140302015-02-05 17:22:18 +00008755 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008756 return;
8757
8758error:
8759 kfree(fb);
8760}
8761
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008762static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008763 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008764{
8765 struct drm_device *dev = crtc->base.dev;
8766 struct drm_i915_private *dev_priv = dev->dev_private;
8767 uint32_t tmp;
8768
8769 tmp = I915_READ(PF_CTL(crtc->pipe));
8770
8771 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008772 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008773 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
8774 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02008775
8776 /* We currently do not free assignements of panel fitters on
8777 * ivb/hsw (since we don't use the higher upscaling modes which
8778 * differentiates them) so just WARN about this case for now. */
8779 if (IS_GEN7(dev)) {
8780 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
8781 PF_PIPE_SEL_IVB(crtc->pipe));
8782 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008783 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008784}
8785
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008786static void
8787ironlake_get_initial_plane_config(struct intel_crtc *crtc,
8788 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008789{
8790 struct drm_device *dev = crtc->base.dev;
8791 struct drm_i915_private *dev_priv = dev->dev_private;
8792 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008793 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008794 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008795 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008796 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008797 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008798
Damien Lespiau42a7b082015-02-05 19:35:13 +00008799 val = I915_READ(DSPCNTR(pipe));
8800 if (!(val & DISPLAY_PLANE_ENABLE))
8801 return;
8802
Damien Lespiaud9806c92015-01-21 14:07:19 +00008803 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008804 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008805 DRM_DEBUG_KMS("failed to alloc fb\n");
8806 return;
8807 }
8808
Damien Lespiau1b842c82015-01-21 13:50:54 +00008809 fb = &intel_fb->base;
8810
Daniel Vetter18c52472015-02-10 17:16:09 +00008811 if (INTEL_INFO(dev)->gen >= 4) {
8812 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008813 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008814 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8815 }
8816 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008817
8818 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008819 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008820 fb->pixel_format = fourcc;
8821 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008822
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008823 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008824 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008825 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008826 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00008827 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008828 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008829 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008830 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008831 }
8832 plane_config->base = base;
8833
8834 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008835 fb->width = ((val >> 16) & 0xfff) + 1;
8836 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008837
8838 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008839 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008840
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008841 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008842 fb->pixel_format,
8843 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008844
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008845 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008846
Damien Lespiau2844a922015-01-20 12:51:48 +00008847 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8848 pipe_name(pipe), fb->width, fb->height,
8849 fb->bits_per_pixel, base, fb->pitches[0],
8850 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008851
Damien Lespiau2d140302015-02-05 17:22:18 +00008852 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008853}
8854
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008855static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008856 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008857{
8858 struct drm_device *dev = crtc->base.dev;
8859 struct drm_i915_private *dev_priv = dev->dev_private;
8860 uint32_t tmp;
8861
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008862 if (!intel_display_power_is_enabled(dev_priv,
8863 POWER_DOMAIN_PIPE(crtc->pipe)))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03008864 return false;
8865
Daniel Vettere143a212013-07-04 12:01:15 +02008866 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008867 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02008868
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008869 tmp = I915_READ(PIPECONF(crtc->pipe));
8870 if (!(tmp & PIPECONF_ENABLE))
8871 return false;
8872
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008873 switch (tmp & PIPECONF_BPC_MASK) {
8874 case PIPECONF_6BPC:
8875 pipe_config->pipe_bpp = 18;
8876 break;
8877 case PIPECONF_8BPC:
8878 pipe_config->pipe_bpp = 24;
8879 break;
8880 case PIPECONF_10BPC:
8881 pipe_config->pipe_bpp = 30;
8882 break;
8883 case PIPECONF_12BPC:
8884 pipe_config->pipe_bpp = 36;
8885 break;
8886 default:
8887 break;
8888 }
8889
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008890 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
8891 pipe_config->limited_color_range = true;
8892
Daniel Vetterab9412b2013-05-03 11:49:46 +02008893 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02008894 struct intel_shared_dpll *pll;
8895
Daniel Vetter88adfff2013-03-28 10:42:01 +01008896 pipe_config->has_pch_encoder = true;
8897
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008898 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
8899 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8900 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02008901
8902 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008903
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008904 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02008905 pipe_config->shared_dpll =
8906 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008907 } else {
8908 tmp = I915_READ(PCH_DPLL_SEL);
8909 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8910 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
8911 else
8912 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
8913 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02008914
8915 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
8916
8917 WARN_ON(!pll->get_hw_state(dev_priv, pll,
8918 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02008919
8920 tmp = pipe_config->dpll_hw_state.dpll;
8921 pipe_config->pixel_multiplier =
8922 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
8923 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03008924
8925 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008926 } else {
8927 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008928 }
8929
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008930 intel_get_pipe_timings(crtc, pipe_config);
8931
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008932 ironlake_get_pfit_config(crtc, pipe_config);
8933
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008934 return true;
8935}
8936
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008937static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
8938{
8939 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008940 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008941
Damien Lespiaud3fcc802014-05-13 23:32:22 +01008942 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05008943 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008944 pipe_name(crtc->pipe));
8945
Rob Clarke2c719b2014-12-15 13:56:32 -05008946 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
8947 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
8948 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
8949 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
8950 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
8951 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008952 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03008953 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05008954 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03008955 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008956 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008957 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008958 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008959 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008960 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008961
Paulo Zanoni9926ada2014-04-01 19:39:47 -03008962 /*
8963 * In theory we can still leave IRQs enabled, as long as only the HPD
8964 * interrupts remain enabled. We used to check for that, but since it's
8965 * gen-specific and since we only disable LCPLL after we fully disable
8966 * the interrupts, the check below should be enough.
8967 */
Rob Clarke2c719b2014-12-15 13:56:32 -05008968 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008969}
8970
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008971static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
8972{
8973 struct drm_device *dev = dev_priv->dev;
8974
8975 if (IS_HASWELL(dev))
8976 return I915_READ(D_COMP_HSW);
8977 else
8978 return I915_READ(D_COMP_BDW);
8979}
8980
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008981static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
8982{
8983 struct drm_device *dev = dev_priv->dev;
8984
8985 if (IS_HASWELL(dev)) {
8986 mutex_lock(&dev_priv->rps.hw_lock);
8987 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
8988 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03008989 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008990 mutex_unlock(&dev_priv->rps.hw_lock);
8991 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008992 I915_WRITE(D_COMP_BDW, val);
8993 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008994 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008995}
8996
8997/*
8998 * This function implements pieces of two sequences from BSpec:
8999 * - Sequence for display software to disable LCPLL
9000 * - Sequence for display software to allow package C8+
9001 * The steps implemented here are just the steps that actually touch the LCPLL
9002 * register. Callers should take care of disabling all the display engine
9003 * functions, doing the mode unset, fixing interrupts, etc.
9004 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009005static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9006 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009007{
9008 uint32_t val;
9009
9010 assert_can_disable_lcpll(dev_priv);
9011
9012 val = I915_READ(LCPLL_CTL);
9013
9014 if (switch_to_fclk) {
9015 val |= LCPLL_CD_SOURCE_FCLK;
9016 I915_WRITE(LCPLL_CTL, val);
9017
9018 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9019 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9020 DRM_ERROR("Switching to FCLK failed\n");
9021
9022 val = I915_READ(LCPLL_CTL);
9023 }
9024
9025 val |= LCPLL_PLL_DISABLE;
9026 I915_WRITE(LCPLL_CTL, val);
9027 POSTING_READ(LCPLL_CTL);
9028
9029 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9030 DRM_ERROR("LCPLL still locked\n");
9031
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009032 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009033 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009034 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009035 ndelay(100);
9036
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009037 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9038 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009039 DRM_ERROR("D_COMP RCOMP still in progress\n");
9040
9041 if (allow_power_down) {
9042 val = I915_READ(LCPLL_CTL);
9043 val |= LCPLL_POWER_DOWN_ALLOW;
9044 I915_WRITE(LCPLL_CTL, val);
9045 POSTING_READ(LCPLL_CTL);
9046 }
9047}
9048
9049/*
9050 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9051 * source.
9052 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009053static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009054{
9055 uint32_t val;
9056
9057 val = I915_READ(LCPLL_CTL);
9058
9059 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9060 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9061 return;
9062
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009063 /*
9064 * Make sure we're not on PC8 state before disabling PC8, otherwise
9065 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009066 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009067 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009068
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009069 if (val & LCPLL_POWER_DOWN_ALLOW) {
9070 val &= ~LCPLL_POWER_DOWN_ALLOW;
9071 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009072 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009073 }
9074
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009075 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009076 val |= D_COMP_COMP_FORCE;
9077 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009078 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009079
9080 val = I915_READ(LCPLL_CTL);
9081 val &= ~LCPLL_PLL_DISABLE;
9082 I915_WRITE(LCPLL_CTL, val);
9083
9084 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9085 DRM_ERROR("LCPLL not locked yet\n");
9086
9087 if (val & LCPLL_CD_SOURCE_FCLK) {
9088 val = I915_READ(LCPLL_CTL);
9089 val &= ~LCPLL_CD_SOURCE_FCLK;
9090 I915_WRITE(LCPLL_CTL, val);
9091
9092 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9093 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9094 DRM_ERROR("Switching back to LCPLL failed\n");
9095 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009096
Mika Kuoppala59bad942015-01-16 11:34:40 +02009097 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009098}
9099
Paulo Zanoni765dab672014-03-07 20:08:18 -03009100/*
9101 * Package states C8 and deeper are really deep PC states that can only be
9102 * reached when all the devices on the system allow it, so even if the graphics
9103 * device allows PC8+, it doesn't mean the system will actually get to these
9104 * states. Our driver only allows PC8+ when going into runtime PM.
9105 *
9106 * The requirements for PC8+ are that all the outputs are disabled, the power
9107 * well is disabled and most interrupts are disabled, and these are also
9108 * requirements for runtime PM. When these conditions are met, we manually do
9109 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9110 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9111 * hang the machine.
9112 *
9113 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9114 * the state of some registers, so when we come back from PC8+ we need to
9115 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9116 * need to take care of the registers kept by RC6. Notice that this happens even
9117 * if we don't put the device in PCI D3 state (which is what currently happens
9118 * because of the runtime PM support).
9119 *
9120 * For more, read "Display Sequences for Package C8" on the hardware
9121 * documentation.
9122 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009123void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009124{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009125 struct drm_device *dev = dev_priv->dev;
9126 uint32_t val;
9127
Paulo Zanonic67a4702013-08-19 13:18:09 -03009128 DRM_DEBUG_KMS("Enabling package C8+\n");
9129
Paulo Zanonic67a4702013-08-19 13:18:09 -03009130 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9131 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9132 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9133 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9134 }
9135
9136 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009137 hsw_disable_lcpll(dev_priv, true, true);
9138}
9139
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009140void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009141{
9142 struct drm_device *dev = dev_priv->dev;
9143 uint32_t val;
9144
Paulo Zanonic67a4702013-08-19 13:18:09 -03009145 DRM_DEBUG_KMS("Disabling package C8+\n");
9146
9147 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009148 lpt_init_pch_refclk(dev);
9149
9150 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9151 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9152 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9153 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9154 }
9155
9156 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009157}
9158
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309159static void broxton_modeset_global_resources(struct drm_atomic_state *state)
9160{
9161 struct drm_device *dev = state->dev;
9162 struct drm_i915_private *dev_priv = dev->dev_private;
9163 int max_pixclk = intel_mode_max_pixclk(state);
9164 int req_cdclk;
9165
9166 /* see the comment in valleyview_modeset_global_resources */
9167 if (WARN_ON(max_pixclk < 0))
9168 return;
9169
9170 req_cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
9171
9172 if (req_cdclk != dev_priv->cdclk_freq)
9173 broxton_set_cdclk(dev, req_cdclk);
9174}
9175
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009176static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9177 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009178{
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009179 if (!intel_ddi_pll_select(crtc, crtc_state))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009180 return -EINVAL;
Daniel Vetter716c2e52014-06-25 22:02:02 +03009181
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009182 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009183
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009184 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009185}
9186
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309187static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9188 enum port port,
9189 struct intel_crtc_state *pipe_config)
9190{
9191 switch (port) {
9192 case PORT_A:
9193 pipe_config->ddi_pll_sel = SKL_DPLL0;
9194 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9195 break;
9196 case PORT_B:
9197 pipe_config->ddi_pll_sel = SKL_DPLL1;
9198 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9199 break;
9200 case PORT_C:
9201 pipe_config->ddi_pll_sel = SKL_DPLL2;
9202 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9203 break;
9204 default:
9205 DRM_ERROR("Incorrect port type\n");
9206 }
9207}
9208
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009209static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9210 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009211 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009212{
Damien Lespiau3148ade2014-11-21 16:14:56 +00009213 u32 temp, dpll_ctl1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009214
9215 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9216 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9217
9218 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009219 case SKL_DPLL0:
9220 /*
9221 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9222 * of the shared DPLL framework and thus needs to be read out
9223 * separately
9224 */
9225 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9226 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9227 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009228 case SKL_DPLL1:
9229 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9230 break;
9231 case SKL_DPLL2:
9232 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9233 break;
9234 case SKL_DPLL3:
9235 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9236 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009237 }
9238}
9239
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009240static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9241 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009242 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009243{
9244 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9245
9246 switch (pipe_config->ddi_pll_sel) {
9247 case PORT_CLK_SEL_WRPLL1:
9248 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9249 break;
9250 case PORT_CLK_SEL_WRPLL2:
9251 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9252 break;
9253 }
9254}
9255
Daniel Vetter26804af2014-06-25 22:01:55 +03009256static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009257 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009258{
9259 struct drm_device *dev = crtc->base.dev;
9260 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009261 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009262 enum port port;
9263 uint32_t tmp;
9264
9265 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9266
9267 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9268
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009269 if (IS_SKYLAKE(dev))
9270 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309271 else if (IS_BROXTON(dev))
9272 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009273 else
9274 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009275
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009276 if (pipe_config->shared_dpll >= 0) {
9277 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9278
9279 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9280 &pipe_config->dpll_hw_state));
9281 }
9282
Daniel Vetter26804af2014-06-25 22:01:55 +03009283 /*
9284 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9285 * DDI E. So just check whether this pipe is wired to DDI E and whether
9286 * the PCH transcoder is on.
9287 */
Damien Lespiauca370452013-12-03 13:56:24 +00009288 if (INTEL_INFO(dev)->gen < 9 &&
9289 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009290 pipe_config->has_pch_encoder = true;
9291
9292 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9293 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9294 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9295
9296 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9297 }
9298}
9299
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009300static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009301 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009302{
9303 struct drm_device *dev = crtc->base.dev;
9304 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009305 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009306 uint32_t tmp;
9307
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009308 if (!intel_display_power_is_enabled(dev_priv,
Imre Deakb5482bd2014-03-05 16:20:55 +02009309 POWER_DOMAIN_PIPE(crtc->pipe)))
9310 return false;
9311
Daniel Vettere143a212013-07-04 12:01:15 +02009312 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009313 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9314
Daniel Vettereccb1402013-05-22 00:50:22 +02009315 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9316 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9317 enum pipe trans_edp_pipe;
9318 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9319 default:
9320 WARN(1, "unknown pipe linked to edp transcoder\n");
9321 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9322 case TRANS_DDI_EDP_INPUT_A_ON:
9323 trans_edp_pipe = PIPE_A;
9324 break;
9325 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9326 trans_edp_pipe = PIPE_B;
9327 break;
9328 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9329 trans_edp_pipe = PIPE_C;
9330 break;
9331 }
9332
9333 if (trans_edp_pipe == crtc->pipe)
9334 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9335 }
9336
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009337 if (!intel_display_power_is_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02009338 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03009339 return false;
9340
Daniel Vettereccb1402013-05-22 00:50:22 +02009341 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009342 if (!(tmp & PIPECONF_ENABLE))
9343 return false;
9344
Daniel Vetter26804af2014-06-25 22:01:55 +03009345 haswell_get_ddi_port_state(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009346
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009347 intel_get_pipe_timings(crtc, pipe_config);
9348
Chandra Kondurua1b22782015-04-07 15:28:45 -07009349 if (INTEL_INFO(dev)->gen >= 9) {
9350 skl_init_scalers(dev, crtc, pipe_config);
9351 }
9352
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009353 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009354 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009355 if (INTEL_INFO(dev)->gen == 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009356 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009357 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009358 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009359 else
9360 MISSING_CASE(INTEL_INFO(dev)->gen);
9361
Chandra Kondurua1b22782015-04-07 15:28:45 -07009362 } else {
9363 pipe_config->scaler_state.scaler_id = -1;
9364 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009365 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01009366
Jesse Barnese59150d2014-01-07 13:30:45 -08009367 if (IS_HASWELL(dev))
9368 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9369 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009370
Clint Taylorebb69c92014-09-30 10:30:22 -07009371 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
9372 pipe_config->pixel_multiplier =
9373 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9374 } else {
9375 pipe_config->pixel_multiplier = 1;
9376 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009377
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009378 return true;
9379}
9380
Chris Wilson560b85b2010-08-07 11:01:38 +01009381static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
9382{
9383 struct drm_device *dev = crtc->dev;
9384 struct drm_i915_private *dev_priv = dev->dev_private;
9385 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +03009386 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01009387
Ville Syrjälädc41c152014-08-13 11:57:05 +03009388 if (base) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009389 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
9390 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009391 unsigned int stride = roundup_pow_of_two(width) * 4;
9392
9393 switch (stride) {
9394 default:
9395 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9396 width, stride);
9397 stride = 256;
9398 /* fallthrough */
9399 case 256:
9400 case 512:
9401 case 1024:
9402 case 2048:
9403 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009404 }
9405
Ville Syrjälädc41c152014-08-13 11:57:05 +03009406 cntl |= CURSOR_ENABLE |
9407 CURSOR_GAMMA_ENABLE |
9408 CURSOR_FORMAT_ARGB |
9409 CURSOR_STRIDE(stride);
9410
9411 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009412 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009413
Ville Syrjälädc41c152014-08-13 11:57:05 +03009414 if (intel_crtc->cursor_cntl != 0 &&
9415 (intel_crtc->cursor_base != base ||
9416 intel_crtc->cursor_size != size ||
9417 intel_crtc->cursor_cntl != cntl)) {
9418 /* On these chipsets we can only modify the base/size/stride
9419 * whilst the cursor is disabled.
9420 */
9421 I915_WRITE(_CURACNTR, 0);
9422 POSTING_READ(_CURACNTR);
9423 intel_crtc->cursor_cntl = 0;
9424 }
9425
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009426 if (intel_crtc->cursor_base != base) {
Ville Syrjälädc41c152014-08-13 11:57:05 +03009427 I915_WRITE(_CURABASE, base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009428 intel_crtc->cursor_base = base;
9429 }
Ville Syrjälädc41c152014-08-13 11:57:05 +03009430
9431 if (intel_crtc->cursor_size != size) {
9432 I915_WRITE(CURSIZE, size);
9433 intel_crtc->cursor_size = size;
9434 }
9435
Chris Wilson4b0e3332014-05-30 16:35:26 +03009436 if (intel_crtc->cursor_cntl != cntl) {
9437 I915_WRITE(_CURACNTR, cntl);
9438 POSTING_READ(_CURACNTR);
9439 intel_crtc->cursor_cntl = cntl;
9440 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009441}
9442
9443static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
9444{
9445 struct drm_device *dev = crtc->dev;
9446 struct drm_i915_private *dev_priv = dev->dev_private;
9447 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9448 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009449 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +01009450
Chris Wilson4b0e3332014-05-30 16:35:26 +03009451 cntl = 0;
9452 if (base) {
9453 cntl = MCURSOR_GAMMA_ENABLE;
Matt Roper3dd512f2015-02-27 10:12:00 -08009454 switch (intel_crtc->base.cursor->state->crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309455 case 64:
9456 cntl |= CURSOR_MODE_64_ARGB_AX;
9457 break;
9458 case 128:
9459 cntl |= CURSOR_MODE_128_ARGB_AX;
9460 break;
9461 case 256:
9462 cntl |= CURSOR_MODE_256_ARGB_AX;
9463 break;
9464 default:
Matt Roper3dd512f2015-02-27 10:12:00 -08009465 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309466 return;
Chris Wilson560b85b2010-08-07 11:01:38 +01009467 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009468 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +03009469
9470 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
9471 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson560b85b2010-08-07 11:01:38 +01009472 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009473
Matt Roper8e7d6882015-01-21 16:35:41 -08009474 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009475 cntl |= CURSOR_ROTATE_180;
9476
Chris Wilson4b0e3332014-05-30 16:35:26 +03009477 if (intel_crtc->cursor_cntl != cntl) {
9478 I915_WRITE(CURCNTR(pipe), cntl);
9479 POSTING_READ(CURCNTR(pipe));
9480 intel_crtc->cursor_cntl = cntl;
9481 }
9482
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009483 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009484 I915_WRITE(CURBASE(pipe), base);
9485 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009486
9487 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009488}
9489
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009490/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01009491static void intel_crtc_update_cursor(struct drm_crtc *crtc,
9492 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009493{
9494 struct drm_device *dev = crtc->dev;
9495 struct drm_i915_private *dev_priv = dev->dev_private;
9496 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9497 int pipe = intel_crtc->pipe;
Matt Roper3d7d6512014-06-10 08:28:13 -07009498 int x = crtc->cursor_x;
9499 int y = crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009500 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009501
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009502 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009503 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009504
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009505 if (x >= intel_crtc->config->pipe_src_w)
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009506 base = 0;
9507
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009508 if (y >= intel_crtc->config->pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009509 base = 0;
9510
9511 if (x < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009512 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009513 base = 0;
9514
9515 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
9516 x = -x;
9517 }
9518 pos |= x << CURSOR_X_SHIFT;
9519
9520 if (y < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009521 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009522 base = 0;
9523
9524 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
9525 y = -y;
9526 }
9527 pos |= y << CURSOR_Y_SHIFT;
9528
Chris Wilson4b0e3332014-05-30 16:35:26 +03009529 if (base == 0 && intel_crtc->cursor_base == 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009530 return;
9531
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009532 I915_WRITE(CURPOS(pipe), pos);
9533
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009534 /* ILK+ do this automagically */
9535 if (HAS_GMCH_DISPLAY(dev) &&
Matt Roper8e7d6882015-01-21 16:35:41 -08009536 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009537 base += (intel_crtc->base.cursor->state->crtc_h *
9538 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009539 }
9540
Ville Syrjälä8ac54662014-08-12 19:39:54 +03009541 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009542 i845_update_cursor(crtc, base);
9543 else
9544 i9xx_update_cursor(crtc, base);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009545}
9546
Ville Syrjälädc41c152014-08-13 11:57:05 +03009547static bool cursor_size_ok(struct drm_device *dev,
9548 uint32_t width, uint32_t height)
9549{
9550 if (width == 0 || height == 0)
9551 return false;
9552
9553 /*
9554 * 845g/865g are special in that they are only limited by
9555 * the width of their cursors, the height is arbitrary up to
9556 * the precision of the register. Everything else requires
9557 * square cursors, limited to a few power-of-two sizes.
9558 */
9559 if (IS_845G(dev) || IS_I865G(dev)) {
9560 if ((width & 63) != 0)
9561 return false;
9562
9563 if (width > (IS_845G(dev) ? 64 : 512))
9564 return false;
9565
9566 if (height > 1023)
9567 return false;
9568 } else {
9569 switch (width | height) {
9570 case 256:
9571 case 128:
9572 if (IS_GEN2(dev))
9573 return false;
9574 case 64:
9575 break;
9576 default:
9577 return false;
9578 }
9579 }
9580
9581 return true;
9582}
9583
Jesse Barnes79e53942008-11-07 14:24:08 -08009584static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01009585 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08009586{
James Simmons72034252010-08-03 01:33:19 +01009587 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08009588 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08009589
James Simmons72034252010-08-03 01:33:19 +01009590 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009591 intel_crtc->lut_r[i] = red[i] >> 8;
9592 intel_crtc->lut_g[i] = green[i] >> 8;
9593 intel_crtc->lut_b[i] = blue[i] >> 8;
9594 }
9595
9596 intel_crtc_load_lut(crtc);
9597}
9598
Jesse Barnes79e53942008-11-07 14:24:08 -08009599/* VESA 640x480x72Hz mode to set on the pipe */
9600static struct drm_display_mode load_detect_mode = {
9601 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
9602 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
9603};
9604
Daniel Vettera8bb6812014-02-10 18:00:39 +01009605struct drm_framebuffer *
9606__intel_framebuffer_create(struct drm_device *dev,
9607 struct drm_mode_fb_cmd2 *mode_cmd,
9608 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +01009609{
9610 struct intel_framebuffer *intel_fb;
9611 int ret;
9612
9613 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
9614 if (!intel_fb) {
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +03009615 drm_gem_object_unreference(&obj->base);
Chris Wilsond2dff872011-04-19 08:36:26 +01009616 return ERR_PTR(-ENOMEM);
9617 }
9618
9619 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009620 if (ret)
9621 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +01009622
9623 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009624err:
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +03009625 drm_gem_object_unreference(&obj->base);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009626 kfree(intel_fb);
9627
9628 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +01009629}
9630
Daniel Vetterb5ea6422014-03-02 21:18:00 +01009631static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +01009632intel_framebuffer_create(struct drm_device *dev,
9633 struct drm_mode_fb_cmd2 *mode_cmd,
9634 struct drm_i915_gem_object *obj)
9635{
9636 struct drm_framebuffer *fb;
9637 int ret;
9638
9639 ret = i915_mutex_lock_interruptible(dev);
9640 if (ret)
9641 return ERR_PTR(ret);
9642 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
9643 mutex_unlock(&dev->struct_mutex);
9644
9645 return fb;
9646}
9647
Chris Wilsond2dff872011-04-19 08:36:26 +01009648static u32
9649intel_framebuffer_pitch_for_width(int width, int bpp)
9650{
9651 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
9652 return ALIGN(pitch, 64);
9653}
9654
9655static u32
9656intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
9657{
9658 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +02009659 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +01009660}
9661
9662static struct drm_framebuffer *
9663intel_framebuffer_create_for_mode(struct drm_device *dev,
9664 struct drm_display_mode *mode,
9665 int depth, int bpp)
9666{
9667 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00009668 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01009669
9670 obj = i915_gem_alloc_object(dev,
9671 intel_framebuffer_size_for_mode(mode, bpp));
9672 if (obj == NULL)
9673 return ERR_PTR(-ENOMEM);
9674
9675 mode_cmd.width = mode->hdisplay;
9676 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009677 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
9678 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00009679 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01009680
9681 return intel_framebuffer_create(dev, &mode_cmd, obj);
9682}
9683
9684static struct drm_framebuffer *
9685mode_fits_in_fbdev(struct drm_device *dev,
9686 struct drm_display_mode *mode)
9687{
Daniel Vetter4520f532013-10-09 09:18:51 +02009688#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +01009689 struct drm_i915_private *dev_priv = dev->dev_private;
9690 struct drm_i915_gem_object *obj;
9691 struct drm_framebuffer *fb;
9692
Daniel Vetter4c0e5522014-02-14 16:35:54 +01009693 if (!dev_priv->fbdev)
9694 return NULL;
9695
9696 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +01009697 return NULL;
9698
Jesse Barnes8bcd4552014-02-07 12:10:38 -08009699 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +01009700 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +01009701
Jesse Barnes8bcd4552014-02-07 12:10:38 -08009702 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009703 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
9704 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01009705 return NULL;
9706
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009707 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01009708 return NULL;
9709
9710 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +02009711#else
9712 return NULL;
9713#endif
Chris Wilsond2dff872011-04-19 08:36:26 +01009714}
9715
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009716bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01009717 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -05009718 struct intel_load_detect_pipe *old,
9719 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08009720{
9721 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009722 struct intel_encoder *intel_encoder =
9723 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08009724 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01009725 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08009726 struct drm_crtc *crtc = NULL;
9727 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02009728 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -05009729 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009730 struct drm_atomic_state *state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009731 struct drm_connector_state *connector_state;
Rob Clark51fd3712013-11-19 12:10:12 -05009732 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -08009733
Chris Wilsond2dff872011-04-19 08:36:26 +01009734 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03009735 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03009736 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01009737
Rob Clark51fd3712013-11-19 12:10:12 -05009738retry:
9739 ret = drm_modeset_lock(&config->connection_mutex, ctx);
9740 if (ret)
9741 goto fail_unlock;
Daniel Vetter6e9f7982014-05-29 23:54:47 +02009742
Jesse Barnes79e53942008-11-07 14:24:08 -08009743 /*
9744 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01009745 *
Jesse Barnes79e53942008-11-07 14:24:08 -08009746 * - if the connector already has an assigned crtc, use it (but make
9747 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01009748 *
Jesse Barnes79e53942008-11-07 14:24:08 -08009749 * - try to find the first unused crtc that can drive this connector,
9750 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08009751 */
9752
9753 /* See if we already have a CRTC for this connector */
9754 if (encoder->crtc) {
9755 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01009756
Rob Clark51fd3712013-11-19 12:10:12 -05009757 ret = drm_modeset_lock(&crtc->mutex, ctx);
9758 if (ret)
9759 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01009760 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
9761 if (ret)
9762 goto fail_unlock;
Daniel Vetter7b240562012-12-12 00:35:33 +01009763
Daniel Vetter24218aa2012-08-12 19:27:11 +02009764 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01009765 old->load_detect_temp = false;
9766
9767 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02009768 if (connector->dpms != DRM_MODE_DPMS_ON)
9769 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01009770
Chris Wilson71731882011-04-19 23:10:58 +01009771 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08009772 }
9773
9774 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01009775 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009776 i++;
9777 if (!(encoder->possible_crtcs & (1 << i)))
9778 continue;
Matt Roper83d65732015-02-25 13:12:16 -08009779 if (possible_crtc->state->enable)
Ville Syrjäläa4592492014-08-11 13:15:36 +03009780 continue;
9781 /* This can occur when applying the pipe A quirk on resume. */
9782 if (to_intel_crtc(possible_crtc)->new_enabled)
9783 continue;
9784
9785 crtc = possible_crtc;
9786 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08009787 }
9788
9789 /*
9790 * If we didn't find an unused CRTC, don't use any.
9791 */
9792 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01009793 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Rob Clark51fd3712013-11-19 12:10:12 -05009794 goto fail_unlock;
Jesse Barnes79e53942008-11-07 14:24:08 -08009795 }
9796
Rob Clark51fd3712013-11-19 12:10:12 -05009797 ret = drm_modeset_lock(&crtc->mutex, ctx);
9798 if (ret)
9799 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01009800 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
9801 if (ret)
9802 goto fail_unlock;
Daniel Vetterfc303102012-07-09 10:40:58 +02009803 intel_encoder->new_crtc = to_intel_crtc(crtc);
9804 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08009805
9806 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009807 intel_crtc->new_enabled = true;
Daniel Vetter24218aa2012-08-12 19:27:11 +02009808 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01009809 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01009810 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08009811
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009812 state = drm_atomic_state_alloc(dev);
9813 if (!state)
9814 return false;
9815
9816 state->acquire_ctx = ctx;
9817
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009818 connector_state = drm_atomic_get_connector_state(state, connector);
9819 if (IS_ERR(connector_state)) {
9820 ret = PTR_ERR(connector_state);
9821 goto fail;
9822 }
9823
9824 connector_state->crtc = crtc;
9825 connector_state->best_encoder = &intel_encoder->base;
9826
Chris Wilson64927112011-04-20 07:25:26 +01009827 if (!mode)
9828 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08009829
Chris Wilsond2dff872011-04-19 08:36:26 +01009830 /* We need a framebuffer large enough to accommodate all accesses
9831 * that the plane may generate whilst we perform load detection.
9832 * We can not rely on the fbcon either being present (we get called
9833 * during its initialisation to detect all boot displays, or it may
9834 * not even exist) or that it is large enough to satisfy the
9835 * requested mode.
9836 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02009837 fb = mode_fits_in_fbdev(dev, mode);
9838 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01009839 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02009840 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
9841 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01009842 } else
9843 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02009844 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01009845 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009846 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08009847 }
Chris Wilsond2dff872011-04-19 08:36:26 +01009848
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009849 if (intel_set_mode(crtc, mode, 0, 0, fb, state)) {
Chris Wilson64927112011-04-20 07:25:26 +01009850 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01009851 if (old->release_fb)
9852 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009853 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08009854 }
Daniel Vetter9128b042015-03-03 17:31:21 +01009855 crtc->primary->crtc = crtc;
Chris Wilson71731882011-04-19 23:10:58 +01009856
Jesse Barnes79e53942008-11-07 14:24:08 -08009857 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07009858 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01009859 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009860
9861 fail:
Matt Roper83d65732015-02-25 13:12:16 -08009862 intel_crtc->new_enabled = crtc->state->enable;
Rob Clark51fd3712013-11-19 12:10:12 -05009863fail_unlock:
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009864 if (state) {
9865 drm_atomic_state_free(state);
9866 state = NULL;
9867 }
9868
Rob Clark51fd3712013-11-19 12:10:12 -05009869 if (ret == -EDEADLK) {
9870 drm_modeset_backoff(ctx);
9871 goto retry;
9872 }
9873
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009874 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08009875}
9876
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009877void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +02009878 struct intel_load_detect_pipe *old,
9879 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08009880{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009881 struct drm_device *dev = connector->dev;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009882 struct intel_encoder *intel_encoder =
9883 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01009884 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01009885 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009886 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009887 struct drm_atomic_state *state;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009888 struct drm_connector_state *connector_state;
Jesse Barnes79e53942008-11-07 14:24:08 -08009889
Chris Wilsond2dff872011-04-19 08:36:26 +01009890 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03009891 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03009892 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01009893
Chris Wilson8261b192011-04-19 23:18:09 +01009894 if (old->load_detect_temp) {
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009895 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009896 if (!state)
9897 goto fail;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009898
9899 state->acquire_ctx = ctx;
9900
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009901 connector_state = drm_atomic_get_connector_state(state, connector);
9902 if (IS_ERR(connector_state))
9903 goto fail;
9904
Daniel Vetterfc303102012-07-09 10:40:58 +02009905 to_intel_connector(connector)->new_encoder = NULL;
9906 intel_encoder->new_crtc = NULL;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009907 intel_crtc->new_enabled = false;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009908
9909 connector_state->best_encoder = NULL;
9910 connector_state->crtc = NULL;
9911
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009912 intel_set_mode(crtc, NULL, 0, 0, NULL, state);
9913
9914 drm_atomic_state_free(state);
Chris Wilsond2dff872011-04-19 08:36:26 +01009915
Daniel Vetter36206362012-12-10 20:42:17 +01009916 if (old->release_fb) {
9917 drm_framebuffer_unregister_private(old->release_fb);
9918 drm_framebuffer_unreference(old->release_fb);
9919 }
Chris Wilsond2dff872011-04-19 08:36:26 +01009920
Chris Wilson0622a532011-04-21 09:32:11 +01009921 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08009922 }
9923
Eric Anholtc751ce42010-03-25 11:48:48 -07009924 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02009925 if (old->dpms_mode != DRM_MODE_DPMS_ON)
9926 connector->funcs->dpms(connector, old->dpms_mode);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009927
9928 return;
9929fail:
9930 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
9931 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -08009932}
9933
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009934static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009935 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009936{
9937 struct drm_i915_private *dev_priv = dev->dev_private;
9938 u32 dpll = pipe_config->dpll_hw_state.dpll;
9939
9940 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +02009941 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009942 else if (HAS_PCH_SPLIT(dev))
9943 return 120000;
9944 else if (!IS_GEN2(dev))
9945 return 96000;
9946 else
9947 return 48000;
9948}
9949
Jesse Barnes79e53942008-11-07 14:24:08 -08009950/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009951static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009952 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08009953{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009954 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08009955 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009956 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +03009957 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08009958 u32 fp;
9959 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009960 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -08009961
9962 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +03009963 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009964 else
Ville Syrjälä293623f2013-09-13 16:18:46 +03009965 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -08009966
9967 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05009968 if (IS_PINEVIEW(dev)) {
9969 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
9970 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08009971 } else {
9972 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
9973 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
9974 }
9975
Chris Wilsona6c45cf2010-09-17 00:32:17 +01009976 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05009977 if (IS_PINEVIEW(dev))
9978 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
9979 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08009980 else
9981 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08009982 DPLL_FPA01_P1_POST_DIV_SHIFT);
9983
9984 switch (dpll & DPLL_MODE_MASK) {
9985 case DPLLB_MODE_DAC_SERIAL:
9986 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
9987 5 : 10;
9988 break;
9989 case DPLLB_MODE_LVDS:
9990 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
9991 7 : 14;
9992 break;
9993 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08009994 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08009995 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009996 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08009997 }
9998
Daniel Vetterac58c3f2013-06-01 17:16:17 +02009999 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010000 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010001 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010002 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010003 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010004 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010005 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010006
10007 if (is_lvds) {
10008 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10009 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010010
10011 if (lvds & LVDS_CLKB_POWER_UP)
10012 clock.p2 = 7;
10013 else
10014 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010015 } else {
10016 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10017 clock.p1 = 2;
10018 else {
10019 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10020 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10021 }
10022 if (dpll & PLL_P2_DIVIDE_BY_4)
10023 clock.p2 = 4;
10024 else
10025 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010026 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010027
10028 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010029 }
10030
Ville Syrjälä18442d02013-09-13 16:00:08 +030010031 /*
10032 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010033 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010034 * encoder's get_config() function.
10035 */
10036 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010037}
10038
Ville Syrjälä6878da02013-09-13 15:59:11 +030010039int intel_dotclock_calculate(int link_freq,
10040 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010041{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010042 /*
10043 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010044 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010045 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010046 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010047 *
10048 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010049 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010050 */
10051
Ville Syrjälä6878da02013-09-13 15:59:11 +030010052 if (!m_n->link_n)
10053 return 0;
10054
10055 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10056}
10057
Ville Syrjälä18442d02013-09-13 16:00:08 +030010058static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010059 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010060{
10061 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +030010062
10063 /* read out port_clock from the DPLL */
10064 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010065
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010066 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +030010067 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +010010068 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +030010069 * agree once we know their relationship in the encoder's
10070 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010071 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010072 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +030010073 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10074 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010075}
10076
10077/** Returns the currently programmed mode of the given pipe. */
10078struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10079 struct drm_crtc *crtc)
10080{
Jesse Barnes548f2452011-02-17 10:40:53 -080010081 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010082 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010083 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010084 struct drm_display_mode *mode;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010085 struct intel_crtc_state pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010086 int htot = I915_READ(HTOTAL(cpu_transcoder));
10087 int hsync = I915_READ(HSYNC(cpu_transcoder));
10088 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10089 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010090 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010091
10092 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10093 if (!mode)
10094 return NULL;
10095
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010096 /*
10097 * Construct a pipe_config sufficient for getting the clock info
10098 * back out of crtc_clock_get.
10099 *
10100 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10101 * to use a real value here instead.
10102 */
Ville Syrjälä293623f2013-09-13 16:18:46 +030010103 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010104 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010105 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10106 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10107 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010108 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10109
Ville Syrjälä773ae032013-09-23 17:48:20 +030010110 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010111 mode->hdisplay = (htot & 0xffff) + 1;
10112 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10113 mode->hsync_start = (hsync & 0xffff) + 1;
10114 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10115 mode->vdisplay = (vtot & 0xffff) + 1;
10116 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10117 mode->vsync_start = (vsync & 0xffff) + 1;
10118 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10119
10120 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010121
10122 return mode;
10123}
10124
Jesse Barnes652c3932009-08-17 13:31:43 -070010125static void intel_decrease_pllclock(struct drm_crtc *crtc)
10126{
10127 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030010128 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -070010129 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -070010130
Sonika Jindalbaff2962014-07-22 11:16:35 +053010131 if (!HAS_GMCH_DISPLAY(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -070010132 return;
10133
10134 if (!dev_priv->lvds_downclock_avail)
10135 return;
10136
10137 /*
10138 * Since this is called by a timer, we should never get here in
10139 * the manual case.
10140 */
10141 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +010010142 int pipe = intel_crtc->pipe;
10143 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +020010144 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +010010145
Zhao Yakui44d98a62009-10-09 11:39:40 +080010146 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -070010147
Sean Paul8ac5a6d2012-02-13 13:14:51 -050010148 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -070010149
Chris Wilson074b5e12012-05-02 12:07:06 +010010150 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -070010151 dpll |= DISPLAY_RATE_SELECT_FPA1;
10152 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010153 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -070010154 dpll = I915_READ(dpll_reg);
10155 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +080010156 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -070010157 }
10158
10159}
10160
Chris Wilsonf047e392012-07-21 12:31:41 +010010161void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010162{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010163 struct drm_i915_private *dev_priv = dev->dev_private;
10164
Chris Wilsonf62a0072014-02-21 17:55:39 +000010165 if (dev_priv->mm.busy)
10166 return;
10167
Paulo Zanoni43694d62014-03-07 20:08:08 -030010168 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010169 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010170 if (INTEL_INFO(dev)->gen >= 6)
10171 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010172 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010173}
10174
10175void intel_mark_idle(struct drm_device *dev)
10176{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010177 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010178 struct drm_crtc *crtc;
10179
Chris Wilsonf62a0072014-02-21 17:55:39 +000010180 if (!dev_priv->mm.busy)
10181 return;
10182
10183 dev_priv->mm.busy = false;
10184
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010185 for_each_crtc(dev, crtc) {
Matt Roperf4510a22014-04-01 15:22:40 -070010186 if (!crtc->primary->fb)
Chris Wilson725a5b52013-01-08 11:02:57 +000010187 continue;
10188
10189 intel_decrease_pllclock(crtc);
10190 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010191
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010192 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010193 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010194
Paulo Zanoni43694d62014-03-07 20:08:08 -030010195 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010196}
10197
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020010198static void intel_crtc_set_state(struct intel_crtc *crtc,
10199 struct intel_crtc_state *crtc_state)
10200{
10201 kfree(crtc->config);
10202 crtc->config = crtc_state;
Ander Conselvan de Oliveira16f3f652015-01-15 14:55:27 +020010203 crtc->base.state = &crtc_state->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020010204}
10205
Jesse Barnes79e53942008-11-07 14:24:08 -080010206static void intel_crtc_destroy(struct drm_crtc *crtc)
10207{
10208 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010209 struct drm_device *dev = crtc->dev;
10210 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010211
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010212 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010213 work = intel_crtc->unpin_work;
10214 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010215 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010216
10217 if (work) {
10218 cancel_work_sync(&work->work);
10219 kfree(work);
10220 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010221
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020010222 intel_crtc_set_state(intel_crtc, NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -080010223 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010224
Jesse Barnes79e53942008-11-07 14:24:08 -080010225 kfree(intel_crtc);
10226}
10227
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010228static void intel_unpin_work_fn(struct work_struct *__work)
10229{
10230 struct intel_unpin_work *work =
10231 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010232 struct drm_device *dev = work->crtc->dev;
Daniel Vetterf99d7062014-06-19 16:01:59 +020010233 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010234
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010235 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000010236 intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
Chris Wilson05394f32010-11-08 19:18:58 +000010237 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010238
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020010239 intel_fbc_update(dev);
John Harrisonf06cc1b2014-11-24 18:49:37 +000010240
10241 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010242 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010243 mutex_unlock(&dev->struct_mutex);
10244
Daniel Vetterf99d7062014-06-19 16:01:59 +020010245 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Chris Wilson89ed88b2015-02-16 14:31:49 +000010246 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010247
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010248 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
10249 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
10250
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010251 kfree(work);
10252}
10253
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010254static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010010255 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010256{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010257 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10258 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010259 unsigned long flags;
10260
10261 /* Ignore early vblank irqs */
10262 if (intel_crtc == NULL)
10263 return;
10264
Daniel Vetterf3260382014-09-15 14:55:23 +020010265 /*
10266 * This is called both by irq handlers and the reset code (to complete
10267 * lost pageflips) so needs the full irqsave spinlocks.
10268 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010269 spin_lock_irqsave(&dev->event_lock, flags);
10270 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000010271
10272 /* Ensure we don't miss a work->pending update ... */
10273 smp_rmb();
10274
10275 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010276 spin_unlock_irqrestore(&dev->event_lock, flags);
10277 return;
10278 }
10279
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010280 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010010281
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010282 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010283}
10284
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010285void intel_finish_page_flip(struct drm_device *dev, int pipe)
10286{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010287 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010288 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10289
Mario Kleiner49b14a52010-12-09 07:00:07 +010010290 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010291}
10292
10293void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10294{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010295 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010296 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10297
Mario Kleiner49b14a52010-12-09 07:00:07 +010010298 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010299}
10300
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010301/* Is 'a' after or equal to 'b'? */
10302static bool g4x_flip_count_after_eq(u32 a, u32 b)
10303{
10304 return !((a - b) & 0x80000000);
10305}
10306
10307static bool page_flip_finished(struct intel_crtc *crtc)
10308{
10309 struct drm_device *dev = crtc->base.dev;
10310 struct drm_i915_private *dev_priv = dev->dev_private;
10311
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030010312 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10313 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10314 return true;
10315
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010316 /*
10317 * The relevant registers doen't exist on pre-ctg.
10318 * As the flip done interrupt doesn't trigger for mmio
10319 * flips on gmch platforms, a flip count check isn't
10320 * really needed there. But since ctg has the registers,
10321 * include it in the check anyway.
10322 */
10323 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10324 return true;
10325
10326 /*
10327 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10328 * used the same base address. In that case the mmio flip might
10329 * have completed, but the CS hasn't even executed the flip yet.
10330 *
10331 * A flip count check isn't enough as the CS might have updated
10332 * the base address just after start of vblank, but before we
10333 * managed to process the interrupt. This means we'd complete the
10334 * CS flip too soon.
10335 *
10336 * Combining both checks should get us a good enough result. It may
10337 * still happen that the CS flip has been executed, but has not
10338 * yet actually completed. But in case the base address is the same
10339 * anyway, we don't really care.
10340 */
10341 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10342 crtc->unpin_work->gtt_offset &&
10343 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
10344 crtc->unpin_work->flip_count);
10345}
10346
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010347void intel_prepare_page_flip(struct drm_device *dev, int plane)
10348{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010349 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010350 struct intel_crtc *intel_crtc =
10351 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10352 unsigned long flags;
10353
Daniel Vetterf3260382014-09-15 14:55:23 +020010354
10355 /*
10356 * This is called both by irq handlers and the reset code (to complete
10357 * lost pageflips) so needs the full irqsave spinlocks.
10358 *
10359 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000010360 * generate a page-flip completion irq, i.e. every modeset
10361 * is also accompanied by a spurious intel_prepare_page_flip().
10362 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010363 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010364 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000010365 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010366 spin_unlock_irqrestore(&dev->event_lock, flags);
10367}
10368
Robin Schroereba905b2014-05-18 02:24:50 +020010369static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
Chris Wilsone7d841c2012-12-03 11:36:30 +000010370{
10371 /* Ensure that the work item is consistent when activating it ... */
10372 smp_wmb();
10373 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
10374 /* and that it is marked active as soon as the irq could fire. */
10375 smp_wmb();
10376}
10377
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010378static int intel_gen2_queue_flip(struct drm_device *dev,
10379 struct drm_crtc *crtc,
10380 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010381 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010382 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010383 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010384{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010385 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010386 u32 flip_mask;
10387 int ret;
10388
Daniel Vetter6d90c952012-04-26 23:28:05 +020010389 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010390 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010391 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010392
10393 /* Can't queue multiple flips, so wait for the previous
10394 * one to finish before executing the next.
10395 */
10396 if (intel_crtc->plane)
10397 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10398 else
10399 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010400 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10401 intel_ring_emit(ring, MI_NOOP);
10402 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10403 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10404 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010405 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010406 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000010407
10408 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010409 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010410 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010411}
10412
10413static int intel_gen3_queue_flip(struct drm_device *dev,
10414 struct drm_crtc *crtc,
10415 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010416 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010417 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010418 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010419{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010420 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010421 u32 flip_mask;
10422 int ret;
10423
Daniel Vetter6d90c952012-04-26 23:28:05 +020010424 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010425 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010426 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010427
10428 if (intel_crtc->plane)
10429 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10430 else
10431 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010432 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10433 intel_ring_emit(ring, MI_NOOP);
10434 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
10435 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10436 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010437 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010438 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010439
Chris Wilsone7d841c2012-12-03 11:36:30 +000010440 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010441 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010442 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010443}
10444
10445static int intel_gen4_queue_flip(struct drm_device *dev,
10446 struct drm_crtc *crtc,
10447 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010448 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010449 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010450 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010451{
10452 struct drm_i915_private *dev_priv = dev->dev_private;
10453 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10454 uint32_t pf, pipesrc;
10455 int ret;
10456
Daniel Vetter6d90c952012-04-26 23:28:05 +020010457 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010458 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010459 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010460
10461 /* i965+ uses the linear or tiled offsets from the
10462 * Display Registers (which do not change across a page-flip)
10463 * so we need only reprogram the base address.
10464 */
Daniel Vetter6d90c952012-04-26 23:28:05 +020010465 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10466 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10467 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010468 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020010469 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010470
10471 /* XXX Enabling the panel-fitter across page-flip is so far
10472 * untested on non-native modes, so ignore it for now.
10473 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
10474 */
10475 pf = 0;
10476 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010477 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010478
10479 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010480 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010481 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010482}
10483
10484static int intel_gen6_queue_flip(struct drm_device *dev,
10485 struct drm_crtc *crtc,
10486 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010487 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010488 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010489 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010490{
10491 struct drm_i915_private *dev_priv = dev->dev_private;
10492 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10493 uint32_t pf, pipesrc;
10494 int ret;
10495
Daniel Vetter6d90c952012-04-26 23:28:05 +020010496 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010497 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010498 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010499
Daniel Vetter6d90c952012-04-26 23:28:05 +020010500 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10501 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10502 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010503 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010504
Chris Wilson99d9acd2012-04-17 20:37:00 +010010505 /* Contrary to the suggestions in the documentation,
10506 * "Enable Panel Fitter" does not seem to be required when page
10507 * flipping with a non-native mode, and worse causes a normal
10508 * modeset to fail.
10509 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
10510 */
10511 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010512 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010513 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010514
10515 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010516 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010517 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010518}
10519
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010520static int intel_gen7_queue_flip(struct drm_device *dev,
10521 struct drm_crtc *crtc,
10522 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010523 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010524 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010525 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010526{
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010527 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010528 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010010529 int len, ret;
10530
Robin Schroereba905b2014-05-18 02:24:50 +020010531 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010532 case PLANE_A:
10533 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
10534 break;
10535 case PLANE_B:
10536 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
10537 break;
10538 case PLANE_C:
10539 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
10540 break;
10541 default:
10542 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010543 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010544 }
10545
Chris Wilsonffe74d72013-08-26 20:58:12 +010010546 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +010010547 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010010548 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010010549 /*
10550 * On Gen 8, SRM is now taking an extra dword to accommodate
10551 * 48bits addresses, and we need a NOOP for the batch size to
10552 * stay even.
10553 */
10554 if (IS_GEN8(dev))
10555 len += 2;
10556 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010010557
Ville Syrjäläf66fab82014-02-11 19:52:06 +020010558 /*
10559 * BSpec MI_DISPLAY_FLIP for IVB:
10560 * "The full packet must be contained within the same cache line."
10561 *
10562 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
10563 * cacheline, if we ever start emitting more commands before
10564 * the MI_DISPLAY_FLIP we may need to first emit everything else,
10565 * then do the cacheline alignment, and finally emit the
10566 * MI_DISPLAY_FLIP.
10567 */
10568 ret = intel_ring_cacheline_align(ring);
10569 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010570 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020010571
Chris Wilsonffe74d72013-08-26 20:58:12 +010010572 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010573 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010574 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010575
Chris Wilsonffe74d72013-08-26 20:58:12 +010010576 /* Unmask the flip-done completion message. Note that the bspec says that
10577 * we should do this for both the BCS and RCS, and that we must not unmask
10578 * more than one flip event at any time (or ensure that one flip message
10579 * can be sent by waiting for flip-done prior to queueing new flips).
10580 * Experimentation says that BCS works despite DERRMR masking all
10581 * flip-done completion events and that unmasking all planes at once
10582 * for the RCS also doesn't appear to drop events. Setting the DERRMR
10583 * to zero does lead to lockups within MI_DISPLAY_FLIP.
10584 */
10585 if (ring->id == RCS) {
10586 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
10587 intel_ring_emit(ring, DERRMR);
10588 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
10589 DERRMR_PIPEB_PRI_FLIP_DONE |
10590 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010010591 if (IS_GEN8(dev))
10592 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
10593 MI_SRM_LRM_GLOBAL_GTT);
10594 else
10595 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
10596 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +010010597 intel_ring_emit(ring, DERRMR);
10598 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010010599 if (IS_GEN8(dev)) {
10600 intel_ring_emit(ring, 0);
10601 intel_ring_emit(ring, MI_NOOP);
10602 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010010603 }
10604
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010605 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010606 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010607 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010608 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000010609
10610 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010611 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010612 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010613}
10614
Sourab Gupta84c33a62014-06-02 16:47:17 +053010615static bool use_mmio_flip(struct intel_engine_cs *ring,
10616 struct drm_i915_gem_object *obj)
10617{
10618 /*
10619 * This is not being used for older platforms, because
10620 * non-availability of flip done interrupt forces us to use
10621 * CS flips. Older platforms derive flip done using some clever
10622 * tricks involving the flip_pending status bits and vblank irqs.
10623 * So using MMIO flips there would disrupt this mechanism.
10624 */
10625
Chris Wilson8e09bf82014-07-08 10:40:30 +010010626 if (ring == NULL)
10627 return true;
10628
Sourab Gupta84c33a62014-06-02 16:47:17 +053010629 if (INTEL_INFO(ring->dev)->gen < 5)
10630 return false;
10631
10632 if (i915.use_mmio_flip < 0)
10633 return false;
10634 else if (i915.use_mmio_flip > 0)
10635 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010010636 else if (i915.enable_execlists)
10637 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053010638 else
John Harrison41c52412014-11-24 18:49:43 +000010639 return ring != i915_gem_request_get_ring(obj->last_read_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010640}
10641
Damien Lespiauff944562014-11-20 14:58:16 +000010642static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
10643{
10644 struct drm_device *dev = intel_crtc->base.dev;
10645 struct drm_i915_private *dev_priv = dev->dev_private;
10646 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000010647 const enum pipe pipe = intel_crtc->pipe;
10648 u32 ctl, stride;
10649
10650 ctl = I915_READ(PLANE_CTL(pipe, 0));
10651 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010010652 switch (fb->modifier[0]) {
10653 case DRM_FORMAT_MOD_NONE:
10654 break;
10655 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000010656 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010010657 break;
10658 case I915_FORMAT_MOD_Y_TILED:
10659 ctl |= PLANE_CTL_TILED_Y;
10660 break;
10661 case I915_FORMAT_MOD_Yf_TILED:
10662 ctl |= PLANE_CTL_TILED_YF;
10663 break;
10664 default:
10665 MISSING_CASE(fb->modifier[0]);
10666 }
Damien Lespiauff944562014-11-20 14:58:16 +000010667
10668 /*
10669 * The stride is either expressed as a multiple of 64 bytes chunks for
10670 * linear buffers or in number of tiles for tiled buffers.
10671 */
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010010672 stride = fb->pitches[0] /
10673 intel_fb_stride_alignment(dev, fb->modifier[0],
10674 fb->pixel_format);
Damien Lespiauff944562014-11-20 14:58:16 +000010675
10676 /*
10677 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
10678 * PLANE_SURF updates, the update is then guaranteed to be atomic.
10679 */
10680 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
10681 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
10682
10683 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
10684 POSTING_READ(PLANE_SURF(pipe, 0));
10685}
10686
10687static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
Sourab Gupta84c33a62014-06-02 16:47:17 +053010688{
10689 struct drm_device *dev = intel_crtc->base.dev;
10690 struct drm_i915_private *dev_priv = dev->dev_private;
10691 struct intel_framebuffer *intel_fb =
10692 to_intel_framebuffer(intel_crtc->base.primary->fb);
10693 struct drm_i915_gem_object *obj = intel_fb->obj;
10694 u32 dspcntr;
10695 u32 reg;
10696
Sourab Gupta84c33a62014-06-02 16:47:17 +053010697 reg = DSPCNTR(intel_crtc->plane);
10698 dspcntr = I915_READ(reg);
10699
Damien Lespiauc5d97472014-10-25 00:11:11 +010010700 if (obj->tiling_mode != I915_TILING_NONE)
10701 dspcntr |= DISPPLANE_TILED;
10702 else
10703 dspcntr &= ~DISPPLANE_TILED;
10704
Sourab Gupta84c33a62014-06-02 16:47:17 +053010705 I915_WRITE(reg, dspcntr);
10706
10707 I915_WRITE(DSPSURF(intel_crtc->plane),
10708 intel_crtc->unpin_work->gtt_offset);
10709 POSTING_READ(DSPSURF(intel_crtc->plane));
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010710
Damien Lespiauff944562014-11-20 14:58:16 +000010711}
10712
10713/*
10714 * XXX: This is the temporary way to update the plane registers until we get
10715 * around to using the usual plane update functions for MMIO flips
10716 */
10717static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
10718{
10719 struct drm_device *dev = intel_crtc->base.dev;
10720 bool atomic_update;
10721 u32 start_vbl_count;
10722
10723 intel_mark_page_flip_active(intel_crtc);
10724
10725 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
10726
10727 if (INTEL_INFO(dev)->gen >= 9)
10728 skl_do_mmio_flip(intel_crtc);
10729 else
10730 /* use_mmio_flip() retricts MMIO flips to ilk+ */
10731 ilk_do_mmio_flip(intel_crtc);
10732
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010733 if (atomic_update)
10734 intel_pipe_update_end(intel_crtc, start_vbl_count);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010735}
10736
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010737static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053010738{
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010739 struct intel_crtc *crtc =
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010740 container_of(work, struct intel_crtc, mmio_flip.work);
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010741 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053010742
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010743 mmio_flip = &crtc->mmio_flip;
10744 if (mmio_flip->req)
John Harrison9c654812014-11-24 18:49:35 +000010745 WARN_ON(__i915_wait_request(mmio_flip->req,
10746 crtc->reset_counter,
10747 false, NULL, NULL) != 0);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010748
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010749 intel_do_mmio_flip(crtc);
10750 if (mmio_flip->req) {
10751 mutex_lock(&crtc->base.dev->struct_mutex);
John Harrison146d84f2014-12-05 13:49:33 +000010752 i915_gem_request_assign(&mmio_flip->req, NULL);
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010753 mutex_unlock(&crtc->base.dev->struct_mutex);
10754 }
Sourab Gupta84c33a62014-06-02 16:47:17 +053010755}
10756
10757static int intel_queue_mmio_flip(struct drm_device *dev,
10758 struct drm_crtc *crtc,
10759 struct drm_framebuffer *fb,
10760 struct drm_i915_gem_object *obj,
10761 struct intel_engine_cs *ring,
10762 uint32_t flags)
10763{
Sourab Gupta84c33a62014-06-02 16:47:17 +053010764 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010765
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010766 i915_gem_request_assign(&intel_crtc->mmio_flip.req,
10767 obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010768
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020010769 schedule_work(&intel_crtc->mmio_flip.work);
10770
Sourab Gupta84c33a62014-06-02 16:47:17 +053010771 return 0;
10772}
10773
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010774static int intel_default_queue_flip(struct drm_device *dev,
10775 struct drm_crtc *crtc,
10776 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010777 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010778 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010779 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010780{
10781 return -ENODEV;
10782}
10783
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010784static bool __intel_pageflip_stall_check(struct drm_device *dev,
10785 struct drm_crtc *crtc)
10786{
10787 struct drm_i915_private *dev_priv = dev->dev_private;
10788 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10789 struct intel_unpin_work *work = intel_crtc->unpin_work;
10790 u32 addr;
10791
10792 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
10793 return true;
10794
10795 if (!work->enable_stall_check)
10796 return false;
10797
10798 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010010799 if (work->flip_queued_req &&
10800 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010801 return false;
10802
Daniel Vetter1e3feef2015-02-13 21:03:45 +010010803 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010804 }
10805
Daniel Vetter1e3feef2015-02-13 21:03:45 +010010806 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010807 return false;
10808
10809 /* Potential stall - if we see that the flip has happened,
10810 * assume a missed interrupt. */
10811 if (INTEL_INFO(dev)->gen >= 4)
10812 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
10813 else
10814 addr = I915_READ(DSPADDR(intel_crtc->plane));
10815
10816 /* There is a potential issue here with a false positive after a flip
10817 * to the same address. We could address this by checking for a
10818 * non-incrementing frame counter.
10819 */
10820 return addr == work->gtt_offset;
10821}
10822
10823void intel_check_page_flip(struct drm_device *dev, int pipe)
10824{
10825 struct drm_i915_private *dev_priv = dev->dev_private;
10826 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10827 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010010828 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020010829
Dave Gordon6c51d462015-03-06 15:34:26 +000010830 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010831
10832 if (crtc == NULL)
10833 return;
10834
Daniel Vetterf3260382014-09-15 14:55:23 +020010835 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010010836 work = intel_crtc->unpin_work;
10837 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010838 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010010839 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010840 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010010841 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010842 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010010843 if (work != NULL &&
10844 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
10845 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020010846 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010847}
10848
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010849static int intel_crtc_page_flip(struct drm_crtc *crtc,
10850 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010851 struct drm_pending_vblank_event *event,
10852 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010853{
10854 struct drm_device *dev = crtc->dev;
10855 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070010856 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070010857 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010858 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080010859 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020010860 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010861 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010862 struct intel_engine_cs *ring;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010010863 bool mmio_flip;
Chris Wilson52e68632010-08-08 10:15:59 +010010864 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010865
Matt Roper2ff8fde2014-07-08 07:50:07 -070010866 /*
10867 * drm_mode_page_flip_ioctl() should already catch this, but double
10868 * check to be safe. In the future we may enable pageflipping from
10869 * a disabled primary plane.
10870 */
10871 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
10872 return -EBUSY;
10873
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030010874 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070010875 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030010876 return -EINVAL;
10877
10878 /*
10879 * TILEOFF/LINOFF registers can't be changed via MI display flips.
10880 * Note that pitch changes could also affect these register.
10881 */
10882 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070010883 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
10884 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030010885 return -EINVAL;
10886
Chris Wilsonf900db42014-02-20 09:26:13 +000010887 if (i915_terminally_wedged(&dev_priv->gpu_error))
10888 goto out_hang;
10889
Daniel Vetterb14c5672013-09-19 12:18:32 +020010890 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010891 if (work == NULL)
10892 return -ENOMEM;
10893
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010894 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010895 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010896 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010897 INIT_WORK(&work->work, intel_unpin_work_fn);
10898
Daniel Vetter87b6b102014-05-15 15:33:46 +020010899 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070010900 if (ret)
10901 goto free_work;
10902
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010903 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010904 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010905 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010906 /* Before declaring the flip queue wedged, check if
10907 * the hardware completed the operation behind our backs.
10908 */
10909 if (__intel_pageflip_stall_check(dev, crtc)) {
10910 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
10911 page_flip_completed(intel_crtc);
10912 } else {
10913 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010914 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010010915
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010916 drm_crtc_vblank_put(crtc);
10917 kfree(work);
10918 return -EBUSY;
10919 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010920 }
10921 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010922 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010923
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010924 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
10925 flush_workqueue(dev_priv->wq);
10926
Jesse Barnes75dfca82010-02-10 15:09:44 -080010927 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010928 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000010929 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010930
Matt Roperf4510a22014-04-01 15:22:40 -070010931 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080010932 update_state_fb(crtc->primary);
Matt Roper1ed1f962015-01-30 16:22:36 -080010933
Chris Wilsone1f99ce2010-10-27 12:45:26 +010010934 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010010935
Chris Wilson89ed88b2015-02-16 14:31:49 +000010936 ret = i915_mutex_lock_interruptible(dev);
10937 if (ret)
10938 goto cleanup;
10939
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010940 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020010941 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010010942
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010943 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Daniel Vettera071fa02014-06-18 23:28:09 +020010944 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010945
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010946 if (IS_VALLEYVIEW(dev)) {
10947 ring = &dev_priv->ring[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010948 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010010949 /* vlv: DISPLAY_FLIP fails to change tiling */
10950 ring = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000010951 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Chris Wilson2a92d5b2014-07-08 10:40:29 +010010952 ring = &dev_priv->ring[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010953 } else if (INTEL_INFO(dev)->gen >= 7) {
John Harrison41c52412014-11-24 18:49:43 +000010954 ring = i915_gem_request_get_ring(obj->last_read_req);
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010955 if (ring == NULL || ring->id != RCS)
10956 ring = &dev_priv->ring[BCS];
10957 } else {
10958 ring = &dev_priv->ring[RCS];
10959 }
10960
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010010961 mmio_flip = use_mmio_flip(ring, obj);
10962
10963 /* When using CS flips, we want to emit semaphores between rings.
10964 * However, when using mmio flips we will create a task to do the
10965 * synchronisation, so all we want here is to pin the framebuffer
10966 * into the display plane and skip any waits.
10967 */
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000010968 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010010969 crtc->primary->state,
10970 mmio_flip ? i915_gem_request_get_ring(obj->last_read_req) : ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010971 if (ret)
10972 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010973
Tvrtko Ursulin121920f2015-03-23 11:10:37 +000010974 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
10975 + intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010976
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010010977 if (mmio_flip) {
Sourab Gupta84c33a62014-06-02 16:47:17 +053010978 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
10979 page_flip_flags);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010980 if (ret)
10981 goto cleanup_unpin;
10982
John Harrisonf06cc1b2014-11-24 18:49:37 +000010983 i915_gem_request_assign(&work->flip_queued_req,
10984 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010985 } else {
Sourab Gupta84c33a62014-06-02 16:47:17 +053010986 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010987 page_flip_flags);
10988 if (ret)
10989 goto cleanup_unpin;
10990
John Harrisonf06cc1b2014-11-24 18:49:37 +000010991 i915_gem_request_assign(&work->flip_queued_req,
10992 intel_ring_get_request(ring));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010993 }
10994
Daniel Vetter1e3feef2015-02-13 21:03:45 +010010995 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010996 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010997
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010998 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Daniel Vettera071fa02014-06-18 23:28:09 +020010999 INTEL_FRONTBUFFER_PRIMARY(pipe));
11000
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020011001 intel_fbc_disable(dev);
Daniel Vetterf99d7062014-06-19 16:01:59 +020011002 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011003 mutex_unlock(&dev->struct_mutex);
11004
Jesse Barnese5510fa2010-07-01 16:48:37 -070011005 trace_i915_flip_request(intel_crtc->plane, obj);
11006
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011007 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010011008
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011009cleanup_unpin:
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011010 intel_unpin_fb_obj(fb, crtc->primary->state);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011011cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011012 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000011013 mutex_unlock(&dev->struct_mutex);
11014cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070011015 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011016 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010011017
Chris Wilson89ed88b2015-02-16 14:31:49 +000011018 drm_gem_object_unreference_unlocked(&obj->base);
11019 drm_framebuffer_unreference(work->old_fb);
11020
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011021 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011022 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011023 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011024
Daniel Vetter87b6b102014-05-15 15:33:46 +020011025 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011026free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010011027 kfree(work);
11028
Chris Wilsonf900db42014-02-20 09:26:13 +000011029 if (ret == -EIO) {
11030out_hang:
Matt Roper53a366b2014-12-23 10:41:53 -080011031 ret = intel_plane_restore(primary);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011032 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011033 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020011034 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011035 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011036 }
Chris Wilsonf900db42014-02-20 09:26:13 +000011037 }
Chris Wilson96b099f2010-06-07 14:03:04 +010011038 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011039}
11040
Jani Nikula65b38e02015-04-13 11:26:56 +030011041static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011042 .mode_set_base_atomic = intel_pipe_set_base_atomic,
11043 .load_lut = intel_crtc_load_lut,
Matt Roperea2c67b2014-12-23 10:41:52 -080011044 .atomic_begin = intel_begin_crtc_commit,
11045 .atomic_flush = intel_finish_crtc_commit,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011046};
11047
Daniel Vetter9a935852012-07-05 22:34:27 +020011048/**
11049 * intel_modeset_update_staged_output_state
11050 *
11051 * Updates the staged output configuration state, e.g. after we've read out the
11052 * current hw state.
11053 */
11054static void intel_modeset_update_staged_output_state(struct drm_device *dev)
11055{
Ville Syrjälä76688512014-01-10 11:28:06 +020011056 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011057 struct intel_encoder *encoder;
11058 struct intel_connector *connector;
11059
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011060 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011061 connector->new_encoder =
11062 to_intel_encoder(connector->base.encoder);
11063 }
11064
Damien Lespiaub2784e12014-08-05 11:29:37 +010011065 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011066 encoder->new_crtc =
11067 to_intel_crtc(encoder->base.crtc);
11068 }
Ville Syrjälä76688512014-01-10 11:28:06 +020011069
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011070 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011071 crtc->new_enabled = crtc->base.state->enable;
Ville Syrjälä76688512014-01-10 11:28:06 +020011072 }
Daniel Vetter9a935852012-07-05 22:34:27 +020011073}
11074
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011075/* Transitional helper to copy current connector/encoder state to
11076 * connector->state. This is needed so that code that is partially
11077 * converted to atomic does the right thing.
11078 */
11079static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11080{
11081 struct intel_connector *connector;
11082
11083 for_each_intel_connector(dev, connector) {
11084 if (connector->base.encoder) {
11085 connector->base.state->best_encoder =
11086 connector->base.encoder;
11087 connector->base.state->crtc =
11088 connector->base.encoder->crtc;
11089 } else {
11090 connector->base.state->best_encoder = NULL;
11091 connector->base.state->crtc = NULL;
11092 }
11093 }
11094}
11095
Daniel Vetter9a935852012-07-05 22:34:27 +020011096/**
11097 * intel_modeset_commit_output_state
11098 *
11099 * This function copies the stage display pipe configuration to the real one.
11100 */
11101static void intel_modeset_commit_output_state(struct drm_device *dev)
11102{
Ville Syrjälä76688512014-01-10 11:28:06 +020011103 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011104 struct intel_encoder *encoder;
11105 struct intel_connector *connector;
11106
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011107 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011108 connector->base.encoder = &connector->new_encoder->base;
11109 }
11110
Damien Lespiaub2784e12014-08-05 11:29:37 +010011111 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011112 encoder->base.crtc = &encoder->new_crtc->base;
11113 }
Ville Syrjälä76688512014-01-10 11:28:06 +020011114
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011115 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011116 crtc->base.state->enable = crtc->new_enabled;
Ville Syrjälä76688512014-01-10 11:28:06 +020011117 crtc->base.enabled = crtc->new_enabled;
11118 }
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011119
11120 intel_modeset_update_connector_atomic_state(dev);
Daniel Vetter9a935852012-07-05 22:34:27 +020011121}
11122
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011123static void
Robin Schroereba905b2014-05-18 02:24:50 +020011124connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011125 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011126{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011127 int bpp = pipe_config->pipe_bpp;
11128
11129 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11130 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011131 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011132
11133 /* Don't use an invalid EDID bpc value */
11134 if (connector->base.display_info.bpc &&
11135 connector->base.display_info.bpc * 3 < bpp) {
11136 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11137 bpp, connector->base.display_info.bpc*3);
11138 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11139 }
11140
11141 /* Clamp bpp to 8 on screens without EDID 1.4 */
11142 if (connector->base.display_info.bpc == 0 && bpp > 24) {
11143 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11144 bpp);
11145 pipe_config->pipe_bpp = 24;
11146 }
11147}
11148
11149static int
11150compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011151 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011152{
11153 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011154 struct drm_atomic_state *state;
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011155 struct intel_connector *connector;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011156 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011157
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011158 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011159 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011160 else if (INTEL_INFO(dev)->gen >= 5)
11161 bpp = 12*3;
11162 else
11163 bpp = 8*3;
11164
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011165
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011166 pipe_config->pipe_bpp = bpp;
11167
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011168 state = pipe_config->base.state;
11169
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011170 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011171 for (i = 0; i < state->num_connector; i++) {
11172 if (!state->connectors[i])
11173 continue;
11174
11175 connector = to_intel_connector(state->connectors[i]);
11176 if (state->connector_states[i]->crtc != &crtc->base)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011177 continue;
11178
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011179 connected_sink_compute_bpp(connector, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011180 }
11181
11182 return bpp;
11183}
11184
Daniel Vetter644db712013-09-19 14:53:58 +020011185static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11186{
11187 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11188 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010011189 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020011190 mode->crtc_hdisplay, mode->crtc_hsync_start,
11191 mode->crtc_hsync_end, mode->crtc_htotal,
11192 mode->crtc_vdisplay, mode->crtc_vsync_start,
11193 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11194}
11195
Daniel Vetterc0b03412013-05-28 12:05:54 +020011196static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011197 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020011198 const char *context)
11199{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011200 struct drm_device *dev = crtc->base.dev;
11201 struct drm_plane *plane;
11202 struct intel_plane *intel_plane;
11203 struct intel_plane_state *state;
11204 struct drm_framebuffer *fb;
11205
11206 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
11207 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020011208
11209 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
11210 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
11211 pipe_config->pipe_bpp, pipe_config->dither);
11212 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11213 pipe_config->has_pch_encoder,
11214 pipe_config->fdi_lanes,
11215 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
11216 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
11217 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011218 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11219 pipe_config->has_dp_encoder,
11220 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
11221 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
11222 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011223
11224 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
11225 pipe_config->has_dp_encoder,
11226 pipe_config->dp_m2_n2.gmch_m,
11227 pipe_config->dp_m2_n2.gmch_n,
11228 pipe_config->dp_m2_n2.link_m,
11229 pipe_config->dp_m2_n2.link_n,
11230 pipe_config->dp_m2_n2.tu);
11231
Daniel Vetter55072d12014-11-20 16:10:28 +010011232 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
11233 pipe_config->has_audio,
11234 pipe_config->has_infoframe);
11235
Daniel Vetterc0b03412013-05-28 12:05:54 +020011236 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011237 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011238 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011239 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11240 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030011241 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030011242 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
11243 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011244 DRM_DEBUG_KMS("num_scalers: %d\n", crtc->num_scalers);
11245 DRM_DEBUG_KMS("scaler_users: 0x%x\n", pipe_config->scaler_state.scaler_users);
11246 DRM_DEBUG_KMS("scaler id: %d\n", pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011247 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11248 pipe_config->gmch_pfit.control,
11249 pipe_config->gmch_pfit.pgm_ratios,
11250 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011251 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020011252 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011253 pipe_config->pch_pfit.size,
11254 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030011255 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030011256 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011257
11258 DRM_DEBUG_KMS("planes on this crtc\n");
11259 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
11260 intel_plane = to_intel_plane(plane);
11261 if (intel_plane->pipe != crtc->pipe)
11262 continue;
11263
11264 state = to_intel_plane_state(plane->state);
11265 fb = state->base.fb;
11266 if (!fb) {
11267 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
11268 "disabled, scaler_id = %d\n",
11269 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11270 plane->base.id, intel_plane->pipe,
11271 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
11272 drm_plane_index(plane), state->scaler_id);
11273 continue;
11274 }
11275
11276 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
11277 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11278 plane->base.id, intel_plane->pipe,
11279 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
11280 drm_plane_index(plane));
11281 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
11282 fb->base.id, fb->width, fb->height, fb->pixel_format);
11283 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
11284 state->scaler_id,
11285 state->src.x1 >> 16, state->src.y1 >> 16,
11286 drm_rect_width(&state->src) >> 16,
11287 drm_rect_height(&state->src) >> 16,
11288 state->dst.x1, state->dst.y1,
11289 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
11290 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020011291}
11292
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011293static bool encoders_cloneable(const struct intel_encoder *a,
11294 const struct intel_encoder *b)
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011295{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011296 /* masks could be asymmetric, so check both ways */
11297 return a == b || (a->cloneable & (1 << b->type) &&
11298 b->cloneable & (1 << a->type));
11299}
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011300
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011301static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11302 struct intel_crtc *crtc,
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011303 struct intel_encoder *encoder)
11304{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011305 struct intel_encoder *source_encoder;
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 Oliveira98a221d2015-04-02 14:48:00 +030011309 for (i = 0; i < state->num_connector; i++) {
11310 if (!state->connectors[i])
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011311 continue;
11312
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011313 connector_state = state->connector_states[i];
11314 if (connector_state->crtc != &crtc->base)
11315 continue;
11316
11317 source_encoder =
11318 to_intel_encoder(connector_state->best_encoder);
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011319 if (!encoders_cloneable(encoder, source_encoder))
11320 return false;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011321 }
11322
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011323 return true;
11324}
11325
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011326static bool check_encoder_cloning(struct drm_atomic_state *state,
11327 struct intel_crtc *crtc)
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011328{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011329 struct intel_encoder *encoder;
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011330 struct drm_connector_state *connector_state;
11331 int i;
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011332
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011333 for (i = 0; i < state->num_connector; i++) {
11334 if (!state->connectors[i])
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011335 continue;
11336
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011337 connector_state = state->connector_states[i];
11338 if (connector_state->crtc != &crtc->base)
11339 continue;
11340
11341 encoder = to_intel_encoder(connector_state->best_encoder);
11342 if (!check_single_encoder_cloning(state, crtc, encoder))
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011343 return false;
11344 }
11345
11346 return true;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011347}
11348
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011349static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011350{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011351 struct drm_device *dev = state->dev;
11352 struct intel_encoder *encoder;
11353 struct drm_connector_state *connector_state;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011354 unsigned int used_ports = 0;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011355 int i;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011356
11357 /*
11358 * Walk the connector list instead of the encoder
11359 * list to detect the problem on ddi platforms
11360 * where there's just one encoder per digital port.
11361 */
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011362 for (i = 0; i < state->num_connector; i++) {
11363 if (!state->connectors[i])
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011364 continue;
11365
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011366 connector_state = state->connector_states[i];
11367 if (!connector_state->best_encoder)
11368 continue;
11369
11370 encoder = to_intel_encoder(connector_state->best_encoder);
11371
11372 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011373
11374 switch (encoder->type) {
11375 unsigned int port_mask;
11376 case INTEL_OUTPUT_UNKNOWN:
11377 if (WARN_ON(!HAS_DDI(dev)))
11378 break;
11379 case INTEL_OUTPUT_DISPLAYPORT:
11380 case INTEL_OUTPUT_HDMI:
11381 case INTEL_OUTPUT_EDP:
11382 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
11383
11384 /* the same port mustn't appear more than once */
11385 if (used_ports & port_mask)
11386 return false;
11387
11388 used_ports |= port_mask;
11389 default:
11390 break;
11391 }
11392 }
11393
11394 return true;
11395}
11396
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011397static void
11398clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
11399{
11400 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070011401 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011402
Chandra Konduru663a3642015-04-07 15:28:41 -070011403 /* Clear only the intel specific part of the crtc state excluding scalers */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011404 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070011405 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011406 memset(crtc_state, 0, sizeof *crtc_state);
11407 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070011408 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011409}
11410
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011411static struct intel_crtc_state *
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011412intel_modeset_pipe_config(struct drm_crtc *crtc,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011413 struct drm_display_mode *mode,
11414 struct drm_atomic_state *state)
Daniel Vetter7758a112012-07-08 19:40:39 +020011415{
Daniel Vetter7758a112012-07-08 19:40:39 +020011416 struct intel_encoder *encoder;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011417 struct intel_connector *connector;
11418 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011419 struct intel_crtc_state *pipe_config;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011420 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011421 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010011422 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020011423
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011424 if (!check_encoder_cloning(state, to_intel_crtc(crtc))) {
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011425 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11426 return ERR_PTR(-EINVAL);
11427 }
11428
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011429 if (!check_digital_port_conflicts(state)) {
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011430 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
11431 return ERR_PTR(-EINVAL);
11432 }
11433
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011434 pipe_config = intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));
11435 if (IS_ERR(pipe_config))
11436 return pipe_config;
11437
11438 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020011439
Matt Roper07878242015-02-25 11:43:26 -080011440 pipe_config->base.crtc = crtc;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011441 drm_mode_copy(&pipe_config->base.adjusted_mode, mode);
11442 drm_mode_copy(&pipe_config->base.mode, mode);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030011443
Daniel Vettere143a212013-07-04 12:01:15 +020011444 pipe_config->cpu_transcoder =
11445 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +020011446 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011447
Imre Deak2960bc92013-07-30 13:36:32 +030011448 /*
11449 * Sanitize sync polarity flags based on requested ones. If neither
11450 * positive or negative polarity is requested, treat this as meaning
11451 * negative polarity.
11452 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011453 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030011454 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011455 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030011456
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011457 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030011458 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011459 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030011460
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011461 /* Compute a starting value for pipe_config->pipe_bpp taking the source
11462 * plane pixel format and any sink constraints into account. Returns the
11463 * source plane bpp so that dithering can be selected on mismatches
11464 * after encoders and crtc also have had their say. */
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011465 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
11466 pipe_config);
11467 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011468 goto fail;
11469
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030011470 /*
11471 * Determine the real pipe dimensions. Note that stereo modes can
11472 * increase the actual pipe size due to the frame doubling and
11473 * insertion of additional space for blanks between the frame. This
11474 * is stored in the crtc timings. We use the requested mode to do this
11475 * computation to clearly distinguish it from the adjusted mode, which
11476 * can be changed by the connectors in the below retry loop.
11477 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011478 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080011479 &pipe_config->pipe_src_w,
11480 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030011481
Daniel Vettere29c22c2013-02-21 00:00:16 +010011482encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020011483 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020011484 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020011485 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020011486
Daniel Vetter135c81b2013-07-21 21:37:09 +020011487 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011488 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
11489 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020011490
Daniel Vetter7758a112012-07-08 19:40:39 +020011491 /* Pass our mode to the connectors and the CRTC to give them a chance to
11492 * adjust it according to limitations or connector properties, and also
11493 * a chance to reject the mode entirely.
11494 */
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011495 for (i = 0; i < state->num_connector; i++) {
11496 connector = to_intel_connector(state->connectors[i]);
11497 if (!connector)
Daniel Vetter7758a112012-07-08 19:40:39 +020011498 continue;
Daniel Vetter7ae89232013-03-27 00:44:52 +010011499
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011500 connector_state = state->connector_states[i];
11501 if (connector_state->crtc != crtc)
11502 continue;
11503
11504 encoder = to_intel_encoder(connector_state->best_encoder);
11505
Daniel Vetterefea6e82013-07-21 21:36:59 +020011506 if (!(encoder->compute_config(encoder, pipe_config))) {
11507 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020011508 goto fail;
11509 }
11510 }
11511
Daniel Vetterff9a6752013-06-01 17:16:21 +020011512 /* Set default port clock if not overwritten by the encoder. Needs to be
11513 * done afterwards in case the encoder adjusts the mode. */
11514 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011515 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010011516 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020011517
Daniel Vettera43f6e02013-06-07 23:10:32 +020011518 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010011519 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020011520 DRM_DEBUG_KMS("CRTC fixup failed\n");
11521 goto fail;
11522 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010011523
11524 if (ret == RETRY) {
11525 if (WARN(!retry, "loop in pipe configuration computation\n")) {
11526 ret = -EINVAL;
11527 goto fail;
11528 }
11529
11530 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
11531 retry = false;
11532 goto encoder_retry;
11533 }
11534
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011535 pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011536 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011537 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011538
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011539 return pipe_config;
Daniel Vetter7758a112012-07-08 19:40:39 +020011540fail:
Daniel Vettere29c22c2013-02-21 00:00:16 +010011541 return ERR_PTR(ret);
Daniel Vetter7758a112012-07-08 19:40:39 +020011542}
11543
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011544/* Computes which crtcs are affected and sets the relevant bits in the mask. For
11545 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
11546static void
11547intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
11548 unsigned *prepare_pipes, unsigned *disable_pipes)
11549{
11550 struct intel_crtc *intel_crtc;
11551 struct drm_device *dev = crtc->dev;
11552 struct intel_encoder *encoder;
11553 struct intel_connector *connector;
11554 struct drm_crtc *tmp_crtc;
11555
11556 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
11557
11558 /* Check which crtcs have changed outputs connected to them, these need
11559 * to be part of the prepare_pipes mask. We don't (yet) support global
11560 * modeset across multiple crtcs, so modeset_pipes will only have one
11561 * bit set at most. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011562 for_each_intel_connector(dev, connector) {
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011563 if (connector->base.encoder == &connector->new_encoder->base)
11564 continue;
11565
11566 if (connector->base.encoder) {
11567 tmp_crtc = connector->base.encoder->crtc;
11568
11569 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
11570 }
11571
11572 if (connector->new_encoder)
11573 *prepare_pipes |=
11574 1 << connector->new_encoder->new_crtc->pipe;
11575 }
11576
Damien Lespiaub2784e12014-08-05 11:29:37 +010011577 for_each_intel_encoder(dev, encoder) {
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011578 if (encoder->base.crtc == &encoder->new_crtc->base)
11579 continue;
11580
11581 if (encoder->base.crtc) {
11582 tmp_crtc = encoder->base.crtc;
11583
11584 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
11585 }
11586
11587 if (encoder->new_crtc)
11588 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
11589 }
11590
Ville Syrjälä76688512014-01-10 11:28:06 +020011591 /* Check for pipes that will be enabled/disabled ... */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011592 for_each_intel_crtc(dev, intel_crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011593 if (intel_crtc->base.state->enable == intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011594 continue;
11595
Ville Syrjälä76688512014-01-10 11:28:06 +020011596 if (!intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011597 *disable_pipes |= 1 << intel_crtc->pipe;
Ville Syrjälä76688512014-01-10 11:28:06 +020011598 else
11599 *prepare_pipes |= 1 << intel_crtc->pipe;
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011600 }
11601
11602
11603 /* set_mode is also used to update properties on life display pipes. */
11604 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä76688512014-01-10 11:28:06 +020011605 if (intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011606 *prepare_pipes |= 1 << intel_crtc->pipe;
11607
Daniel Vetterb6c51642013-04-12 18:48:43 +020011608 /*
11609 * For simplicity do a full modeset on any pipe where the output routing
11610 * changed. We could be more clever, but that would require us to be
11611 * more careful with calling the relevant encoder->mode_set functions.
11612 */
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011613 if (*prepare_pipes)
11614 *modeset_pipes = *prepare_pipes;
11615
11616 /* ... and mask these out. */
11617 *modeset_pipes &= ~(*disable_pipes);
11618 *prepare_pipes &= ~(*disable_pipes);
Daniel Vetterb6c51642013-04-12 18:48:43 +020011619
11620 /*
11621 * HACK: We don't (yet) fully support global modesets. intel_set_config
11622 * obies this rule, but the modeset restore mode of
11623 * intel_modeset_setup_hw_state does not.
11624 */
11625 *modeset_pipes &= 1 << intel_crtc->pipe;
11626 *prepare_pipes &= 1 << intel_crtc->pipe;
Daniel Vettere3641d32013-04-11 19:49:07 +020011627
11628 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
11629 *modeset_pipes, *prepare_pipes, *disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011630}
11631
Daniel Vetterea9d7582012-07-10 10:42:52 +020011632static bool intel_crtc_in_use(struct drm_crtc *crtc)
11633{
11634 struct drm_encoder *encoder;
11635 struct drm_device *dev = crtc->dev;
11636
11637 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
11638 if (encoder->crtc == crtc)
11639 return true;
11640
11641 return false;
11642}
11643
11644static void
11645intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
11646{
Daniel Vetterba41c0de2014-11-03 15:04:55 +010011647 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011648 struct intel_encoder *intel_encoder;
11649 struct intel_crtc *intel_crtc;
11650 struct drm_connector *connector;
11651
Daniel Vetterba41c0de2014-11-03 15:04:55 +010011652 intel_shared_dpll_commit(dev_priv);
11653
Damien Lespiaub2784e12014-08-05 11:29:37 +010011654 for_each_intel_encoder(dev, intel_encoder) {
Daniel Vetterea9d7582012-07-10 10:42:52 +020011655 if (!intel_encoder->base.crtc)
11656 continue;
11657
11658 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
11659
11660 if (prepare_pipes & (1 << intel_crtc->pipe))
11661 intel_encoder->connectors_active = false;
11662 }
11663
11664 intel_modeset_commit_output_state(dev);
11665
Ville Syrjälä76688512014-01-10 11:28:06 +020011666 /* Double check state. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011667 for_each_intel_crtc(dev, intel_crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011668 WARN_ON(intel_crtc->base.state->enable != intel_crtc_in_use(&intel_crtc->base));
Daniel Vetterea9d7582012-07-10 10:42:52 +020011669 }
11670
11671 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
11672 if (!connector->encoder || !connector->encoder->crtc)
11673 continue;
11674
11675 intel_crtc = to_intel_crtc(connector->encoder->crtc);
11676
11677 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +020011678 struct drm_property *dpms_property =
11679 dev->mode_config.dpms_property;
11680
Daniel Vetterea9d7582012-07-10 10:42:52 +020011681 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -050011682 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +020011683 dpms_property,
11684 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +020011685
11686 intel_encoder = to_intel_encoder(connector->encoder);
11687 intel_encoder->connectors_active = true;
11688 }
11689 }
11690
11691}
11692
Ville Syrjälä3bd26262013-09-06 23:29:02 +030011693static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011694{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030011695 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011696
11697 if (clock1 == clock2)
11698 return true;
11699
11700 if (!clock1 || !clock2)
11701 return false;
11702
11703 diff = abs(clock1 - clock2);
11704
11705 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
11706 return true;
11707
11708 return false;
11709}
11710
Daniel Vetter25c5b262012-07-08 22:08:04 +020011711#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
11712 list_for_each_entry((intel_crtc), \
11713 &(dev)->mode_config.crtc_list, \
11714 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +020011715 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020011716
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011717static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020011718intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011719 struct intel_crtc_state *current_config,
11720 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011721{
Daniel Vetter66e985c2013-06-05 13:34:20 +020011722#define PIPE_CONF_CHECK_X(name) \
11723 if (current_config->name != pipe_config->name) { \
11724 DRM_ERROR("mismatch in " #name " " \
11725 "(expected 0x%08x, found 0x%08x)\n", \
11726 current_config->name, \
11727 pipe_config->name); \
11728 return false; \
11729 }
11730
Daniel Vetter08a24032013-04-19 11:25:34 +020011731#define PIPE_CONF_CHECK_I(name) \
11732 if (current_config->name != pipe_config->name) { \
11733 DRM_ERROR("mismatch in " #name " " \
11734 "(expected %i, found %i)\n", \
11735 current_config->name, \
11736 pipe_config->name); \
11737 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010011738 }
11739
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011740/* This is required for BDW+ where there is only one set of registers for
11741 * switching between high and low RR.
11742 * This macro can be used whenever a comparison has to be made between one
11743 * hw state and multiple sw state variables.
11744 */
11745#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
11746 if ((current_config->name != pipe_config->name) && \
11747 (current_config->alt_name != pipe_config->name)) { \
11748 DRM_ERROR("mismatch in " #name " " \
11749 "(expected %i or %i, found %i)\n", \
11750 current_config->name, \
11751 current_config->alt_name, \
11752 pipe_config->name); \
11753 return false; \
11754 }
11755
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011756#define PIPE_CONF_CHECK_FLAGS(name, mask) \
11757 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -070011758 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011759 "(expected %i, found %i)\n", \
11760 current_config->name & (mask), \
11761 pipe_config->name & (mask)); \
11762 return false; \
11763 }
11764
Ville Syrjälä5e550652013-09-06 23:29:07 +030011765#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
11766 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
11767 DRM_ERROR("mismatch in " #name " " \
11768 "(expected %i, found %i)\n", \
11769 current_config->name, \
11770 pipe_config->name); \
11771 return false; \
11772 }
11773
Daniel Vetterbb760062013-06-06 14:55:52 +020011774#define PIPE_CONF_QUIRK(quirk) \
11775 ((current_config->quirks | pipe_config->quirks) & (quirk))
11776
Daniel Vettereccb1402013-05-22 00:50:22 +020011777 PIPE_CONF_CHECK_I(cpu_transcoder);
11778
Daniel Vetter08a24032013-04-19 11:25:34 +020011779 PIPE_CONF_CHECK_I(has_pch_encoder);
11780 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +020011781 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
11782 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
11783 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
11784 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
11785 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +020011786
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011787 PIPE_CONF_CHECK_I(has_dp_encoder);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011788
11789 if (INTEL_INFO(dev)->gen < 8) {
11790 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
11791 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
11792 PIPE_CONF_CHECK_I(dp_m_n.link_m);
11793 PIPE_CONF_CHECK_I(dp_m_n.link_n);
11794 PIPE_CONF_CHECK_I(dp_m_n.tu);
11795
11796 if (current_config->has_drrs) {
11797 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
11798 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
11799 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
11800 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
11801 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
11802 }
11803 } else {
11804 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
11805 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
11806 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
11807 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
11808 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
11809 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011810
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011811 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
11812 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
11813 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
11814 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
11815 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
11816 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011817
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011818 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
11819 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
11820 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
11821 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
11822 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
11823 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011824
Daniel Vetterc93f54c2013-06-27 19:47:19 +020011825 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b2014-04-24 23:54:47 +020011826 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020011827 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
11828 IS_VALLEYVIEW(dev))
11829 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080011830 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020011831
Daniel Vetter9ed109a2014-04-24 23:54:52 +020011832 PIPE_CONF_CHECK_I(has_audio);
11833
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011834 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011835 DRM_MODE_FLAG_INTERLACE);
11836
Daniel Vetterbb760062013-06-06 14:55:52 +020011837 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011838 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011839 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011840 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011841 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011842 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011843 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011844 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011845 DRM_MODE_FLAG_NVSYNC);
11846 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070011847
Ville Syrjälä37327ab2013-09-04 18:25:28 +030011848 PIPE_CONF_CHECK_I(pipe_src_w);
11849 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011850
Daniel Vetter99535992014-04-13 12:00:33 +020011851 /*
11852 * FIXME: BIOS likes to set up a cloned config with lvds+external
11853 * screen. Since we don't yet re-compute the pipe config when moving
11854 * just the lvds port away to another pipe the sw tracking won't match.
11855 *
11856 * Proper atomic modesets with recomputed global state will fix this.
11857 * Until then just don't check gmch state for inherited modes.
11858 */
11859 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
11860 PIPE_CONF_CHECK_I(gmch_pfit.control);
11861 /* pfit ratios are autocomputed by the hw on gen4+ */
11862 if (INTEL_INFO(dev)->gen < 4)
11863 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
11864 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
11865 }
11866
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011867 PIPE_CONF_CHECK_I(pch_pfit.enabled);
11868 if (current_config->pch_pfit.enabled) {
11869 PIPE_CONF_CHECK_I(pch_pfit.pos);
11870 PIPE_CONF_CHECK_I(pch_pfit.size);
11871 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020011872
Chandra Kondurua1b22782015-04-07 15:28:45 -070011873 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
11874
Jesse Barnese59150d2014-01-07 13:30:45 -080011875 /* BDW+ don't expose a synchronous way to read the state */
11876 if (IS_HASWELL(dev))
11877 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030011878
Ville Syrjälä282740f2013-09-04 18:30:03 +030011879 PIPE_CONF_CHECK_I(double_wide);
11880
Daniel Vetter26804af2014-06-25 22:01:55 +030011881 PIPE_CONF_CHECK_X(ddi_pll_sel);
11882
Daniel Vetterc0d43d62013-06-07 23:11:08 +020011883 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020011884 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020011885 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020011886 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
11887 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030011888 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000011889 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
11890 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
11891 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020011892
Ville Syrjälä42571ae2013-09-06 23:29:00 +030011893 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
11894 PIPE_CONF_CHECK_I(pipe_bpp);
11895
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011896 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080011897 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030011898
Daniel Vetter66e985c2013-06-05 13:34:20 +020011899#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020011900#undef PIPE_CONF_CHECK_I
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011901#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011902#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030011903#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020011904#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +020011905
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011906 return true;
11907}
11908
Damien Lespiau08db6652014-11-04 17:06:52 +000011909static void check_wm_state(struct drm_device *dev)
11910{
11911 struct drm_i915_private *dev_priv = dev->dev_private;
11912 struct skl_ddb_allocation hw_ddb, *sw_ddb;
11913 struct intel_crtc *intel_crtc;
11914 int plane;
11915
11916 if (INTEL_INFO(dev)->gen < 9)
11917 return;
11918
11919 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
11920 sw_ddb = &dev_priv->wm.skl_hw.ddb;
11921
11922 for_each_intel_crtc(dev, intel_crtc) {
11923 struct skl_ddb_entry *hw_entry, *sw_entry;
11924 const enum pipe pipe = intel_crtc->pipe;
11925
11926 if (!intel_crtc->active)
11927 continue;
11928
11929 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000011930 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000011931 hw_entry = &hw_ddb.plane[pipe][plane];
11932 sw_entry = &sw_ddb->plane[pipe][plane];
11933
11934 if (skl_ddb_entry_equal(hw_entry, sw_entry))
11935 continue;
11936
11937 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
11938 "(expected (%u,%u), found (%u,%u))\n",
11939 pipe_name(pipe), plane + 1,
11940 sw_entry->start, sw_entry->end,
11941 hw_entry->start, hw_entry->end);
11942 }
11943
11944 /* cursor */
11945 hw_entry = &hw_ddb.cursor[pipe];
11946 sw_entry = &sw_ddb->cursor[pipe];
11947
11948 if (skl_ddb_entry_equal(hw_entry, sw_entry))
11949 continue;
11950
11951 DRM_ERROR("mismatch in DDB state pipe %c cursor "
11952 "(expected (%u,%u), found (%u,%u))\n",
11953 pipe_name(pipe),
11954 sw_entry->start, sw_entry->end,
11955 hw_entry->start, hw_entry->end);
11956 }
11957}
11958
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011959static void
11960check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011961{
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011962 struct intel_connector *connector;
11963
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011964 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011965 /* This also checks the encoder/connector hw state with the
11966 * ->get_hw_state callbacks. */
11967 intel_connector_check_state(connector);
11968
Rob Clarke2c719b2014-12-15 13:56:32 -050011969 I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011970 "connector's staged encoder doesn't match current encoder\n");
11971 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011972}
11973
11974static void
11975check_encoder_state(struct drm_device *dev)
11976{
11977 struct intel_encoder *encoder;
11978 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011979
Damien Lespiaub2784e12014-08-05 11:29:37 +010011980 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011981 bool enabled = false;
11982 bool active = false;
11983 enum pipe pipe, tracked_pipe;
11984
11985 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
11986 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030011987 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011988
Rob Clarke2c719b2014-12-15 13:56:32 -050011989 I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011990 "encoder's stage crtc doesn't match current crtc\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050011991 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011992 "encoder's active_connectors set, but no crtc\n");
11993
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011994 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011995 if (connector->base.encoder != &encoder->base)
11996 continue;
11997 enabled = true;
11998 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
11999 active = true;
12000 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012001 /*
12002 * for MST connectors if we unplug the connector is gone
12003 * away but the encoder is still connected to a crtc
12004 * until a modeset happens in response to the hotplug.
12005 */
12006 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
12007 continue;
12008
Rob Clarke2c719b2014-12-15 13:56:32 -050012009 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012010 "encoder's enabled state mismatch "
12011 "(expected %i, found %i)\n",
12012 !!encoder->base.crtc, enabled);
Rob Clarke2c719b2014-12-15 13:56:32 -050012013 I915_STATE_WARN(active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012014 "active encoder with no crtc\n");
12015
Rob Clarke2c719b2014-12-15 13:56:32 -050012016 I915_STATE_WARN(encoder->connectors_active != active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012017 "encoder's computed active state doesn't match tracked active state "
12018 "(expected %i, found %i)\n", active, encoder->connectors_active);
12019
12020 active = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -050012021 I915_STATE_WARN(active != encoder->connectors_active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012022 "encoder's hw state doesn't match sw tracking "
12023 "(expected %i, found %i)\n",
12024 encoder->connectors_active, active);
12025
12026 if (!encoder->base.crtc)
12027 continue;
12028
12029 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
Rob Clarke2c719b2014-12-15 13:56:32 -050012030 I915_STATE_WARN(active && pipe != tracked_pipe,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012031 "active encoder's pipe doesn't match"
12032 "(expected %i, found %i)\n",
12033 tracked_pipe, pipe);
12034
12035 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012036}
12037
12038static void
12039check_crtc_state(struct drm_device *dev)
12040{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012041 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012042 struct intel_crtc *crtc;
12043 struct intel_encoder *encoder;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012044 struct intel_crtc_state pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012045
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012046 for_each_intel_crtc(dev, crtc) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012047 bool enabled = false;
12048 bool active = false;
12049
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012050 memset(&pipe_config, 0, sizeof(pipe_config));
12051
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012052 DRM_DEBUG_KMS("[CRTC:%d]\n",
12053 crtc->base.base.id);
12054
Matt Roper83d65732015-02-25 13:12:16 -080012055 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012056 "active crtc, but not enabled in sw tracking\n");
12057
Damien Lespiaub2784e12014-08-05 11:29:37 +010012058 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012059 if (encoder->base.crtc != &crtc->base)
12060 continue;
12061 enabled = true;
12062 if (encoder->connectors_active)
12063 active = true;
12064 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020012065
Rob Clarke2c719b2014-12-15 13:56:32 -050012066 I915_STATE_WARN(active != crtc->active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012067 "crtc's computed active state doesn't match tracked active state "
12068 "(expected %i, found %i)\n", active, crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080012069 I915_STATE_WARN(enabled != crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012070 "crtc's computed enabled state doesn't match tracked enabled state "
Matt Roper83d65732015-02-25 13:12:16 -080012071 "(expected %i, found %i)\n", enabled,
12072 crtc->base.state->enable);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012073
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012074 active = dev_priv->display.get_pipe_config(crtc,
12075 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +020012076
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030012077 /* hw state is inconsistent with the pipe quirk */
12078 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12079 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetterd62cf622013-05-29 10:41:29 +020012080 active = crtc->active;
12081
Damien Lespiaub2784e12014-08-05 11:29:37 +010012082 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +030012083 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +020012084 if (encoder->base.crtc != &crtc->base)
12085 continue;
Daniel Vetter1d37b682013-11-18 09:00:59 +010012086 if (encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +020012087 encoder->get_config(encoder, &pipe_config);
12088 }
12089
Rob Clarke2c719b2014-12-15 13:56:32 -050012090 I915_STATE_WARN(crtc->active != active,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012091 "crtc active state doesn't match with hw state "
12092 "(expected %i, found %i)\n", crtc->active, active);
12093
Daniel Vetterc0b03412013-05-28 12:05:54 +020012094 if (active &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012095 !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050012096 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Daniel Vetterc0b03412013-05-28 12:05:54 +020012097 intel_dump_pipe_config(crtc, &pipe_config,
12098 "[hw state]");
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012099 intel_dump_pipe_config(crtc, crtc->config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012100 "[sw state]");
12101 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012102 }
12103}
12104
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012105static void
12106check_shared_dpll_state(struct drm_device *dev)
12107{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012108 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012109 struct intel_crtc *crtc;
12110 struct intel_dpll_hw_state dpll_hw_state;
12111 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020012112
12113 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12114 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12115 int enabled_crtcs = 0, active_crtcs = 0;
12116 bool active;
12117
12118 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12119
12120 DRM_DEBUG_KMS("%s\n", pll->name);
12121
12122 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
12123
Rob Clarke2c719b2014-12-15 13:56:32 -050012124 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
Daniel Vetter53589012013-06-05 13:34:16 +020012125 "more active pll users than references: %i vs %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012126 pll->active, hweight32(pll->config.crtc_mask));
Rob Clarke2c719b2014-12-15 13:56:32 -050012127 I915_STATE_WARN(pll->active && !pll->on,
Daniel Vetter53589012013-06-05 13:34:16 +020012128 "pll in active use but not on in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012129 I915_STATE_WARN(pll->on && !pll->active,
Daniel Vetter35c95372013-07-17 06:55:04 +020012130 "pll in on but not on in use in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012131 I915_STATE_WARN(pll->on != active,
Daniel Vetter53589012013-06-05 13:34:16 +020012132 "pll on state mismatch (expected %i, found %i)\n",
12133 pll->on, active);
12134
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012135 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080012136 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
Daniel Vetter53589012013-06-05 13:34:16 +020012137 enabled_crtcs++;
12138 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12139 active_crtcs++;
12140 }
Rob Clarke2c719b2014-12-15 13:56:32 -050012141 I915_STATE_WARN(pll->active != active_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012142 "pll active crtcs mismatch (expected %i, found %i)\n",
12143 pll->active, active_crtcs);
Rob Clarke2c719b2014-12-15 13:56:32 -050012144 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012145 "pll enabled crtcs mismatch (expected %i, found %i)\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012146 hweight32(pll->config.crtc_mask), enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012147
Rob Clarke2c719b2014-12-15 13:56:32 -050012148 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020012149 sizeof(dpll_hw_state)),
12150 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020012151 }
Daniel Vettera6778b32012-07-02 09:56:42 +020012152}
12153
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012154void
12155intel_modeset_check_state(struct drm_device *dev)
12156{
Damien Lespiau08db6652014-11-04 17:06:52 +000012157 check_wm_state(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012158 check_connector_state(dev);
12159 check_encoder_state(dev);
12160 check_crtc_state(dev);
12161 check_shared_dpll_state(dev);
12162}
12163
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012164void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
Ville Syrjälä18442d02013-09-13 16:00:08 +030012165 int dotclock)
12166{
12167 /*
12168 * FDI already provided one idea for the dotclock.
12169 * Yell if the encoder disagrees.
12170 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012171 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030012172 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012173 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030012174}
12175
Ville Syrjälä80715b22014-05-15 20:23:23 +030012176static void update_scanline_offset(struct intel_crtc *crtc)
12177{
12178 struct drm_device *dev = crtc->base.dev;
12179
12180 /*
12181 * The scanline counter increments at the leading edge of hsync.
12182 *
12183 * On most platforms it starts counting from vtotal-1 on the
12184 * first active line. That means the scanline counter value is
12185 * always one less than what we would expect. Ie. just after
12186 * start of vblank, which also occurs at start of hsync (on the
12187 * last active line), the scanline counter will read vblank_start-1.
12188 *
12189 * On gen2 the scanline counter starts counting from 1 instead
12190 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12191 * to keep the value positive), instead of adding one.
12192 *
12193 * On HSW+ the behaviour of the scanline counter depends on the output
12194 * type. For DP ports it behaves like most other platforms, but on HDMI
12195 * there's an extra 1 line difference. So we need to add two instead of
12196 * one to the value.
12197 */
12198 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012199 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012200 int vtotal;
12201
12202 vtotal = mode->crtc_vtotal;
12203 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
12204 vtotal /= 2;
12205
12206 crtc->scanline_offset = vtotal - 1;
12207 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030012208 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030012209 crtc->scanline_offset = 2;
12210 } else
12211 crtc->scanline_offset = 1;
12212}
12213
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012214static struct intel_crtc_state *
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012215intel_modeset_compute_config(struct drm_crtc *crtc,
12216 struct drm_display_mode *mode,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012217 struct drm_atomic_state *state,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012218 unsigned *modeset_pipes,
12219 unsigned *prepare_pipes,
12220 unsigned *disable_pipes)
12221{
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020012222 struct drm_device *dev = crtc->dev;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012223 struct intel_crtc_state *pipe_config = NULL;
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020012224 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012225 int ret = 0;
12226
12227 ret = drm_atomic_add_affected_connectors(state, crtc);
12228 if (ret)
12229 return ERR_PTR(ret);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012230
12231 intel_modeset_affected_pipes(crtc, modeset_pipes,
12232 prepare_pipes, disable_pipes);
12233
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020012234 for_each_intel_crtc_masked(dev, *disable_pipes, intel_crtc) {
12235 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
12236 if (IS_ERR(pipe_config))
12237 return pipe_config;
12238
12239 pipe_config->base.enable = false;
12240 }
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012241
12242 /*
12243 * Note this needs changes when we start tracking multiple modes
12244 * and crtcs. At that point we'll need to compute the whole config
12245 * (i.e. one pipe_config for each crtc) rather than just the one
12246 * for this crtc.
12247 */
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020012248 for_each_intel_crtc_masked(dev, *modeset_pipes, intel_crtc) {
12249 /* FIXME: For now we still expect modeset_pipes has at most
12250 * one bit set. */
12251 if (WARN_ON(&intel_crtc->base != crtc))
12252 continue;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012253
Daniel Vetter8805aa72015-04-10 16:22:38 +020012254 pipe_config = intel_modeset_pipe_config(crtc, mode, state);
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020012255 if (IS_ERR(pipe_config))
12256 return pipe_config;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012257
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030012258 pipe_config->base.enable = true;
12259
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020012260 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
12261 "[modeset]");
12262 }
12263
12264 return intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012265}
12266
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012267static int __intel_set_mode_setup_plls(struct drm_atomic_state *state,
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012268 unsigned modeset_pipes,
12269 unsigned disable_pipes)
12270{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012271 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012272 struct drm_i915_private *dev_priv = to_i915(dev);
12273 unsigned clear_pipes = modeset_pipes | disable_pipes;
12274 struct intel_crtc *intel_crtc;
12275 int ret = 0;
12276
12277 if (!dev_priv->display.crtc_compute_clock)
12278 return 0;
12279
12280 ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
12281 if (ret)
12282 goto done;
12283
12284 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012285 struct intel_crtc_state *crtc_state =
12286 intel_atomic_get_crtc_state(state, intel_crtc);
12287
12288 /* Modeset pipes should have a new state by now */
12289 if (WARN_ON(IS_ERR(crtc_state)))
12290 continue;
12291
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012292 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012293 crtc_state);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012294 if (ret) {
12295 intel_shared_dpll_abort_config(dev_priv);
12296 goto done;
12297 }
12298 }
12299
12300done:
12301 return ret;
12302}
12303
Daniel Vetterf30da182013-04-11 20:22:50 +020012304static int __intel_set_mode(struct drm_crtc *crtc,
12305 struct drm_display_mode *mode,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012306 int x, int y, struct drm_framebuffer *fb,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012307 struct intel_crtc_state *pipe_config,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012308 unsigned modeset_pipes,
12309 unsigned prepare_pipes,
12310 unsigned disable_pipes)
Daniel Vettera6778b32012-07-02 09:56:42 +020012311{
12312 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030012313 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030012314 struct drm_display_mode *saved_mode;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030012315 struct drm_atomic_state *state = pipe_config->base.state;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012316 struct intel_crtc_state *crtc_state_copy = NULL;
Daniel Vetter25c5b262012-07-08 22:08:04 +020012317 struct intel_crtc *intel_crtc;
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012318 int ret = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020012319
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030012320 saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012321 if (!saved_mode)
12322 return -ENOMEM;
Daniel Vettera6778b32012-07-02 09:56:42 +020012323
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012324 crtc_state_copy = kmalloc(sizeof(*crtc_state_copy), GFP_KERNEL);
12325 if (!crtc_state_copy) {
12326 ret = -ENOMEM;
12327 goto done;
12328 }
12329
Tim Gardner3ac18232012-12-07 07:54:26 -070012330 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +020012331
Jesse Barnes30a970c2013-11-04 13:48:12 -080012332 /*
12333 * See if the config requires any additional preparation, e.g.
12334 * to adjust global state with pipes off. We need to do this
12335 * here so we can get the modeset_pipe updated config for the new
12336 * mode set on this crtc. For other crtcs we need to use the
12337 * adjusted_mode bits in the crtc directly.
12338 */
Vandana Kannanf8437dd12014-11-24 13:37:39 +053012339 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030012340 ret = valleyview_modeset_global_pipes(state, &prepare_pipes);
12341 if (ret)
12342 goto done;
Jesse Barnes30a970c2013-11-04 13:48:12 -080012343
Ville Syrjäläc164f832013-11-05 22:34:12 +020012344 /* may have added more to prepare_pipes than we should */
12345 prepare_pipes &= ~disable_pipes;
12346 }
12347
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012348 ret = __intel_set_mode_setup_plls(state, modeset_pipes, disable_pipes);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012349 if (ret)
12350 goto done;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +020012351
Daniel Vetter460da9162013-03-27 00:44:51 +010012352 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
12353 intel_crtc_disable(&intel_crtc->base);
12354
Daniel Vetterea9d7582012-07-10 10:42:52 +020012355 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080012356 if (intel_crtc->base.state->enable)
Daniel Vetterea9d7582012-07-10 10:42:52 +020012357 dev_priv->display.crtc_disable(&intel_crtc->base);
12358 }
Daniel Vettera6778b32012-07-02 09:56:42 +020012359
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020012360 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
12361 * to set it here already despite that we pass it down the callchain.
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012362 *
12363 * Note we'll need to fix this up when we start tracking multiple
12364 * pipes; here we assume a single modeset_pipe and only track the
12365 * single crtc and mode.
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020012366 */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012367 if (modeset_pipes) {
Daniel Vetter25c5b262012-07-08 22:08:04 +020012368 crtc->mode = *mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012369 /* mode_set/enable/disable functions rely on a correct pipe
12370 * config. */
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020012371 intel_crtc_set_state(to_intel_crtc(crtc), pipe_config);
Ville Syrjäläc326c0a2013-10-28 12:53:41 +020012372
12373 /*
12374 * Calculate and store various constants which
12375 * are later needed by vblank and swap-completion
12376 * timestamping. They are derived from true hwmode.
12377 */
12378 drm_calc_timestamping_constants(crtc,
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012379 &pipe_config->base.adjusted_mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012380 }
Daniel Vetter7758a112012-07-08 19:40:39 +020012381
Daniel Vetterea9d7582012-07-10 10:42:52 +020012382 /* Only after disabling all output pipelines that will be changed can we
12383 * update the the output configuration. */
12384 intel_modeset_update_state(dev, prepare_pipes);
12385
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030012386 modeset_update_crtc_power_domains(state);
Daniel Vetter47fab732012-10-26 10:58:18 +020012387
Daniel Vetter25c5b262012-07-08 22:08:04 +020012388 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Gustavo Padovan455a6802014-12-01 15:40:11 -080012389 struct drm_plane *primary = intel_crtc->base.primary;
12390 int vdisplay, hdisplay;
Daniel Vetter4c107942014-04-24 23:55:05 +020012391
Gustavo Padovan455a6802014-12-01 15:40:11 -080012392 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
Matt Roper70a101f2015-04-08 18:56:53 -070012393 ret = drm_plane_helper_update(primary, &intel_crtc->base,
12394 fb, 0, 0,
12395 hdisplay, vdisplay,
12396 x << 16, y << 16,
12397 hdisplay << 16, vdisplay << 16);
Daniel Vettera6778b32012-07-02 09:56:42 +020012398 }
12399
12400 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ville Syrjälä80715b22014-05-15 20:23:23 +030012401 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
12402 update_scanline_offset(intel_crtc);
12403
Daniel Vetter25c5b262012-07-08 22:08:04 +020012404 dev_priv->display.crtc_enable(&intel_crtc->base);
Ville Syrjälä80715b22014-05-15 20:23:23 +030012405 }
Daniel Vettera6778b32012-07-02 09:56:42 +020012406
Daniel Vettera6778b32012-07-02 09:56:42 +020012407 /* FIXME: add subpixel order */
12408done:
Matt Roper83d65732015-02-25 13:12:16 -080012409 if (ret && crtc->state->enable)
Tim Gardner3ac18232012-12-07 07:54:26 -070012410 crtc->mode = *saved_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +020012411
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012412 if (ret == 0 && pipe_config) {
12413 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12414
12415 /* The pipe_config will be freed with the atomic state, so
12416 * make a copy. */
12417 memcpy(crtc_state_copy, intel_crtc->config,
12418 sizeof *crtc_state_copy);
12419 intel_crtc->config = crtc_state_copy;
12420 intel_crtc->base.state = &crtc_state_copy->base;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012421 } else {
12422 kfree(crtc_state_copy);
12423 }
12424
Tim Gardner3ac18232012-12-07 07:54:26 -070012425 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +020012426 return ret;
12427}
12428
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012429static int intel_set_mode_pipes(struct drm_crtc *crtc,
12430 struct drm_display_mode *mode,
12431 int x, int y, struct drm_framebuffer *fb,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012432 struct intel_crtc_state *pipe_config,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012433 unsigned modeset_pipes,
12434 unsigned prepare_pipes,
12435 unsigned disable_pipes)
12436{
12437 int ret;
12438
12439 ret = __intel_set_mode(crtc, mode, x, y, fb, pipe_config, modeset_pipes,
12440 prepare_pipes, disable_pipes);
12441
12442 if (ret == 0)
12443 intel_modeset_check_state(crtc->dev);
12444
12445 return ret;
12446}
12447
Damien Lespiaue7457a92013-08-08 22:28:59 +010012448static int intel_set_mode(struct drm_crtc *crtc,
12449 struct drm_display_mode *mode,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012450 int x, int y, struct drm_framebuffer *fb,
12451 struct drm_atomic_state *state)
Daniel Vetterf30da182013-04-11 20:22:50 +020012452{
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012453 struct intel_crtc_state *pipe_config;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012454 unsigned modeset_pipes, prepare_pipes, disable_pipes;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012455 int ret = 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020012456
Daniel Vetter8805aa72015-04-10 16:22:38 +020012457 pipe_config = intel_modeset_compute_config(crtc, mode, state,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012458 &modeset_pipes,
12459 &prepare_pipes,
12460 &disable_pipes);
Daniel Vetterf30da182013-04-11 20:22:50 +020012461
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012462 if (IS_ERR(pipe_config)) {
12463 ret = PTR_ERR(pipe_config);
12464 goto out;
12465 }
Daniel Vetterf30da182013-04-11 20:22:50 +020012466
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012467 ret = intel_set_mode_pipes(crtc, mode, x, y, fb, pipe_config,
12468 modeset_pipes, prepare_pipes,
12469 disable_pipes);
12470 if (ret)
12471 goto out;
12472
12473out:
12474 return ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020012475}
12476
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012477void intel_crtc_restore_mode(struct drm_crtc *crtc)
12478{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012479 struct drm_device *dev = crtc->dev;
12480 struct drm_atomic_state *state;
12481 struct intel_encoder *encoder;
12482 struct intel_connector *connector;
12483 struct drm_connector_state *connector_state;
12484
12485 state = drm_atomic_state_alloc(dev);
12486 if (!state) {
12487 DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
12488 crtc->base.id);
12489 return;
12490 }
12491
12492 state->acquire_ctx = dev->mode_config.acquire_ctx;
12493
12494 /* The force restore path in the HW readout code relies on the staged
12495 * config still keeping the user requested config while the actual
12496 * state has been overwritten by the configuration read from HW. We
12497 * need to copy the staged config to the atomic state, otherwise the
12498 * mode set will just reapply the state the HW is already in. */
12499 for_each_intel_encoder(dev, encoder) {
12500 if (&encoder->new_crtc->base != crtc)
12501 continue;
12502
12503 for_each_intel_connector(dev, connector) {
12504 if (connector->new_encoder != encoder)
12505 continue;
12506
12507 connector_state = drm_atomic_get_connector_state(state, &connector->base);
12508 if (IS_ERR(connector_state)) {
12509 DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
12510 connector->base.base.id,
12511 connector->base.name,
12512 PTR_ERR(connector_state));
12513 continue;
12514 }
12515
12516 connector_state->crtc = crtc;
12517 connector_state->best_encoder = &encoder->base;
12518 }
12519 }
12520
12521 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb,
12522 state);
12523
12524 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012525}
12526
Daniel Vetter25c5b262012-07-08 22:08:04 +020012527#undef for_each_intel_crtc_masked
12528
Daniel Vetterd9e55602012-07-04 22:16:09 +020012529static void intel_set_config_free(struct intel_set_config *config)
12530{
12531 if (!config)
12532 return;
12533
Daniel Vetter1aa4b622012-07-05 16:20:48 +020012534 kfree(config->save_connector_encoders);
12535 kfree(config->save_encoder_crtcs);
Ville Syrjälä76688512014-01-10 11:28:06 +020012536 kfree(config->save_crtc_enabled);
Daniel Vetterd9e55602012-07-04 22:16:09 +020012537 kfree(config);
12538}
12539
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012540static int intel_set_config_save_state(struct drm_device *dev,
12541 struct intel_set_config *config)
12542{
Ville Syrjälä76688512014-01-10 11:28:06 +020012543 struct drm_crtc *crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012544 struct drm_encoder *encoder;
12545 struct drm_connector *connector;
12546 int count;
12547
Ville Syrjälä76688512014-01-10 11:28:06 +020012548 config->save_crtc_enabled =
12549 kcalloc(dev->mode_config.num_crtc,
12550 sizeof(bool), GFP_KERNEL);
12551 if (!config->save_crtc_enabled)
12552 return -ENOMEM;
12553
Daniel Vetter1aa4b622012-07-05 16:20:48 +020012554 config->save_encoder_crtcs =
12555 kcalloc(dev->mode_config.num_encoder,
12556 sizeof(struct drm_crtc *), GFP_KERNEL);
12557 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012558 return -ENOMEM;
12559
Daniel Vetter1aa4b622012-07-05 16:20:48 +020012560 config->save_connector_encoders =
12561 kcalloc(dev->mode_config.num_connector,
12562 sizeof(struct drm_encoder *), GFP_KERNEL);
12563 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012564 return -ENOMEM;
12565
12566 /* Copy data. Note that driver private data is not affected.
12567 * Should anything bad happen only the expected state is
12568 * restored, not the drivers personal bookkeeping.
12569 */
12570 count = 0;
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010012571 for_each_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080012572 config->save_crtc_enabled[count++] = crtc->state->enable;
Ville Syrjälä76688512014-01-10 11:28:06 +020012573 }
12574
12575 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012576 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +020012577 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012578 }
12579
12580 count = 0;
12581 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +020012582 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012583 }
12584
12585 return 0;
12586}
12587
12588static void intel_set_config_restore_state(struct drm_device *dev,
12589 struct intel_set_config *config)
12590{
Ville Syrjälä76688512014-01-10 11:28:06 +020012591 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020012592 struct intel_encoder *encoder;
12593 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012594 int count;
12595
12596 count = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012597 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020012598 crtc->new_enabled = config->save_crtc_enabled[count++];
12599 }
12600
12601 count = 0;
Damien Lespiaub2784e12014-08-05 11:29:37 +010012602 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012603 encoder->new_crtc =
12604 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012605 }
12606
12607 count = 0;
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012608 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012609 connector->new_encoder =
12610 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012611 }
12612}
12613
Imre Deake3de42b2013-05-03 19:44:07 +020012614static bool
Chris Wilson2e57f472013-07-17 12:14:40 +010012615is_crtc_connector_off(struct drm_mode_set *set)
Imre Deake3de42b2013-05-03 19:44:07 +020012616{
12617 int i;
12618
Chris Wilson2e57f472013-07-17 12:14:40 +010012619 if (set->num_connectors == 0)
12620 return false;
12621
12622 if (WARN_ON(set->connectors == NULL))
12623 return false;
12624
12625 for (i = 0; i < set->num_connectors; i++)
12626 if (set->connectors[i]->encoder &&
12627 set->connectors[i]->encoder->crtc == set->crtc &&
12628 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
Imre Deake3de42b2013-05-03 19:44:07 +020012629 return true;
12630
12631 return false;
12632}
12633
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012634static void
12635intel_set_config_compute_mode_changes(struct drm_mode_set *set,
12636 struct intel_set_config *config)
12637{
12638
12639 /* We should be able to check here if the fb has the same properties
12640 * and then just flip_or_move it */
Chris Wilson2e57f472013-07-17 12:14:40 +010012641 if (is_crtc_connector_off(set)) {
12642 config->mode_changed = true;
Matt Roperf4510a22014-04-01 15:22:40 -070012643 } else if (set->crtc->primary->fb != set->fb) {
Matt Roper3b150f02014-05-29 08:06:53 -070012644 /*
12645 * If we have no fb, we can only flip as long as the crtc is
12646 * active, otherwise we need a full mode set. The crtc may
12647 * be active if we've only disabled the primary plane, or
12648 * in fastboot situations.
12649 */
Matt Roperf4510a22014-04-01 15:22:40 -070012650 if (set->crtc->primary->fb == NULL) {
Jesse Barnes319d9822013-06-26 01:38:19 +030012651 struct intel_crtc *intel_crtc =
12652 to_intel_crtc(set->crtc);
12653
Matt Roper3b150f02014-05-29 08:06:53 -070012654 if (intel_crtc->active) {
Jesse Barnes319d9822013-06-26 01:38:19 +030012655 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
12656 config->fb_changed = true;
12657 } else {
12658 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
12659 config->mode_changed = true;
12660 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012661 } else if (set->fb == NULL) {
12662 config->mode_changed = true;
Daniel Vetter72f49012013-03-28 16:01:35 +010012663 } else if (set->fb->pixel_format !=
Matt Roperf4510a22014-04-01 15:22:40 -070012664 set->crtc->primary->fb->pixel_format) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012665 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +020012666 } else {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012667 config->fb_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +020012668 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012669 }
12670
Daniel Vetter835c5872012-07-10 18:11:08 +020012671 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012672 config->fb_changed = true;
12673
12674 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
12675 DRM_DEBUG_KMS("modes are different, full mode set\n");
12676 drm_mode_debug_printmodeline(&set->crtc->mode);
12677 drm_mode_debug_printmodeline(set->mode);
12678 config->mode_changed = true;
12679 }
Chris Wilsona1d95702013-08-13 18:48:47 +010012680
12681 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
12682 set->crtc->base.id, config->mode_changed, config->fb_changed);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012683}
12684
Daniel Vetter2e431052012-07-04 22:42:15 +020012685static int
Daniel Vetter9a935852012-07-05 22:34:27 +020012686intel_modeset_stage_output_state(struct drm_device *dev,
12687 struct drm_mode_set *set,
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012688 struct intel_set_config *config,
12689 struct drm_atomic_state *state)
Daniel Vetter50f56112012-07-02 09:35:43 +020012690{
Daniel Vetter9a935852012-07-05 22:34:27 +020012691 struct intel_connector *connector;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012692 struct drm_connector_state *connector_state;
Daniel Vetter9a935852012-07-05 22:34:27 +020012693 struct intel_encoder *encoder;
Ville Syrjälä76688512014-01-10 11:28:06 +020012694 struct intel_crtc *crtc;
Paulo Zanonif3f08572013-08-12 14:56:53 -030012695 int ro;
Daniel Vetter50f56112012-07-02 09:35:43 +020012696
Damien Lespiau9abdda72013-02-13 13:29:23 +000012697 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +020012698 * of connectors. For paranoia, double-check this. */
12699 WARN_ON(!set->fb && (set->num_connectors != 0));
12700 WARN_ON(set->fb && (set->num_connectors == 0));
12701
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012702 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012703 /* Otherwise traverse passed in connector list and get encoders
12704 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +020012705 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012706 if (set->connectors[ro] == &connector->base) {
Dave Airlie0e32b392014-05-02 14:02:48 +100012707 connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
Daniel Vetter50f56112012-07-02 09:35:43 +020012708 break;
12709 }
12710 }
12711
Daniel Vetter9a935852012-07-05 22:34:27 +020012712 /* If we disable the crtc, disable all its connectors. Also, if
12713 * the connector is on the changing crtc but not on the new
12714 * connector list, disable it. */
12715 if ((!set->fb || ro == set->num_connectors) &&
12716 connector->base.encoder &&
12717 connector->base.encoder->crtc == set->crtc) {
12718 connector->new_encoder = NULL;
12719
12720 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
12721 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012722 connector->base.name);
Daniel Vetter9a935852012-07-05 22:34:27 +020012723 }
12724
12725
12726 if (&connector->new_encoder->base != connector->base.encoder) {
Ander Conselvan de Oliveira10634182015-03-03 15:21:57 +020012727 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] encoder changed, full mode switch\n",
12728 connector->base.base.id,
12729 connector->base.name);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012730 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +020012731 }
Daniel Vetter9a935852012-07-05 22:34:27 +020012732 }
12733 /* connector->new_encoder is now updated for all connectors. */
12734
12735 /* Update crtc of enabled connectors. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012736 for_each_intel_connector(dev, connector) {
Ville Syrjälä76688512014-01-10 11:28:06 +020012737 struct drm_crtc *new_crtc;
12738
Daniel Vetter9a935852012-07-05 22:34:27 +020012739 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +020012740 continue;
12741
Daniel Vetter9a935852012-07-05 22:34:27 +020012742 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +020012743
12744 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012745 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +020012746 new_crtc = set->crtc;
12747 }
12748
12749 /* Make sure the new CRTC will work with the encoder */
Thierry Reding14509912014-01-13 12:00:22 +010012750 if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
12751 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012752 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +020012753 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012754 connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
Daniel Vetter9a935852012-07-05 22:34:27 +020012755
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012756 connector_state =
12757 drm_atomic_get_connector_state(state, &connector->base);
12758 if (IS_ERR(connector_state))
12759 return PTR_ERR(connector_state);
12760
12761 connector_state->crtc = new_crtc;
12762 connector_state->best_encoder = &connector->new_encoder->base;
12763
Daniel Vetter9a935852012-07-05 22:34:27 +020012764 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
12765 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012766 connector->base.name,
Daniel Vetter9a935852012-07-05 22:34:27 +020012767 new_crtc->base.id);
12768 }
12769
12770 /* Check for any encoders that needs to be disabled. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010012771 for_each_intel_encoder(dev, encoder) {
Paulo Zanoni5a65f352014-01-07 14:55:53 -020012772 int num_connectors = 0;
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012773 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012774 if (connector->new_encoder == encoder) {
12775 WARN_ON(!connector->new_encoder->new_crtc);
Paulo Zanoni5a65f352014-01-07 14:55:53 -020012776 num_connectors++;
Daniel Vetter9a935852012-07-05 22:34:27 +020012777 }
12778 }
Paulo Zanoni5a65f352014-01-07 14:55:53 -020012779
12780 if (num_connectors == 0)
12781 encoder->new_crtc = NULL;
12782 else if (num_connectors > 1)
12783 return -EINVAL;
12784
Daniel Vetter9a935852012-07-05 22:34:27 +020012785 /* Only now check for crtc changes so we don't miss encoders
12786 * that will be disabled. */
12787 if (&encoder->new_crtc->base != encoder->base.crtc) {
Ander Conselvan de Oliveira10634182015-03-03 15:21:57 +020012788 DRM_DEBUG_KMS("[ENCODER:%d:%s] crtc changed, full mode switch\n",
12789 encoder->base.base.id,
12790 encoder->base.name);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012791 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +020012792 }
12793 }
Daniel Vetter9a935852012-07-05 22:34:27 +020012794 /* Now we've also updated encoder->new_crtc for all encoders. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012795 for_each_intel_connector(dev, connector) {
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012796 connector_state =
12797 drm_atomic_get_connector_state(state, &connector->base);
Ander Conselvan de Oliveira9d918c12015-03-27 15:33:51 +020012798 if (IS_ERR(connector_state))
12799 return PTR_ERR(connector_state);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012800
12801 if (connector->new_encoder) {
Dave Airlie0e32b392014-05-02 14:02:48 +100012802 if (connector->new_encoder != connector->encoder)
12803 connector->encoder = connector->new_encoder;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012804 } else {
12805 connector_state->crtc = NULL;
Ander Conselvan de Oliveiraf61cccf2015-03-31 11:35:00 +030012806 connector_state->best_encoder = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012807 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012808 }
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012809 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020012810 crtc->new_enabled = false;
12811
Damien Lespiaub2784e12014-08-05 11:29:37 +010012812 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä76688512014-01-10 11:28:06 +020012813 if (encoder->new_crtc == crtc) {
12814 crtc->new_enabled = true;
12815 break;
12816 }
12817 }
12818
Matt Roper83d65732015-02-25 13:12:16 -080012819 if (crtc->new_enabled != crtc->base.state->enable) {
Ander Conselvan de Oliveira10634182015-03-03 15:21:57 +020012820 DRM_DEBUG_KMS("[CRTC:%d] %sabled, full mode switch\n",
12821 crtc->base.base.id,
Ville Syrjälä76688512014-01-10 11:28:06 +020012822 crtc->new_enabled ? "en" : "dis");
12823 config->mode_changed = true;
12824 }
12825 }
12826
Daniel Vetter2e431052012-07-04 22:42:15 +020012827 return 0;
12828}
12829
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020012830static void disable_crtc_nofb(struct intel_crtc *crtc)
12831{
12832 struct drm_device *dev = crtc->base.dev;
12833 struct intel_encoder *encoder;
12834 struct intel_connector *connector;
12835
12836 DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
12837 pipe_name(crtc->pipe));
12838
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012839 for_each_intel_connector(dev, connector) {
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020012840 if (connector->new_encoder &&
12841 connector->new_encoder->new_crtc == crtc)
12842 connector->new_encoder = NULL;
12843 }
12844
Damien Lespiaub2784e12014-08-05 11:29:37 +010012845 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020012846 if (encoder->new_crtc == crtc)
12847 encoder->new_crtc = NULL;
12848 }
12849
12850 crtc->new_enabled = false;
12851}
12852
Daniel Vetter2e431052012-07-04 22:42:15 +020012853static int intel_crtc_set_config(struct drm_mode_set *set)
12854{
12855 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +020012856 struct drm_mode_set save_set;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012857 struct drm_atomic_state *state = NULL;
Daniel Vetter2e431052012-07-04 22:42:15 +020012858 struct intel_set_config *config;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012859 struct intel_crtc_state *pipe_config;
Jesse Barnes50f52752014-11-07 13:11:00 -080012860 unsigned modeset_pipes, prepare_pipes, disable_pipes;
Daniel Vetter2e431052012-07-04 22:42:15 +020012861 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +020012862
Daniel Vetter8d3e3752012-07-05 16:09:09 +020012863 BUG_ON(!set);
12864 BUG_ON(!set->crtc);
12865 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +020012866
Daniel Vetter7e53f3a2013-01-21 10:52:17 +010012867 /* Enforce sane interface api - has been abused by the fb helper. */
12868 BUG_ON(!set->mode && set->fb);
12869 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +020012870
Daniel Vetter2e431052012-07-04 22:42:15 +020012871 if (set->fb) {
12872 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
12873 set->crtc->base.id, set->fb->base.id,
12874 (int)set->num_connectors, set->x, set->y);
12875 } else {
12876 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +020012877 }
12878
12879 dev = set->crtc->dev;
12880
12881 ret = -ENOMEM;
12882 config = kzalloc(sizeof(*config), GFP_KERNEL);
12883 if (!config)
12884 goto out_config;
12885
12886 ret = intel_set_config_save_state(dev, config);
12887 if (ret)
12888 goto out_config;
12889
12890 save_set.crtc = set->crtc;
12891 save_set.mode = &set->crtc->mode;
12892 save_set.x = set->crtc->x;
12893 save_set.y = set->crtc->y;
Matt Roperf4510a22014-04-01 15:22:40 -070012894 save_set.fb = set->crtc->primary->fb;
Daniel Vetter2e431052012-07-04 22:42:15 +020012895
12896 /* Compute whether we need a full modeset, only an fb base update or no
12897 * change at all. In the future we might also check whether only the
12898 * mode changed, e.g. for LVDS where we only change the panel fitter in
12899 * such cases. */
12900 intel_set_config_compute_mode_changes(set, config);
12901
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012902 state = drm_atomic_state_alloc(dev);
12903 if (!state) {
12904 ret = -ENOMEM;
12905 goto out_config;
12906 }
12907
12908 state->acquire_ctx = dev->mode_config.acquire_ctx;
12909
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012910 ret = intel_modeset_stage_output_state(dev, set, config, state);
Daniel Vetter2e431052012-07-04 22:42:15 +020012911 if (ret)
12912 goto fail;
12913
Jesse Barnes50f52752014-11-07 13:11:00 -080012914 pipe_config = intel_modeset_compute_config(set->crtc, set->mode,
Daniel Vetter8805aa72015-04-10 16:22:38 +020012915 state,
Jesse Barnes50f52752014-11-07 13:11:00 -080012916 &modeset_pipes,
12917 &prepare_pipes,
12918 &disable_pipes);
Jesse Barnes20664592014-11-05 14:26:09 -080012919 if (IS_ERR(pipe_config)) {
Matt Roper6ac04832014-11-17 09:59:28 -080012920 ret = PTR_ERR(pipe_config);
Jesse Barnes50f52752014-11-07 13:11:00 -080012921 goto fail;
Jesse Barnes20664592014-11-05 14:26:09 -080012922 } else if (pipe_config) {
Ville Syrjäläb9950a12014-11-21 21:00:36 +020012923 if (pipe_config->has_audio !=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012924 to_intel_crtc(set->crtc)->config->has_audio)
Jesse Barnes20664592014-11-05 14:26:09 -080012925 config->mode_changed = true;
12926
Jesse Barnesaf15d2c2014-12-01 09:54:28 -080012927 /*
12928 * Note we have an issue here with infoframes: current code
12929 * only updates them on the full mode set path per hw
12930 * requirements. So here we should be checking for any
12931 * required changes and forcing a mode set.
12932 */
Jesse Barnes20664592014-11-05 14:26:09 -080012933 }
Jesse Barnes50f52752014-11-07 13:11:00 -080012934
Jesse Barnes1f9954d2014-11-05 14:26:10 -080012935 intel_update_pipe_size(to_intel_crtc(set->crtc));
12936
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012937 if (config->mode_changed) {
Jesse Barnes50f52752014-11-07 13:11:00 -080012938 ret = intel_set_mode_pipes(set->crtc, set->mode,
12939 set->x, set->y, set->fb, pipe_config,
12940 modeset_pipes, prepare_pipes,
12941 disable_pipes);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012942 } else if (config->fb_changed) {
Matt Roper3b150f02014-05-29 08:06:53 -070012943 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080012944 struct drm_plane *primary = set->crtc->primary;
12945 int vdisplay, hdisplay;
Matt Roper3b150f02014-05-29 08:06:53 -070012946
Gustavo Padovan455a6802014-12-01 15:40:11 -080012947 drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
Matt Roper70a101f2015-04-08 18:56:53 -070012948 ret = drm_plane_helper_update(primary, set->crtc, set->fb,
12949 0, 0, hdisplay, vdisplay,
12950 set->x << 16, set->y << 16,
12951 hdisplay << 16, vdisplay << 16);
Matt Roper3b150f02014-05-29 08:06:53 -070012952
12953 /*
12954 * We need to make sure the primary plane is re-enabled if it
12955 * has previously been turned off.
12956 */
12957 if (!intel_crtc->primary_enabled && ret == 0) {
12958 WARN_ON(!intel_crtc->active);
Ville Syrjäläfdd508a2014-08-08 21:51:11 +030012959 intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070012960 }
12961
Jesse Barnes7ca51a32014-01-07 13:50:49 -080012962 /*
12963 * In the fastboot case this may be our only check of the
12964 * state after boot. It would be better to only do it on
12965 * the first update, but we don't have a nice way of doing that
12966 * (and really, set_config isn't used much for high freq page
12967 * flipping, so increasing its cost here shouldn't be a big
12968 * deal).
12969 */
Jani Nikulad330a952014-01-21 11:24:25 +020012970 if (i915.fastboot && ret == 0)
Jesse Barnes7ca51a32014-01-07 13:50:49 -080012971 intel_modeset_check_state(set->crtc->dev);
Daniel Vetter50f56112012-07-02 09:35:43 +020012972 }
12973
Chris Wilson2d05eae2013-05-03 17:36:25 +010012974 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +020012975 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
12976 set->crtc->base.id, ret);
Daniel Vetter50f56112012-07-02 09:35:43 +020012977fail:
Chris Wilson2d05eae2013-05-03 17:36:25 +010012978 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +020012979
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012980 drm_atomic_state_clear(state);
12981
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020012982 /*
12983 * HACK: if the pipe was on, but we didn't have a framebuffer,
12984 * force the pipe off to avoid oopsing in the modeset code
12985 * due to fb==NULL. This should only happen during boot since
12986 * we don't yet reconstruct the FB from the hardware state.
12987 */
12988 if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
12989 disable_crtc_nofb(to_intel_crtc(save_set.crtc));
12990
Chris Wilson2d05eae2013-05-03 17:36:25 +010012991 /* Try to restore the config */
12992 if (config->mode_changed &&
12993 intel_set_mode(save_set.crtc, save_set.mode,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012994 save_set.x, save_set.y, save_set.fb,
12995 state))
Chris Wilson2d05eae2013-05-03 17:36:25 +010012996 DRM_ERROR("failed to restore config after modeset failure\n");
12997 }
Daniel Vetter50f56112012-07-02 09:35:43 +020012998
Daniel Vetterd9e55602012-07-04 22:16:09 +020012999out_config:
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013000 if (state)
13001 drm_atomic_state_free(state);
13002
Daniel Vetterd9e55602012-07-04 22:16:09 +020013003 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +020013004 return ret;
13005}
13006
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013007static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013008 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +020013009 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013010 .destroy = intel_crtc_destroy,
13011 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013012 .atomic_duplicate_state = intel_crtc_duplicate_state,
13013 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013014};
13015
Daniel Vetter53589012013-06-05 13:34:16 +020013016static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
13017 struct intel_shared_dpll *pll,
13018 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013019{
Daniel Vetter53589012013-06-05 13:34:16 +020013020 uint32_t val;
13021
Daniel Vetterf458ebb2014-09-30 10:56:39 +020013022 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030013023 return false;
13024
Daniel Vetter53589012013-06-05 13:34:16 +020013025 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020013026 hw_state->dpll = val;
13027 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
13028 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020013029
13030 return val & DPLL_VCO_ENABLE;
13031}
13032
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013033static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
13034 struct intel_shared_dpll *pll)
13035{
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013036 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
13037 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013038}
13039
Daniel Vettere7b903d2013-06-05 13:34:14 +020013040static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
13041 struct intel_shared_dpll *pll)
13042{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013043 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020013044 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020013045
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013046 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013047
13048 /* Wait for the clocks to stabilize. */
13049 POSTING_READ(PCH_DPLL(pll->id));
13050 udelay(150);
13051
13052 /* The pixel multiplier can only be updated once the
13053 * DPLL is enabled and the clocks are stable.
13054 *
13055 * So write it again.
13056 */
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013057 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013058 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013059 udelay(200);
13060}
13061
13062static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
13063 struct intel_shared_dpll *pll)
13064{
13065 struct drm_device *dev = dev_priv->dev;
13066 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013067
13068 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013069 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020013070 if (intel_crtc_to_shared_dpll(crtc) == pll)
13071 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
13072 }
13073
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013074 I915_WRITE(PCH_DPLL(pll->id), 0);
13075 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013076 udelay(200);
13077}
13078
Daniel Vetter46edb022013-06-05 13:34:12 +020013079static char *ibx_pch_dpll_names[] = {
13080 "PCH DPLL A",
13081 "PCH DPLL B",
13082};
13083
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013084static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013085{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013086 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013087 int i;
13088
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013089 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013090
Daniel Vettere72f9fb2013-06-05 13:34:06 +020013091 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020013092 dev_priv->shared_dplls[i].id = i;
13093 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013094 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013095 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
13096 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020013097 dev_priv->shared_dplls[i].get_hw_state =
13098 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013099 }
13100}
13101
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013102static void intel_shared_dpll_init(struct drm_device *dev)
13103{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013104 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013105
Daniel Vetter9cd86932014-06-25 22:01:57 +030013106 if (HAS_DDI(dev))
13107 intel_ddi_pll_init(dev);
13108 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013109 ibx_pch_dpll_init(dev);
13110 else
13111 dev_priv->num_shared_dpll = 0;
13112
13113 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013114}
13115
Matt Roper6beb8c232014-12-01 15:40:14 -080013116/**
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +000013117 * intel_wm_need_update - Check whether watermarks need updating
13118 * @plane: drm plane
13119 * @state: new plane state
13120 *
13121 * Check current plane state versus the new one to determine whether
13122 * watermarks need to be recalculated.
13123 *
13124 * Returns true or false.
13125 */
13126bool intel_wm_need_update(struct drm_plane *plane,
13127 struct drm_plane_state *state)
13128{
13129 /* Update watermarks on tiling changes. */
13130 if (!plane->state->fb || !state->fb ||
13131 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
13132 plane->state->rotation != state->rotation)
13133 return true;
13134
13135 return false;
13136}
13137
13138/**
Matt Roper6beb8c232014-12-01 15:40:14 -080013139 * intel_prepare_plane_fb - Prepare fb for usage on plane
13140 * @plane: drm plane to prepare for
13141 * @fb: framebuffer to prepare for presentation
13142 *
13143 * Prepares a framebuffer for usage on a display plane. Generally this
13144 * involves pinning the underlying object and updating the frontbuffer tracking
13145 * bits. Some older platforms need special physical address handling for
13146 * cursor planes.
13147 *
13148 * Returns 0 on success, negative error code on failure.
13149 */
13150int
13151intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013152 struct drm_framebuffer *fb,
13153 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013154{
13155 struct drm_device *dev = plane->dev;
Matt Roper6beb8c232014-12-01 15:40:14 -080013156 struct intel_plane *intel_plane = to_intel_plane(plane);
13157 enum pipe pipe = intel_plane->pipe;
13158 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13159 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
13160 unsigned frontbuffer_bits = 0;
13161 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013162
Matt Roperea2c67b2014-12-23 10:41:52 -080013163 if (!obj)
Matt Roper465c1202014-05-29 08:06:54 -070013164 return 0;
13165
Matt Roper6beb8c232014-12-01 15:40:14 -080013166 switch (plane->type) {
13167 case DRM_PLANE_TYPE_PRIMARY:
13168 frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
13169 break;
13170 case DRM_PLANE_TYPE_CURSOR:
13171 frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
13172 break;
13173 case DRM_PLANE_TYPE_OVERLAY:
13174 frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
13175 break;
13176 }
Matt Roper465c1202014-05-29 08:06:54 -070013177
Matt Roper4c345742014-07-09 16:22:10 -070013178 mutex_lock(&dev->struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070013179
Matt Roper6beb8c232014-12-01 15:40:14 -080013180 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13181 INTEL_INFO(dev)->cursor_needs_physical) {
13182 int align = IS_I830(dev) ? 16 * 1024 : 256;
13183 ret = i915_gem_object_attach_phys(obj, align);
13184 if (ret)
13185 DRM_DEBUG_KMS("failed to attach phys object\n");
13186 } else {
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000013187 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
Matt Roper6beb8c232014-12-01 15:40:14 -080013188 }
13189
13190 if (ret == 0)
13191 i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
13192
13193 mutex_unlock(&dev->struct_mutex);
13194
13195 return ret;
13196}
13197
Matt Roper38f3ce32014-12-02 07:45:25 -080013198/**
13199 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13200 * @plane: drm plane to clean up for
13201 * @fb: old framebuffer that was on plane
13202 *
13203 * Cleans up a framebuffer that has just been removed from a plane.
13204 */
13205void
13206intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013207 struct drm_framebuffer *fb,
13208 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013209{
13210 struct drm_device *dev = plane->dev;
13211 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13212
13213 if (WARN_ON(!obj))
13214 return;
13215
13216 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
13217 !INTEL_INFO(dev)->cursor_needs_physical) {
13218 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000013219 intel_unpin_fb_obj(fb, old_state);
Matt Roper38f3ce32014-12-02 07:45:25 -080013220 mutex_unlock(&dev->struct_mutex);
13221 }
Matt Roper465c1202014-05-29 08:06:54 -070013222}
13223
Chandra Konduru6156a452015-04-27 13:48:39 -070013224int
13225skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13226{
13227 int max_scale;
13228 struct drm_device *dev;
13229 struct drm_i915_private *dev_priv;
13230 int crtc_clock, cdclk;
13231
13232 if (!intel_crtc || !crtc_state)
13233 return DRM_PLANE_HELPER_NO_SCALING;
13234
13235 dev = intel_crtc->base.dev;
13236 dev_priv = dev->dev_private;
13237 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
13238 cdclk = dev_priv->display.get_display_clock_speed(dev);
13239
13240 if (!crtc_clock || !cdclk)
13241 return DRM_PLANE_HELPER_NO_SCALING;
13242
13243 /*
13244 * skl max scale is lower of:
13245 * close to 3 but not 3, -1 is for that purpose
13246 * or
13247 * cdclk/crtc_clock
13248 */
13249 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13250
13251 return max_scale;
13252}
13253
Matt Roper465c1202014-05-29 08:06:54 -070013254static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013255intel_check_primary_plane(struct drm_plane *plane,
13256 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013257{
Matt Roper32b7eee2014-12-24 07:59:06 -080013258 struct drm_device *dev = plane->dev;
13259 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roper2b875c22014-12-01 15:40:13 -080013260 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013261 struct intel_crtc *intel_crtc;
Chandra Konduru6156a452015-04-27 13:48:39 -070013262 struct intel_crtc_state *crtc_state;
Matt Roper2b875c22014-12-01 15:40:13 -080013263 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013264 struct drm_rect *dest = &state->dst;
13265 struct drm_rect *src = &state->src;
13266 const struct drm_rect *clip = &state->clip;
Sonika Jindald8106362015-04-10 14:37:28 +053013267 bool can_position = false;
Chandra Konduru6156a452015-04-27 13:48:39 -070013268 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13269 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013270 int ret;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013271
Matt Roperea2c67b2014-12-23 10:41:52 -080013272 crtc = crtc ? crtc : plane->crtc;
13273 intel_crtc = to_intel_crtc(crtc);
Chandra Konduru6156a452015-04-27 13:48:39 -070013274 crtc_state = state->base.state ?
13275 intel_atomic_get_crtc_state(state->base.state, intel_crtc) : NULL;
Matt Roperea2c67b2014-12-23 10:41:52 -080013276
Chandra Konduru6156a452015-04-27 13:48:39 -070013277 if (INTEL_INFO(dev)->gen >= 9) {
13278 min_scale = 1;
13279 max_scale = skl_max_scale(intel_crtc, crtc_state);
Sonika Jindald8106362015-04-10 14:37:28 +053013280 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070013281 }
Sonika Jindald8106362015-04-10 14:37:28 +053013282
Matt Roperc59cb172014-12-01 15:40:16 -080013283 ret = drm_plane_helper_check_update(plane, crtc, fb,
13284 src, dest, clip,
Chandra Konduru6156a452015-04-27 13:48:39 -070013285 min_scale,
13286 max_scale,
Sonika Jindald8106362015-04-10 14:37:28 +053013287 can_position, true,
13288 &state->visible);
Matt Roperc59cb172014-12-01 15:40:16 -080013289 if (ret)
13290 return ret;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013291
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013292 if (intel_crtc->active) {
Matt Roper32b7eee2014-12-24 07:59:06 -080013293 intel_crtc->atomic.wait_for_flips = true;
13294
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013295 /*
13296 * FBC does not work on some platforms for rotated
13297 * planes, so disable it when rotation is not 0 and
13298 * update it when rotation is set back to 0.
13299 *
13300 * FIXME: This is redundant with the fbc update done in
13301 * the primary plane enable function except that that
13302 * one is done too late. We eventually need to unify
13303 * this.
13304 */
13305 if (intel_crtc->primary_enabled &&
13306 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
Paulo Zanonie35fef22015-02-09 14:46:29 -020013307 dev_priv->fbc.crtc == intel_crtc &&
Matt Roper8e7d6882015-01-21 16:35:41 -080013308 state->base.rotation != BIT(DRM_ROTATE_0)) {
Matt Roper32b7eee2014-12-24 07:59:06 -080013309 intel_crtc->atomic.disable_fbc = true;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013310 }
13311
13312 if (state->visible) {
Matt Roper32b7eee2014-12-24 07:59:06 -080013313 /*
13314 * BDW signals flip done immediately if the plane
13315 * is disabled, even if the plane enable is already
13316 * armed to occur at the next vblank :(
13317 */
13318 if (IS_BROADWELL(dev) && !intel_crtc->primary_enabled)
13319 intel_crtc->atomic.wait_vblank = true;
13320 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013321
Matt Roper32b7eee2014-12-24 07:59:06 -080013322 intel_crtc->atomic.fb_bits |=
13323 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
13324
13325 intel_crtc->atomic.update_fbc = true;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +000013326
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +000013327 if (intel_wm_need_update(plane, &state->base))
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +000013328 intel_crtc->atomic.update_wm = true;
Matt Roperc59cb172014-12-01 15:40:16 -080013329 }
13330
Chandra Konduru6156a452015-04-27 13:48:39 -070013331 if (INTEL_INFO(dev)->gen >= 9) {
13332 ret = skl_update_scaler_users(intel_crtc, crtc_state,
13333 to_intel_plane(plane), state, 0);
13334 if (ret)
13335 return ret;
13336 }
13337
Matt Roperc59cb172014-12-01 15:40:16 -080013338 return 0;
Matt Roper465c1202014-05-29 08:06:54 -070013339}
13340
Sonika Jindal48404c12014-08-22 14:06:04 +053013341static void
13342intel_commit_primary_plane(struct drm_plane *plane,
13343 struct intel_plane_state *state)
13344{
Matt Roper2b875c22014-12-01 15:40:13 -080013345 struct drm_crtc *crtc = state->base.crtc;
13346 struct drm_framebuffer *fb = state->base.fb;
13347 struct drm_device *dev = plane->dev;
Sonika Jindal48404c12014-08-22 14:06:04 +053013348 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperea2c67b2014-12-23 10:41:52 -080013349 struct intel_crtc *intel_crtc;
Sonika Jindalce54d852014-08-21 11:44:39 +053013350 struct drm_rect *src = &state->src;
Matt Ropercf4c7c12014-12-04 10:27:42 -080013351
Matt Roperea2c67b2014-12-23 10:41:52 -080013352 crtc = crtc ? crtc : plane->crtc;
13353 intel_crtc = to_intel_crtc(crtc);
13354
Matt Ropercf4c7c12014-12-04 10:27:42 -080013355 plane->fb = fb;
Sonika Jindalce54d852014-08-21 11:44:39 +053013356 crtc->x = src->x1 >> 16;
Matt Roper465c1202014-05-29 08:06:54 -070013357 crtc->y = src->y1 >> 16;
13358
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013359 if (intel_crtc->active) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013360 if (state->visible) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013361 /* FIXME: kill this fastboot hack */
13362 intel_update_pipe_size(intel_crtc);
13363
13364 intel_crtc->primary_enabled = true;
13365
13366 dev_priv->display.update_primary_plane(crtc, plane->fb,
13367 crtc->x, crtc->y);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013368 } else {
13369 /*
13370 * If clipping results in a non-visible primary plane,
13371 * we'll disable the primary plane. Note that this is
13372 * a bit different than what happens if userspace
13373 * explicitly disables the plane by passing fb=0
13374 * because plane->fb still gets set and pinned.
13375 */
13376 intel_disable_primary_hw_plane(plane, crtc);
13377 }
Matt Roper32b7eee2014-12-24 07:59:06 -080013378 }
13379}
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013380
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013381static void
13382intel_disable_primary_plane(struct drm_plane *plane,
13383 struct drm_crtc *crtc,
13384 bool force)
13385{
13386 struct drm_device *dev = plane->dev;
13387 struct drm_i915_private *dev_priv = dev->dev_private;
13388
13389 if (!force)
13390 to_intel_crtc(crtc)->primary_enabled = false;
13391
13392 dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
13393}
13394
Matt Roper32b7eee2014-12-24 07:59:06 -080013395static void intel_begin_crtc_commit(struct drm_crtc *crtc)
13396{
13397 struct drm_device *dev = crtc->dev;
13398 struct drm_i915_private *dev_priv = dev->dev_private;
13399 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080013400 struct intel_plane *intel_plane;
13401 struct drm_plane *p;
13402 unsigned fb_bits = 0;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013403
Matt Roperea2c67b2014-12-23 10:41:52 -080013404 /* Track fb's for any planes being disabled */
13405 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
13406 intel_plane = to_intel_plane(p);
13407
13408 if (intel_crtc->atomic.disabled_planes &
13409 (1 << drm_plane_index(p))) {
13410 switch (p->type) {
13411 case DRM_PLANE_TYPE_PRIMARY:
13412 fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
13413 break;
13414 case DRM_PLANE_TYPE_CURSOR:
13415 fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
13416 break;
13417 case DRM_PLANE_TYPE_OVERLAY:
13418 fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
13419 break;
13420 }
13421
13422 mutex_lock(&dev->struct_mutex);
13423 i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
13424 mutex_unlock(&dev->struct_mutex);
13425 }
13426 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013427
Matt Roper32b7eee2014-12-24 07:59:06 -080013428 if (intel_crtc->atomic.wait_for_flips)
13429 intel_crtc_wait_for_pending_flips(crtc);
13430
13431 if (intel_crtc->atomic.disable_fbc)
13432 intel_fbc_disable(dev);
13433
13434 if (intel_crtc->atomic.pre_disable_primary)
13435 intel_pre_disable_primary(crtc);
13436
13437 if (intel_crtc->atomic.update_wm)
13438 intel_update_watermarks(crtc);
13439
13440 intel_runtime_pm_get(dev_priv);
Matt Roperc34c9ee2014-12-23 10:41:50 -080013441
13442 /* Perform vblank evasion around commit operation */
13443 if (intel_crtc->active)
13444 intel_crtc->atomic.evade =
13445 intel_pipe_update_start(intel_crtc,
13446 &intel_crtc->atomic.start_vbl_count);
Matt Roper32b7eee2014-12-24 07:59:06 -080013447}
13448
13449static void intel_finish_crtc_commit(struct drm_crtc *crtc)
13450{
13451 struct drm_device *dev = crtc->dev;
13452 struct drm_i915_private *dev_priv = dev->dev_private;
13453 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13454 struct drm_plane *p;
13455
Matt Roperc34c9ee2014-12-23 10:41:50 -080013456 if (intel_crtc->atomic.evade)
13457 intel_pipe_update_end(intel_crtc,
13458 intel_crtc->atomic.start_vbl_count);
13459
Matt Roper32b7eee2014-12-24 07:59:06 -080013460 intel_runtime_pm_put(dev_priv);
13461
13462 if (intel_crtc->atomic.wait_vblank)
13463 intel_wait_for_vblank(dev, intel_crtc->pipe);
13464
13465 intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
13466
13467 if (intel_crtc->atomic.update_fbc) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013468 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020013469 intel_fbc_update(dev);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013470 mutex_unlock(&dev->struct_mutex);
13471 }
Matt Roper465c1202014-05-29 08:06:54 -070013472
Matt Roper32b7eee2014-12-24 07:59:06 -080013473 if (intel_crtc->atomic.post_enable_primary)
13474 intel_post_enable_primary(crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013475
Matt Roper32b7eee2014-12-24 07:59:06 -080013476 drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
13477 if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
13478 intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
13479 false, false);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013480
Matt Roper32b7eee2014-12-24 07:59:06 -080013481 memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013482}
13483
Matt Ropercf4c7c12014-12-04 10:27:42 -080013484/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013485 * intel_plane_destroy - destroy a plane
13486 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013487 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013488 * Common destruction function for all types of planes (primary, cursor,
13489 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013490 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013491void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013492{
13493 struct intel_plane *intel_plane = to_intel_plane(plane);
13494 drm_plane_cleanup(plane);
13495 kfree(intel_plane);
13496}
13497
Matt Roper65a3fea2015-01-21 16:35:42 -080013498const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013499 .update_plane = drm_atomic_helper_update_plane,
13500 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013501 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080013502 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080013503 .atomic_get_property = intel_plane_atomic_get_property,
13504 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013505 .atomic_duplicate_state = intel_plane_duplicate_state,
13506 .atomic_destroy_state = intel_plane_destroy_state,
13507
Matt Roper465c1202014-05-29 08:06:54 -070013508};
13509
13510static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13511 int pipe)
13512{
13513 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080013514 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070013515 const uint32_t *intel_primary_formats;
13516 int num_formats;
13517
13518 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13519 if (primary == NULL)
13520 return NULL;
13521
Matt Roper8e7d6882015-01-21 16:35:41 -080013522 state = intel_create_plane_state(&primary->base);
13523 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013524 kfree(primary);
13525 return NULL;
13526 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013527 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013528
Matt Roper465c1202014-05-29 08:06:54 -070013529 primary->can_scale = false;
13530 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013531 if (INTEL_INFO(dev)->gen >= 9) {
13532 primary->can_scale = true;
13533 }
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013534 state->scaler_id = -1;
Matt Roper465c1202014-05-29 08:06:54 -070013535 primary->pipe = pipe;
13536 primary->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080013537 primary->check_plane = intel_check_primary_plane;
13538 primary->commit_plane = intel_commit_primary_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013539 primary->disable_plane = intel_disable_primary_plane;
Chandra Konduru08e221f2015-04-07 15:28:37 -070013540 primary->ckey.flags = I915_SET_COLORKEY_NONE;
Matt Roper465c1202014-05-29 08:06:54 -070013541 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13542 primary->plane = !pipe;
13543
13544 if (INTEL_INFO(dev)->gen <= 3) {
13545 intel_primary_formats = intel_primary_formats_gen2;
13546 num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
13547 } else {
13548 intel_primary_formats = intel_primary_formats_gen4;
13549 num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
13550 }
13551
13552 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013553 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070013554 intel_primary_formats, num_formats,
13555 DRM_PLANE_TYPE_PRIMARY);
Sonika Jindal48404c12014-08-22 14:06:04 +053013556
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013557 if (INTEL_INFO(dev)->gen >= 4)
13558 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053013559
Matt Roperea2c67b2014-12-23 10:41:52 -080013560 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13561
Matt Roper465c1202014-05-29 08:06:54 -070013562 return &primary->base;
13563}
13564
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013565void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13566{
13567 if (!dev->mode_config.rotation_property) {
13568 unsigned long flags = BIT(DRM_ROTATE_0) |
13569 BIT(DRM_ROTATE_180);
13570
13571 if (INTEL_INFO(dev)->gen >= 9)
13572 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13573
13574 dev->mode_config.rotation_property =
13575 drm_mode_create_rotation_property(dev, flags);
13576 }
13577 if (dev->mode_config.rotation_property)
13578 drm_object_attach_property(&plane->base.base,
13579 dev->mode_config.rotation_property,
13580 plane->base.state->rotation);
13581}
13582
Matt Roper3d7d6512014-06-10 08:28:13 -070013583static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030013584intel_check_cursor_plane(struct drm_plane *plane,
13585 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070013586{
Matt Roper2b875c22014-12-01 15:40:13 -080013587 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013588 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080013589 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013590 struct drm_rect *dest = &state->dst;
13591 struct drm_rect *src = &state->src;
13592 const struct drm_rect *clip = &state->clip;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013593 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Matt Roperea2c67b2014-12-23 10:41:52 -080013594 struct intel_crtc *intel_crtc;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013595 unsigned stride;
13596 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013597
Matt Roperea2c67b2014-12-23 10:41:52 -080013598 crtc = crtc ? crtc : plane->crtc;
13599 intel_crtc = to_intel_crtc(crtc);
13600
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013601 ret = drm_plane_helper_check_update(plane, crtc, fb,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013602 src, dest, clip,
13603 DRM_PLANE_HELPER_NO_SCALING,
13604 DRM_PLANE_HELPER_NO_SCALING,
13605 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013606 if (ret)
13607 return ret;
13608
13609
13610 /* if we want to turn off the cursor ignore width and height */
13611 if (!obj)
Matt Roper32b7eee2014-12-24 07:59:06 -080013612 goto finish;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013613
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013614 /* Check for which cursor types we support */
Matt Roperea2c67b2014-12-23 10:41:52 -080013615 if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
13616 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13617 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013618 return -EINVAL;
13619 }
13620
Matt Roperea2c67b2014-12-23 10:41:52 -080013621 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13622 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013623 DRM_DEBUG_KMS("buffer is too small\n");
13624 return -ENOMEM;
13625 }
13626
Ville Syrjälä3a656b52015-03-09 21:08:37 +020013627 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013628 DRM_DEBUG_KMS("cursor cannot be tiled\n");
13629 ret = -EINVAL;
13630 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013631
Matt Roper32b7eee2014-12-24 07:59:06 -080013632finish:
13633 if (intel_crtc->active) {
Ville Syrjälä3749f462015-03-10 13:15:22 +020013634 if (plane->state->crtc_w != state->base.crtc_w)
Matt Roper32b7eee2014-12-24 07:59:06 -080013635 intel_crtc->atomic.update_wm = true;
13636
13637 intel_crtc->atomic.fb_bits |=
13638 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
13639 }
13640
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013641 return ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013642}
13643
Matt Roperf4a2cf22014-12-01 15:40:12 -080013644static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013645intel_disable_cursor_plane(struct drm_plane *plane,
13646 struct drm_crtc *crtc,
13647 bool force)
13648{
13649 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13650
13651 if (!force) {
13652 plane->fb = NULL;
13653 intel_crtc->cursor_bo = NULL;
13654 intel_crtc->cursor_addr = 0;
13655 }
13656
13657 intel_crtc_update_cursor(crtc, false);
13658}
13659
13660static void
Gustavo Padovan852e7872014-09-05 17:22:31 -030013661intel_commit_cursor_plane(struct drm_plane *plane,
13662 struct intel_plane_state *state)
13663{
Matt Roper2b875c22014-12-01 15:40:13 -080013664 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013665 struct drm_device *dev = plane->dev;
13666 struct intel_crtc *intel_crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080013667 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080013668 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070013669
Matt Roperea2c67b2014-12-23 10:41:52 -080013670 crtc = crtc ? crtc : plane->crtc;
13671 intel_crtc = to_intel_crtc(crtc);
Sonika Jindala919db92014-10-23 07:41:33 -070013672
Matt Roperea2c67b2014-12-23 10:41:52 -080013673 plane->fb = state->base.fb;
13674 crtc->cursor_x = state->base.crtc_x;
13675 crtc->cursor_y = state->base.crtc_y;
13676
Gustavo Padovana912f122014-12-01 15:40:10 -080013677 if (intel_crtc->cursor_bo == obj)
13678 goto update;
13679
Matt Roperf4a2cf22014-12-01 15:40:12 -080013680 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080013681 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080013682 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080013683 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080013684 else
Gustavo Padovana912f122014-12-01 15:40:10 -080013685 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080013686
Gustavo Padovana912f122014-12-01 15:40:10 -080013687 intel_crtc->cursor_addr = addr;
13688 intel_crtc->cursor_bo = obj;
13689update:
Gustavo Padovana912f122014-12-01 15:40:10 -080013690
Matt Roper32b7eee2014-12-24 07:59:06 -080013691 if (intel_crtc->active)
Gustavo Padovan852e7872014-09-05 17:22:31 -030013692 intel_crtc_update_cursor(crtc, state->visible);
Matt Roper3d7d6512014-06-10 08:28:13 -070013693}
Gustavo Padovan852e7872014-09-05 17:22:31 -030013694
Matt Roper3d7d6512014-06-10 08:28:13 -070013695static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
13696 int pipe)
13697{
13698 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080013699 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070013700
13701 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
13702 if (cursor == NULL)
13703 return NULL;
13704
Matt Roper8e7d6882015-01-21 16:35:41 -080013705 state = intel_create_plane_state(&cursor->base);
13706 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013707 kfree(cursor);
13708 return NULL;
13709 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013710 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013711
Matt Roper3d7d6512014-06-10 08:28:13 -070013712 cursor->can_scale = false;
13713 cursor->max_downscale = 1;
13714 cursor->pipe = pipe;
13715 cursor->plane = pipe;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013716 state->scaler_id = -1;
Matt Roperc59cb172014-12-01 15:40:16 -080013717 cursor->check_plane = intel_check_cursor_plane;
13718 cursor->commit_plane = intel_commit_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013719 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070013720
13721 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013722 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070013723 intel_cursor_formats,
13724 ARRAY_SIZE(intel_cursor_formats),
13725 DRM_PLANE_TYPE_CURSOR);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013726
13727 if (INTEL_INFO(dev)->gen >= 4) {
13728 if (!dev->mode_config.rotation_property)
13729 dev->mode_config.rotation_property =
13730 drm_mode_create_rotation_property(dev,
13731 BIT(DRM_ROTATE_0) |
13732 BIT(DRM_ROTATE_180));
13733 if (dev->mode_config.rotation_property)
13734 drm_object_attach_property(&cursor->base.base,
13735 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080013736 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013737 }
13738
Matt Roperea2c67b2014-12-23 10:41:52 -080013739 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
13740
Matt Roper3d7d6512014-06-10 08:28:13 -070013741 return &cursor->base;
13742}
13743
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013744static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
13745 struct intel_crtc_state *crtc_state)
13746{
13747 int i;
13748 struct intel_scaler *intel_scaler;
13749 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
13750
13751 for (i = 0; i < intel_crtc->num_scalers; i++) {
13752 intel_scaler = &scaler_state->scalers[i];
13753 intel_scaler->in_use = 0;
13754 intel_scaler->id = i;
13755
13756 intel_scaler->mode = PS_SCALER_MODE_DYN;
13757 }
13758
13759 scaler_state->scaler_id = -1;
13760}
13761
Hannes Ederb358d0a2008-12-18 21:18:47 +010013762static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080013763{
Jani Nikulafbee40d2014-03-31 14:27:18 +030013764 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080013765 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013766 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070013767 struct drm_plane *primary = NULL;
13768 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070013769 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080013770
Daniel Vetter955382f2013-09-19 14:05:45 +020013771 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080013772 if (intel_crtc == NULL)
13773 return;
13774
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013775 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
13776 if (!crtc_state)
13777 goto fail;
13778 intel_crtc_set_state(intel_crtc, crtc_state);
Matt Roper07878242015-02-25 11:43:26 -080013779 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013780
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013781 /* initialize shared scalers */
13782 if (INTEL_INFO(dev)->gen >= 9) {
13783 if (pipe == PIPE_C)
13784 intel_crtc->num_scalers = 1;
13785 else
13786 intel_crtc->num_scalers = SKL_NUM_SCALERS;
13787
13788 skl_init_scalers(dev, intel_crtc, crtc_state);
13789 }
13790
Matt Roper465c1202014-05-29 08:06:54 -070013791 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070013792 if (!primary)
13793 goto fail;
13794
13795 cursor = intel_cursor_plane_create(dev, pipe);
13796 if (!cursor)
13797 goto fail;
13798
Matt Roper465c1202014-05-29 08:06:54 -070013799 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Matt Roper3d7d6512014-06-10 08:28:13 -070013800 cursor, &intel_crtc_funcs);
13801 if (ret)
13802 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080013803
13804 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080013805 for (i = 0; i < 256; i++) {
13806 intel_crtc->lut_r[i] = i;
13807 intel_crtc->lut_g[i] = i;
13808 intel_crtc->lut_b[i] = i;
13809 }
13810
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020013811 /*
13812 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020013813 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020013814 */
Jesse Barnes80824002009-09-10 15:28:06 -070013815 intel_crtc->pipe = pipe;
13816 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010013817 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080013818 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010013819 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070013820 }
13821
Chris Wilson4b0e3332014-05-30 16:35:26 +030013822 intel_crtc->cursor_base = ~0;
13823 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030013824 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030013825
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080013826 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
13827 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
13828 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
13829 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
13830
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020013831 INIT_WORK(&intel_crtc->mmio_flip.work, intel_mmio_flip_work_func);
13832
Jesse Barnes79e53942008-11-07 14:24:08 -080013833 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020013834
13835 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070013836 return;
13837
13838fail:
13839 if (primary)
13840 drm_plane_cleanup(primary);
13841 if (cursor)
13842 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013843 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070013844 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080013845}
13846
Jesse Barnes752aa882013-10-31 18:55:49 +020013847enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
13848{
13849 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020013850 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020013851
Rob Clark51fd3712013-11-19 12:10:12 -050013852 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020013853
Ville Syrjäläd3babd32014-11-07 11:16:01 +020013854 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020013855 return INVALID_PIPE;
13856
13857 return to_intel_crtc(encoder->crtc)->pipe;
13858}
13859
Carl Worth08d7b3d2009-04-29 14:43:54 -070013860int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000013861 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070013862{
Carl Worth08d7b3d2009-04-29 14:43:54 -070013863 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040013864 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020013865 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013866
Rob Clark7707e652014-07-17 23:30:04 -040013867 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070013868
Rob Clark7707e652014-07-17 23:30:04 -040013869 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070013870 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030013871 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013872 }
13873
Rob Clark7707e652014-07-17 23:30:04 -040013874 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020013875 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013876
Daniel Vetterc05422d2009-08-11 16:05:30 +020013877 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013878}
13879
Daniel Vetter66a92782012-07-12 20:08:18 +020013880static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080013881{
Daniel Vetter66a92782012-07-12 20:08:18 +020013882 struct drm_device *dev = encoder->base.dev;
13883 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080013884 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080013885 int entry = 0;
13886
Damien Lespiaub2784e12014-08-05 11:29:37 +010013887 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020013888 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020013889 index_mask |= (1 << entry);
13890
Jesse Barnes79e53942008-11-07 14:24:08 -080013891 entry++;
13892 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010013893
Jesse Barnes79e53942008-11-07 14:24:08 -080013894 return index_mask;
13895}
13896
Chris Wilson4d302442010-12-14 19:21:29 +000013897static bool has_edp_a(struct drm_device *dev)
13898{
13899 struct drm_i915_private *dev_priv = dev->dev_private;
13900
13901 if (!IS_MOBILE(dev))
13902 return false;
13903
13904 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
13905 return false;
13906
Damien Lespiaue3589902014-02-07 19:12:50 +000013907 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000013908 return false;
13909
13910 return true;
13911}
13912
Jesse Barnes84b4e042014-06-25 08:24:29 -070013913static bool intel_crt_present(struct drm_device *dev)
13914{
13915 struct drm_i915_private *dev_priv = dev->dev_private;
13916
Damien Lespiau884497e2013-12-03 13:56:23 +000013917 if (INTEL_INFO(dev)->gen >= 9)
13918 return false;
13919
Damien Lespiaucf404ce2014-10-01 20:04:15 +010013920 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070013921 return false;
13922
13923 if (IS_CHERRYVIEW(dev))
13924 return false;
13925
13926 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
13927 return false;
13928
13929 return true;
13930}
13931
Jesse Barnes79e53942008-11-07 14:24:08 -080013932static void intel_setup_outputs(struct drm_device *dev)
13933{
Eric Anholt725e30a2009-01-22 13:01:02 -080013934 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010013935 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013936 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080013937
Daniel Vetterc9093352013-06-06 22:22:47 +020013938 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080013939
Jesse Barnes84b4e042014-06-25 08:24:29 -070013940 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020013941 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013942
Vandana Kannanc776eb22014-08-19 12:05:01 +053013943 if (IS_BROXTON(dev)) {
13944 /*
13945 * FIXME: Broxton doesn't support port detection via the
13946 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
13947 * detect the ports.
13948 */
13949 intel_ddi_init(dev, PORT_A);
13950 intel_ddi_init(dev, PORT_B);
13951 intel_ddi_init(dev, PORT_C);
13952 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013953 int found;
13954
Jesse Barnesde31fac2015-03-06 15:53:32 -080013955 /*
13956 * Haswell uses DDI functions to detect digital outputs.
13957 * On SKL pre-D0 the strap isn't connected, so we assume
13958 * it's there.
13959 */
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013960 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080013961 /* WaIgnoreDDIAStrap: skl */
13962 if (found ||
13963 (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013964 intel_ddi_init(dev, PORT_A);
13965
13966 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
13967 * register */
13968 found = I915_READ(SFUSE_STRAP);
13969
13970 if (found & SFUSE_STRAP_DDIB_DETECTED)
13971 intel_ddi_init(dev, PORT_B);
13972 if (found & SFUSE_STRAP_DDIC_DETECTED)
13973 intel_ddi_init(dev, PORT_C);
13974 if (found & SFUSE_STRAP_DDID_DETECTED)
13975 intel_ddi_init(dev, PORT_D);
13976 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013977 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020013978 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020013979
13980 if (has_edp_a(dev))
13981 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013982
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013983 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080013984 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010013985 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013986 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013987 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013988 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013989 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013990 }
13991
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013992 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013993 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013994
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013995 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013996 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013997
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013998 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013999 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014000
Daniel Vetter270b3042012-10-27 15:52:05 +020014001 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014002 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070014003 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014004 /*
14005 * The DP_DETECTED bit is the latched state of the DDC
14006 * SDA pin at boot. However since eDP doesn't require DDC
14007 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14008 * eDP ports may have been muxed to an alternate function.
14009 * Thus we can't rely on the DP_DETECTED bit alone to detect
14010 * eDP ports. Consult the VBT as well as DP_DETECTED to
14011 * detect eDP ports.
14012 */
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014013 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
14014 !intel_dp_is_edp(dev, PORT_B))
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014015 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
14016 PORT_B);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014017 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
14018 intel_dp_is_edp(dev, PORT_B))
14019 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014020
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014021 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
14022 !intel_dp_is_edp(dev, PORT_C))
Jesse Barnes6f6005a2013-08-09 09:34:35 -070014023 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
14024 PORT_C);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014025 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
14026 intel_dp_is_edp(dev, PORT_C))
14027 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014028
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014029 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014030 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014031 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
14032 PORT_D);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014033 /* eDP not supported on port D, so don't check VBT */
14034 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
14035 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014036 }
14037
Jani Nikula3cfca972013-08-27 15:12:26 +030014038 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +080014039 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014040 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014041
Paulo Zanonie2debe92013-02-18 19:00:27 -030014042 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014043 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014044 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014045 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
14046 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014047 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014048 }
Ma Ling27185ae2009-08-24 13:50:23 +080014049
Imre Deake7281ea2013-05-08 13:14:08 +030014050 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014051 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014052 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014053
14054 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014055
Paulo Zanonie2debe92013-02-18 19:00:27 -030014056 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014057 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014058 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014059 }
Ma Ling27185ae2009-08-24 13:50:23 +080014060
Paulo Zanonie2debe92013-02-18 19:00:27 -030014061 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014062
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014063 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
14064 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014065 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014066 }
Imre Deake7281ea2013-05-08 13:14:08 +030014067 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014068 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014069 }
Ma Ling27185ae2009-08-24 13:50:23 +080014070
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014071 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014072 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014073 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014074 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014075 intel_dvo_init(dev);
14076
Zhenyu Wang103a1962009-11-27 11:44:36 +080014077 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014078 intel_tv_init(dev);
14079
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014080 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014081
Damien Lespiaub2784e12014-08-05 11:29:37 +010014082 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014083 encoder->base.possible_crtcs = encoder->crtc_mask;
14084 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014085 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014086 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014087
Paulo Zanonidde86e22012-12-01 12:04:25 -020014088 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014089
14090 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014091}
14092
14093static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14094{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014095 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014096 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014097
Daniel Vetteref2d6332014-02-10 18:00:38 +010014098 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014099 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014100 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014101 drm_gem_object_unreference(&intel_fb->obj->base);
14102 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014103 kfree(intel_fb);
14104}
14105
14106static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014107 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014108 unsigned int *handle)
14109{
14110 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014111 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014112
Chris Wilson05394f32010-11-08 19:18:58 +000014113 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014114}
14115
14116static const struct drm_framebuffer_funcs intel_fb_funcs = {
14117 .destroy = intel_user_framebuffer_destroy,
14118 .create_handle = intel_user_framebuffer_create_handle,
14119};
14120
Damien Lespiaub3218032015-02-27 11:15:18 +000014121static
14122u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14123 uint32_t pixel_format)
14124{
14125 u32 gen = INTEL_INFO(dev)->gen;
14126
14127 if (gen >= 9) {
14128 /* "The stride in bytes must not exceed the of the size of 8K
14129 * pixels and 32K bytes."
14130 */
14131 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
14132 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
14133 return 32*1024;
14134 } else if (gen >= 4) {
14135 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14136 return 16*1024;
14137 else
14138 return 32*1024;
14139 } else if (gen >= 3) {
14140 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14141 return 8*1024;
14142 else
14143 return 16*1024;
14144 } else {
14145 /* XXX DSPC is limited to 4k tiled */
14146 return 8*1024;
14147 }
14148}
14149
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014150static int intel_framebuffer_init(struct drm_device *dev,
14151 struct intel_framebuffer *intel_fb,
14152 struct drm_mode_fb_cmd2 *mode_cmd,
14153 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014154{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014155 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014156 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014157 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014158
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014159 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14160
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014161 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14162 /* Enforce that fb modifier and tiling mode match, but only for
14163 * X-tiled. This is needed for FBC. */
14164 if (!!(obj->tiling_mode == I915_TILING_X) !=
14165 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14166 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14167 return -EINVAL;
14168 }
14169 } else {
14170 if (obj->tiling_mode == I915_TILING_X)
14171 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14172 else if (obj->tiling_mode == I915_TILING_Y) {
14173 DRM_DEBUG("No Y tiling for legacy addfb\n");
14174 return -EINVAL;
14175 }
14176 }
14177
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014178 /* Passed in modifier sanity checking. */
14179 switch (mode_cmd->modifier[0]) {
14180 case I915_FORMAT_MOD_Y_TILED:
14181 case I915_FORMAT_MOD_Yf_TILED:
14182 if (INTEL_INFO(dev)->gen < 9) {
14183 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14184 mode_cmd->modifier[0]);
14185 return -EINVAL;
14186 }
14187 case DRM_FORMAT_MOD_NONE:
14188 case I915_FORMAT_MOD_X_TILED:
14189 break;
14190 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014191 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14192 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014193 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014194 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014195
Damien Lespiaub3218032015-02-27 11:15:18 +000014196 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
14197 mode_cmd->pixel_format);
14198 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14199 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14200 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014201 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014202 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014203
Damien Lespiaub3218032015-02-27 11:15:18 +000014204 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14205 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014206 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014207 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14208 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014209 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014210 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014211 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014212 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014213
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014214 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014215 mode_cmd->pitches[0] != obj->stride) {
14216 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14217 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014218 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014219 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014220
Ville Syrjälä57779d02012-10-31 17:50:14 +020014221 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014222 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014223 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014224 case DRM_FORMAT_RGB565:
14225 case DRM_FORMAT_XRGB8888:
14226 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014227 break;
14228 case DRM_FORMAT_XRGB1555:
14229 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014230 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014231 DRM_DEBUG("unsupported pixel format: %s\n",
14232 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014233 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014234 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014235 break;
14236 case DRM_FORMAT_XBGR8888:
14237 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014238 case DRM_FORMAT_XRGB2101010:
14239 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014240 case DRM_FORMAT_XBGR2101010:
14241 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014242 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014243 DRM_DEBUG("unsupported pixel format: %s\n",
14244 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014245 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014246 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014247 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014248 case DRM_FORMAT_YUYV:
14249 case DRM_FORMAT_UYVY:
14250 case DRM_FORMAT_YVYU:
14251 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014252 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014253 DRM_DEBUG("unsupported pixel format: %s\n",
14254 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014255 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014256 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014257 break;
14258 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014259 DRM_DEBUG("unsupported pixel format: %s\n",
14260 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014261 return -EINVAL;
14262 }
14263
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014264 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14265 if (mode_cmd->offsets[0] != 0)
14266 return -EINVAL;
14267
Damien Lespiauec2c9812015-01-20 12:51:45 +000014268 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014269 mode_cmd->pixel_format,
14270 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014271 /* FIXME drm helper for size checks (especially planar formats)? */
14272 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14273 return -EINVAL;
14274
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014275 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14276 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020014277 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014278
Jesse Barnes79e53942008-11-07 14:24:08 -080014279 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14280 if (ret) {
14281 DRM_ERROR("framebuffer init failed %d\n", ret);
14282 return ret;
14283 }
14284
Jesse Barnes79e53942008-11-07 14:24:08 -080014285 return 0;
14286}
14287
Jesse Barnes79e53942008-11-07 14:24:08 -080014288static struct drm_framebuffer *
14289intel_user_framebuffer_create(struct drm_device *dev,
14290 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014291 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014292{
Chris Wilson05394f32010-11-08 19:18:58 +000014293 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014294
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014295 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
14296 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014297 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014298 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014299
Chris Wilsond2dff872011-04-19 08:36:26 +010014300 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080014301}
14302
Daniel Vetter4520f532013-10-09 09:18:51 +020014303#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020014304static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014305{
14306}
14307#endif
14308
Jesse Barnes79e53942008-11-07 14:24:08 -080014309static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014310 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014311 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014312 .atomic_check = intel_atomic_check,
14313 .atomic_commit = intel_atomic_commit,
Jesse Barnes79e53942008-11-07 14:24:08 -080014314};
14315
Jesse Barnese70236a2009-09-21 10:42:27 -070014316/* Set up chip specific display functions */
14317static void intel_init_display(struct drm_device *dev)
14318{
14319 struct drm_i915_private *dev_priv = dev->dev_private;
14320
Daniel Vetteree9300b2013-06-03 22:40:22 +020014321 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14322 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030014323 else if (IS_CHERRYVIEW(dev))
14324 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020014325 else if (IS_VALLEYVIEW(dev))
14326 dev_priv->display.find_dpll = vlv_find_best_dpll;
14327 else if (IS_PINEVIEW(dev))
14328 dev_priv->display.find_dpll = pnv_find_best_dpll;
14329 else
14330 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14331
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014332 if (INTEL_INFO(dev)->gen >= 9) {
14333 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014334 dev_priv->display.get_initial_plane_config =
14335 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014336 dev_priv->display.crtc_compute_clock =
14337 haswell_crtc_compute_clock;
14338 dev_priv->display.crtc_enable = haswell_crtc_enable;
14339 dev_priv->display.crtc_disable = haswell_crtc_disable;
14340 dev_priv->display.off = ironlake_crtc_off;
14341 dev_priv->display.update_primary_plane =
14342 skylake_update_primary_plane;
14343 } else if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014344 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014345 dev_priv->display.get_initial_plane_config =
14346 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014347 dev_priv->display.crtc_compute_clock =
14348 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014349 dev_priv->display.crtc_enable = haswell_crtc_enable;
14350 dev_priv->display.crtc_disable = haswell_crtc_disable;
Daniel Vetterdf8ad702014-06-25 22:02:03 +030014351 dev_priv->display.off = ironlake_crtc_off;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014352 dev_priv->display.update_primary_plane =
14353 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030014354 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014355 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014356 dev_priv->display.get_initial_plane_config =
14357 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014358 dev_priv->display.crtc_compute_clock =
14359 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014360 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14361 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014362 dev_priv->display.off = ironlake_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070014363 dev_priv->display.update_primary_plane =
14364 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014365 } else if (IS_VALLEYVIEW(dev)) {
14366 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014367 dev_priv->display.get_initial_plane_config =
14368 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014369 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014370 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14371 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14372 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070014373 dev_priv->display.update_primary_plane =
14374 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014375 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014376 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014377 dev_priv->display.get_initial_plane_config =
14378 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014379 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014380 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14381 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014382 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070014383 dev_priv->display.update_primary_plane =
14384 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014385 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014386
Jesse Barnese70236a2009-09-21 10:42:27 -070014387 /* Returns the core display clock speed */
Ville Syrjälä1652d192015-03-31 14:12:01 +030014388 if (IS_SKYLAKE(dev))
14389 dev_priv->display.get_display_clock_speed =
14390 skylake_get_display_clock_speed;
14391 else if (IS_BROADWELL(dev))
14392 dev_priv->display.get_display_clock_speed =
14393 broadwell_get_display_clock_speed;
14394 else if (IS_HASWELL(dev))
14395 dev_priv->display.get_display_clock_speed =
14396 haswell_get_display_clock_speed;
14397 else if (IS_VALLEYVIEW(dev))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070014398 dev_priv->display.get_display_clock_speed =
14399 valleyview_get_display_clock_speed;
Ville Syrjäläb37a6432015-03-31 14:11:54 +030014400 else if (IS_GEN5(dev))
14401 dev_priv->display.get_display_clock_speed =
14402 ilk_get_display_clock_speed;
Ville Syrjäläa7c66cd2015-03-31 14:11:56 +030014403 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
14404 IS_GEN6(dev) || IS_IVYBRIDGE(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -070014405 dev_priv->display.get_display_clock_speed =
14406 i945_get_display_clock_speed;
14407 else if (IS_I915G(dev))
14408 dev_priv->display.get_display_clock_speed =
14409 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014410 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014411 dev_priv->display.get_display_clock_speed =
14412 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014413 else if (IS_PINEVIEW(dev))
14414 dev_priv->display.get_display_clock_speed =
14415 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014416 else if (IS_I915GM(dev))
14417 dev_priv->display.get_display_clock_speed =
14418 i915gm_get_display_clock_speed;
14419 else if (IS_I865G(dev))
14420 dev_priv->display.get_display_clock_speed =
14421 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020014422 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014423 dev_priv->display.get_display_clock_speed =
14424 i855_get_display_clock_speed;
14425 else /* 852, 830 */
14426 dev_priv->display.get_display_clock_speed =
14427 i830_get_display_clock_speed;
14428
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014429 if (IS_GEN5(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014430 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014431 } else if (IS_GEN6(dev)) {
14432 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014433 } else if (IS_IVYBRIDGE(dev)) {
14434 /* FIXME: detect B0+ stepping and use auto training */
14435 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Paulo Zanoni059b2fe2014-09-02 16:53:57 -030014436 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014437 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Jesse Barnes30a970c2013-11-04 13:48:12 -080014438 } else if (IS_VALLEYVIEW(dev)) {
14439 dev_priv->display.modeset_global_resources =
14440 valleyview_modeset_global_resources;
Vandana Kannanf8437dd12014-11-24 13:37:39 +053014441 } else if (IS_BROXTON(dev)) {
14442 dev_priv->display.modeset_global_resources =
14443 broxton_modeset_global_resources;
Jesse Barnese70236a2009-09-21 10:42:27 -070014444 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014445
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014446 switch (INTEL_INFO(dev)->gen) {
14447 case 2:
14448 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14449 break;
14450
14451 case 3:
14452 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14453 break;
14454
14455 case 4:
14456 case 5:
14457 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14458 break;
14459
14460 case 6:
14461 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14462 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014463 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070014464 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014465 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14466 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000014467 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000014468 /* Drop through - unsupported since execlist only. */
14469 default:
14470 /* Default just returns -ENODEV to indicate unsupported */
14471 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014472 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020014473
14474 intel_panel_init_backlight_funcs(dev);
Ville Syrjäläe39b9992014-09-04 14:53:14 +030014475
14476 mutex_init(&dev_priv->pps_mutex);
Jesse Barnese70236a2009-09-21 10:42:27 -070014477}
14478
Jesse Barnesb690e962010-07-19 13:53:12 -070014479/*
14480 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14481 * resume, or other times. This quirk makes sure that's the case for
14482 * affected systems.
14483 */
Akshay Joshi0206e352011-08-16 15:34:10 -040014484static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070014485{
14486 struct drm_i915_private *dev_priv = dev->dev_private;
14487
14488 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014489 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014490}
14491
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014492static void quirk_pipeb_force(struct drm_device *dev)
14493{
14494 struct drm_i915_private *dev_priv = dev->dev_private;
14495
14496 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14497 DRM_INFO("applying pipe b force quirk\n");
14498}
14499
Keith Packard435793d2011-07-12 14:56:22 -070014500/*
14501 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14502 */
14503static void quirk_ssc_force_disable(struct drm_device *dev)
14504{
14505 struct drm_i915_private *dev_priv = dev->dev_private;
14506 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014507 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070014508}
14509
Carsten Emde4dca20e2012-03-15 15:56:26 +010014510/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010014511 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14512 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010014513 */
14514static void quirk_invert_brightness(struct drm_device *dev)
14515{
14516 struct drm_i915_private *dev_priv = dev->dev_private;
14517 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014518 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014519}
14520
Scot Doyle9c72cc62014-07-03 23:27:50 +000014521/* Some VBT's incorrectly indicate no backlight is present */
14522static void quirk_backlight_present(struct drm_device *dev)
14523{
14524 struct drm_i915_private *dev_priv = dev->dev_private;
14525 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14526 DRM_INFO("applying backlight present quirk\n");
14527}
14528
Jesse Barnesb690e962010-07-19 13:53:12 -070014529struct intel_quirk {
14530 int device;
14531 int subsystem_vendor;
14532 int subsystem_device;
14533 void (*hook)(struct drm_device *dev);
14534};
14535
Egbert Eich5f85f172012-10-14 15:46:38 +020014536/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14537struct intel_dmi_quirk {
14538 void (*hook)(struct drm_device *dev);
14539 const struct dmi_system_id (*dmi_id_list)[];
14540};
14541
14542static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14543{
14544 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14545 return 1;
14546}
14547
14548static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14549 {
14550 .dmi_id_list = &(const struct dmi_system_id[]) {
14551 {
14552 .callback = intel_dmi_reverse_brightness,
14553 .ident = "NCR Corporation",
14554 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14555 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14556 },
14557 },
14558 { } /* terminating entry */
14559 },
14560 .hook = quirk_invert_brightness,
14561 },
14562};
14563
Ben Widawskyc43b5632012-04-16 14:07:40 -070014564static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070014565 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -040014566 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -070014567
Jesse Barnesb690e962010-07-19 13:53:12 -070014568 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14569 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14570
Jesse Barnesb690e962010-07-19 13:53:12 -070014571 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14572 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14573
Ville Syrjälä5f080c02014-08-15 01:22:06 +030014574 /* 830 needs to leave pipe A & dpll A up */
14575 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14576
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014577 /* 830 needs to leave pipe B & dpll B up */
14578 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14579
Keith Packard435793d2011-07-12 14:56:22 -070014580 /* Lenovo U160 cannot use SSC on LVDS */
14581 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020014582
14583 /* Sony Vaio Y cannot use SSC on LVDS */
14584 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010014585
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010014586 /* Acer Aspire 5734Z must invert backlight brightness */
14587 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14588
14589 /* Acer/eMachines G725 */
14590 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14591
14592 /* Acer/eMachines e725 */
14593 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14594
14595 /* Acer/Packard Bell NCL20 */
14596 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14597
14598 /* Acer Aspire 4736Z */
14599 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020014600
14601 /* Acer Aspire 5336 */
14602 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000014603
14604 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14605 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000014606
Scot Doyledfb3d47b2014-08-21 16:08:02 +000014607 /* Acer C720 Chromebook (Core i3 4005U) */
14608 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14609
jens steinb2a96012014-10-28 20:25:53 +010014610 /* Apple Macbook 2,1 (Core 2 T7400) */
14611 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14612
Scot Doyled4967d82014-07-03 23:27:52 +000014613 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14614 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000014615
14616 /* HP Chromebook 14 (Celeron 2955U) */
14617 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020014618
14619 /* Dell Chromebook 11 */
14620 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070014621};
14622
14623static void intel_init_quirks(struct drm_device *dev)
14624{
14625 struct pci_dev *d = dev->pdev;
14626 int i;
14627
14628 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14629 struct intel_quirk *q = &intel_quirks[i];
14630
14631 if (d->device == q->device &&
14632 (d->subsystem_vendor == q->subsystem_vendor ||
14633 q->subsystem_vendor == PCI_ANY_ID) &&
14634 (d->subsystem_device == q->subsystem_device ||
14635 q->subsystem_device == PCI_ANY_ID))
14636 q->hook(dev);
14637 }
Egbert Eich5f85f172012-10-14 15:46:38 +020014638 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14639 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14640 intel_dmi_quirks[i].hook(dev);
14641 }
Jesse Barnesb690e962010-07-19 13:53:12 -070014642}
14643
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014644/* Disable the VGA plane that we never use */
14645static void i915_disable_vga(struct drm_device *dev)
14646{
14647 struct drm_i915_private *dev_priv = dev->dev_private;
14648 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020014649 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014650
Ville Syrjälä2b37c612014-01-22 21:32:38 +020014651 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014652 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070014653 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014654 sr1 = inb(VGA_SR_DATA);
14655 outb(sr1 | 1<<5, VGA_SR_DATA);
14656 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
14657 udelay(300);
14658
Ville Syrjälä01f5a622014-12-16 18:38:37 +020014659 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014660 POSTING_READ(vga_reg);
14661}
14662
Daniel Vetterf8175862012-04-10 15:50:11 +020014663void intel_modeset_init_hw(struct drm_device *dev)
14664{
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030014665 intel_prepare_ddi(dev);
14666
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +030014667 if (IS_VALLEYVIEW(dev))
14668 vlv_update_cdclk(dev);
14669
Daniel Vetterf8175862012-04-10 15:50:11 +020014670 intel_init_clock_gating(dev);
14671
Daniel Vetter8090c6b2012-06-24 16:42:32 +020014672 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020014673}
14674
Jesse Barnes79e53942008-11-07 14:24:08 -080014675void intel_modeset_init(struct drm_device *dev)
14676{
Jesse Barnes652c3932009-08-17 13:31:43 -070014677 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000014678 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014679 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080014680 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080014681
14682 drm_mode_config_init(dev);
14683
14684 dev->mode_config.min_width = 0;
14685 dev->mode_config.min_height = 0;
14686
Dave Airlie019d96c2011-09-29 16:20:42 +010014687 dev->mode_config.preferred_depth = 24;
14688 dev->mode_config.prefer_shadow = 1;
14689
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000014690 dev->mode_config.allow_fb_modifiers = true;
14691
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020014692 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080014693
Jesse Barnesb690e962010-07-19 13:53:12 -070014694 intel_init_quirks(dev);
14695
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030014696 intel_init_pm(dev);
14697
Ben Widawskye3c74752013-04-05 13:12:39 -070014698 if (INTEL_INFO(dev)->num_pipes == 0)
14699 return;
14700
Jesse Barnese70236a2009-09-21 10:42:27 -070014701 intel_init_display(dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014702 intel_init_audio(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070014703
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014704 if (IS_GEN2(dev)) {
14705 dev->mode_config.max_width = 2048;
14706 dev->mode_config.max_height = 2048;
14707 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070014708 dev->mode_config.max_width = 4096;
14709 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080014710 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014711 dev->mode_config.max_width = 8192;
14712 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080014713 }
Damien Lespiau068be562014-03-28 14:17:49 +000014714
Ville Syrjälädc41c152014-08-13 11:57:05 +030014715 if (IS_845G(dev) || IS_I865G(dev)) {
14716 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
14717 dev->mode_config.cursor_height = 1023;
14718 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000014719 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
14720 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
14721 } else {
14722 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
14723 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
14724 }
14725
Ben Widawsky5d4545a2013-01-17 12:45:15 -080014726 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080014727
Zhao Yakui28c97732009-10-09 11:39:41 +080014728 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014729 INTEL_INFO(dev)->num_pipes,
14730 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080014731
Damien Lespiau055e3932014-08-18 13:49:10 +010014732 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014733 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000014734 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000014735 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070014736 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030014737 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000014738 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070014739 }
Jesse Barnes79e53942008-11-07 14:24:08 -080014740 }
14741
Jesse Barnesf42bb702013-12-16 16:34:23 -080014742 intel_init_dpio(dev);
14743
Daniel Vettere72f9fb2013-06-05 13:34:06 +020014744 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014745
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014746 /* Just disable it once at startup */
14747 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014748 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000014749
14750 /* Just in case the BIOS is doing something questionable. */
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020014751 intel_fbc_disable(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080014752
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014753 drm_modeset_lock_all(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080014754 intel_modeset_setup_hw_state(dev, false);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014755 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014756
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014757 for_each_intel_crtc(dev, crtc) {
Jesse Barnes46f297f2014-03-07 08:57:48 -080014758 if (!crtc->active)
14759 continue;
14760
Jesse Barnes46f297f2014-03-07 08:57:48 -080014761 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080014762 * Note that reserving the BIOS fb up front prevents us
14763 * from stuffing other stolen allocations like the ring
14764 * on top. This prevents some ugliness at boot time, and
14765 * can even allow for smooth boot transitions if the BIOS
14766 * fb is large enough for the active pipe configuration.
14767 */
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014768 if (dev_priv->display.get_initial_plane_config) {
14769 dev_priv->display.get_initial_plane_config(crtc,
Jesse Barnes46f297f2014-03-07 08:57:48 -080014770 &crtc->plane_config);
14771 /*
14772 * If the fb is shared between multiple heads, we'll
14773 * just get the first one.
14774 */
Daniel Vetterf6936e22015-03-26 12:17:05 +010014775 intel_find_initial_plane_obj(crtc, &crtc->plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014776 }
Jesse Barnes46f297f2014-03-07 08:57:48 -080014777 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010014778}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080014779
Daniel Vetter7fad7982012-07-04 17:51:47 +020014780static void intel_enable_pipe_a(struct drm_device *dev)
14781{
14782 struct intel_connector *connector;
14783 struct drm_connector *crt = NULL;
14784 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030014785 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020014786
14787 /* We can't just switch on the pipe A, we need to set things up with a
14788 * proper mode and output configuration. As a gross hack, enable pipe A
14789 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014790 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020014791 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
14792 crt = &connector->base;
14793 break;
14794 }
14795 }
14796
14797 if (!crt)
14798 return;
14799
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030014800 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020014801 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020014802}
14803
Daniel Vetterfa555832012-10-10 23:14:00 +020014804static bool
14805intel_check_plane_mapping(struct intel_crtc *crtc)
14806{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014807 struct drm_device *dev = crtc->base.dev;
14808 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020014809 u32 reg, val;
14810
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014811 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020014812 return true;
14813
14814 reg = DSPCNTR(!crtc->plane);
14815 val = I915_READ(reg);
14816
14817 if ((val & DISPLAY_PLANE_ENABLE) &&
14818 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
14819 return false;
14820
14821 return true;
14822}
14823
Daniel Vetter24929352012-07-02 20:28:59 +020014824static void intel_sanitize_crtc(struct intel_crtc *crtc)
14825{
14826 struct drm_device *dev = crtc->base.dev;
14827 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020014828 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020014829
Daniel Vetter24929352012-07-02 20:28:59 +020014830 /* Clear any frame start delays used for debugging left by the BIOS */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014831 reg = PIPECONF(crtc->config->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020014832 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
14833
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030014834 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010014835 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030014836 if (crtc->active) {
14837 update_scanline_offset(crtc);
Daniel Vetter96256042015-02-13 21:03:42 +010014838 drm_crtc_vblank_on(&crtc->base);
14839 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030014840
Daniel Vetter24929352012-07-02 20:28:59 +020014841 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020014842 * disable the crtc (and hence change the state) if it is wrong. Note
14843 * that gen4+ has a fixed plane -> pipe mapping. */
14844 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020014845 struct intel_connector *connector;
14846 bool plane;
14847
Daniel Vetter24929352012-07-02 20:28:59 +020014848 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
14849 crtc->base.base.id);
14850
14851 /* Pipe has the wrong plane attached and the plane is active.
14852 * Temporarily change the plane mapping and disable everything
14853 * ... */
14854 plane = crtc->plane;
14855 crtc->plane = !plane;
Daniel Vetter9c8958b2014-07-14 19:35:31 +020014856 crtc->primary_enabled = true;
Daniel Vetter24929352012-07-02 20:28:59 +020014857 dev_priv->display.crtc_disable(&crtc->base);
14858 crtc->plane = plane;
14859
14860 /* ... and break all links. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014861 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020014862 if (connector->encoder->base.crtc != &crtc->base)
14863 continue;
14864
Egbert Eich7f1950f2014-04-25 10:56:22 +020014865 connector->base.dpms = DRM_MODE_DPMS_OFF;
14866 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020014867 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020014868 /* multiple connectors may have the same encoder:
14869 * handle them and break crtc link separately */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014870 for_each_intel_connector(dev, connector)
Egbert Eich7f1950f2014-04-25 10:56:22 +020014871 if (connector->encoder->base.crtc == &crtc->base) {
14872 connector->encoder->base.crtc = NULL;
14873 connector->encoder->connectors_active = false;
14874 }
Daniel Vetter24929352012-07-02 20:28:59 +020014875
14876 WARN_ON(crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080014877 crtc->base.state->enable = false;
Daniel Vetter24929352012-07-02 20:28:59 +020014878 crtc->base.enabled = false;
14879 }
Daniel Vetter24929352012-07-02 20:28:59 +020014880
Daniel Vetter7fad7982012-07-04 17:51:47 +020014881 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
14882 crtc->pipe == PIPE_A && !crtc->active) {
14883 /* BIOS forgot to enable pipe A, this mostly happens after
14884 * resume. Force-enable the pipe to fix this, the update_dpms
14885 * call below we restore the pipe to the right state, but leave
14886 * the required bits on. */
14887 intel_enable_pipe_a(dev);
14888 }
14889
Daniel Vetter24929352012-07-02 20:28:59 +020014890 /* Adjust the state of the output pipe according to whether we
14891 * have active connectors/encoders. */
14892 intel_crtc_update_dpms(&crtc->base);
14893
Matt Roper83d65732015-02-25 13:12:16 -080014894 if (crtc->active != crtc->base.state->enable) {
Daniel Vetter24929352012-07-02 20:28:59 +020014895 struct intel_encoder *encoder;
14896
14897 /* This can happen either due to bugs in the get_hw_state
14898 * functions or because the pipe is force-enabled due to the
14899 * pipe A quirk. */
14900 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
14901 crtc->base.base.id,
Matt Roper83d65732015-02-25 13:12:16 -080014902 crtc->base.state->enable ? "enabled" : "disabled",
Daniel Vetter24929352012-07-02 20:28:59 +020014903 crtc->active ? "enabled" : "disabled");
14904
Matt Roper83d65732015-02-25 13:12:16 -080014905 crtc->base.state->enable = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020014906 crtc->base.enabled = crtc->active;
14907
14908 /* Because we only establish the connector -> encoder ->
14909 * crtc links if something is active, this means the
14910 * crtc is now deactivated. Break the links. connector
14911 * -> encoder links are only establish when things are
14912 * actually up, hence no need to break them. */
14913 WARN_ON(crtc->active);
14914
14915 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
14916 WARN_ON(encoder->connectors_active);
14917 encoder->base.crtc = NULL;
14918 }
14919 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020014920
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030014921 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010014922 /*
14923 * We start out with underrun reporting disabled to avoid races.
14924 * For correct bookkeeping mark this on active crtcs.
14925 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020014926 * Also on gmch platforms we dont have any hardware bits to
14927 * disable the underrun reporting. Which means we need to start
14928 * out with underrun reporting disabled also on inactive pipes,
14929 * since otherwise we'll complain about the garbage we read when
14930 * e.g. coming up after runtime pm.
14931 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010014932 * No protection against concurrent access is required - at
14933 * worst a fifo underrun happens which also sets this to false.
14934 */
14935 crtc->cpu_fifo_underrun_disabled = true;
14936 crtc->pch_fifo_underrun_disabled = true;
14937 }
Daniel Vetter24929352012-07-02 20:28:59 +020014938}
14939
14940static void intel_sanitize_encoder(struct intel_encoder *encoder)
14941{
14942 struct intel_connector *connector;
14943 struct drm_device *dev = encoder->base.dev;
14944
14945 /* We need to check both for a crtc link (meaning that the
14946 * encoder is active and trying to read from a pipe) and the
14947 * pipe itself being active. */
14948 bool has_active_crtc = encoder->base.crtc &&
14949 to_intel_crtc(encoder->base.crtc)->active;
14950
14951 if (encoder->connectors_active && !has_active_crtc) {
14952 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
14953 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014954 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020014955
14956 /* Connector is active, but has no active pipe. This is
14957 * fallout from our resume register restoring. Disable
14958 * the encoder manually again. */
14959 if (encoder->base.crtc) {
14960 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
14961 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014962 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020014963 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030014964 if (encoder->post_disable)
14965 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020014966 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020014967 encoder->base.crtc = NULL;
14968 encoder->connectors_active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020014969
14970 /* Inconsistent output/port/pipe state happens presumably due to
14971 * a bug in one of the get_hw_state functions. Or someplace else
14972 * in our code, like the register restore mess on resume. Clamp
14973 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014974 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020014975 if (connector->encoder != encoder)
14976 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020014977 connector->base.dpms = DRM_MODE_DPMS_OFF;
14978 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020014979 }
14980 }
14981 /* Enabled encoders without active connectors will be fixed in
14982 * the crtc fixup. */
14983}
14984
Imre Deak04098752014-02-18 00:02:16 +020014985void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014986{
14987 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020014988 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014989
Imre Deak04098752014-02-18 00:02:16 +020014990 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
14991 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
14992 i915_disable_vga(dev);
14993 }
14994}
14995
14996void i915_redisable_vga(struct drm_device *dev)
14997{
14998 struct drm_i915_private *dev_priv = dev->dev_private;
14999
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015000 /* This function can be called both from intel_modeset_setup_hw_state or
15001 * at a very early point in our resume sequence, where the power well
15002 * structures are not yet restored. Since this function is at a very
15003 * paranoid "someone might have enabled VGA while we were not looking"
15004 * level, just check if the power well is enabled instead of trying to
15005 * follow the "don't touch the power well if we don't need it" policy
15006 * the rest of the driver uses. */
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015007 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015008 return;
15009
Imre Deak04098752014-02-18 00:02:16 +020015010 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015011}
15012
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015013static bool primary_get_hw_state(struct intel_crtc *crtc)
15014{
15015 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
15016
15017 if (!crtc->active)
15018 return false;
15019
15020 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
15021}
15022
Daniel Vetter30e984d2013-06-05 13:34:17 +020015023static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015024{
15025 struct drm_i915_private *dev_priv = dev->dev_private;
15026 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015027 struct intel_crtc *crtc;
15028 struct intel_encoder *encoder;
15029 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020015030 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015031
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015032 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015033 memset(crtc->config, 0, sizeof(*crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +020015034
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015035 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
Daniel Vetter99535992014-04-13 12:00:33 +020015036
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015037 crtc->active = dev_priv->display.get_pipe_config(crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015038 crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015039
Matt Roper83d65732015-02-25 13:12:16 -080015040 crtc->base.state->enable = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020015041 crtc->base.enabled = crtc->active;
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015042 crtc->primary_enabled = primary_get_hw_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020015043
15044 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15045 crtc->base.base.id,
15046 crtc->active ? "enabled" : "disabled");
15047 }
15048
Daniel Vetter53589012013-06-05 13:34:16 +020015049 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15050 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15051
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015052 pll->on = pll->get_hw_state(dev_priv, pll,
15053 &pll->config.hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020015054 pll->active = 0;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015055 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015056 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015057 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
Daniel Vetter53589012013-06-05 13:34:16 +020015058 pll->active++;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015059 pll->config.crtc_mask |= 1 << crtc->pipe;
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015060 }
Daniel Vetter53589012013-06-05 13:34:16 +020015061 }
Daniel Vetter53589012013-06-05 13:34:16 +020015062
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015063 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015064 pll->name, pll->config.crtc_mask, pll->on);
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015065
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015066 if (pll->config.crtc_mask)
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015067 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
Daniel Vetter53589012013-06-05 13:34:16 +020015068 }
15069
Damien Lespiaub2784e12014-08-05 11:29:37 +010015070 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015071 pipe = 0;
15072
15073 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015074 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15075 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015076 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015077 } else {
15078 encoder->base.crtc = NULL;
15079 }
15080
15081 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015082 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020015083 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015084 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015085 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015086 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015087 }
15088
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015089 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015090 if (connector->get_hw_state(connector)) {
15091 connector->base.dpms = DRM_MODE_DPMS_ON;
15092 connector->encoder->connectors_active = true;
15093 connector->base.encoder = &connector->encoder->base;
15094 } else {
15095 connector->base.dpms = DRM_MODE_DPMS_OFF;
15096 connector->base.encoder = NULL;
15097 }
15098 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15099 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030015100 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015101 connector->base.encoder ? "enabled" : "disabled");
15102 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015103}
15104
15105/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
15106 * and i915 state tracking structures. */
15107void intel_modeset_setup_hw_state(struct drm_device *dev,
15108 bool force_restore)
15109{
15110 struct drm_i915_private *dev_priv = dev->dev_private;
15111 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015112 struct intel_crtc *crtc;
15113 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015114 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015115
15116 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015117
Jesse Barnesbabea612013-06-26 18:57:38 +030015118 /*
15119 * Now that we have the config, copy it to each CRTC struct
15120 * Note that this could go away if we move to using crtc_config
15121 * checking everywhere.
15122 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015123 for_each_intel_crtc(dev, crtc) {
Jani Nikulad330a952014-01-21 11:24:25 +020015124 if (crtc->active && i915.fastboot) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015125 intel_mode_from_pipe_config(&crtc->base.mode,
15126 crtc->config);
Jesse Barnesbabea612013-06-26 18:57:38 +030015127 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
15128 crtc->base.base.id);
15129 drm_mode_debug_printmodeline(&crtc->base.mode);
15130 }
15131 }
15132
Daniel Vetter24929352012-07-02 20:28:59 +020015133 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010015134 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015135 intel_sanitize_encoder(encoder);
15136 }
15137
Damien Lespiau055e3932014-08-18 13:49:10 +010015138 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020015139 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15140 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015141 intel_dump_pipe_config(crtc, crtc->config,
15142 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015143 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015144
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015145 intel_modeset_update_connector_atomic_state(dev);
15146
Daniel Vetter35c95372013-07-17 06:55:04 +020015147 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15148 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15149
15150 if (!pll->on || pll->active)
15151 continue;
15152
15153 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15154
15155 pll->disable(dev_priv, pll);
15156 pll->on = false;
15157 }
15158
Pradeep Bhat30789992014-11-04 17:06:45 +000015159 if (IS_GEN9(dev))
15160 skl_wm_get_hw_state(dev);
15161 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015162 ilk_wm_get_hw_state(dev);
15163
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015164 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015165 i915_redisable_vga(dev);
15166
Daniel Vetterf30da182013-04-11 20:22:50 +020015167 /*
15168 * We need to use raw interfaces for restoring state to avoid
15169 * checking (bogus) intermediate states.
15170 */
Damien Lespiau055e3932014-08-18 13:49:10 +010015171 for_each_pipe(dev_priv, pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070015172 struct drm_crtc *crtc =
15173 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020015174
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020015175 intel_crtc_restore_mode(crtc);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015176 }
15177 } else {
15178 intel_modeset_update_staged_output_state(dev);
15179 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020015180
15181 intel_modeset_check_state(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015182}
15183
15184void intel_modeset_gem_init(struct drm_device *dev)
15185{
Jesse Barnes92122782014-10-09 12:57:42 -070015186 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015187 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070015188 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015189 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015190
Imre Deakae484342014-03-31 15:10:44 +030015191 mutex_lock(&dev->struct_mutex);
15192 intel_init_gt_powersave(dev);
15193 mutex_unlock(&dev->struct_mutex);
15194
Jesse Barnes92122782014-10-09 12:57:42 -070015195 /*
15196 * There may be no VBT; and if the BIOS enabled SSC we can
15197 * just keep using it to avoid unnecessary flicker. Whereas if the
15198 * BIOS isn't using it, don't assume it will work even if the VBT
15199 * indicates as much.
15200 */
15201 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
15202 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15203 DREF_SSC1_ENABLE);
15204
Chris Wilson1833b132012-05-09 11:56:28 +010015205 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020015206
15207 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015208
15209 /*
15210 * Make sure any fbs we allocated at startup are properly
15211 * pinned & fenced. When we do the allocation it's too early
15212 * for this.
15213 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010015214 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070015215 obj = intel_fb_obj(c->primary->fb);
15216 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080015217 continue;
15218
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015219 mutex_lock(&dev->struct_mutex);
15220 ret = intel_pin_and_fence_fb_obj(c->primary,
15221 c->primary->fb,
15222 c->primary->state,
15223 NULL);
15224 mutex_unlock(&dev->struct_mutex);
15225 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080015226 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15227 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100015228 drm_framebuffer_unreference(c->primary->fb);
15229 c->primary->fb = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080015230 update_state_fb(c->primary);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015231 }
15232 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015233
15234 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015235}
15236
Imre Deak4932e2c2014-02-11 17:12:48 +020015237void intel_connector_unregister(struct intel_connector *intel_connector)
15238{
15239 struct drm_connector *connector = &intel_connector->base;
15240
15241 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010015242 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015243}
15244
Jesse Barnes79e53942008-11-07 14:24:08 -080015245void intel_modeset_cleanup(struct drm_device *dev)
15246{
Jesse Barnes652c3932009-08-17 13:31:43 -070015247 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid9255d52013-09-26 20:05:59 -030015248 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070015249
Imre Deak2eb52522014-11-19 15:30:05 +020015250 intel_disable_gt_powersave(dev);
15251
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015252 intel_backlight_unregister(dev);
15253
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015254 /*
15255 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020015256 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015257 * experience fancy races otherwise.
15258 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020015259 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070015260
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015261 /*
15262 * Due to the hpd irq storm handling the hotplug work can re-arm the
15263 * poll handlers. Hence disable polling after hpd handling is shut down.
15264 */
Keith Packardf87ea762010-10-03 19:36:26 -070015265 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015266
Jesse Barnes652c3932009-08-17 13:31:43 -070015267 mutex_lock(&dev->struct_mutex);
15268
Jesse Barnes723bfd72010-10-07 16:01:13 -070015269 intel_unregister_dsm_handler();
15270
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020015271 intel_fbc_disable(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070015272
Kristian Høgsberg69341a52009-11-11 12:19:17 -050015273 mutex_unlock(&dev->struct_mutex);
15274
Chris Wilson1630fe72011-07-08 12:22:42 +010015275 /* flush any delayed tasks or pending work */
15276 flush_scheduled_work();
15277
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015278 /* destroy the backlight and sysfs files before encoders/connectors */
15279 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020015280 struct intel_connector *intel_connector;
15281
15282 intel_connector = to_intel_connector(connector);
15283 intel_connector->unregister(intel_connector);
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015284 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030015285
Jesse Barnes79e53942008-11-07 14:24:08 -080015286 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010015287
15288 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030015289
15290 mutex_lock(&dev->struct_mutex);
15291 intel_cleanup_gt_powersave(dev);
15292 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080015293}
15294
Dave Airlie28d52042009-09-21 14:33:58 +100015295/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080015296 * Return which encoder is currently attached for connector.
15297 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010015298struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080015299{
Chris Wilsondf0e9242010-09-09 16:20:55 +010015300 return &intel_attached_encoder(connector)->base;
15301}
Jesse Barnes79e53942008-11-07 14:24:08 -080015302
Chris Wilsondf0e9242010-09-09 16:20:55 +010015303void intel_connector_attach_encoder(struct intel_connector *connector,
15304 struct intel_encoder *encoder)
15305{
15306 connector->encoder = encoder;
15307 drm_mode_connector_attach_encoder(&connector->base,
15308 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080015309}
Dave Airlie28d52042009-09-21 14:33:58 +100015310
15311/*
15312 * set vga decode state - true == enable VGA decode
15313 */
15314int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15315{
15316 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000015317 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100015318 u16 gmch_ctrl;
15319
Chris Wilson75fa0412014-02-07 18:37:02 -020015320 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15321 DRM_ERROR("failed to read control word\n");
15322 return -EIO;
15323 }
15324
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020015325 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15326 return 0;
15327
Dave Airlie28d52042009-09-21 14:33:58 +100015328 if (state)
15329 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15330 else
15331 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020015332
15333 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15334 DRM_ERROR("failed to write control word\n");
15335 return -EIO;
15336 }
15337
Dave Airlie28d52042009-09-21 14:33:58 +100015338 return 0;
15339}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015340
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015341struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015342
15343 u32 power_well_driver;
15344
Chris Wilson63b66e52013-08-08 15:12:06 +020015345 int num_transcoders;
15346
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015347 struct intel_cursor_error_state {
15348 u32 control;
15349 u32 position;
15350 u32 base;
15351 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010015352 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015353
15354 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015355 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015356 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030015357 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010015358 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015359
15360 struct intel_plane_error_state {
15361 u32 control;
15362 u32 stride;
15363 u32 size;
15364 u32 pos;
15365 u32 addr;
15366 u32 surface;
15367 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010015368 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020015369
15370 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015371 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020015372 enum transcoder cpu_transcoder;
15373
15374 u32 conf;
15375
15376 u32 htotal;
15377 u32 hblank;
15378 u32 hsync;
15379 u32 vtotal;
15380 u32 vblank;
15381 u32 vsync;
15382 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015383};
15384
15385struct intel_display_error_state *
15386intel_display_capture_error_state(struct drm_device *dev)
15387{
Jani Nikulafbee40d2014-03-31 14:27:18 +030015388 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015389 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020015390 int transcoders[] = {
15391 TRANSCODER_A,
15392 TRANSCODER_B,
15393 TRANSCODER_C,
15394 TRANSCODER_EDP,
15395 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015396 int i;
15397
Chris Wilson63b66e52013-08-08 15:12:06 +020015398 if (INTEL_INFO(dev)->num_pipes == 0)
15399 return NULL;
15400
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015401 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015402 if (error == NULL)
15403 return NULL;
15404
Imre Deak190be112013-11-25 17:15:31 +020015405 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015406 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15407
Damien Lespiau055e3932014-08-18 13:49:10 +010015408 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020015409 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015410 __intel_display_power_is_enabled(dev_priv,
15411 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020015412 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015413 continue;
15414
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030015415 error->cursor[i].control = I915_READ(CURCNTR(i));
15416 error->cursor[i].position = I915_READ(CURPOS(i));
15417 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015418
15419 error->plane[i].control = I915_READ(DSPCNTR(i));
15420 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015421 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030015422 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015423 error->plane[i].pos = I915_READ(DSPPOS(i));
15424 }
Paulo Zanonica291362013-03-06 20:03:14 -030015425 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15426 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015427 if (INTEL_INFO(dev)->gen >= 4) {
15428 error->plane[i].surface = I915_READ(DSPSURF(i));
15429 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15430 }
15431
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015432 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030015433
Sonika Jindal3abfce72014-07-21 15:23:43 +053015434 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e2014-04-18 15:55:04 +030015435 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020015436 }
15437
15438 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
15439 if (HAS_DDI(dev_priv->dev))
15440 error->num_transcoders++; /* Account for eDP. */
15441
15442 for (i = 0; i < error->num_transcoders; i++) {
15443 enum transcoder cpu_transcoder = transcoders[i];
15444
Imre Deakddf9c532013-11-27 22:02:02 +020015445 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015446 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020015447 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015448 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015449 continue;
15450
Chris Wilson63b66e52013-08-08 15:12:06 +020015451 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15452
15453 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15454 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15455 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15456 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15457 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15458 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15459 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015460 }
15461
15462 return error;
15463}
15464
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015465#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15466
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015467void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015468intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015469 struct drm_device *dev,
15470 struct intel_display_error_state *error)
15471{
Damien Lespiau055e3932014-08-18 13:49:10 +010015472 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015473 int i;
15474
Chris Wilson63b66e52013-08-08 15:12:06 +020015475 if (!error)
15476 return;
15477
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015478 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020015479 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015480 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015481 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010015482 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015483 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020015484 err_printf(m, " Power: %s\n",
15485 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015486 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030015487 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015488
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015489 err_printf(m, "Plane [%d]:\n", i);
15490 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15491 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015492 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015493 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15494 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015495 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030015496 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015497 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015498 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015499 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15500 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015501 }
15502
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015503 err_printf(m, "Cursor [%d]:\n", i);
15504 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15505 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15506 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015507 }
Chris Wilson63b66e52013-08-08 15:12:06 +020015508
15509 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010015510 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020015511 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015512 err_printf(m, " Power: %s\n",
15513 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020015514 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15515 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15516 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15517 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15518 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15519 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15520 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15521 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015522}
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015523
15524void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
15525{
15526 struct intel_crtc *crtc;
15527
15528 for_each_intel_crtc(dev, crtc) {
15529 struct intel_unpin_work *work;
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015530
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015531 spin_lock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015532
15533 work = crtc->unpin_work;
15534
15535 if (work && work->event &&
15536 work->event->base.file_priv == file) {
15537 kfree(work->event);
15538 work->event = NULL;
15539 }
15540
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015541 spin_unlock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015542 }
15543}