blob: 3094b0807b407f740f80b751d2398c5ba0ec7af4 [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
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002697 if (!intel_crtc->primary_enabled) {
2698 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
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002826 if (!intel_crtc->primary_enabled) {
2827 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
Damien Lespiau70d21f02013-07-03 21:06:04 +01002988static void skylake_update_primary_plane(struct drm_crtc *crtc,
2989 struct drm_framebuffer *fb,
2990 int x, int y)
2991{
2992 struct drm_device *dev = crtc->dev;
2993 struct drm_i915_private *dev_priv = dev->dev_private;
2994 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Damien Lespiau70d21f02013-07-03 21:06:04 +01002995 struct drm_i915_gem_object *obj;
2996 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05302997 u32 plane_ctl, stride_div, stride;
2998 u32 tile_height, plane_offset, plane_size;
2999 unsigned int rotation;
3000 int x_offset, y_offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003001 unsigned long surf_addr;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303002 struct drm_plane *plane;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003003
3004 if (!intel_crtc->primary_enabled) {
3005 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3006 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3007 POSTING_READ(PLANE_CTL(pipe, 0));
3008 return;
3009 }
3010
3011 plane_ctl = PLANE_CTL_ENABLE |
3012 PLANE_CTL_PIPE_GAMMA_ENABLE |
3013 PLANE_CTL_PIPE_CSC_ENABLE;
3014
3015 switch (fb->pixel_format) {
3016 case DRM_FORMAT_RGB565:
3017 plane_ctl |= PLANE_CTL_FORMAT_RGB_565;
3018 break;
3019 case DRM_FORMAT_XRGB8888:
3020 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
3021 break;
Jani Nikulaf75fb422015-02-10 13:15:49 +02003022 case DRM_FORMAT_ARGB8888:
3023 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
3024 plane_ctl |= PLANE_CTL_ALPHA_SW_PREMULTIPLY;
3025 break;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003026 case DRM_FORMAT_XBGR8888:
3027 plane_ctl |= PLANE_CTL_ORDER_RGBX;
3028 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
3029 break;
Jani Nikulaf75fb422015-02-10 13:15:49 +02003030 case DRM_FORMAT_ABGR8888:
3031 plane_ctl |= PLANE_CTL_ORDER_RGBX;
3032 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
3033 plane_ctl |= PLANE_CTL_ALPHA_SW_PREMULTIPLY;
3034 break;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003035 case DRM_FORMAT_XRGB2101010:
3036 plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
3037 break;
3038 case DRM_FORMAT_XBGR2101010:
3039 plane_ctl |= PLANE_CTL_ORDER_RGBX;
3040 plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
3041 break;
3042 default:
3043 BUG();
3044 }
3045
Daniel Vetter30af77c2015-02-10 17:16:11 +00003046 switch (fb->modifier[0]) {
3047 case DRM_FORMAT_MOD_NONE:
Damien Lespiau70d21f02013-07-03 21:06:04 +01003048 break;
Daniel Vetter30af77c2015-02-10 17:16:11 +00003049 case I915_FORMAT_MOD_X_TILED:
Damien Lespiau70d21f02013-07-03 21:06:04 +01003050 plane_ctl |= PLANE_CTL_TILED_X;
Damien Lespiaub3218032015-02-27 11:15:18 +00003051 break;
3052 case I915_FORMAT_MOD_Y_TILED:
3053 plane_ctl |= PLANE_CTL_TILED_Y;
3054 break;
3055 case I915_FORMAT_MOD_Yf_TILED:
3056 plane_ctl |= PLANE_CTL_TILED_YF;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003057 break;
3058 default:
Damien Lespiaub3218032015-02-27 11:15:18 +00003059 MISSING_CASE(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003060 }
3061
3062 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303063
3064 plane = crtc->primary;
3065 rotation = plane->state->rotation;
3066 switch (rotation) {
3067 case BIT(DRM_ROTATE_90):
3068 plane_ctl |= PLANE_CTL_ROTATE_90;
3069 break;
3070
3071 case BIT(DRM_ROTATE_180):
Sonika Jindal1447dde2014-10-04 10:53:31 +01003072 plane_ctl |= PLANE_CTL_ROTATE_180;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303073 break;
3074
3075 case BIT(DRM_ROTATE_270):
3076 plane_ctl |= PLANE_CTL_ROTATE_270;
3077 break;
3078 }
Damien Lespiau70d21f02013-07-03 21:06:04 +01003079
Damien Lespiaub3218032015-02-27 11:15:18 +00003080 obj = intel_fb_obj(fb);
3081 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3082 fb->pixel_format);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303083 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
3084
3085 if (intel_rotation_90_or_270(rotation)) {
3086 /* stride = Surface height in tiles */
3087 tile_height = intel_tile_height(dev, fb->bits_per_pixel,
3088 fb->modifier[0]);
3089 stride = DIV_ROUND_UP(fb->height, tile_height);
3090 x_offset = stride * tile_height - y - (plane->state->src_h >> 16);
3091 y_offset = x;
3092 plane_size = ((plane->state->src_w >> 16) - 1) << 16 |
3093 ((plane->state->src_h >> 16) - 1);
3094 } else {
3095 stride = fb->pitches[0] / stride_div;
3096 x_offset = x;
3097 y_offset = y;
3098 plane_size = ((plane->state->src_h >> 16) - 1) << 16 |
3099 ((plane->state->src_w >> 16) - 1);
3100 }
3101 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003102
Damien Lespiau70d21f02013-07-03 21:06:04 +01003103 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003104 I915_WRITE(PLANE_POS(pipe, 0), 0);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303105 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3106 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3107 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003108 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003109
3110 POSTING_READ(PLANE_SURF(pipe, 0));
3111}
3112
Jesse Barnes17638cd2011-06-24 12:19:23 -07003113/* Assume fb object is pinned & idle & fenced and just update base pointers */
3114static int
3115intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3116 int x, int y, enum mode_set_atomic state)
3117{
3118 struct drm_device *dev = crtc->dev;
3119 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003120
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01003121 if (dev_priv->display.disable_fbc)
3122 dev_priv->display.disable_fbc(dev);
Jesse Barnes81255562010-08-02 12:07:50 -07003123
Daniel Vetter29b9bde2014-04-24 23:55:01 +02003124 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3125
3126 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07003127}
3128
Ville Syrjälä75147472014-11-24 18:28:11 +02003129static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003130{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003131 struct drm_crtc *crtc;
3132
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003133 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003134 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3135 enum plane plane = intel_crtc->plane;
3136
3137 intel_prepare_page_flip(dev, plane);
3138 intel_finish_page_flip_plane(dev, plane);
3139 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003140}
3141
3142static void intel_update_primary_planes(struct drm_device *dev)
3143{
3144 struct drm_i915_private *dev_priv = dev->dev_private;
3145 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003146
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003147 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003148 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3149
Rob Clark51fd3712013-11-19 12:10:12 -05003150 drm_modeset_lock(&crtc->mutex, NULL);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003151 /*
3152 * FIXME: Once we have proper support for primary planes (and
3153 * disabling them without disabling the entire crtc) allow again
Dave Airlie66e514c2014-04-03 07:51:54 +10003154 * a NULL crtc->primary->fb.
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003155 */
Matt Roperf4510a22014-04-01 15:22:40 -07003156 if (intel_crtc->active && crtc->primary->fb)
Matt Roper262ca2b2014-03-18 17:22:55 -07003157 dev_priv->display.update_primary_plane(crtc,
Dave Airlie66e514c2014-04-03 07:51:54 +10003158 crtc->primary->fb,
Matt Roper262ca2b2014-03-18 17:22:55 -07003159 crtc->x,
3160 crtc->y);
Rob Clark51fd3712013-11-19 12:10:12 -05003161 drm_modeset_unlock(&crtc->mutex);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003162 }
3163}
3164
Ville Syrjälä75147472014-11-24 18:28:11 +02003165void intel_prepare_reset(struct drm_device *dev)
3166{
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003167 struct drm_i915_private *dev_priv = to_i915(dev);
3168 struct intel_crtc *crtc;
3169
Ville Syrjälä75147472014-11-24 18:28:11 +02003170 /* no reset support for gen2 */
3171 if (IS_GEN2(dev))
3172 return;
3173
3174 /* reset doesn't touch the display */
3175 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3176 return;
3177
3178 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003179
3180 /*
3181 * Disabling the crtcs gracefully seems nicer. Also the
3182 * g33 docs say we should at least disable all the planes.
3183 */
3184 for_each_intel_crtc(dev, crtc) {
3185 if (crtc->active)
3186 dev_priv->display.crtc_disable(&crtc->base);
3187 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003188}
3189
3190void intel_finish_reset(struct drm_device *dev)
3191{
3192 struct drm_i915_private *dev_priv = to_i915(dev);
3193
3194 /*
3195 * Flips in the rings will be nuked by the reset,
3196 * so complete all pending flips so that user space
3197 * will get its events and not get stuck.
3198 */
3199 intel_complete_page_flips(dev);
3200
3201 /* no reset support for gen2 */
3202 if (IS_GEN2(dev))
3203 return;
3204
3205 /* reset doesn't touch the display */
3206 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3207 /*
3208 * Flips in the rings have been nuked by the reset,
3209 * so update the base address of all primary
3210 * planes to the the last fb to make sure we're
3211 * showing the correct fb after a reset.
3212 */
3213 intel_update_primary_planes(dev);
3214 return;
3215 }
3216
3217 /*
3218 * The display has been reset as well,
3219 * so need a full re-initialization.
3220 */
3221 intel_runtime_pm_disable_interrupts(dev_priv);
3222 intel_runtime_pm_enable_interrupts(dev_priv);
3223
3224 intel_modeset_init_hw(dev);
3225
3226 spin_lock_irq(&dev_priv->irq_lock);
3227 if (dev_priv->display.hpd_irq_setup)
3228 dev_priv->display.hpd_irq_setup(dev);
3229 spin_unlock_irq(&dev_priv->irq_lock);
3230
3231 intel_modeset_setup_hw_state(dev, true);
3232
3233 intel_hpd_init(dev_priv);
3234
3235 drm_modeset_unlock_all(dev);
3236}
3237
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003238static int
Chris Wilson14667a42012-04-03 17:58:35 +01003239intel_finish_fb(struct drm_framebuffer *old_fb)
3240{
Matt Roper2ff8fde2014-07-08 07:50:07 -07003241 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
Chris Wilson14667a42012-04-03 17:58:35 +01003242 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
3243 bool was_interruptible = dev_priv->mm.interruptible;
3244 int ret;
3245
Chris Wilson14667a42012-04-03 17:58:35 +01003246 /* Big Hammer, we also need to ensure that any pending
3247 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3248 * current scanout is retired before unpinning the old
3249 * framebuffer.
3250 *
3251 * This should only fail upon a hung GPU, in which case we
3252 * can safely continue.
3253 */
3254 dev_priv->mm.interruptible = false;
3255 ret = i915_gem_object_finish_gpu(obj);
3256 dev_priv->mm.interruptible = was_interruptible;
3257
3258 return ret;
3259}
3260
Chris Wilson7d5e3792014-03-04 13:15:08 +00003261static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3262{
3263 struct drm_device *dev = crtc->dev;
3264 struct drm_i915_private *dev_priv = dev->dev_private;
3265 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003266 bool pending;
3267
3268 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3269 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3270 return false;
3271
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003272 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003273 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003274 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003275
3276 return pending;
3277}
3278
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003279static void intel_update_pipe_size(struct intel_crtc *crtc)
3280{
3281 struct drm_device *dev = crtc->base.dev;
3282 struct drm_i915_private *dev_priv = dev->dev_private;
3283 const struct drm_display_mode *adjusted_mode;
3284
3285 if (!i915.fastboot)
3286 return;
3287
3288 /*
3289 * Update pipe size and adjust fitter if needed: the reason for this is
3290 * that in compute_mode_changes we check the native mode (not the pfit
3291 * mode) to see if we can flip rather than do a full mode set. In the
3292 * fastboot case, we'll flip, but if we don't update the pipesrc and
3293 * pfit state, we'll end up with a big fb scanned out into the wrong
3294 * sized surface.
3295 *
3296 * To fix this properly, we need to hoist the checks up into
3297 * compute_mode_changes (or above), check the actual pfit state and
3298 * whether the platform allows pfit disable with pipe active, and only
3299 * then update the pipesrc and pfit state, even on the flip path.
3300 */
3301
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003302 adjusted_mode = &crtc->config->base.adjusted_mode;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003303
3304 I915_WRITE(PIPESRC(crtc->pipe),
3305 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3306 (adjusted_mode->crtc_vdisplay - 1));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003307 if (!crtc->config->pch_pfit.enabled &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03003308 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3309 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003310 I915_WRITE(PF_CTL(crtc->pipe), 0);
3311 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3312 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3313 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003314 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3315 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003316}
3317
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003318static void intel_fdi_normal_train(struct drm_crtc *crtc)
3319{
3320 struct drm_device *dev = crtc->dev;
3321 struct drm_i915_private *dev_priv = dev->dev_private;
3322 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3323 int pipe = intel_crtc->pipe;
3324 u32 reg, temp;
3325
3326 /* enable normal train */
3327 reg = FDI_TX_CTL(pipe);
3328 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003329 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003330 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3331 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003332 } else {
3333 temp &= ~FDI_LINK_TRAIN_NONE;
3334 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003335 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003336 I915_WRITE(reg, temp);
3337
3338 reg = FDI_RX_CTL(pipe);
3339 temp = I915_READ(reg);
3340 if (HAS_PCH_CPT(dev)) {
3341 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3342 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3343 } else {
3344 temp &= ~FDI_LINK_TRAIN_NONE;
3345 temp |= FDI_LINK_TRAIN_NONE;
3346 }
3347 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3348
3349 /* wait one idle pattern time */
3350 POSTING_READ(reg);
3351 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003352
3353 /* IVB wants error correction enabled */
3354 if (IS_IVYBRIDGE(dev))
3355 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3356 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003357}
3358
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003359/* The FDI link training functions for ILK/Ibexpeak. */
3360static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3361{
3362 struct drm_device *dev = crtc->dev;
3363 struct drm_i915_private *dev_priv = dev->dev_private;
3364 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3365 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003366 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003367
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003368 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003369 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003370
Adam Jacksone1a44742010-06-25 15:32:14 -04003371 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3372 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003373 reg = FDI_RX_IMR(pipe);
3374 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003375 temp &= ~FDI_RX_SYMBOL_LOCK;
3376 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003377 I915_WRITE(reg, temp);
3378 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003379 udelay(150);
3380
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003381 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003382 reg = FDI_TX_CTL(pipe);
3383 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003384 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003385 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003386 temp &= ~FDI_LINK_TRAIN_NONE;
3387 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003388 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003389
Chris Wilson5eddb702010-09-11 13:48:45 +01003390 reg = FDI_RX_CTL(pipe);
3391 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003392 temp &= ~FDI_LINK_TRAIN_NONE;
3393 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003394 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3395
3396 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003397 udelay(150);
3398
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003399 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003400 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3401 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3402 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003403
Chris Wilson5eddb702010-09-11 13:48:45 +01003404 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003405 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003406 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003407 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3408
3409 if ((temp & FDI_RX_BIT_LOCK)) {
3410 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003411 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003412 break;
3413 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003414 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003415 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003416 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003417
3418 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003419 reg = FDI_TX_CTL(pipe);
3420 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003421 temp &= ~FDI_LINK_TRAIN_NONE;
3422 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003423 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003424
Chris Wilson5eddb702010-09-11 13:48:45 +01003425 reg = FDI_RX_CTL(pipe);
3426 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003427 temp &= ~FDI_LINK_TRAIN_NONE;
3428 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003429 I915_WRITE(reg, temp);
3430
3431 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003432 udelay(150);
3433
Chris Wilson5eddb702010-09-11 13:48:45 +01003434 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003435 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003436 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003437 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3438
3439 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003440 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003441 DRM_DEBUG_KMS("FDI train 2 done.\n");
3442 break;
3443 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003444 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003445 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003446 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003447
3448 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003449
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003450}
3451
Akshay Joshi0206e352011-08-16 15:34:10 -04003452static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003453 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3454 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3455 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3456 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3457};
3458
3459/* The FDI link training functions for SNB/Cougarpoint. */
3460static void gen6_fdi_link_train(struct drm_crtc *crtc)
3461{
3462 struct drm_device *dev = crtc->dev;
3463 struct drm_i915_private *dev_priv = dev->dev_private;
3464 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3465 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05003466 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003467
Adam Jacksone1a44742010-06-25 15:32:14 -04003468 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3469 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003470 reg = FDI_RX_IMR(pipe);
3471 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003472 temp &= ~FDI_RX_SYMBOL_LOCK;
3473 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003474 I915_WRITE(reg, temp);
3475
3476 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003477 udelay(150);
3478
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003479 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003480 reg = FDI_TX_CTL(pipe);
3481 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003482 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003483 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003484 temp &= ~FDI_LINK_TRAIN_NONE;
3485 temp |= FDI_LINK_TRAIN_PATTERN_1;
3486 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3487 /* SNB-B */
3488 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003489 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003490
Daniel Vetterd74cf322012-10-26 10:58:13 +02003491 I915_WRITE(FDI_RX_MISC(pipe),
3492 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3493
Chris Wilson5eddb702010-09-11 13:48:45 +01003494 reg = FDI_RX_CTL(pipe);
3495 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003496 if (HAS_PCH_CPT(dev)) {
3497 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3498 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3499 } else {
3500 temp &= ~FDI_LINK_TRAIN_NONE;
3501 temp |= FDI_LINK_TRAIN_PATTERN_1;
3502 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003503 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3504
3505 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003506 udelay(150);
3507
Akshay Joshi0206e352011-08-16 15:34:10 -04003508 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003509 reg = FDI_TX_CTL(pipe);
3510 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003511 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3512 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003513 I915_WRITE(reg, temp);
3514
3515 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003516 udelay(500);
3517
Sean Paulfa37d392012-03-02 12:53:39 -05003518 for (retry = 0; retry < 5; retry++) {
3519 reg = FDI_RX_IIR(pipe);
3520 temp = I915_READ(reg);
3521 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3522 if (temp & FDI_RX_BIT_LOCK) {
3523 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3524 DRM_DEBUG_KMS("FDI train 1 done.\n");
3525 break;
3526 }
3527 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003528 }
Sean Paulfa37d392012-03-02 12:53:39 -05003529 if (retry < 5)
3530 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003531 }
3532 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003533 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003534
3535 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003536 reg = FDI_TX_CTL(pipe);
3537 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003538 temp &= ~FDI_LINK_TRAIN_NONE;
3539 temp |= FDI_LINK_TRAIN_PATTERN_2;
3540 if (IS_GEN6(dev)) {
3541 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3542 /* SNB-B */
3543 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3544 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003545 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003546
Chris Wilson5eddb702010-09-11 13:48:45 +01003547 reg = FDI_RX_CTL(pipe);
3548 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003549 if (HAS_PCH_CPT(dev)) {
3550 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3551 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3552 } else {
3553 temp &= ~FDI_LINK_TRAIN_NONE;
3554 temp |= FDI_LINK_TRAIN_PATTERN_2;
3555 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003556 I915_WRITE(reg, temp);
3557
3558 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003559 udelay(150);
3560
Akshay Joshi0206e352011-08-16 15:34:10 -04003561 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003562 reg = FDI_TX_CTL(pipe);
3563 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003564 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3565 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003566 I915_WRITE(reg, temp);
3567
3568 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003569 udelay(500);
3570
Sean Paulfa37d392012-03-02 12:53:39 -05003571 for (retry = 0; retry < 5; retry++) {
3572 reg = FDI_RX_IIR(pipe);
3573 temp = I915_READ(reg);
3574 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3575 if (temp & FDI_RX_SYMBOL_LOCK) {
3576 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3577 DRM_DEBUG_KMS("FDI train 2 done.\n");
3578 break;
3579 }
3580 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003581 }
Sean Paulfa37d392012-03-02 12:53:39 -05003582 if (retry < 5)
3583 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003584 }
3585 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003586 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003587
3588 DRM_DEBUG_KMS("FDI train done.\n");
3589}
3590
Jesse Barnes357555c2011-04-28 15:09:55 -07003591/* Manual link training for Ivy Bridge A0 parts */
3592static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3593{
3594 struct drm_device *dev = crtc->dev;
3595 struct drm_i915_private *dev_priv = dev->dev_private;
3596 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3597 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003598 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003599
3600 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3601 for train result */
3602 reg = FDI_RX_IMR(pipe);
3603 temp = I915_READ(reg);
3604 temp &= ~FDI_RX_SYMBOL_LOCK;
3605 temp &= ~FDI_RX_BIT_LOCK;
3606 I915_WRITE(reg, temp);
3607
3608 POSTING_READ(reg);
3609 udelay(150);
3610
Daniel Vetter01a415f2012-10-27 15:58:40 +02003611 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3612 I915_READ(FDI_RX_IIR(pipe)));
3613
Jesse Barnes139ccd32013-08-19 11:04:55 -07003614 /* Try each vswing and preemphasis setting twice before moving on */
3615 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3616 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003617 reg = FDI_TX_CTL(pipe);
3618 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003619 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3620 temp &= ~FDI_TX_ENABLE;
3621 I915_WRITE(reg, temp);
3622
3623 reg = FDI_RX_CTL(pipe);
3624 temp = I915_READ(reg);
3625 temp &= ~FDI_LINK_TRAIN_AUTO;
3626 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3627 temp &= ~FDI_RX_ENABLE;
3628 I915_WRITE(reg, temp);
3629
3630 /* enable CPU FDI TX and PCH FDI RX */
3631 reg = FDI_TX_CTL(pipe);
3632 temp = I915_READ(reg);
3633 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003634 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003635 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003636 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003637 temp |= snb_b_fdi_train_param[j/2];
3638 temp |= FDI_COMPOSITE_SYNC;
3639 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3640
3641 I915_WRITE(FDI_RX_MISC(pipe),
3642 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3643
3644 reg = FDI_RX_CTL(pipe);
3645 temp = I915_READ(reg);
3646 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3647 temp |= FDI_COMPOSITE_SYNC;
3648 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3649
3650 POSTING_READ(reg);
3651 udelay(1); /* should be 0.5us */
3652
3653 for (i = 0; i < 4; i++) {
3654 reg = FDI_RX_IIR(pipe);
3655 temp = I915_READ(reg);
3656 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3657
3658 if (temp & FDI_RX_BIT_LOCK ||
3659 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3660 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3661 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3662 i);
3663 break;
3664 }
3665 udelay(1); /* should be 0.5us */
3666 }
3667 if (i == 4) {
3668 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3669 continue;
3670 }
3671
3672 /* Train 2 */
3673 reg = FDI_TX_CTL(pipe);
3674 temp = I915_READ(reg);
3675 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3676 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3677 I915_WRITE(reg, temp);
3678
3679 reg = FDI_RX_CTL(pipe);
3680 temp = I915_READ(reg);
3681 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3682 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003683 I915_WRITE(reg, temp);
3684
3685 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003686 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003687
Jesse Barnes139ccd32013-08-19 11:04:55 -07003688 for (i = 0; i < 4; i++) {
3689 reg = FDI_RX_IIR(pipe);
3690 temp = I915_READ(reg);
3691 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003692
Jesse Barnes139ccd32013-08-19 11:04:55 -07003693 if (temp & FDI_RX_SYMBOL_LOCK ||
3694 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3695 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3696 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3697 i);
3698 goto train_done;
3699 }
3700 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003701 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003702 if (i == 4)
3703 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003704 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003705
Jesse Barnes139ccd32013-08-19 11:04:55 -07003706train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003707 DRM_DEBUG_KMS("FDI train done.\n");
3708}
3709
Daniel Vetter88cefb62012-08-12 19:27:14 +02003710static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003711{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003712 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003713 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003714 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003715 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003716
Jesse Barnesc64e3112010-09-10 11:27:03 -07003717
Jesse Barnes0e23b992010-09-10 11:10:00 -07003718 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003719 reg = FDI_RX_CTL(pipe);
3720 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003721 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003722 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003723 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003724 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3725
3726 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003727 udelay(200);
3728
3729 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003730 temp = I915_READ(reg);
3731 I915_WRITE(reg, temp | FDI_PCDCLK);
3732
3733 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003734 udelay(200);
3735
Paulo Zanoni20749732012-11-23 15:30:38 -02003736 /* Enable CPU FDI TX PLL, always on for Ironlake */
3737 reg = FDI_TX_CTL(pipe);
3738 temp = I915_READ(reg);
3739 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3740 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003741
Paulo Zanoni20749732012-11-23 15:30:38 -02003742 POSTING_READ(reg);
3743 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003744 }
3745}
3746
Daniel Vetter88cefb62012-08-12 19:27:14 +02003747static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3748{
3749 struct drm_device *dev = intel_crtc->base.dev;
3750 struct drm_i915_private *dev_priv = dev->dev_private;
3751 int pipe = intel_crtc->pipe;
3752 u32 reg, temp;
3753
3754 /* Switch from PCDclk to Rawclk */
3755 reg = FDI_RX_CTL(pipe);
3756 temp = I915_READ(reg);
3757 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3758
3759 /* Disable CPU FDI TX PLL */
3760 reg = FDI_TX_CTL(pipe);
3761 temp = I915_READ(reg);
3762 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3763
3764 POSTING_READ(reg);
3765 udelay(100);
3766
3767 reg = FDI_RX_CTL(pipe);
3768 temp = I915_READ(reg);
3769 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3770
3771 /* Wait for the clocks to turn off. */
3772 POSTING_READ(reg);
3773 udelay(100);
3774}
3775
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003776static void ironlake_fdi_disable(struct drm_crtc *crtc)
3777{
3778 struct drm_device *dev = crtc->dev;
3779 struct drm_i915_private *dev_priv = dev->dev_private;
3780 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3781 int pipe = intel_crtc->pipe;
3782 u32 reg, temp;
3783
3784 /* disable CPU FDI tx and PCH FDI rx */
3785 reg = FDI_TX_CTL(pipe);
3786 temp = I915_READ(reg);
3787 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3788 POSTING_READ(reg);
3789
3790 reg = FDI_RX_CTL(pipe);
3791 temp = I915_READ(reg);
3792 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003793 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003794 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3795
3796 POSTING_READ(reg);
3797 udelay(100);
3798
3799 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003800 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003801 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003802
3803 /* still set train pattern 1 */
3804 reg = FDI_TX_CTL(pipe);
3805 temp = I915_READ(reg);
3806 temp &= ~FDI_LINK_TRAIN_NONE;
3807 temp |= FDI_LINK_TRAIN_PATTERN_1;
3808 I915_WRITE(reg, temp);
3809
3810 reg = FDI_RX_CTL(pipe);
3811 temp = I915_READ(reg);
3812 if (HAS_PCH_CPT(dev)) {
3813 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3814 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3815 } else {
3816 temp &= ~FDI_LINK_TRAIN_NONE;
3817 temp |= FDI_LINK_TRAIN_PATTERN_1;
3818 }
3819 /* BPC in FDI rx is consistent with that in PIPECONF */
3820 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003821 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003822 I915_WRITE(reg, temp);
3823
3824 POSTING_READ(reg);
3825 udelay(100);
3826}
3827
Chris Wilson5dce5b932014-01-20 10:17:36 +00003828bool intel_has_pending_fb_unpin(struct drm_device *dev)
3829{
3830 struct intel_crtc *crtc;
3831
3832 /* Note that we don't need to be called with mode_config.lock here
3833 * as our list of CRTC objects is static for the lifetime of the
3834 * device and so cannot disappear as we iterate. Similarly, we can
3835 * happily treat the predicates as racy, atomic checks as userspace
3836 * cannot claim and pin a new fb without at least acquring the
3837 * struct_mutex and so serialising with us.
3838 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003839 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003840 if (atomic_read(&crtc->unpin_work_count) == 0)
3841 continue;
3842
3843 if (crtc->unpin_work)
3844 intel_wait_for_vblank(dev, crtc->pipe);
3845
3846 return true;
3847 }
3848
3849 return false;
3850}
3851
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003852static void page_flip_completed(struct intel_crtc *intel_crtc)
3853{
3854 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3855 struct intel_unpin_work *work = intel_crtc->unpin_work;
3856
3857 /* ensure that the unpin work is consistent wrt ->pending. */
3858 smp_rmb();
3859 intel_crtc->unpin_work = NULL;
3860
3861 if (work->event)
3862 drm_send_vblank_event(intel_crtc->base.dev,
3863 intel_crtc->pipe,
3864 work->event);
3865
3866 drm_crtc_vblank_put(&intel_crtc->base);
3867
3868 wake_up_all(&dev_priv->pending_flip_queue);
3869 queue_work(dev_priv->wq, &work->work);
3870
3871 trace_i915_flip_complete(intel_crtc->plane,
3872 work->pending_flip_obj);
3873}
3874
Ville Syrjälä46a55d32014-05-21 14:04:46 +03003875void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003876{
Chris Wilson0f911282012-04-17 10:05:38 +01003877 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003878 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003879
Daniel Vetter2c10d572012-12-20 21:24:07 +01003880 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Chris Wilson9c787942014-09-05 07:13:25 +01003881 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3882 !intel_crtc_has_pending_flip(crtc),
3883 60*HZ) == 0)) {
3884 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003885
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003886 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003887 if (intel_crtc->unpin_work) {
3888 WARN_ONCE(1, "Removing stuck page flip\n");
3889 page_flip_completed(intel_crtc);
3890 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003891 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003892 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003893
Chris Wilson975d5682014-08-20 13:13:34 +01003894 if (crtc->primary->fb) {
3895 mutex_lock(&dev->struct_mutex);
3896 intel_finish_fb(crtc->primary->fb);
3897 mutex_unlock(&dev->struct_mutex);
3898 }
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003899}
3900
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003901/* Program iCLKIP clock to the desired frequency */
3902static void lpt_program_iclkip(struct drm_crtc *crtc)
3903{
3904 struct drm_device *dev = crtc->dev;
3905 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003906 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003907 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3908 u32 temp;
3909
Daniel Vetter09153002012-12-12 14:06:44 +01003910 mutex_lock(&dev_priv->dpio_lock);
3911
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003912 /* It is necessary to ungate the pixclk gate prior to programming
3913 * the divisors, and gate it back when it is done.
3914 */
3915 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3916
3917 /* Disable SSCCTL */
3918 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003919 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3920 SBI_SSCCTL_DISABLE,
3921 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003922
3923 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003924 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003925 auxdiv = 1;
3926 divsel = 0x41;
3927 phaseinc = 0x20;
3928 } else {
3929 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003930 * but the adjusted_mode->crtc_clock in in KHz. To get the
3931 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003932 * convert the virtual clock precision to KHz here for higher
3933 * precision.
3934 */
3935 u32 iclk_virtual_root_freq = 172800 * 1000;
3936 u32 iclk_pi_range = 64;
3937 u32 desired_divisor, msb_divisor_value, pi_value;
3938
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003939 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003940 msb_divisor_value = desired_divisor / iclk_pi_range;
3941 pi_value = desired_divisor % iclk_pi_range;
3942
3943 auxdiv = 0;
3944 divsel = msb_divisor_value - 2;
3945 phaseinc = pi_value;
3946 }
3947
3948 /* This should not happen with any sane values */
3949 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3950 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3951 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3952 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3953
3954 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 +03003955 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003956 auxdiv,
3957 divsel,
3958 phasedir,
3959 phaseinc);
3960
3961 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003962 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003963 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3964 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3965 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3966 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3967 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3968 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003969 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003970
3971 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003972 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003973 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3974 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003975 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003976
3977 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003978 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003979 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003980 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003981
3982 /* Wait for initialization time */
3983 udelay(24);
3984
3985 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01003986
3987 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003988}
3989
Daniel Vetter275f01b22013-05-03 11:49:47 +02003990static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3991 enum pipe pch_transcoder)
3992{
3993 struct drm_device *dev = crtc->base.dev;
3994 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003995 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02003996
3997 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3998 I915_READ(HTOTAL(cpu_transcoder)));
3999 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4000 I915_READ(HBLANK(cpu_transcoder)));
4001 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4002 I915_READ(HSYNC(cpu_transcoder)));
4003
4004 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4005 I915_READ(VTOTAL(cpu_transcoder)));
4006 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4007 I915_READ(VBLANK(cpu_transcoder)));
4008 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4009 I915_READ(VSYNC(cpu_transcoder)));
4010 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4011 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4012}
4013
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004014static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004015{
4016 struct drm_i915_private *dev_priv = dev->dev_private;
4017 uint32_t temp;
4018
4019 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004020 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004021 return;
4022
4023 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4024 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4025
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004026 temp &= ~FDI_BC_BIFURCATION_SELECT;
4027 if (enable)
4028 temp |= FDI_BC_BIFURCATION_SELECT;
4029
4030 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004031 I915_WRITE(SOUTH_CHICKEN1, temp);
4032 POSTING_READ(SOUTH_CHICKEN1);
4033}
4034
4035static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4036{
4037 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004038
4039 switch (intel_crtc->pipe) {
4040 case PIPE_A:
4041 break;
4042 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004043 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004044 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004045 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004046 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004047
4048 break;
4049 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004050 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004051
4052 break;
4053 default:
4054 BUG();
4055 }
4056}
4057
Jesse Barnesf67a5592011-01-05 10:31:48 -08004058/*
4059 * Enable PCH resources required for PCH ports:
4060 * - PCH PLLs
4061 * - FDI training & RX/TX
4062 * - update transcoder timings
4063 * - DP transcoding bits
4064 * - transcoder
4065 */
4066static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004067{
4068 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004069 struct drm_i915_private *dev_priv = dev->dev_private;
4070 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4071 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004072 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004073
Daniel Vetterab9412b2013-05-03 11:49:46 +02004074 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004075
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004076 if (IS_IVYBRIDGE(dev))
4077 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4078
Daniel Vettercd986ab2012-10-26 10:58:12 +02004079 /* Write the TU size bits before fdi link training, so that error
4080 * detection works. */
4081 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4082 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4083
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004084 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004085 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004086
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004087 /* We need to program the right clock selection before writing the pixel
4088 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004089 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004090 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004091
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004092 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004093 temp |= TRANS_DPLL_ENABLE(pipe);
4094 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004095 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004096 temp |= sel;
4097 else
4098 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004099 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004100 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004101
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004102 /* XXX: pch pll's can be enabled any time before we enable the PCH
4103 * transcoder, and we actually should do this to not upset any PCH
4104 * transcoder that already use the clock when we share it.
4105 *
4106 * Note that enable_shared_dpll tries to do the right thing, but
4107 * get_shared_dpll unconditionally resets the pll - we need that to have
4108 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004109 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004110
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004111 /* set transcoder timing, panel must allow it */
4112 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004113 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004114
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004115 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004116
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004117 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004118 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004119 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01004120 reg = TRANS_DP_CTL(pipe);
4121 temp = I915_READ(reg);
4122 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004123 TRANS_DP_SYNC_MASK |
4124 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01004125 temp |= (TRANS_DP_OUTPUT_ENABLE |
4126 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004127 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004128
4129 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004130 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004131 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004132 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004133
4134 switch (intel_trans_dp_port_sel(crtc)) {
4135 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004136 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004137 break;
4138 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004139 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004140 break;
4141 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004142 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004143 break;
4144 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004145 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004146 }
4147
Chris Wilson5eddb702010-09-11 13:48:45 +01004148 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004149 }
4150
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004151 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004152}
4153
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004154static void lpt_pch_enable(struct drm_crtc *crtc)
4155{
4156 struct drm_device *dev = crtc->dev;
4157 struct drm_i915_private *dev_priv = dev->dev_private;
4158 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004159 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004160
Daniel Vetterab9412b2013-05-03 11:49:46 +02004161 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004162
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004163 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004164
Paulo Zanoni0540e482012-10-31 18:12:40 -02004165 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004166 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004167
Paulo Zanoni937bb612012-10-31 18:12:47 -02004168 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004169}
4170
Daniel Vetter716c2e52014-06-25 22:02:02 +03004171void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004172{
Daniel Vettere2b78262013-06-07 23:10:03 +02004173 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004174
4175 if (pll == NULL)
4176 return;
4177
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02004178 if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004179 WARN(1, "bad %s crtc mask\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004180 return;
4181 }
4182
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02004183 pll->config.crtc_mask &= ~(1 << crtc->pipe);
4184 if (pll->config.crtc_mask == 0) {
Daniel Vetterf4a091c2013-06-10 17:28:22 +02004185 WARN_ON(pll->on);
4186 WARN_ON(pll->active);
4187 }
4188
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004189 crtc->config->shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004190}
4191
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004192struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4193 struct intel_crtc_state *crtc_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004194{
Daniel Vettere2b78262013-06-07 23:10:03 +02004195 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004196 struct intel_shared_dpll *pll;
Daniel Vettere2b78262013-06-07 23:10:03 +02004197 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004198
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004199 if (HAS_PCH_IBX(dev_priv->dev)) {
4200 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02004201 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004202 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004203
Daniel Vetter46edb022013-06-05 13:34:12 +02004204 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4205 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004206
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004207 WARN_ON(pll->new_config->crtc_mask);
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004208
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004209 goto found;
4210 }
4211
Satheeshakrishna Mbcddf612014-08-22 09:49:10 +05304212 if (IS_BROXTON(dev_priv->dev)) {
4213 /* PLL is attached to port in bxt */
4214 struct intel_encoder *encoder;
4215 struct intel_digital_port *intel_dig_port;
4216
4217 encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
4218 if (WARN_ON(!encoder))
4219 return NULL;
4220
4221 intel_dig_port = enc_to_dig_port(&encoder->base);
4222 /* 1:1 mapping between ports and PLLs */
4223 i = (enum intel_dpll_id)intel_dig_port->port;
4224 pll = &dev_priv->shared_dplls[i];
4225 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4226 crtc->base.base.id, pll->name);
4227 WARN_ON(pll->new_config->crtc_mask);
4228
4229 goto found;
4230 }
4231
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004232 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4233 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004234
4235 /* Only want to check enabled timings first */
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004236 if (pll->new_config->crtc_mask == 0)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004237 continue;
4238
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004239 if (memcmp(&crtc_state->dpll_hw_state,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004240 &pll->new_config->hw_state,
4241 sizeof(pll->new_config->hw_state)) == 0) {
4242 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004243 crtc->base.base.id, pll->name,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004244 pll->new_config->crtc_mask,
4245 pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004246 goto found;
4247 }
4248 }
4249
4250 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004251 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4252 pll = &dev_priv->shared_dplls[i];
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004253 if (pll->new_config->crtc_mask == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02004254 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4255 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004256 goto found;
4257 }
4258 }
4259
4260 return NULL;
4261
4262found:
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004263 if (pll->new_config->crtc_mask == 0)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004264 pll->new_config->hw_state = crtc_state->dpll_hw_state;
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004265
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004266 crtc_state->shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02004267 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4268 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02004269
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004270 pll->new_config->crtc_mask |= 1 << crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004271
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004272 return pll;
4273}
4274
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004275/**
4276 * intel_shared_dpll_start_config - start a new PLL staged config
4277 * @dev_priv: DRM device
4278 * @clear_pipes: mask of pipes that will have their PLLs freed
4279 *
4280 * Starts a new PLL staged config, copying the current config but
4281 * releasing the references of pipes specified in clear_pipes.
4282 */
4283static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
4284 unsigned clear_pipes)
4285{
4286 struct intel_shared_dpll *pll;
4287 enum intel_dpll_id i;
4288
4289 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4290 pll = &dev_priv->shared_dplls[i];
4291
4292 pll->new_config = kmemdup(&pll->config, sizeof pll->config,
4293 GFP_KERNEL);
4294 if (!pll->new_config)
4295 goto cleanup;
4296
4297 pll->new_config->crtc_mask &= ~clear_pipes;
4298 }
4299
4300 return 0;
4301
4302cleanup:
4303 while (--i >= 0) {
4304 pll = &dev_priv->shared_dplls[i];
Ander Conselvan de Oliveiraf354d732014-11-07 14:07:41 +02004305 kfree(pll->new_config);
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004306 pll->new_config = NULL;
4307 }
4308
4309 return -ENOMEM;
4310}
4311
4312static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
4313{
4314 struct intel_shared_dpll *pll;
4315 enum intel_dpll_id i;
4316
4317 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4318 pll = &dev_priv->shared_dplls[i];
4319
4320 WARN_ON(pll->new_config == &pll->config);
4321
4322 pll->config = *pll->new_config;
4323 kfree(pll->new_config);
4324 pll->new_config = NULL;
4325 }
4326}
4327
4328static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
4329{
4330 struct intel_shared_dpll *pll;
4331 enum intel_dpll_id i;
4332
4333 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4334 pll = &dev_priv->shared_dplls[i];
4335
4336 WARN_ON(pll->new_config == &pll->config);
4337
4338 kfree(pll->new_config);
4339 pll->new_config = NULL;
4340 }
4341}
4342
Daniel Vettera1520312013-05-03 11:49:50 +02004343static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004344{
4345 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01004346 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004347 u32 temp;
4348
4349 temp = I915_READ(dslreg);
4350 udelay(500);
4351 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004352 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004353 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004354 }
4355}
4356
Chandra Kondurua1b22782015-04-07 15:28:45 -07004357/**
4358 * skl_update_scaler_users - Stages update to crtc's scaler state
4359 * @intel_crtc: crtc
4360 * @crtc_state: crtc_state
4361 * @plane: plane (NULL indicates crtc is requesting update)
4362 * @plane_state: plane's state
4363 * @force_detach: request unconditional detachment of scaler
4364 *
4365 * This function updates scaler state for requested plane or crtc.
4366 * To request scaler usage update for a plane, caller shall pass plane pointer.
4367 * To request scaler usage update for crtc, caller shall pass plane pointer
4368 * as NULL.
4369 *
4370 * Return
4371 * 0 - scaler_usage updated successfully
4372 * error - requested scaling cannot be supported or other error condition
4373 */
4374int
4375skl_update_scaler_users(
4376 struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state,
4377 struct intel_plane *intel_plane, struct intel_plane_state *plane_state,
4378 int force_detach)
4379{
4380 int need_scaling;
4381 int idx;
4382 int src_w, src_h, dst_w, dst_h;
4383 int *scaler_id;
4384 struct drm_framebuffer *fb;
4385 struct intel_crtc_scaler_state *scaler_state;
4386
4387 if (!intel_crtc || !crtc_state)
4388 return 0;
4389
4390 scaler_state = &crtc_state->scaler_state;
4391
4392 idx = intel_plane ? drm_plane_index(&intel_plane->base) : SKL_CRTC_INDEX;
4393 fb = intel_plane ? plane_state->base.fb : NULL;
4394
4395 if (intel_plane) {
4396 src_w = drm_rect_width(&plane_state->src) >> 16;
4397 src_h = drm_rect_height(&plane_state->src) >> 16;
4398 dst_w = drm_rect_width(&plane_state->dst);
4399 dst_h = drm_rect_height(&plane_state->dst);
4400 scaler_id = &plane_state->scaler_id;
4401 } else {
4402 struct drm_display_mode *adjusted_mode =
4403 &crtc_state->base.adjusted_mode;
4404 src_w = crtc_state->pipe_src_w;
4405 src_h = crtc_state->pipe_src_h;
4406 dst_w = adjusted_mode->hdisplay;
4407 dst_h = adjusted_mode->vdisplay;
4408 scaler_id = &scaler_state->scaler_id;
4409 }
4410 need_scaling = (src_w != dst_w || src_h != dst_h);
4411
4412 /*
4413 * if plane is being disabled or scaler is no more required or force detach
4414 * - free scaler binded to this plane/crtc
4415 * - in order to do this, update crtc->scaler_usage
4416 *
4417 * Here scaler state in crtc_state is set free so that
4418 * scaler can be assigned to other user. Actual register
4419 * update to free the scaler is done in plane/panel-fit programming.
4420 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4421 */
4422 if (force_detach || !need_scaling || (intel_plane &&
4423 (!fb || !plane_state->visible))) {
4424 if (*scaler_id >= 0) {
4425 scaler_state->scaler_users &= ~(1 << idx);
4426 scaler_state->scalers[*scaler_id].in_use = 0;
4427
4428 DRM_DEBUG_KMS("Staged freeing scaler id %d.%d from %s:%d "
4429 "crtc_state = %p scaler_users = 0x%x\n",
4430 intel_crtc->pipe, *scaler_id, intel_plane ? "PLANE" : "CRTC",
4431 intel_plane ? intel_plane->base.base.id :
4432 intel_crtc->base.base.id, crtc_state,
4433 scaler_state->scaler_users);
4434 *scaler_id = -1;
4435 }
4436 return 0;
4437 }
4438
4439 /* range checks */
4440 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4441 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4442
4443 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4444 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
4445 DRM_DEBUG_KMS("%s:%d scaler_user index %u.%u: src %ux%u dst %ux%u "
4446 "size is out of scaler range\n",
4447 intel_plane ? "PLANE" : "CRTC",
4448 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4449 intel_crtc->pipe, idx, src_w, src_h, dst_w, dst_h);
4450 return -EINVAL;
4451 }
4452
4453 /* check colorkey */
4454 if (intel_plane && intel_plane->ckey.flags != I915_SET_COLORKEY_NONE) {
4455 DRM_DEBUG_KMS("PLANE:%d scaling with color key not allowed",
4456 intel_plane->base.base.id);
4457 return -EINVAL;
4458 }
4459
4460 /* Check src format */
4461 if (intel_plane) {
4462 switch (fb->pixel_format) {
4463 case DRM_FORMAT_RGB565:
4464 case DRM_FORMAT_XBGR8888:
4465 case DRM_FORMAT_XRGB8888:
4466 case DRM_FORMAT_ABGR8888:
4467 case DRM_FORMAT_ARGB8888:
4468 case DRM_FORMAT_XRGB2101010:
4469 case DRM_FORMAT_ARGB2101010:
4470 case DRM_FORMAT_XBGR2101010:
4471 case DRM_FORMAT_ABGR2101010:
4472 case DRM_FORMAT_YUYV:
4473 case DRM_FORMAT_YVYU:
4474 case DRM_FORMAT_UYVY:
4475 case DRM_FORMAT_VYUY:
4476 break;
4477 default:
4478 DRM_DEBUG_KMS("PLANE:%d FB:%d unsupported scaling format 0x%x\n",
4479 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4480 return -EINVAL;
4481 }
4482 }
4483
4484 /* mark this plane as a scaler user in crtc_state */
4485 scaler_state->scaler_users |= (1 << idx);
4486 DRM_DEBUG_KMS("%s:%d staged scaling request for %ux%u->%ux%u "
4487 "crtc_state = %p scaler_users = 0x%x\n",
4488 intel_plane ? "PLANE" : "CRTC",
4489 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4490 src_w, src_h, dst_w, dst_h, crtc_state, scaler_state->scaler_users);
4491 return 0;
4492}
4493
4494static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004495{
4496 struct drm_device *dev = crtc->base.dev;
4497 struct drm_i915_private *dev_priv = dev->dev_private;
4498 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004499 struct intel_crtc_scaler_state *scaler_state =
4500 &crtc->config->scaler_state;
4501
4502 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4503
4504 /* To update pfit, first update scaler state */
4505 skl_update_scaler_users(crtc, crtc->config, NULL, NULL, !enable);
4506 intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
4507 skl_detach_scalers(crtc);
4508 if (!enable)
4509 return;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004510
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004511 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004512 int id;
4513
4514 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4515 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4516 return;
4517 }
4518
4519 id = scaler_state->scaler_id;
4520 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4521 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4522 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4523 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4524
4525 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004526 }
4527}
4528
Jesse Barnesb074cec2013-04-25 12:55:02 -07004529static void ironlake_pfit_enable(struct intel_crtc *crtc)
4530{
4531 struct drm_device *dev = crtc->base.dev;
4532 struct drm_i915_private *dev_priv = dev->dev_private;
4533 int pipe = crtc->pipe;
4534
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004535 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004536 /* Force use of hard-coded filter coefficients
4537 * as some pre-programmed values are broken,
4538 * e.g. x201.
4539 */
4540 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4541 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4542 PF_PIPE_SEL_IVB(pipe));
4543 else
4544 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004545 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4546 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004547 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004548}
4549
Matt Roper4a3b8762014-12-23 10:41:51 -08004550static void intel_enable_sprite_planes(struct drm_crtc *crtc)
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004551{
4552 struct drm_device *dev = crtc->dev;
4553 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07004554 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004555 struct intel_plane *intel_plane;
4556
Matt Roperaf2b6532014-04-01 15:22:32 -07004557 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4558 intel_plane = to_intel_plane(plane);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004559 if (intel_plane->pipe == pipe)
4560 intel_plane_restore(&intel_plane->base);
Matt Roperaf2b6532014-04-01 15:22:32 -07004561 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004562}
4563
Matt Roper0d703d42015-03-04 10:49:04 -08004564/*
4565 * Disable a plane internally without actually modifying the plane's state.
4566 * This will allow us to easily restore the plane later by just reprogramming
4567 * its state.
4568 */
4569static void disable_plane_internal(struct drm_plane *plane)
4570{
4571 struct intel_plane *intel_plane = to_intel_plane(plane);
4572 struct drm_plane_state *state =
4573 plane->funcs->atomic_duplicate_state(plane);
4574 struct intel_plane_state *intel_state = to_intel_plane_state(state);
4575
4576 intel_state->visible = false;
4577 intel_plane->commit_plane(plane, intel_state);
4578
4579 intel_plane_destroy_state(plane, state);
4580}
4581
Matt Roper4a3b8762014-12-23 10:41:51 -08004582static void intel_disable_sprite_planes(struct drm_crtc *crtc)
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004583{
4584 struct drm_device *dev = crtc->dev;
4585 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07004586 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004587 struct intel_plane *intel_plane;
4588
Matt Roperaf2b6532014-04-01 15:22:32 -07004589 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4590 intel_plane = to_intel_plane(plane);
Matt Roper0d703d42015-03-04 10:49:04 -08004591 if (plane->fb && intel_plane->pipe == pipe)
4592 disable_plane_internal(plane);
Matt Roperaf2b6532014-04-01 15:22:32 -07004593 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004594}
4595
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004596void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004597{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004598 struct drm_device *dev = crtc->base.dev;
4599 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004600
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004601 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004602 return;
4603
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004604 /* We can only enable IPS after we enable a plane and wait for a vblank */
4605 intel_wait_for_vblank(dev, crtc->pipe);
4606
Paulo Zanonid77e4532013-09-24 13:52:55 -03004607 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004608 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004609 mutex_lock(&dev_priv->rps.hw_lock);
4610 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4611 mutex_unlock(&dev_priv->rps.hw_lock);
4612 /* Quoting Art Runyan: "its not safe to expect any particular
4613 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004614 * mailbox." Moreover, the mailbox may return a bogus state,
4615 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004616 */
4617 } else {
4618 I915_WRITE(IPS_CTL, IPS_ENABLE);
4619 /* The bit only becomes 1 in the next vblank, so this wait here
4620 * is essentially intel_wait_for_vblank. If we don't have this
4621 * and don't wait for vblanks until the end of crtc_enable, then
4622 * the HW state readout code will complain that the expected
4623 * IPS_CTL value is not the one we read. */
4624 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4625 DRM_ERROR("Timed out waiting for IPS enable\n");
4626 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004627}
4628
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004629void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004630{
4631 struct drm_device *dev = crtc->base.dev;
4632 struct drm_i915_private *dev_priv = dev->dev_private;
4633
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004634 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004635 return;
4636
4637 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004638 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004639 mutex_lock(&dev_priv->rps.hw_lock);
4640 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4641 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004642 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4643 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4644 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004645 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004646 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004647 POSTING_READ(IPS_CTL);
4648 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004649
4650 /* We need to wait for a vblank before we can disable the plane. */
4651 intel_wait_for_vblank(dev, crtc->pipe);
4652}
4653
4654/** Loads the palette/gamma unit for the CRTC with the prepared values */
4655static void intel_crtc_load_lut(struct drm_crtc *crtc)
4656{
4657 struct drm_device *dev = crtc->dev;
4658 struct drm_i915_private *dev_priv = dev->dev_private;
4659 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4660 enum pipe pipe = intel_crtc->pipe;
4661 int palreg = PALETTE(pipe);
4662 int i;
4663 bool reenable_ips = false;
4664
4665 /* The clocks have to be on to load the palette. */
Matt Roper83d65732015-02-25 13:12:16 -08004666 if (!crtc->state->enable || !intel_crtc->active)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004667 return;
4668
Imre Deak50360402015-01-16 00:55:16 -08004669 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03004670 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004671 assert_dsi_pll_enabled(dev_priv);
4672 else
4673 assert_pll_enabled(dev_priv, pipe);
4674 }
4675
4676 /* use legacy palette for Ironlake */
Sonika Jindal7a1db492014-07-22 11:18:27 +05304677 if (!HAS_GMCH_DISPLAY(dev))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004678 palreg = LGC_PALETTE(pipe);
4679
4680 /* Workaround : Do not read or write the pipe palette/gamma data while
4681 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4682 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004683 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03004684 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4685 GAMMA_MODE_MODE_SPLIT)) {
4686 hsw_disable_ips(intel_crtc);
4687 reenable_ips = true;
4688 }
4689
4690 for (i = 0; i < 256; i++) {
4691 I915_WRITE(palreg + 4 * i,
4692 (intel_crtc->lut_r[i] << 16) |
4693 (intel_crtc->lut_g[i] << 8) |
4694 intel_crtc->lut_b[i]);
4695 }
4696
4697 if (reenable_ips)
4698 hsw_enable_ips(intel_crtc);
4699}
4700
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004701static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
4702{
4703 if (!enable && intel_crtc->overlay) {
4704 struct drm_device *dev = intel_crtc->base.dev;
4705 struct drm_i915_private *dev_priv = dev->dev_private;
4706
4707 mutex_lock(&dev->struct_mutex);
4708 dev_priv->mm.interruptible = false;
4709 (void) intel_overlay_switch_off(intel_crtc->overlay);
4710 dev_priv->mm.interruptible = true;
4711 mutex_unlock(&dev->struct_mutex);
4712 }
4713
4714 /* Let userspace switch the overlay on again. In most cases userspace
4715 * has to recompute where to put it anyway.
4716 */
4717}
4718
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004719static void intel_crtc_enable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004720{
4721 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004722 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4723 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004724
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03004725 intel_enable_primary_hw_plane(crtc->primary, crtc);
Matt Roper4a3b8762014-12-23 10:41:51 -08004726 intel_enable_sprite_planes(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004727 intel_crtc_update_cursor(crtc, true);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004728 intel_crtc_dpms_overlay(intel_crtc, true);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004729
4730 hsw_enable_ips(intel_crtc);
4731
4732 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004733 intel_fbc_update(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004734 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf99d7062014-06-19 16:01:59 +02004735
4736 /*
4737 * FIXME: Once we grow proper nuclear flip support out of this we need
4738 * to compute the mask of flip planes precisely. For the time being
4739 * consider this a flip from a NULL plane.
4740 */
4741 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004742}
4743
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004744static void intel_crtc_disable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004745{
4746 struct drm_device *dev = crtc->dev;
4747 struct drm_i915_private *dev_priv = dev->dev_private;
4748 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4749 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004750
4751 intel_crtc_wait_for_pending_flips(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004752
Paulo Zanonie35fef22015-02-09 14:46:29 -02004753 if (dev_priv->fbc.crtc == intel_crtc)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004754 intel_fbc_disable(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004755
4756 hsw_disable_ips(intel_crtc);
4757
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004758 intel_crtc_dpms_overlay(intel_crtc, false);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004759 intel_crtc_update_cursor(crtc, false);
Matt Roper4a3b8762014-12-23 10:41:51 -08004760 intel_disable_sprite_planes(crtc);
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03004761 intel_disable_primary_hw_plane(crtc->primary, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004762
Daniel Vetterf99d7062014-06-19 16:01:59 +02004763 /*
4764 * FIXME: Once we grow proper nuclear flip support out of this we need
4765 * to compute the mask of flip planes precisely. For the time being
4766 * consider this a flip to a NULL plane.
4767 */
4768 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004769}
4770
Jesse Barnesf67a5592011-01-05 10:31:48 -08004771static void ironlake_crtc_enable(struct drm_crtc *crtc)
4772{
4773 struct drm_device *dev = crtc->dev;
4774 struct drm_i915_private *dev_priv = dev->dev_private;
4775 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004776 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004777 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004778
Matt Roper83d65732015-02-25 13:12:16 -08004779 WARN_ON(!crtc->state->enable);
Daniel Vetter08a48462012-07-02 11:43:47 +02004780
Jesse Barnesf67a5592011-01-05 10:31:48 -08004781 if (intel_crtc->active)
4782 return;
4783
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004784 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004785 intel_prepare_shared_dpll(intel_crtc);
4786
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004787 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304788 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004789
4790 intel_set_pipe_timings(intel_crtc);
4791
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004792 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004793 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004794 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004795 }
4796
4797 ironlake_set_pipeconf(crtc);
4798
Jesse Barnesf67a5592011-01-05 10:31:48 -08004799 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004800
Daniel Vettera72e4c92014-09-30 10:56:47 +02004801 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4802 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004803
Daniel Vetterf6736a12013-06-05 13:34:30 +02004804 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004805 if (encoder->pre_enable)
4806 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004807
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004808 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004809 /* Note: FDI PLL enabling _must_ be done before we enable the
4810 * cpu pipes, hence this is separate from all the other fdi/pch
4811 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004812 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004813 } else {
4814 assert_fdi_tx_disabled(dev_priv, pipe);
4815 assert_fdi_rx_disabled(dev_priv, pipe);
4816 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004817
Jesse Barnesb074cec2013-04-25 12:55:02 -07004818 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004819
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004820 /*
4821 * On ILK+ LUT must be loaded before the pipe is running but with
4822 * clocks enabled
4823 */
4824 intel_crtc_load_lut(crtc);
4825
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004826 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004827 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004828
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004829 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004830 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004831
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004832 assert_vblank_disabled(crtc);
4833 drm_crtc_vblank_on(crtc);
4834
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004835 for_each_encoder_on_crtc(dev, crtc, encoder)
4836 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004837
4838 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004839 cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02004840
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004841 intel_crtc_enable_planes(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004842}
4843
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004844/* IPS only exists on ULT machines and is tied to pipe A. */
4845static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4846{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004847 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004848}
4849
Paulo Zanonie4916942013-09-20 16:21:19 -03004850/*
4851 * This implements the workaround described in the "notes" section of the mode
4852 * set sequence documentation. When going from no pipes or single pipe to
4853 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4854 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4855 */
4856static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4857{
4858 struct drm_device *dev = crtc->base.dev;
4859 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4860
4861 /* We want to get the other_active_crtc only if there's only 1 other
4862 * active crtc. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004863 for_each_intel_crtc(dev, crtc_it) {
Paulo Zanonie4916942013-09-20 16:21:19 -03004864 if (!crtc_it->active || crtc_it == crtc)
4865 continue;
4866
4867 if (other_active_crtc)
4868 return;
4869
4870 other_active_crtc = crtc_it;
4871 }
4872 if (!other_active_crtc)
4873 return;
4874
4875 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4876 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4877}
4878
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004879static void haswell_crtc_enable(struct drm_crtc *crtc)
4880{
4881 struct drm_device *dev = crtc->dev;
4882 struct drm_i915_private *dev_priv = dev->dev_private;
4883 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4884 struct intel_encoder *encoder;
4885 int pipe = intel_crtc->pipe;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004886
Matt Roper83d65732015-02-25 13:12:16 -08004887 WARN_ON(!crtc->state->enable);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004888
4889 if (intel_crtc->active)
4890 return;
4891
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004892 if (intel_crtc_to_shared_dpll(intel_crtc))
4893 intel_enable_shared_dpll(intel_crtc);
4894
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004895 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304896 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004897
4898 intel_set_pipe_timings(intel_crtc);
4899
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004900 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
4901 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
4902 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004903 }
4904
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004905 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004906 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004907 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004908 }
4909
4910 haswell_set_pipeconf(crtc);
4911
4912 intel_set_pipe_csc(crtc);
4913
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004914 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004915
Daniel Vettera72e4c92014-09-30 10:56:47 +02004916 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004917 for_each_encoder_on_crtc(dev, crtc, encoder)
4918 if (encoder->pre_enable)
4919 encoder->pre_enable(encoder);
4920
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004921 if (intel_crtc->config->has_pch_encoder) {
Daniel Vettera72e4c92014-09-30 10:56:47 +02004922 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4923 true);
Imre Deak4fe94672014-06-25 22:01:49 +03004924 dev_priv->display.fdi_link_train(crtc);
4925 }
4926
Paulo Zanoni1f544382012-10-24 11:32:00 -02004927 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004928
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004929 if (INTEL_INFO(dev)->gen == 9)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004930 skylake_pfit_update(intel_crtc, 1);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004931 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004932 ironlake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004933 else
4934 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004935
4936 /*
4937 * On ILK+ LUT must be loaded before the pipe is running but with
4938 * clocks enabled
4939 */
4940 intel_crtc_load_lut(crtc);
4941
Paulo Zanoni1f544382012-10-24 11:32:00 -02004942 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00004943 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004944
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004945 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004946 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004947
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004948 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004949 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004950
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004951 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10004952 intel_ddi_set_vc_payload_alloc(crtc, true);
4953
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004954 assert_vblank_disabled(crtc);
4955 drm_crtc_vblank_on(crtc);
4956
Jani Nikula8807e552013-08-30 19:40:32 +03004957 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004958 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004959 intel_opregion_notify_encoder(encoder, true);
4960 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004961
Paulo Zanonie4916942013-09-20 16:21:19 -03004962 /* If we change the relative order between pipe/planes enabling, we need
4963 * to change the workaround. */
4964 haswell_mode_set_planes_workaround(intel_crtc);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004965 intel_crtc_enable_planes(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004966}
4967
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004968static void ironlake_pfit_disable(struct intel_crtc *crtc)
4969{
4970 struct drm_device *dev = crtc->base.dev;
4971 struct drm_i915_private *dev_priv = dev->dev_private;
4972 int pipe = crtc->pipe;
4973
4974 /* To avoid upsetting the power well on haswell only disable the pfit if
4975 * it's in use. The hw state code will make sure we get this right. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004976 if (crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004977 I915_WRITE(PF_CTL(pipe), 0);
4978 I915_WRITE(PF_WIN_POS(pipe), 0);
4979 I915_WRITE(PF_WIN_SZ(pipe), 0);
4980 }
4981}
4982
Jesse Barnes6be4a602010-09-10 10:26:01 -07004983static void ironlake_crtc_disable(struct drm_crtc *crtc)
4984{
4985 struct drm_device *dev = crtc->dev;
4986 struct drm_i915_private *dev_priv = dev->dev_private;
4987 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004988 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004989 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01004990 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004991
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004992 if (!intel_crtc->active)
4993 return;
4994
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004995 intel_crtc_disable_planes(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004996
Daniel Vetterea9d7582012-07-10 10:42:52 +02004997 for_each_encoder_on_crtc(dev, crtc, encoder)
4998 encoder->disable(encoder);
4999
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005000 drm_crtc_vblank_off(crtc);
5001 assert_vblank_disabled(crtc);
5002
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005003 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005004 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Daniel Vetterd925c592013-06-05 13:34:04 +02005005
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005006 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005007
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005008 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005009
Daniel Vetterbf49ec82012-09-06 22:15:40 +02005010 for_each_encoder_on_crtc(dev, crtc, encoder)
5011 if (encoder->post_disable)
5012 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005013
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005014 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005015 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005016
Daniel Vetterd925c592013-06-05 13:34:04 +02005017 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005018
Daniel Vetterd925c592013-06-05 13:34:04 +02005019 if (HAS_PCH_CPT(dev)) {
5020 /* disable TRANS_DP_CTL */
5021 reg = TRANS_DP_CTL(pipe);
5022 temp = I915_READ(reg);
5023 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5024 TRANS_DP_PORT_SEL_MASK);
5025 temp |= TRANS_DP_PORT_SEL_NONE;
5026 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005027
Daniel Vetterd925c592013-06-05 13:34:04 +02005028 /* disable DPLL_SEL */
5029 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005030 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005031 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005032 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005033
5034 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005035 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02005036
5037 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005038 }
5039
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005040 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005041 intel_update_watermarks(crtc);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01005042
5043 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005044 intel_fbc_update(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01005045 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005046}
5047
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005048static void haswell_crtc_disable(struct drm_crtc *crtc)
5049{
5050 struct drm_device *dev = crtc->dev;
5051 struct drm_i915_private *dev_priv = dev->dev_private;
5052 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5053 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005054 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005055
5056 if (!intel_crtc->active)
5057 return;
5058
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03005059 intel_crtc_disable_planes(crtc);
Ville Syrjälädda9a662013-09-19 17:00:37 -03005060
Jani Nikula8807e552013-08-30 19:40:32 +03005061 for_each_encoder_on_crtc(dev, crtc, encoder) {
5062 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005063 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005064 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005065
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005066 drm_crtc_vblank_off(crtc);
5067 assert_vblank_disabled(crtc);
5068
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005069 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005070 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5071 false);
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005072 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005073
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005074 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005075 intel_ddi_set_vc_payload_alloc(crtc, false);
5076
Paulo Zanoniad80a812012-10-24 16:06:19 -02005077 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005078
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005079 if (INTEL_INFO(dev)->gen == 9)
Chandra Kondurua1b22782015-04-07 15:28:45 -07005080 skylake_pfit_update(intel_crtc, 0);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005081 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00005082 ironlake_pfit_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005083 else
5084 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005085
Paulo Zanoni1f544382012-10-24 11:32:00 -02005086 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005087
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005088 if (intel_crtc->config->has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02005089 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02005090 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02005091 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005092
Imre Deak97b040a2014-06-25 22:01:50 +03005093 for_each_encoder_on_crtc(dev, crtc, encoder)
5094 if (encoder->post_disable)
5095 encoder->post_disable(encoder);
5096
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005097 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005098 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005099
5100 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005101 intel_fbc_update(dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005102 mutex_unlock(&dev->struct_mutex);
Daniel Vetterdf8ad702014-06-25 22:02:03 +03005103
5104 if (intel_crtc_to_shared_dpll(intel_crtc))
5105 intel_disable_shared_dpll(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005106}
5107
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005108static void ironlake_crtc_off(struct drm_crtc *crtc)
5109{
5110 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005111 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005112}
5113
Paulo Zanoni6441ab52012-10-05 12:05:58 -03005114
Jesse Barnes2dd24552013-04-25 12:55:01 -07005115static void i9xx_pfit_enable(struct intel_crtc *crtc)
5116{
5117 struct drm_device *dev = crtc->base.dev;
5118 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005119 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005120
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005121 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005122 return;
5123
Daniel Vetterc0b03412013-05-28 12:05:54 +02005124 /*
5125 * The panel fitter should only be adjusted whilst the pipe is disabled,
5126 * according to register description and PRM.
5127 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005128 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5129 assert_pipe_disabled(dev_priv, crtc->pipe);
5130
Jesse Barnesb074cec2013-04-25 12:55:02 -07005131 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5132 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005133
5134 /* Border color in case we don't scale up to the full screen. Black by
5135 * default, change to something else for debugging. */
5136 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005137}
5138
Dave Airlied05410f2014-06-05 13:22:59 +10005139static enum intel_display_power_domain port_to_power_domain(enum port port)
5140{
5141 switch (port) {
5142 case PORT_A:
5143 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
5144 case PORT_B:
5145 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
5146 case PORT_C:
5147 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
5148 case PORT_D:
5149 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
5150 default:
5151 WARN_ON_ONCE(1);
5152 return POWER_DOMAIN_PORT_OTHER;
5153 }
5154}
5155
Imre Deak77d22dc2014-03-05 16:20:52 +02005156#define for_each_power_domain(domain, mask) \
5157 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
5158 if ((1 << (domain)) & (mask))
5159
Imre Deak319be8a2014-03-04 19:22:57 +02005160enum intel_display_power_domain
5161intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005162{
Imre Deak319be8a2014-03-04 19:22:57 +02005163 struct drm_device *dev = intel_encoder->base.dev;
5164 struct intel_digital_port *intel_dig_port;
5165
5166 switch (intel_encoder->type) {
5167 case INTEL_OUTPUT_UNKNOWN:
5168 /* Only DDI platforms should ever use this output type */
5169 WARN_ON_ONCE(!HAS_DDI(dev));
5170 case INTEL_OUTPUT_DISPLAYPORT:
5171 case INTEL_OUTPUT_HDMI:
5172 case INTEL_OUTPUT_EDP:
5173 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005174 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005175 case INTEL_OUTPUT_DP_MST:
5176 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5177 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005178 case INTEL_OUTPUT_ANALOG:
5179 return POWER_DOMAIN_PORT_CRT;
5180 case INTEL_OUTPUT_DSI:
5181 return POWER_DOMAIN_PORT_DSI;
5182 default:
5183 return POWER_DOMAIN_PORT_OTHER;
5184 }
5185}
5186
5187static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
5188{
5189 struct drm_device *dev = crtc->dev;
5190 struct intel_encoder *intel_encoder;
5191 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5192 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005193 unsigned long mask;
5194 enum transcoder transcoder;
5195
5196 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
5197
5198 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5199 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005200 if (intel_crtc->config->pch_pfit.enabled ||
5201 intel_crtc->config->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005202 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5203
Imre Deak319be8a2014-03-04 19:22:57 +02005204 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5205 mask |= BIT(intel_display_port_power_domain(intel_encoder));
5206
Imre Deak77d22dc2014-03-05 16:20:52 +02005207 return mask;
5208}
5209
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005210static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
Imre Deak77d22dc2014-03-05 16:20:52 +02005211{
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005212 struct drm_device *dev = state->dev;
Imre Deak77d22dc2014-03-05 16:20:52 +02005213 struct drm_i915_private *dev_priv = dev->dev_private;
5214 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
5215 struct intel_crtc *crtc;
5216
5217 /*
5218 * First get all needed power domains, then put all unneeded, to avoid
5219 * any unnecessary toggling of the power wells.
5220 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005221 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005222 enum intel_display_power_domain domain;
5223
Matt Roper83d65732015-02-25 13:12:16 -08005224 if (!crtc->base.state->enable)
Imre Deak77d22dc2014-03-05 16:20:52 +02005225 continue;
5226
Imre Deak319be8a2014-03-04 19:22:57 +02005227 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
Imre Deak77d22dc2014-03-05 16:20:52 +02005228
5229 for_each_power_domain(domain, pipe_domains[crtc->pipe])
5230 intel_display_power_get(dev_priv, domain);
5231 }
5232
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005233 if (dev_priv->display.modeset_global_resources)
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005234 dev_priv->display.modeset_global_resources(state);
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005235
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005236 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005237 enum intel_display_power_domain domain;
5238
5239 for_each_power_domain(domain, crtc->enabled_power_domains)
5240 intel_display_power_put(dev_priv, domain);
5241
5242 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
5243 }
5244
5245 intel_display_set_init_power(dev_priv, false);
5246}
5247
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305248void broxton_set_cdclk(struct drm_device *dev, int frequency)
5249{
5250 struct drm_i915_private *dev_priv = dev->dev_private;
5251 uint32_t divider;
5252 uint32_t ratio;
5253 uint32_t current_freq;
5254 int ret;
5255
5256 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5257 switch (frequency) {
5258 case 144000:
5259 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5260 ratio = BXT_DE_PLL_RATIO(60);
5261 break;
5262 case 288000:
5263 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5264 ratio = BXT_DE_PLL_RATIO(60);
5265 break;
5266 case 384000:
5267 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5268 ratio = BXT_DE_PLL_RATIO(60);
5269 break;
5270 case 576000:
5271 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5272 ratio = BXT_DE_PLL_RATIO(60);
5273 break;
5274 case 624000:
5275 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5276 ratio = BXT_DE_PLL_RATIO(65);
5277 break;
5278 case 19200:
5279 /*
5280 * Bypass frequency with DE PLL disabled. Init ratio, divider
5281 * to suppress GCC warning.
5282 */
5283 ratio = 0;
5284 divider = 0;
5285 break;
5286 default:
5287 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5288
5289 return;
5290 }
5291
5292 mutex_lock(&dev_priv->rps.hw_lock);
5293 /* Inform power controller of upcoming frequency change */
5294 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5295 0x80000000);
5296 mutex_unlock(&dev_priv->rps.hw_lock);
5297
5298 if (ret) {
5299 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5300 ret, frequency);
5301 return;
5302 }
5303
5304 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5305 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5306 current_freq = current_freq * 500 + 1000;
5307
5308 /*
5309 * DE PLL has to be disabled when
5310 * - setting to 19.2MHz (bypass, PLL isn't used)
5311 * - before setting to 624MHz (PLL needs toggling)
5312 * - before setting to any frequency from 624MHz (PLL needs toggling)
5313 */
5314 if (frequency == 19200 || frequency == 624000 ||
5315 current_freq == 624000) {
5316 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5317 /* Timeout 200us */
5318 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5319 1))
5320 DRM_ERROR("timout waiting for DE PLL unlock\n");
5321 }
5322
5323 if (frequency != 19200) {
5324 uint32_t val;
5325
5326 val = I915_READ(BXT_DE_PLL_CTL);
5327 val &= ~BXT_DE_PLL_RATIO_MASK;
5328 val |= ratio;
5329 I915_WRITE(BXT_DE_PLL_CTL, val);
5330
5331 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5332 /* Timeout 200us */
5333 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5334 DRM_ERROR("timeout waiting for DE PLL lock\n");
5335
5336 val = I915_READ(CDCLK_CTL);
5337 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5338 val |= divider;
5339 /*
5340 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5341 * enable otherwise.
5342 */
5343 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5344 if (frequency >= 500000)
5345 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5346
5347 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5348 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5349 val |= (frequency - 1000) / 500;
5350 I915_WRITE(CDCLK_CTL, val);
5351 }
5352
5353 mutex_lock(&dev_priv->rps.hw_lock);
5354 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5355 DIV_ROUND_UP(frequency, 25000));
5356 mutex_unlock(&dev_priv->rps.hw_lock);
5357
5358 if (ret) {
5359 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5360 ret, frequency);
5361 return;
5362 }
5363
5364 dev_priv->cdclk_freq = frequency;
5365}
5366
5367void broxton_init_cdclk(struct drm_device *dev)
5368{
5369 struct drm_i915_private *dev_priv = dev->dev_private;
5370 uint32_t val;
5371
5372 /*
5373 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5374 * or else the reset will hang because there is no PCH to respond.
5375 * Move the handshake programming to initialization sequence.
5376 * Previously was left up to BIOS.
5377 */
5378 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5379 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5380 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5381
5382 /* Enable PG1 for cdclk */
5383 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5384
5385 /* check if cd clock is enabled */
5386 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5387 DRM_DEBUG_KMS("Display already initialized\n");
5388 return;
5389 }
5390
5391 /*
5392 * FIXME:
5393 * - The initial CDCLK needs to be read from VBT.
5394 * Need to make this change after VBT has changes for BXT.
5395 * - check if setting the max (or any) cdclk freq is really necessary
5396 * here, it belongs to modeset time
5397 */
5398 broxton_set_cdclk(dev, 624000);
5399
5400 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5401 udelay(10);
5402
5403 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5404 DRM_ERROR("DBuf power enable timeout!\n");
5405}
5406
5407void broxton_uninit_cdclk(struct drm_device *dev)
5408{
5409 struct drm_i915_private *dev_priv = dev->dev_private;
5410
5411 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5412 udelay(10);
5413
5414 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5415 DRM_ERROR("DBuf power disable timeout!\n");
5416
5417 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5418 broxton_set_cdclk(dev, 19200);
5419
5420 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5421}
5422
Ville Syrjälädfcab172014-06-13 13:37:47 +03005423/* returns HPLL frequency in kHz */
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005424static int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005425{
Jesse Barnes586f49d2013-11-04 16:06:59 -08005426 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08005427
Jesse Barnes586f49d2013-11-04 16:06:59 -08005428 /* Obtain SKU information */
5429 mutex_lock(&dev_priv->dpio_lock);
5430 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
5431 CCK_FUSE_HPLL_FREQ_MASK;
5432 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005433
Ville Syrjälädfcab172014-06-13 13:37:47 +03005434 return vco_freq[hpll_freq] * 1000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005435}
5436
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005437static void vlv_update_cdclk(struct drm_device *dev)
5438{
5439 struct drm_i915_private *dev_priv = dev->dev_private;
5440
Vandana Kannan164dfd22014-11-24 13:37:41 +05305441 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
Ville Syrjälä43dc52c2014-10-07 17:41:20 +03005442 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
Vandana Kannan164dfd22014-11-24 13:37:41 +05305443 dev_priv->cdclk_freq);
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005444
5445 /*
5446 * Program the gmbus_freq based on the cdclk frequency.
5447 * BSpec erroneously claims we should aim for 4MHz, but
5448 * in fact 1MHz is the correct frequency.
5449 */
Vandana Kannan164dfd22014-11-24 13:37:41 +05305450 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005451}
5452
Jesse Barnes30a970c2013-11-04 13:48:12 -08005453/* Adjust CDclk dividers to allow high res or save power if possible */
5454static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5455{
5456 struct drm_i915_private *dev_priv = dev->dev_private;
5457 u32 val, cmd;
5458
Vandana Kannan164dfd22014-11-24 13:37:41 +05305459 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5460 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005461
Ville Syrjälädfcab172014-06-13 13:37:47 +03005462 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005463 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005464 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005465 cmd = 1;
5466 else
5467 cmd = 0;
5468
5469 mutex_lock(&dev_priv->rps.hw_lock);
5470 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5471 val &= ~DSPFREQGUAR_MASK;
5472 val |= (cmd << DSPFREQGUAR_SHIFT);
5473 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5474 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5475 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5476 50)) {
5477 DRM_ERROR("timed out waiting for CDclk change\n");
5478 }
5479 mutex_unlock(&dev_priv->rps.hw_lock);
5480
Ville Syrjälädfcab172014-06-13 13:37:47 +03005481 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005482 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005483
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005484 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005485
5486 mutex_lock(&dev_priv->dpio_lock);
5487 /* adjust cdclk divider */
5488 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjälä9cf33db2014-06-13 13:37:48 +03005489 val &= ~DISPLAY_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005490 val |= divider;
5491 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005492
5493 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5494 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5495 50))
5496 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005497 mutex_unlock(&dev_priv->dpio_lock);
5498 }
5499
5500 mutex_lock(&dev_priv->dpio_lock);
5501 /* adjust self-refresh exit latency value */
5502 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5503 val &= ~0x7f;
5504
5505 /*
5506 * For high bandwidth configs, we set a higher latency in the bunit
5507 * so that the core display fetch happens in time to avoid underruns.
5508 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005509 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005510 val |= 4500 / 250; /* 4.5 usec */
5511 else
5512 val |= 3000 / 250; /* 3.0 usec */
5513 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
5514 mutex_unlock(&dev_priv->dpio_lock);
5515
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005516 vlv_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005517}
5518
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005519static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5520{
5521 struct drm_i915_private *dev_priv = dev->dev_private;
5522 u32 val, cmd;
5523
Vandana Kannan164dfd22014-11-24 13:37:41 +05305524 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5525 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005526
5527 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005528 case 333333:
5529 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005530 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005531 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005532 break;
5533 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005534 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005535 return;
5536 }
5537
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005538 /*
5539 * Specs are full of misinformation, but testing on actual
5540 * hardware has shown that we just need to write the desired
5541 * CCK divider into the Punit register.
5542 */
5543 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5544
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005545 mutex_lock(&dev_priv->rps.hw_lock);
5546 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5547 val &= ~DSPFREQGUAR_MASK_CHV;
5548 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5549 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5550 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5551 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5552 50)) {
5553 DRM_ERROR("timed out waiting for CDclk change\n");
5554 }
5555 mutex_unlock(&dev_priv->rps.hw_lock);
5556
5557 vlv_update_cdclk(dev);
5558}
5559
Jesse Barnes30a970c2013-11-04 13:48:12 -08005560static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5561 int max_pixclk)
5562{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005563 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005564 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005565
Jesse Barnes30a970c2013-11-04 13:48:12 -08005566 /*
5567 * Really only a few cases to deal with, as only 4 CDclks are supported:
5568 * 200MHz
5569 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005570 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005571 * 400MHz (VLV only)
5572 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5573 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005574 *
5575 * We seem to get an unstable or solid color picture at 200MHz.
5576 * Not sure what's wrong. For now use 200MHz only when all pipes
5577 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005578 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005579 if (!IS_CHERRYVIEW(dev_priv) &&
5580 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005581 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005582 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005583 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005584 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005585 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005586 else
5587 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005588}
5589
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305590static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5591 int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005592{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305593 /*
5594 * FIXME:
5595 * - remove the guardband, it's not needed on BXT
5596 * - set 19.2MHz bypass frequency if there are no active pipes
5597 */
5598 if (max_pixclk > 576000*9/10)
5599 return 624000;
5600 else if (max_pixclk > 384000*9/10)
5601 return 576000;
5602 else if (max_pixclk > 288000*9/10)
5603 return 384000;
5604 else if (max_pixclk > 144000*9/10)
5605 return 288000;
5606 else
5607 return 144000;
5608}
5609
Jesse Barnes30a970c2013-11-04 13:48:12 -08005610/* compute the max pixel clock for new configuration */
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005611static int intel_mode_max_pixclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005612{
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005613 struct drm_device *dev = state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005614 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005615 struct intel_crtc_state *crtc_state;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005616 int max_pixclk = 0;
5617
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005618 for_each_intel_crtc(dev, intel_crtc) {
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005619 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
5620 if (IS_ERR(crtc_state))
5621 return PTR_ERR(crtc_state);
5622
5623 if (!crtc_state->base.enable)
5624 continue;
5625
5626 max_pixclk = max(max_pixclk,
5627 crtc_state->base.adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005628 }
5629
5630 return max_pixclk;
5631}
5632
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005633static int valleyview_modeset_global_pipes(struct drm_atomic_state *state,
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005634 unsigned *prepare_pipes)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005635{
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005636 struct drm_i915_private *dev_priv = to_i915(state->dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005637 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005638 int max_pixclk = intel_mode_max_pixclk(state);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305639 int cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005640
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005641 if (max_pixclk < 0)
5642 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005643
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305644 if (IS_VALLEYVIEW(dev_priv))
5645 cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
5646 else
5647 cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
5648
5649 if (cdclk == dev_priv->cdclk_freq)
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005650 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005651
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005652 /* disable/enable all currently active pipes while we change cdclk */
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005653 for_each_intel_crtc(state->dev, intel_crtc)
Matt Roper83d65732015-02-25 13:12:16 -08005654 if (intel_crtc->base.state->enable)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005655 *prepare_pipes |= (1 << intel_crtc->pipe);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005656
5657 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005658}
5659
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005660static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5661{
5662 unsigned int credits, default_credits;
5663
5664 if (IS_CHERRYVIEW(dev_priv))
5665 default_credits = PFI_CREDIT(12);
5666 else
5667 default_credits = PFI_CREDIT(8);
5668
Vandana Kannan164dfd22014-11-24 13:37:41 +05305669 if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005670 /* CHV suggested value is 31 or 63 */
5671 if (IS_CHERRYVIEW(dev_priv))
5672 credits = PFI_CREDIT_31;
5673 else
5674 credits = PFI_CREDIT(15);
5675 } else {
5676 credits = default_credits;
5677 }
5678
5679 /*
5680 * WA - write default credits before re-programming
5681 * FIXME: should we also set the resend bit here?
5682 */
5683 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5684 default_credits);
5685
5686 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5687 credits | PFI_CREDIT_RESEND);
5688
5689 /*
5690 * FIXME is this guaranteed to clear
5691 * immediately or should we poll for it?
5692 */
5693 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
5694}
5695
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005696static void valleyview_modeset_global_resources(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005697{
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005698 struct drm_device *dev = state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005699 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005700 int max_pixclk = intel_mode_max_pixclk(state);
5701 int req_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005702
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005703 /* The only reason this can fail is if we fail to add the crtc_state
5704 * to the atomic state. But that can't happen since the call to
5705 * intel_mode_max_pixclk() in valleyview_modeset_global_pipes() (which
5706 * can't have failed otherwise the mode set would be aborted) added all
5707 * the states already. */
5708 if (WARN_ON(max_pixclk < 0))
5709 return;
5710
5711 req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005712
Vandana Kannan164dfd22014-11-24 13:37:41 +05305713 if (req_cdclk != dev_priv->cdclk_freq) {
Imre Deak738c05c2014-11-19 16:25:37 +02005714 /*
5715 * FIXME: We can end up here with all power domains off, yet
5716 * with a CDCLK frequency other than the minimum. To account
5717 * for this take the PIPE-A power domain, which covers the HW
5718 * blocks needed for the following programming. This can be
5719 * removed once it's guaranteed that we get here either with
5720 * the minimum CDCLK set, or the required power domains
5721 * enabled.
5722 */
5723 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
5724
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005725 if (IS_CHERRYVIEW(dev))
5726 cherryview_set_cdclk(dev, req_cdclk);
5727 else
5728 valleyview_set_cdclk(dev, req_cdclk);
Imre Deak738c05c2014-11-19 16:25:37 +02005729
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005730 vlv_program_pfi_credits(dev_priv);
5731
Imre Deak738c05c2014-11-19 16:25:37 +02005732 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005733 }
Jesse Barnes30a970c2013-11-04 13:48:12 -08005734}
5735
Jesse Barnes89b667f2013-04-18 14:51:36 -07005736static void valleyview_crtc_enable(struct drm_crtc *crtc)
5737{
5738 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02005739 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005740 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5741 struct intel_encoder *encoder;
5742 int pipe = intel_crtc->pipe;
Jani Nikula23538ef2013-08-27 15:12:22 +03005743 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005744
Matt Roper83d65732015-02-25 13:12:16 -08005745 WARN_ON(!crtc->state->enable);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005746
5747 if (intel_crtc->active)
5748 return;
5749
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005750 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
Shobhit Kumar8525a232014-06-25 12:20:39 +05305751
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005752 if (!is_dsi) {
5753 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005754 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005755 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005756 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005757 }
Daniel Vetter5b18e572014-04-24 23:55:06 +02005758
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005759 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05305760 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005761
5762 intel_set_pipe_timings(intel_crtc);
5763
Ville Syrjäläc14b0482014-10-16 20:52:34 +03005764 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
5765 struct drm_i915_private *dev_priv = dev->dev_private;
5766
5767 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
5768 I915_WRITE(CHV_CANVAS(pipe), 0);
5769 }
5770
Daniel Vetter5b18e572014-04-24 23:55:06 +02005771 i9xx_set_pipeconf(intel_crtc);
5772
Jesse Barnes89b667f2013-04-18 14:51:36 -07005773 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005774
Daniel Vettera72e4c92014-09-30 10:56:47 +02005775 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005776
Jesse Barnes89b667f2013-04-18 14:51:36 -07005777 for_each_encoder_on_crtc(dev, crtc, encoder)
5778 if (encoder->pre_pll_enable)
5779 encoder->pre_pll_enable(encoder);
5780
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005781 if (!is_dsi) {
5782 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005783 chv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005784 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005785 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005786 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07005787
5788 for_each_encoder_on_crtc(dev, crtc, encoder)
5789 if (encoder->pre_enable)
5790 encoder->pre_enable(encoder);
5791
Jesse Barnes2dd24552013-04-25 12:55:01 -07005792 i9xx_pfit_enable(intel_crtc);
5793
Ville Syrjälä63cbb072013-06-04 13:48:59 +03005794 intel_crtc_load_lut(crtc);
5795
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005796 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005797 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02005798
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005799 assert_vblank_disabled(crtc);
5800 drm_crtc_vblank_on(crtc);
5801
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005802 for_each_encoder_on_crtc(dev, crtc, encoder)
5803 encoder->enable(encoder);
5804
Ville Syrjälä9ab04602014-05-08 19:23:14 +03005805 intel_crtc_enable_planes(crtc);
Daniel Vetterd40d9182014-05-21 11:45:40 +02005806
Ville Syrjälä56b80e12014-05-16 19:40:22 +03005807 /* Underruns don't raise interrupts, so check manually. */
Daniel Vettera72e4c92014-09-30 10:56:47 +02005808 i9xx_check_fifo_underruns(dev_priv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005809}
5810
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005811static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
5812{
5813 struct drm_device *dev = crtc->base.dev;
5814 struct drm_i915_private *dev_priv = dev->dev_private;
5815
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005816 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
5817 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005818}
5819
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005820static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005821{
5822 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02005823 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08005824 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005825 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08005826 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08005827
Matt Roper83d65732015-02-25 13:12:16 -08005828 WARN_ON(!crtc->state->enable);
Daniel Vetter08a48462012-07-02 11:43:47 +02005829
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005830 if (intel_crtc->active)
5831 return;
5832
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005833 i9xx_set_pll_dividers(intel_crtc);
5834
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005835 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05305836 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005837
5838 intel_set_pipe_timings(intel_crtc);
5839
Daniel Vetter5b18e572014-04-24 23:55:06 +02005840 i9xx_set_pipeconf(intel_crtc);
5841
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005842 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01005843
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005844 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005845 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005846
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02005847 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02005848 if (encoder->pre_enable)
5849 encoder->pre_enable(encoder);
5850
Daniel Vetterf6736a12013-06-05 13:34:30 +02005851 i9xx_enable_pll(intel_crtc);
5852
Jesse Barnes2dd24552013-04-25 12:55:01 -07005853 i9xx_pfit_enable(intel_crtc);
5854
Ville Syrjälä63cbb072013-06-04 13:48:59 +03005855 intel_crtc_load_lut(crtc);
5856
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005857 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005858 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02005859
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005860 assert_vblank_disabled(crtc);
5861 drm_crtc_vblank_on(crtc);
5862
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005863 for_each_encoder_on_crtc(dev, crtc, encoder)
5864 encoder->enable(encoder);
5865
Ville Syrjälä9ab04602014-05-08 19:23:14 +03005866 intel_crtc_enable_planes(crtc);
Daniel Vetterd40d9182014-05-21 11:45:40 +02005867
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005868 /*
5869 * Gen2 reports pipe underruns whenever all planes are disabled.
5870 * So don't enable underrun reporting before at least some planes
5871 * are enabled.
5872 * FIXME: Need to fix the logic to work when we turn off all planes
5873 * but leave the pipe running.
5874 */
5875 if (IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005876 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005877
Ville Syrjälä56b80e12014-05-16 19:40:22 +03005878 /* Underruns don't raise interrupts, so check manually. */
Daniel Vettera72e4c92014-09-30 10:56:47 +02005879 i9xx_check_fifo_underruns(dev_priv);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005880}
5881
Daniel Vetter87476d62013-04-11 16:29:06 +02005882static void i9xx_pfit_disable(struct intel_crtc *crtc)
5883{
5884 struct drm_device *dev = crtc->base.dev;
5885 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02005886
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005887 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02005888 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02005889
5890 assert_pipe_disabled(dev_priv, crtc->pipe);
5891
Daniel Vetter328d8e82013-05-08 10:36:31 +02005892 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
5893 I915_READ(PFIT_CONTROL));
5894 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02005895}
5896
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005897static void i9xx_crtc_disable(struct drm_crtc *crtc)
5898{
5899 struct drm_device *dev = crtc->dev;
5900 struct drm_i915_private *dev_priv = dev->dev_private;
5901 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005902 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005903 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005904
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005905 if (!intel_crtc->active)
5906 return;
5907
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005908 /*
5909 * Gen2 reports pipe underruns whenever all planes are disabled.
5910 * So diasble underrun reporting before all the planes get disabled.
5911 * FIXME: Need to fix the logic to work when we turn off all planes
5912 * but leave the pipe running.
5913 */
5914 if (IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005915 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005916
Imre Deak564ed192014-06-13 14:54:21 +03005917 /*
5918 * Vblank time updates from the shadow to live plane control register
5919 * are blocked if the memory self-refresh mode is active at that
5920 * moment. So to make sure the plane gets truly disabled, disable
5921 * first the self-refresh mode. The self-refresh enable bit in turn
5922 * will be checked/applied by the HW only at the next frame start
5923 * event which is after the vblank start event, so we need to have a
5924 * wait-for-vblank between disabling the plane and the pipe.
5925 */
5926 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä9ab04602014-05-08 19:23:14 +03005927 intel_crtc_disable_planes(crtc);
5928
Ville Syrjälä6304cd92014-04-25 13:30:12 +03005929 /*
5930 * On gen2 planes are double buffered but the pipe isn't, so we must
5931 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03005932 * We also need to wait on all gmch platforms because of the
5933 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03005934 */
Imre Deak564ed192014-06-13 14:54:21 +03005935 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03005936
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005937 for_each_encoder_on_crtc(dev, crtc, encoder)
5938 encoder->disable(encoder);
5939
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005940 drm_crtc_vblank_off(crtc);
5941 assert_vblank_disabled(crtc);
5942
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005943 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02005944
Daniel Vetter87476d62013-04-11 16:29:06 +02005945 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02005946
Jesse Barnes89b667f2013-04-18 14:51:36 -07005947 for_each_encoder_on_crtc(dev, crtc, encoder)
5948 if (encoder->post_disable)
5949 encoder->post_disable(encoder);
5950
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005951 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03005952 if (IS_CHERRYVIEW(dev))
5953 chv_disable_pll(dev_priv, pipe);
5954 else if (IS_VALLEYVIEW(dev))
5955 vlv_disable_pll(dev_priv, pipe);
5956 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03005957 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03005958 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005959
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005960 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005961 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005962
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005963 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005964 intel_update_watermarks(crtc);
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005965
Daniel Vetterefa96242014-04-24 23:55:02 +02005966 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005967 intel_fbc_update(dev);
Daniel Vetterefa96242014-04-24 23:55:02 +02005968 mutex_unlock(&dev->struct_mutex);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005969}
5970
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005971static void i9xx_crtc_off(struct drm_crtc *crtc)
5972{
5973}
5974
Borun Fub04c5bd2014-07-12 10:02:27 +05305975/* Master function to enable/disable CRTC and corresponding power wells */
5976void intel_crtc_control(struct drm_crtc *crtc, bool enable)
Chris Wilsoncdd59982010-09-08 16:30:16 +01005977{
Chris Wilsoncdd59982010-09-08 16:30:16 +01005978 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005979 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005980 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005981 enum intel_display_power_domain domain;
5982 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02005983
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005984 if (enable) {
5985 if (!intel_crtc->active) {
Daniel Vettere1e9fb82014-06-25 22:02:04 +03005986 domains = get_crtc_power_domains(crtc);
5987 for_each_power_domain(domain, domains)
5988 intel_display_power_get(dev_priv, domain);
5989 intel_crtc->enabled_power_domains = domains;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005990
5991 dev_priv->display.crtc_enable(crtc);
5992 }
5993 } else {
5994 if (intel_crtc->active) {
5995 dev_priv->display.crtc_disable(crtc);
5996
Daniel Vettere1e9fb82014-06-25 22:02:04 +03005997 domains = intel_crtc->enabled_power_domains;
5998 for_each_power_domain(domain, domains)
5999 intel_display_power_put(dev_priv, domain);
6000 intel_crtc->enabled_power_domains = 0;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006001 }
6002 }
Borun Fub04c5bd2014-07-12 10:02:27 +05306003}
6004
6005/**
6006 * Sets the power management mode of the pipe and plane.
6007 */
6008void intel_crtc_update_dpms(struct drm_crtc *crtc)
6009{
6010 struct drm_device *dev = crtc->dev;
6011 struct intel_encoder *intel_encoder;
6012 bool enable = false;
6013
6014 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
6015 enable |= intel_encoder->connectors_active;
6016
6017 intel_crtc_control(crtc, enable);
Daniel Vetter976f8a22012-07-08 22:34:21 +02006018}
6019
Daniel Vetter976f8a22012-07-08 22:34:21 +02006020static void intel_crtc_disable(struct drm_crtc *crtc)
6021{
6022 struct drm_device *dev = crtc->dev;
6023 struct drm_connector *connector;
6024 struct drm_i915_private *dev_priv = dev->dev_private;
6025
6026 /* crtc should still be enabled when we disable it. */
Matt Roper83d65732015-02-25 13:12:16 -08006027 WARN_ON(!crtc->state->enable);
Daniel Vetter976f8a22012-07-08 22:34:21 +02006028
6029 dev_priv->display.crtc_disable(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006030 dev_priv->display.off(crtc);
6031
Matt Roper70a101f2015-04-08 18:56:53 -07006032 drm_plane_helper_disable(crtc->primary);
Daniel Vetter976f8a22012-07-08 22:34:21 +02006033
6034 /* Update computed state. */
6035 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
6036 if (!connector->encoder || !connector->encoder->crtc)
6037 continue;
6038
6039 if (connector->encoder->crtc != crtc)
6040 continue;
6041
6042 connector->dpms = DRM_MODE_DPMS_OFF;
6043 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01006044 }
6045}
6046
Chris Wilsonea5b2132010-08-04 13:50:23 +01006047void intel_encoder_destroy(struct drm_encoder *encoder)
6048{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006049 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006050
Chris Wilsonea5b2132010-08-04 13:50:23 +01006051 drm_encoder_cleanup(encoder);
6052 kfree(intel_encoder);
6053}
6054
Damien Lespiau92373292013-08-08 22:28:57 +01006055/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006056 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
6057 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01006058static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006059{
6060 if (mode == DRM_MODE_DPMS_ON) {
6061 encoder->connectors_active = true;
6062
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006063 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006064 } else {
6065 encoder->connectors_active = false;
6066
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006067 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006068 }
6069}
6070
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006071/* Cross check the actual hw state with our own modeset state tracking (and it's
6072 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02006073static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006074{
6075 if (connector->get_hw_state(connector)) {
6076 struct intel_encoder *encoder = connector->encoder;
6077 struct drm_crtc *crtc;
6078 bool encoder_enabled;
6079 enum pipe pipe;
6080
6081 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6082 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03006083 connector->base.name);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006084
Dave Airlie0e32b392014-05-02 14:02:48 +10006085 /* there is no real hw state for MST connectors */
6086 if (connector->mst_port)
6087 return;
6088
Rob Clarke2c719b2014-12-15 13:56:32 -05006089 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006090 "wrong connector dpms state\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006091 I915_STATE_WARN(connector->base.encoder != &encoder->base,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006092 "active connector not linked to encoder\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006093
Dave Airlie36cd7442014-05-02 13:44:18 +10006094 if (encoder) {
Rob Clarke2c719b2014-12-15 13:56:32 -05006095 I915_STATE_WARN(!encoder->connectors_active,
Dave Airlie36cd7442014-05-02 13:44:18 +10006096 "encoder->connectors_active not set\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006097
Dave Airlie36cd7442014-05-02 13:44:18 +10006098 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -05006099 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
6100 if (I915_STATE_WARN_ON(!encoder->base.crtc))
Dave Airlie36cd7442014-05-02 13:44:18 +10006101 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006102
Dave Airlie36cd7442014-05-02 13:44:18 +10006103 crtc = encoder->base.crtc;
6104
Matt Roper83d65732015-02-25 13:12:16 -08006105 I915_STATE_WARN(!crtc->state->enable,
6106 "crtc not enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006107 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
6108 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
Dave Airlie36cd7442014-05-02 13:44:18 +10006109 "encoder active on the wrong pipe\n");
6110 }
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006111 }
6112}
6113
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006114int intel_connector_init(struct intel_connector *connector)
6115{
6116 struct drm_connector_state *connector_state;
6117
6118 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6119 if (!connector_state)
6120 return -ENOMEM;
6121
6122 connector->base.state = connector_state;
6123 return 0;
6124}
6125
6126struct intel_connector *intel_connector_alloc(void)
6127{
6128 struct intel_connector *connector;
6129
6130 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6131 if (!connector)
6132 return NULL;
6133
6134 if (intel_connector_init(connector) < 0) {
6135 kfree(connector);
6136 return NULL;
6137 }
6138
6139 return connector;
6140}
6141
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006142/* Even simpler default implementation, if there's really no special case to
6143 * consider. */
6144void intel_connector_dpms(struct drm_connector *connector, int mode)
6145{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006146 /* All the simple cases only support two dpms states. */
6147 if (mode != DRM_MODE_DPMS_ON)
6148 mode = DRM_MODE_DPMS_OFF;
6149
6150 if (mode == connector->dpms)
6151 return;
6152
6153 connector->dpms = mode;
6154
6155 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dcf2013-09-29 19:15:07 +01006156 if (connector->encoder)
6157 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006158
Daniel Vetterb9805142012-08-31 17:37:33 +02006159 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006160}
6161
Daniel Vetterf0947c32012-07-02 13:10:34 +02006162/* Simple connector->get_hw_state implementation for encoders that support only
6163 * one connector and no cloning and hence the encoder state determines the state
6164 * of the connector. */
6165bool intel_connector_get_hw_state(struct intel_connector *connector)
6166{
Daniel Vetter24929352012-07-02 20:28:59 +02006167 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006168 struct intel_encoder *encoder = connector->encoder;
6169
6170 return encoder->get_hw_state(encoder, &pipe);
6171}
6172
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006173static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006174{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006175 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6176 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006177
6178 return 0;
6179}
6180
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006181static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006182 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006183{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006184 struct drm_atomic_state *state = pipe_config->base.state;
6185 struct intel_crtc *other_crtc;
6186 struct intel_crtc_state *other_crtc_state;
6187
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006188 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6189 pipe_name(pipe), pipe_config->fdi_lanes);
6190 if (pipe_config->fdi_lanes > 4) {
6191 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6192 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006193 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006194 }
6195
Paulo Zanonibafb6552013-11-02 21:07:44 -07006196 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006197 if (pipe_config->fdi_lanes > 2) {
6198 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6199 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006200 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006201 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006202 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006203 }
6204 }
6205
6206 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006207 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006208
6209 /* Ivybridge 3 pipe is really complicated */
6210 switch (pipe) {
6211 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006212 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006213 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006214 if (pipe_config->fdi_lanes <= 2)
6215 return 0;
6216
6217 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6218 other_crtc_state =
6219 intel_atomic_get_crtc_state(state, other_crtc);
6220 if (IS_ERR(other_crtc_state))
6221 return PTR_ERR(other_crtc_state);
6222
6223 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006224 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6225 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006226 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006227 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006228 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006229 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006230 if (pipe_config->fdi_lanes > 2) {
6231 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6232 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006233 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006234 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006235
6236 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6237 other_crtc_state =
6238 intel_atomic_get_crtc_state(state, other_crtc);
6239 if (IS_ERR(other_crtc_state))
6240 return PTR_ERR(other_crtc_state);
6241
6242 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006243 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006244 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006245 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006246 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006247 default:
6248 BUG();
6249 }
6250}
6251
Daniel Vettere29c22c2013-02-21 00:00:16 +01006252#define RETRY 1
6253static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006254 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006255{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006256 struct drm_device *dev = intel_crtc->base.dev;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006257 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006258 int lane, link_bw, fdi_dotclock, ret;
6259 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006260
Daniel Vettere29c22c2013-02-21 00:00:16 +01006261retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006262 /* FDI is a binary signal running at ~2.7GHz, encoding
6263 * each output octet as 10 bits. The actual frequency
6264 * is stored as a divider into a 100MHz clock, and the
6265 * mode pixel clock is stored in units of 1KHz.
6266 * Hence the bw of each lane in terms of the mode signal
6267 * is:
6268 */
6269 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6270
Damien Lespiau241bfc32013-09-25 16:45:37 +01006271 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006272
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006273 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006274 pipe_config->pipe_bpp);
6275
6276 pipe_config->fdi_lanes = lane;
6277
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006278 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006279 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006280
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006281 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6282 intel_crtc->pipe, pipe_config);
6283 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006284 pipe_config->pipe_bpp -= 2*3;
6285 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6286 pipe_config->pipe_bpp);
6287 needs_recompute = true;
6288 pipe_config->bw_constrained = true;
6289
6290 goto retry;
6291 }
6292
6293 if (needs_recompute)
6294 return RETRY;
6295
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006296 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006297}
6298
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006299static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006300 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006301{
Jani Nikulad330a952014-01-21 11:24:25 +02006302 pipe_config->ips_enabled = i915.enable_ips &&
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03006303 hsw_crtc_supports_ips(crtc) &&
Jesse Barnesb6dfdc92013-07-25 10:06:50 -07006304 pipe_config->pipe_bpp <= 24;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006305}
6306
Daniel Vettera43f6e02013-06-07 23:10:32 +02006307static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006308 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006309{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006310 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006311 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006312 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chandra Kondurud03c93d2015-04-09 16:42:46 -07006313 int ret;
Chris Wilson89749352010-09-12 18:25:19 +01006314
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006315 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006316 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006317 int clock_limit =
6318 dev_priv->display.get_display_clock_speed(dev);
6319
6320 /*
6321 * Enable pixel doubling when the dot clock
6322 * is > 90% of the (display) core speed.
6323 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03006324 * GDG double wide on either pipe,
6325 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006326 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03006327 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01006328 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006329 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006330 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006331 }
6332
Damien Lespiau241bfc32013-09-25 16:45:37 +01006333 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006334 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006335 }
Chris Wilson89749352010-09-12 18:25:19 +01006336
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006337 /*
6338 * Pipe horizontal size must be even in:
6339 * - DVO ganged mode
6340 * - LVDS dual channel mode
6341 * - Double wide pipe
6342 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006343 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006344 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6345 pipe_config->pipe_src_w &= ~1;
6346
Damien Lespiau8693a822013-05-03 18:48:11 +01006347 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6348 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006349 */
6350 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
6351 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006352 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006353
Damien Lespiauf5adf942013-06-24 18:29:34 +01006354 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006355 hsw_compute_ips_config(crtc, pipe_config);
6356
Daniel Vetter877d48d2013-04-19 11:24:43 +02006357 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006358 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006359
Chandra Kondurud03c93d2015-04-09 16:42:46 -07006360 /* FIXME: remove below call once atomic mode set is place and all crtc
6361 * related checks called from atomic_crtc_check function */
6362 ret = 0;
6363 DRM_DEBUG_KMS("intel_crtc = %p drm_state (pipe_config->base.state) = %p\n",
6364 crtc, pipe_config->base.state);
6365 ret = intel_atomic_setup_scalers(dev, crtc, pipe_config);
6366
6367 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006368}
6369
Ville Syrjälä1652d192015-03-31 14:12:01 +03006370static int skylake_get_display_clock_speed(struct drm_device *dev)
6371{
6372 struct drm_i915_private *dev_priv = to_i915(dev);
6373 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6374 uint32_t cdctl = I915_READ(CDCLK_CTL);
6375 uint32_t linkrate;
6376
6377 if (!(lcpll1 & LCPLL_PLL_ENABLE)) {
6378 WARN(1, "LCPLL1 not enabled\n");
6379 return 24000; /* 24MHz is the cd freq with NSSC ref */
6380 }
6381
6382 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6383 return 540000;
6384
6385 linkrate = (I915_READ(DPLL_CTRL1) &
6386 DPLL_CRTL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
6387
6388 if (linkrate == DPLL_CRTL1_LINK_RATE_2160 ||
6389 linkrate == DPLL_CRTL1_LINK_RATE_1080) {
6390 /* vco 8640 */
6391 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6392 case CDCLK_FREQ_450_432:
6393 return 432000;
6394 case CDCLK_FREQ_337_308:
6395 return 308570;
6396 case CDCLK_FREQ_675_617:
6397 return 617140;
6398 default:
6399 WARN(1, "Unknown cd freq selection\n");
6400 }
6401 } else {
6402 /* vco 8100 */
6403 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6404 case CDCLK_FREQ_450_432:
6405 return 450000;
6406 case CDCLK_FREQ_337_308:
6407 return 337500;
6408 case CDCLK_FREQ_675_617:
6409 return 675000;
6410 default:
6411 WARN(1, "Unknown cd freq selection\n");
6412 }
6413 }
6414
6415 /* error case, do as if DPLL0 isn't enabled */
6416 return 24000;
6417}
6418
6419static int broadwell_get_display_clock_speed(struct drm_device *dev)
6420{
6421 struct drm_i915_private *dev_priv = dev->dev_private;
6422 uint32_t lcpll = I915_READ(LCPLL_CTL);
6423 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6424
6425 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6426 return 800000;
6427 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6428 return 450000;
6429 else if (freq == LCPLL_CLK_FREQ_450)
6430 return 450000;
6431 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6432 return 540000;
6433 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6434 return 337500;
6435 else
6436 return 675000;
6437}
6438
6439static int haswell_get_display_clock_speed(struct drm_device *dev)
6440{
6441 struct drm_i915_private *dev_priv = dev->dev_private;
6442 uint32_t lcpll = I915_READ(LCPLL_CTL);
6443 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6444
6445 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6446 return 800000;
6447 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6448 return 450000;
6449 else if (freq == LCPLL_CLK_FREQ_450)
6450 return 450000;
6451 else if (IS_HSW_ULT(dev))
6452 return 337500;
6453 else
6454 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006455}
6456
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006457static int valleyview_get_display_clock_speed(struct drm_device *dev)
6458{
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006459 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006460 u32 val;
6461 int divider;
6462
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006463 if (dev_priv->hpll_freq == 0)
6464 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
6465
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006466 mutex_lock(&dev_priv->dpio_lock);
6467 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
6468 mutex_unlock(&dev_priv->dpio_lock);
6469
6470 divider = val & DISPLAY_FREQUENCY_VALUES;
6471
Ville Syrjälä7d007f42014-06-13 13:37:53 +03006472 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
6473 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
6474 "cdclk change in progress\n");
6475
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006476 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006477}
6478
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006479static int ilk_get_display_clock_speed(struct drm_device *dev)
6480{
6481 return 450000;
6482}
6483
Jesse Barnese70236a2009-09-21 10:42:27 -07006484static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006485{
Jesse Barnese70236a2009-09-21 10:42:27 -07006486 return 400000;
6487}
Jesse Barnes79e53942008-11-07 14:24:08 -08006488
Jesse Barnese70236a2009-09-21 10:42:27 -07006489static int i915_get_display_clock_speed(struct drm_device *dev)
6490{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006491 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006492}
Jesse Barnes79e53942008-11-07 14:24:08 -08006493
Jesse Barnese70236a2009-09-21 10:42:27 -07006494static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6495{
6496 return 200000;
6497}
Jesse Barnes79e53942008-11-07 14:24:08 -08006498
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006499static int pnv_get_display_clock_speed(struct drm_device *dev)
6500{
6501 u16 gcfgc = 0;
6502
6503 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6504
6505 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6506 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006507 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006508 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006509 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006510 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006511 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006512 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6513 return 200000;
6514 default:
6515 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6516 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006517 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006518 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006519 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006520 }
6521}
6522
Jesse Barnese70236a2009-09-21 10:42:27 -07006523static int i915gm_get_display_clock_speed(struct drm_device *dev)
6524{
6525 u16 gcfgc = 0;
6526
6527 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6528
6529 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006530 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006531 else {
6532 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6533 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006534 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006535 default:
6536 case GC_DISPLAY_CLOCK_190_200_MHZ:
6537 return 190000;
6538 }
6539 }
6540}
Jesse Barnes79e53942008-11-07 14:24:08 -08006541
Jesse Barnese70236a2009-09-21 10:42:27 -07006542static int i865_get_display_clock_speed(struct drm_device *dev)
6543{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006544 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006545}
6546
6547static int i855_get_display_clock_speed(struct drm_device *dev)
6548{
6549 u16 hpllcc = 0;
6550 /* Assume that the hardware is in the high speed state. This
6551 * should be the default.
6552 */
6553 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6554 case GC_CLOCK_133_200:
6555 case GC_CLOCK_100_200:
6556 return 200000;
6557 case GC_CLOCK_166_250:
6558 return 250000;
6559 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006560 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006561 }
6562
6563 /* Shouldn't happen */
6564 return 0;
6565}
6566
6567static int i830_get_display_clock_speed(struct drm_device *dev)
6568{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006569 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006570}
6571
Zhenyu Wang2c072452009-06-05 15:38:42 +08006572static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006573intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006574{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006575 while (*num > DATA_LINK_M_N_MASK ||
6576 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08006577 *num >>= 1;
6578 *den >>= 1;
6579 }
6580}
6581
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006582static void compute_m_n(unsigned int m, unsigned int n,
6583 uint32_t *ret_m, uint32_t *ret_n)
6584{
6585 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
6586 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6587 intel_reduce_m_n_ratio(ret_m, ret_n);
6588}
6589
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006590void
6591intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6592 int pixel_clock, int link_clock,
6593 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006594{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006595 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006596
6597 compute_m_n(bits_per_pixel * pixel_clock,
6598 link_clock * nlanes * 8,
6599 &m_n->gmch_m, &m_n->gmch_n);
6600
6601 compute_m_n(pixel_clock, link_clock,
6602 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08006603}
6604
Chris Wilsona7615032011-01-12 17:04:08 +00006605static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
6606{
Jani Nikulad330a952014-01-21 11:24:25 +02006607 if (i915.panel_use_ssc >= 0)
6608 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006609 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07006610 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00006611}
6612
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006613static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
6614 int num_connectors)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006615{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006616 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006617 struct drm_i915_private *dev_priv = dev->dev_private;
6618 int refclk;
6619
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006620 WARN_ON(!crtc_state->base.state);
6621
Imre Deak5ab7b0b2015-03-06 03:29:25 +02006622 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02006623 refclk = 100000;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006624 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006625 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02006626 refclk = dev_priv->vbt.lvds_ssc_freq;
6627 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006628 } else if (!IS_GEN2(dev)) {
6629 refclk = 96000;
6630 } else {
6631 refclk = 48000;
6632 }
6633
6634 return refclk;
6635}
6636
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006637static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006638{
Daniel Vetter7df00d72013-05-21 21:54:55 +02006639 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006640}
Daniel Vetterf47709a2013-03-28 10:42:02 +01006641
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006642static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
6643{
6644 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006645}
6646
Daniel Vetterf47709a2013-03-28 10:42:02 +01006647static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006648 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08006649 intel_clock_t *reduced_clock)
6650{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006651 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006652 u32 fp, fp2 = 0;
6653
6654 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006655 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006656 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006657 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006658 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006659 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006660 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006661 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006662 }
6663
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006664 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006665
Daniel Vetterf47709a2013-03-28 10:42:02 +01006666 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006667 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07006668 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006669 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01006670 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006671 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006672 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006673 }
6674}
6675
Chon Ming Lee5e69f972013-09-05 20:41:49 +08006676static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
6677 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07006678{
6679 u32 reg_val;
6680
6681 /*
6682 * PLLB opamp always calibrates to max value of 0x3f, force enable it
6683 * and set it to a reasonable value instead.
6684 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006685 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006686 reg_val &= 0xffffff00;
6687 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006688 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006689
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006690 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006691 reg_val &= 0x8cffffff;
6692 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006693 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006694
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006695 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006696 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006697 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006698
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006699 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006700 reg_val &= 0x00ffffff;
6701 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006702 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006703}
6704
Daniel Vetterb5518422013-05-03 11:49:48 +02006705static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
6706 struct intel_link_m_n *m_n)
6707{
6708 struct drm_device *dev = crtc->base.dev;
6709 struct drm_i915_private *dev_priv = dev->dev_private;
6710 int pipe = crtc->pipe;
6711
Daniel Vettere3b95f12013-05-03 11:49:49 +02006712 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6713 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
6714 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
6715 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02006716}
6717
6718static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07006719 struct intel_link_m_n *m_n,
6720 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02006721{
6722 struct drm_device *dev = crtc->base.dev;
6723 struct drm_i915_private *dev_priv = dev->dev_private;
6724 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006725 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02006726
6727 if (INTEL_INFO(dev)->gen >= 5) {
6728 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
6729 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
6730 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
6731 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07006732 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
6733 * for gen < 8) and if DRRS is supported (to make sure the
6734 * registers are not unnecessarily accessed).
6735 */
Durgadoss R44395bf2015-02-13 15:33:02 +05306736 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006737 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07006738 I915_WRITE(PIPE_DATA_M2(transcoder),
6739 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
6740 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
6741 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
6742 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
6743 }
Daniel Vetterb5518422013-05-03 11:49:48 +02006744 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02006745 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6746 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
6747 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
6748 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02006749 }
6750}
6751
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306752void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006753{
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306754 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
6755
6756 if (m_n == M1_N1) {
6757 dp_m_n = &crtc->config->dp_m_n;
6758 dp_m2_n2 = &crtc->config->dp_m2_n2;
6759 } else if (m_n == M2_N2) {
6760
6761 /*
6762 * M2_N2 registers are not supported. Hence m2_n2 divider value
6763 * needs to be programmed into M1_N1.
6764 */
6765 dp_m_n = &crtc->config->dp_m2_n2;
6766 } else {
6767 DRM_ERROR("Unsupported divider value\n");
6768 return;
6769 }
6770
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006771 if (crtc->config->has_pch_encoder)
6772 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006773 else
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306774 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006775}
6776
Ville Syrjäläd288f652014-10-28 13:20:22 +02006777static void vlv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006778 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006779{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006780 u32 dpll, dpll_md;
6781
6782 /*
6783 * Enable DPIO clock input. We should never disable the reference
6784 * clock for pipe B, since VGA hotplug / manual detection depends
6785 * on it.
6786 */
6787 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
6788 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
6789 /* We should never disable this, set it here for state tracking */
6790 if (crtc->pipe == PIPE_B)
6791 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
6792 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02006793 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006794
Ville Syrjäläd288f652014-10-28 13:20:22 +02006795 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006796 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02006797 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006798}
6799
Ville Syrjäläd288f652014-10-28 13:20:22 +02006800static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006801 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006802{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006803 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006804 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01006805 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006806 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006807 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006808 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006809
Daniel Vetter09153002012-12-12 14:06:44 +01006810 mutex_lock(&dev_priv->dpio_lock);
6811
Ville Syrjäläd288f652014-10-28 13:20:22 +02006812 bestn = pipe_config->dpll.n;
6813 bestm1 = pipe_config->dpll.m1;
6814 bestm2 = pipe_config->dpll.m2;
6815 bestp1 = pipe_config->dpll.p1;
6816 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006817
Jesse Barnes89b667f2013-04-18 14:51:36 -07006818 /* See eDP HDMI DPIO driver vbios notes doc */
6819
6820 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006821 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08006822 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006823
6824 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006825 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006826
6827 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006828 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006829 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006830 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006831
6832 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006833 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006834
6835 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006836 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
6837 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
6838 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006839 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07006840
6841 /*
6842 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
6843 * but we don't support that).
6844 * Note: don't use the DAC post divider as it seems unstable.
6845 */
6846 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006847 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006848
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006849 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006850 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006851
Jesse Barnes89b667f2013-04-18 14:51:36 -07006852 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02006853 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006854 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
6855 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006856 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03006857 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006858 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006859 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006860 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006861
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02006862 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07006863 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006864 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006865 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006866 0x0df40000);
6867 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006868 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006869 0x0df70000);
6870 } else { /* HDMI or VGA */
6871 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006872 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006873 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006874 0x0df70000);
6875 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006876 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006877 0x0df40000);
6878 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006879
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006880 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006881 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006882 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
6883 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006884 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006885 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006886
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006887 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Daniel Vetter09153002012-12-12 14:06:44 +01006888 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006889}
6890
Ville Syrjäläd288f652014-10-28 13:20:22 +02006891static void chv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006892 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006893{
Ville Syrjäläd288f652014-10-28 13:20:22 +02006894 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006895 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
6896 DPLL_VCO_ENABLE;
6897 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02006898 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006899
Ville Syrjäläd288f652014-10-28 13:20:22 +02006900 pipe_config->dpll_hw_state.dpll_md =
6901 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006902}
6903
Ville Syrjäläd288f652014-10-28 13:20:22 +02006904static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006905 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006906{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006907 struct drm_device *dev = crtc->base.dev;
6908 struct drm_i915_private *dev_priv = dev->dev_private;
6909 int pipe = crtc->pipe;
6910 int dpll_reg = DPLL(crtc->pipe);
6911 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306912 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006913 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05306914 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306915 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006916
Ville Syrjäläd288f652014-10-28 13:20:22 +02006917 bestn = pipe_config->dpll.n;
6918 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
6919 bestm1 = pipe_config->dpll.m1;
6920 bestm2 = pipe_config->dpll.m2 >> 22;
6921 bestp1 = pipe_config->dpll.p1;
6922 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306923 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05306924 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306925 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006926
6927 /*
6928 * Enable Refclk and SSC
6929 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03006930 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02006931 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03006932
6933 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006934
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006935 /* p1 and p2 divider */
6936 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
6937 5 << DPIO_CHV_S1_DIV_SHIFT |
6938 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
6939 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
6940 1 << DPIO_CHV_K_DIV_SHIFT);
6941
6942 /* Feedback post-divider - m2 */
6943 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
6944
6945 /* Feedback refclk divider - n and m1 */
6946 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
6947 DPIO_CHV_M1_DIV_BY_2 |
6948 1 << DPIO_CHV_N_DIV_SHIFT);
6949
6950 /* M2 fraction division */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05306951 if (bestm2_frac)
6952 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006953
6954 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05306955 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
6956 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
6957 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
6958 if (bestm2_frac)
6959 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
6960 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006961
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05306962 /* Program digital lock detect threshold */
6963 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
6964 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
6965 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
6966 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
6967 if (!bestm2_frac)
6968 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
6969 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
6970
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006971 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306972 if (vco == 5400000) {
6973 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
6974 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
6975 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
6976 tribuf_calcntr = 0x9;
6977 } else if (vco <= 6200000) {
6978 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
6979 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
6980 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6981 tribuf_calcntr = 0x9;
6982 } else if (vco <= 6480000) {
6983 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
6984 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
6985 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6986 tribuf_calcntr = 0x8;
6987 } else {
6988 /* Not supported. Apply the same limits as in the max case */
6989 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
6990 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
6991 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6992 tribuf_calcntr = 0;
6993 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006994 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
6995
Ville Syrjälä968040b2015-03-11 22:52:08 +02006996 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306997 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
6998 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
6999 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7000
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007001 /* AFC Recal */
7002 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7003 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7004 DPIO_AFC_RECAL);
7005
7006 mutex_unlock(&dev_priv->dpio_lock);
7007}
7008
Ville Syrjäläd288f652014-10-28 13:20:22 +02007009/**
7010 * vlv_force_pll_on - forcibly enable just the PLL
7011 * @dev_priv: i915 private structure
7012 * @pipe: pipe PLL to enable
7013 * @dpll: PLL configuration
7014 *
7015 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7016 * in cases where we need the PLL enabled even when @pipe is not going to
7017 * be enabled.
7018 */
7019void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7020 const struct dpll *dpll)
7021{
7022 struct intel_crtc *crtc =
7023 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007024 struct intel_crtc_state pipe_config = {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007025 .base.crtc = &crtc->base,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007026 .pixel_multiplier = 1,
7027 .dpll = *dpll,
7028 };
7029
7030 if (IS_CHERRYVIEW(dev)) {
7031 chv_update_pll(crtc, &pipe_config);
7032 chv_prepare_pll(crtc, &pipe_config);
7033 chv_enable_pll(crtc, &pipe_config);
7034 } else {
7035 vlv_update_pll(crtc, &pipe_config);
7036 vlv_prepare_pll(crtc, &pipe_config);
7037 vlv_enable_pll(crtc, &pipe_config);
7038 }
7039}
7040
7041/**
7042 * vlv_force_pll_off - forcibly disable just the PLL
7043 * @dev_priv: i915 private structure
7044 * @pipe: pipe PLL to disable
7045 *
7046 * Disable the PLL for @pipe. To be used in cases where we need
7047 * the PLL enabled even when @pipe is not going to be enabled.
7048 */
7049void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7050{
7051 if (IS_CHERRYVIEW(dev))
7052 chv_disable_pll(to_i915(dev), pipe);
7053 else
7054 vlv_disable_pll(to_i915(dev), pipe);
7055}
7056
Daniel Vetterf47709a2013-03-28 10:42:02 +01007057static void i9xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007058 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01007059 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007060 int num_connectors)
7061{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007062 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007063 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007064 u32 dpll;
7065 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007066 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007067
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007068 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307069
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007070 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7071 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007072
7073 dpll = DPLL_VGA_MODE_DIS;
7074
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007075 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007076 dpll |= DPLLB_MODE_LVDS;
7077 else
7078 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007079
Daniel Vetteref1b4602013-06-01 17:17:04 +02007080 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007081 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007082 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007083 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007084
7085 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007086 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007087
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007088 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007089 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007090
7091 /* compute bitmask from p1 value */
7092 if (IS_PINEVIEW(dev))
7093 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7094 else {
7095 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7096 if (IS_G4X(dev) && reduced_clock)
7097 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7098 }
7099 switch (clock->p2) {
7100 case 5:
7101 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7102 break;
7103 case 7:
7104 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7105 break;
7106 case 10:
7107 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7108 break;
7109 case 14:
7110 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7111 break;
7112 }
7113 if (INTEL_INFO(dev)->gen >= 4)
7114 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7115
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007116 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007117 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007118 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007119 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7120 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7121 else
7122 dpll |= PLL_REF_INPUT_DREFCLK;
7123
7124 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007125 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007126
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007127 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007128 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007129 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007130 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007131 }
7132}
7133
Daniel Vetterf47709a2013-03-28 10:42:02 +01007134static void i8xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007135 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01007136 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007137 int num_connectors)
7138{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007139 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007140 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007141 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007142 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007143
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007144 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307145
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007146 dpll = DPLL_VGA_MODE_DIS;
7147
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007148 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007149 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7150 } else {
7151 if (clock->p1 == 2)
7152 dpll |= PLL_P1_DIVIDE_BY_TWO;
7153 else
7154 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7155 if (clock->p2 == 4)
7156 dpll |= PLL_P2_DIVIDE_BY_4;
7157 }
7158
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007159 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007160 dpll |= DPLL_DVO_2X_MODE;
7161
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007162 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007163 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7164 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7165 else
7166 dpll |= PLL_REF_INPUT_DREFCLK;
7167
7168 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007169 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007170}
7171
Daniel Vetter8a654f32013-06-01 17:16:22 +02007172static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007173{
7174 struct drm_device *dev = intel_crtc->base.dev;
7175 struct drm_i915_private *dev_priv = dev->dev_private;
7176 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007177 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02007178 struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007179 &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007180 uint32_t crtc_vtotal, crtc_vblank_end;
7181 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007182
7183 /* We need to be careful not to changed the adjusted mode, for otherwise
7184 * the hw state checker will get angry at the mismatch. */
7185 crtc_vtotal = adjusted_mode->crtc_vtotal;
7186 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007187
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007188 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007189 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007190 crtc_vtotal -= 1;
7191 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007192
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007193 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007194 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7195 else
7196 vsyncshift = adjusted_mode->crtc_hsync_start -
7197 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007198 if (vsyncshift < 0)
7199 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007200 }
7201
7202 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007203 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007204
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007205 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007206 (adjusted_mode->crtc_hdisplay - 1) |
7207 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007208 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007209 (adjusted_mode->crtc_hblank_start - 1) |
7210 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007211 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007212 (adjusted_mode->crtc_hsync_start - 1) |
7213 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7214
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007215 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007216 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007217 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007218 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007219 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007220 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007221 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007222 (adjusted_mode->crtc_vsync_start - 1) |
7223 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7224
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007225 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7226 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7227 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7228 * bits. */
7229 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7230 (pipe == PIPE_B || pipe == PIPE_C))
7231 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7232
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007233 /* pipesrc controls the size that is scaled from, which should
7234 * always be the user's requested size.
7235 */
7236 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007237 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7238 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007239}
7240
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007241static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007242 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007243{
7244 struct drm_device *dev = crtc->base.dev;
7245 struct drm_i915_private *dev_priv = dev->dev_private;
7246 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7247 uint32_t tmp;
7248
7249 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007250 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7251 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007252 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007253 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7254 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007255 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007256 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7257 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007258
7259 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007260 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7261 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007262 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007263 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7264 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007265 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007266 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7267 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007268
7269 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007270 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7271 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7272 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007273 }
7274
7275 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007276 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7277 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7278
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007279 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7280 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007281}
7282
Daniel Vetterf6a83282014-02-11 15:28:57 -08007283void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007284 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007285{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007286 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7287 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7288 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7289 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007290
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007291 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7292 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7293 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7294 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007295
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007296 mode->flags = pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007297
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007298 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7299 mode->flags |= pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007300}
7301
Daniel Vetter84b046f2013-02-19 18:48:54 +01007302static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7303{
7304 struct drm_device *dev = intel_crtc->base.dev;
7305 struct drm_i915_private *dev_priv = dev->dev_private;
7306 uint32_t pipeconf;
7307
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007308 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007309
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007310 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7311 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7312 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007313
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007314 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007315 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007316
Daniel Vetterff9ce462013-04-24 14:57:17 +02007317 /* only g4x and later have fancy bpc/dither controls */
7318 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007319 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007320 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007321 pipeconf |= PIPECONF_DITHER_EN |
7322 PIPECONF_DITHER_TYPE_SP;
7323
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007324 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007325 case 18:
7326 pipeconf |= PIPECONF_6BPC;
7327 break;
7328 case 24:
7329 pipeconf |= PIPECONF_8BPC;
7330 break;
7331 case 30:
7332 pipeconf |= PIPECONF_10BPC;
7333 break;
7334 default:
7335 /* Case prevented by intel_choose_pipe_bpp_dither. */
7336 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007337 }
7338 }
7339
7340 if (HAS_PIPE_CXSR(dev)) {
7341 if (intel_crtc->lowfreq_avail) {
7342 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7343 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7344 } else {
7345 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007346 }
7347 }
7348
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007349 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007350 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007351 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007352 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7353 else
7354 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7355 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007356 pipeconf |= PIPECONF_PROGRESSIVE;
7357
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007358 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007359 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007360
Daniel Vetter84b046f2013-02-19 18:48:54 +01007361 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7362 POSTING_READ(PIPECONF(intel_crtc->pipe));
7363}
7364
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007365static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7366 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007367{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007368 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007369 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc751ce42010-03-25 11:48:48 -07007370 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07007371 intel_clock_t clock, reduced_clock;
Daniel Vettera16af722013-04-30 14:01:44 +02007372 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007373 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01007374 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08007375 const intel_limit_t *limit;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007376 struct drm_atomic_state *state = crtc_state->base.state;
7377 struct drm_connector_state *connector_state;
7378 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -08007379
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007380 for (i = 0; i < state->num_connector; i++) {
7381 if (!state->connectors[i])
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02007382 continue;
7383
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007384 connector_state = state->connector_states[i];
7385 if (connector_state->crtc != &crtc->base)
7386 continue;
7387
7388 encoder = to_intel_encoder(connector_state->best_encoder);
7389
Chris Wilson5eddb702010-09-11 13:48:45 +01007390 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007391 case INTEL_OUTPUT_LVDS:
7392 is_lvds = true;
7393 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007394 case INTEL_OUTPUT_DSI:
7395 is_dsi = true;
7396 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007397 default:
7398 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007399 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05007400
Eric Anholtc751ce42010-03-25 11:48:48 -07007401 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08007402 }
7403
Jani Nikulaf2335332013-09-13 11:03:09 +03007404 if (is_dsi)
Daniel Vetter5b18e572014-04-24 23:55:06 +02007405 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007406
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007407 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007408 refclk = i9xx_get_refclk(crtc_state, num_connectors);
Jani Nikulaf2335332013-09-13 11:03:09 +03007409
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007410 /*
7411 * Returns a set of divisors for the desired target clock with
7412 * the given refclk, or FALSE. The returned values represent
7413 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7414 * 2) / p1 / p2.
7415 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007416 limit = intel_limit(crtc_state, refclk);
7417 ok = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007418 crtc_state->port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007419 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03007420 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007421 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7422 return -EINVAL;
7423 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007424
Jani Nikulaf2335332013-09-13 11:03:09 +03007425 if (is_lvds && dev_priv->lvds_downclock_avail) {
7426 /*
7427 * Ensure we match the reduced clock's P to the target
7428 * clock. If the clocks don't match, we can't switch
7429 * the display clock by using the FP0/FP1. In such case
7430 * we will disable the LVDS downclock feature.
7431 */
7432 has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007433 dev_priv->display.find_dpll(limit, crtc_state,
Jani Nikulaf2335332013-09-13 11:03:09 +03007434 dev_priv->lvds_downclock,
7435 refclk, &clock,
7436 &reduced_clock);
7437 }
7438 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007439 crtc_state->dpll.n = clock.n;
7440 crtc_state->dpll.m1 = clock.m1;
7441 crtc_state->dpll.m2 = clock.m2;
7442 crtc_state->dpll.p1 = clock.p1;
7443 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007444 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007445
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007446 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007447 i8xx_update_pll(crtc, crtc_state,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307448 has_reduced_clock ? &reduced_clock : NULL,
7449 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007450 } else if (IS_CHERRYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007451 chv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007452 } else if (IS_VALLEYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007453 vlv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007454 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007455 i9xx_update_pll(crtc, crtc_state,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007456 has_reduced_clock ? &reduced_clock : NULL,
Robin Schroereba905b2014-05-18 02:24:50 +02007457 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007458 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007459
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007460 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007461}
7462
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007463static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007464 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007465{
7466 struct drm_device *dev = crtc->base.dev;
7467 struct drm_i915_private *dev_priv = dev->dev_private;
7468 uint32_t tmp;
7469
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007470 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7471 return;
7472
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007473 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007474 if (!(tmp & PFIT_ENABLE))
7475 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007476
Daniel Vetter06922822013-07-11 13:35:40 +02007477 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007478 if (INTEL_INFO(dev)->gen < 4) {
7479 if (crtc->pipe != PIPE_B)
7480 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007481 } else {
7482 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7483 return;
7484 }
7485
Daniel Vetter06922822013-07-11 13:35:40 +02007486 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007487 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7488 if (INTEL_INFO(dev)->gen < 5)
7489 pipe_config->gmch_pfit.lvds_border_bits =
7490 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7491}
7492
Jesse Barnesacbec812013-09-20 11:29:32 -07007493static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007494 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07007495{
7496 struct drm_device *dev = crtc->base.dev;
7497 struct drm_i915_private *dev_priv = dev->dev_private;
7498 int pipe = pipe_config->cpu_transcoder;
7499 intel_clock_t clock;
7500 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07007501 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07007502
Shobhit Kumarf573de52014-07-30 20:32:37 +05307503 /* In case of MIPI DPLL will not even be used */
7504 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7505 return;
7506
Jesse Barnesacbec812013-09-20 11:29:32 -07007507 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007508 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Jesse Barnesacbec812013-09-20 11:29:32 -07007509 mutex_unlock(&dev_priv->dpio_lock);
7510
7511 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7512 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7513 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7514 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7515 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7516
Ville Syrjäläf6466282013-10-14 14:50:31 +03007517 vlv_clock(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007518
Ville Syrjäläf6466282013-10-14 14:50:31 +03007519 /* clock.dot is the fast clock */
7520 pipe_config->port_clock = clock.dot / 5;
Jesse Barnesacbec812013-09-20 11:29:32 -07007521}
7522
Damien Lespiau5724dbd2015-01-20 12:51:52 +00007523static void
7524i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7525 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007526{
7527 struct drm_device *dev = crtc->base.dev;
7528 struct drm_i915_private *dev_priv = dev->dev_private;
7529 u32 val, base, offset;
7530 int pipe = crtc->pipe, plane = crtc->plane;
7531 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00007532 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007533 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00007534 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007535
Damien Lespiau42a7b082015-02-05 19:35:13 +00007536 val = I915_READ(DSPCNTR(plane));
7537 if (!(val & DISPLAY_PLANE_ENABLE))
7538 return;
7539
Damien Lespiaud9806c92015-01-21 14:07:19 +00007540 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00007541 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007542 DRM_DEBUG_KMS("failed to alloc fb\n");
7543 return;
7544 }
7545
Damien Lespiau1b842c82015-01-21 13:50:54 +00007546 fb = &intel_fb->base;
7547
Daniel Vetter18c52472015-02-10 17:16:09 +00007548 if (INTEL_INFO(dev)->gen >= 4) {
7549 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007550 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00007551 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
7552 }
7553 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007554
7555 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00007556 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007557 fb->pixel_format = fourcc;
7558 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007559
7560 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007561 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007562 offset = I915_READ(DSPTILEOFF(plane));
7563 else
7564 offset = I915_READ(DSPLINOFF(plane));
7565 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7566 } else {
7567 base = I915_READ(DSPADDR(plane));
7568 }
7569 plane_config->base = base;
7570
7571 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007572 fb->width = ((val >> 16) & 0xfff) + 1;
7573 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007574
7575 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007576 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007577
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007578 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00007579 fb->pixel_format,
7580 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007581
Daniel Vetterf37b5c22015-02-10 23:12:27 +01007582 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007583
Damien Lespiau2844a922015-01-20 12:51:48 +00007584 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7585 pipe_name(pipe), plane, fb->width, fb->height,
7586 fb->bits_per_pixel, base, fb->pitches[0],
7587 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007588
Damien Lespiau2d140302015-02-05 17:22:18 +00007589 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007590}
7591
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007592static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007593 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007594{
7595 struct drm_device *dev = crtc->base.dev;
7596 struct drm_i915_private *dev_priv = dev->dev_private;
7597 int pipe = pipe_config->cpu_transcoder;
7598 enum dpio_channel port = vlv_pipe_to_channel(pipe);
7599 intel_clock_t clock;
7600 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
7601 int refclk = 100000;
7602
7603 mutex_lock(&dev_priv->dpio_lock);
7604 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
7605 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
7606 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
7607 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
7608 mutex_unlock(&dev_priv->dpio_lock);
7609
7610 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
7611 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
7612 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
7613 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
7614 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
7615
7616 chv_clock(refclk, &clock);
7617
7618 /* clock.dot is the fast clock */
7619 pipe_config->port_clock = clock.dot / 5;
7620}
7621
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007622static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007623 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007624{
7625 struct drm_device *dev = crtc->base.dev;
7626 struct drm_i915_private *dev_priv = dev->dev_private;
7627 uint32_t tmp;
7628
Daniel Vetterf458ebb2014-09-30 10:56:39 +02007629 if (!intel_display_power_is_enabled(dev_priv,
7630 POWER_DOMAIN_PIPE(crtc->pipe)))
Imre Deakb5482bd2014-03-05 16:20:55 +02007631 return false;
7632
Daniel Vettere143a212013-07-04 12:01:15 +02007633 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007634 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02007635
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007636 tmp = I915_READ(PIPECONF(crtc->pipe));
7637 if (!(tmp & PIPECONF_ENABLE))
7638 return false;
7639
Ville Syrjälä42571ae2013-09-06 23:29:00 +03007640 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
7641 switch (tmp & PIPECONF_BPC_MASK) {
7642 case PIPECONF_6BPC:
7643 pipe_config->pipe_bpp = 18;
7644 break;
7645 case PIPECONF_8BPC:
7646 pipe_config->pipe_bpp = 24;
7647 break;
7648 case PIPECONF_10BPC:
7649 pipe_config->pipe_bpp = 30;
7650 break;
7651 default:
7652 break;
7653 }
7654 }
7655
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02007656 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
7657 pipe_config->limited_color_range = true;
7658
Ville Syrjälä282740f2013-09-04 18:30:03 +03007659 if (INTEL_INFO(dev)->gen < 4)
7660 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
7661
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007662 intel_get_pipe_timings(crtc, pipe_config);
7663
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007664 i9xx_get_pfit_config(crtc, pipe_config);
7665
Daniel Vetter6c49f242013-06-06 12:45:25 +02007666 if (INTEL_INFO(dev)->gen >= 4) {
7667 tmp = I915_READ(DPLL_MD(crtc->pipe));
7668 pipe_config->pixel_multiplier =
7669 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
7670 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007671 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02007672 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
7673 tmp = I915_READ(DPLL(crtc->pipe));
7674 pipe_config->pixel_multiplier =
7675 ((tmp & SDVO_MULTIPLIER_MASK)
7676 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
7677 } else {
7678 /* Note that on i915G/GM the pixel multiplier is in the sdvo
7679 * port and will be fixed up in the encoder->get_config
7680 * function. */
7681 pipe_config->pixel_multiplier = 1;
7682 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007683 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
7684 if (!IS_VALLEYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03007685 /*
7686 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
7687 * on 830. Filter it out here so that we don't
7688 * report errors due to that.
7689 */
7690 if (IS_I830(dev))
7691 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
7692
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007693 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
7694 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03007695 } else {
7696 /* Mask out read-only status bits. */
7697 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
7698 DPLL_PORTC_READY_MASK |
7699 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007700 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02007701
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007702 if (IS_CHERRYVIEW(dev))
7703 chv_crtc_clock_get(crtc, pipe_config);
7704 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07007705 vlv_crtc_clock_get(crtc, pipe_config);
7706 else
7707 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03007708
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007709 return true;
7710}
7711
Paulo Zanonidde86e22012-12-01 12:04:25 -02007712static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07007713{
7714 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007715 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007716 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007717 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07007718 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07007719 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07007720 bool has_ck505 = false;
7721 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007722
7723 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01007724 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07007725 switch (encoder->type) {
7726 case INTEL_OUTPUT_LVDS:
7727 has_panel = true;
7728 has_lvds = true;
7729 break;
7730 case INTEL_OUTPUT_EDP:
7731 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03007732 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07007733 has_cpu_edp = true;
7734 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007735 default:
7736 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007737 }
7738 }
7739
Keith Packard99eb6a02011-09-26 14:29:12 -07007740 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007741 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07007742 can_ssc = has_ck505;
7743 } else {
7744 has_ck505 = false;
7745 can_ssc = true;
7746 }
7747
Imre Deak2de69052013-05-08 13:14:04 +03007748 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
7749 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007750
7751 /* Ironlake: try to setup display ref clock before DPLL
7752 * enabling. This is only under driver's control after
7753 * PCH B stepping, previous chipset stepping should be
7754 * ignoring this setting.
7755 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007756 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007757
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007758 /* As we must carefully and slowly disable/enable each source in turn,
7759 * compute the final state we want first and check if we need to
7760 * make any changes at all.
7761 */
7762 final = val;
7763 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07007764 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007765 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07007766 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007767 final |= DREF_NONSPREAD_SOURCE_ENABLE;
7768
7769 final &= ~DREF_SSC_SOURCE_MASK;
7770 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
7771 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007772
Keith Packard199e5d72011-09-22 12:01:57 -07007773 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007774 final |= DREF_SSC_SOURCE_ENABLE;
7775
7776 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7777 final |= DREF_SSC1_ENABLE;
7778
7779 if (has_cpu_edp) {
7780 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7781 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
7782 else
7783 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
7784 } else
7785 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
7786 } else {
7787 final |= DREF_SSC_SOURCE_DISABLE;
7788 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
7789 }
7790
7791 if (final == val)
7792 return;
7793
7794 /* Always enable nonspread source */
7795 val &= ~DREF_NONSPREAD_SOURCE_MASK;
7796
7797 if (has_ck505)
7798 val |= DREF_NONSPREAD_CK505_ENABLE;
7799 else
7800 val |= DREF_NONSPREAD_SOURCE_ENABLE;
7801
7802 if (has_panel) {
7803 val &= ~DREF_SSC_SOURCE_MASK;
7804 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007805
Keith Packard199e5d72011-09-22 12:01:57 -07007806 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07007807 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07007808 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007809 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02007810 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007811 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007812
7813 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007814 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007815 POSTING_READ(PCH_DREF_CONTROL);
7816 udelay(200);
7817
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007818 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007819
7820 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07007821 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07007822 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07007823 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007824 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02007825 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007826 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07007827 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007828 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007829
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007830 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007831 POSTING_READ(PCH_DREF_CONTROL);
7832 udelay(200);
7833 } else {
7834 DRM_DEBUG_KMS("Disabling SSC entirely\n");
7835
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007836 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07007837
7838 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007839 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007840
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007841 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007842 POSTING_READ(PCH_DREF_CONTROL);
7843 udelay(200);
7844
7845 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007846 val &= ~DREF_SSC_SOURCE_MASK;
7847 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007848
7849 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007850 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007851
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007852 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007853 POSTING_READ(PCH_DREF_CONTROL);
7854 udelay(200);
7855 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007856
7857 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007858}
7859
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007860static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02007861{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007862 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02007863
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007864 tmp = I915_READ(SOUTH_CHICKEN2);
7865 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
7866 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007867
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007868 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
7869 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
7870 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02007871
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007872 tmp = I915_READ(SOUTH_CHICKEN2);
7873 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
7874 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007875
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007876 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
7877 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
7878 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007879}
7880
7881/* WaMPhyProgramming:hsw */
7882static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
7883{
7884 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02007885
7886 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
7887 tmp &= ~(0xFF << 24);
7888 tmp |= (0x12 << 24);
7889 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
7890
Paulo Zanonidde86e22012-12-01 12:04:25 -02007891 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
7892 tmp |= (1 << 11);
7893 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
7894
7895 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
7896 tmp |= (1 << 11);
7897 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
7898
Paulo Zanonidde86e22012-12-01 12:04:25 -02007899 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
7900 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7901 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
7902
7903 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
7904 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7905 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
7906
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007907 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
7908 tmp &= ~(7 << 13);
7909 tmp |= (5 << 13);
7910 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007911
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007912 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
7913 tmp &= ~(7 << 13);
7914 tmp |= (5 << 13);
7915 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007916
7917 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
7918 tmp &= ~0xFF;
7919 tmp |= 0x1C;
7920 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
7921
7922 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
7923 tmp &= ~0xFF;
7924 tmp |= 0x1C;
7925 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
7926
7927 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
7928 tmp &= ~(0xFF << 16);
7929 tmp |= (0x1C << 16);
7930 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
7931
7932 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
7933 tmp &= ~(0xFF << 16);
7934 tmp |= (0x1C << 16);
7935 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
7936
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007937 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
7938 tmp |= (1 << 27);
7939 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007940
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007941 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
7942 tmp |= (1 << 27);
7943 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007944
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007945 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
7946 tmp &= ~(0xF << 28);
7947 tmp |= (4 << 28);
7948 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007949
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007950 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
7951 tmp &= ~(0xF << 28);
7952 tmp |= (4 << 28);
7953 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007954}
7955
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007956/* Implements 3 different sequences from BSpec chapter "Display iCLK
7957 * Programming" based on the parameters passed:
7958 * - Sequence to enable CLKOUT_DP
7959 * - Sequence to enable CLKOUT_DP without spread
7960 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
7961 */
7962static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
7963 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007964{
7965 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007966 uint32_t reg, tmp;
7967
7968 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
7969 with_spread = true;
7970 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
7971 with_fdi, "LP PCH doesn't have FDI\n"))
7972 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007973
7974 mutex_lock(&dev_priv->dpio_lock);
7975
7976 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7977 tmp &= ~SBI_SSCCTL_DISABLE;
7978 tmp |= SBI_SSCCTL_PATHALT;
7979 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7980
7981 udelay(24);
7982
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007983 if (with_spread) {
7984 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7985 tmp &= ~SBI_SSCCTL_PATHALT;
7986 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007987
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007988 if (with_fdi) {
7989 lpt_reset_fdi_mphy(dev_priv);
7990 lpt_program_fdi_mphy(dev_priv);
7991 }
7992 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02007993
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007994 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
7995 SBI_GEN0 : SBI_DBUFF0;
7996 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
7997 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
7998 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01007999
8000 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008001}
8002
Paulo Zanoni47701c32013-07-23 11:19:25 -03008003/* Sequence to disable CLKOUT_DP */
8004static void lpt_disable_clkout_dp(struct drm_device *dev)
8005{
8006 struct drm_i915_private *dev_priv = dev->dev_private;
8007 uint32_t reg, tmp;
8008
8009 mutex_lock(&dev_priv->dpio_lock);
8010
8011 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8012 SBI_GEN0 : SBI_DBUFF0;
8013 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8014 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8015 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8016
8017 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8018 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8019 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8020 tmp |= SBI_SSCCTL_PATHALT;
8021 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8022 udelay(32);
8023 }
8024 tmp |= SBI_SSCCTL_DISABLE;
8025 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8026 }
8027
8028 mutex_unlock(&dev_priv->dpio_lock);
8029}
8030
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008031static void lpt_init_pch_refclk(struct drm_device *dev)
8032{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008033 struct intel_encoder *encoder;
8034 bool has_vga = false;
8035
Damien Lespiaub2784e12014-08-05 11:29:37 +01008036 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008037 switch (encoder->type) {
8038 case INTEL_OUTPUT_ANALOG:
8039 has_vga = true;
8040 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008041 default:
8042 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008043 }
8044 }
8045
Paulo Zanoni47701c32013-07-23 11:19:25 -03008046 if (has_vga)
8047 lpt_enable_clkout_dp(dev, true, true);
8048 else
8049 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008050}
8051
Paulo Zanonidde86e22012-12-01 12:04:25 -02008052/*
8053 * Initialize reference clocks when the driver loads
8054 */
8055void intel_init_pch_refclk(struct drm_device *dev)
8056{
8057 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8058 ironlake_init_pch_refclk(dev);
8059 else if (HAS_PCH_LPT(dev))
8060 lpt_init_pch_refclk(dev);
8061}
8062
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008063static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008064{
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008065 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008066 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008067 struct drm_atomic_state *state = crtc_state->base.state;
8068 struct drm_connector_state *connector_state;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008069 struct intel_encoder *encoder;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008070 int num_connectors = 0, i;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008071 bool is_lvds = false;
8072
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008073 for (i = 0; i < state->num_connector; i++) {
8074 if (!state->connectors[i])
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02008075 continue;
8076
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008077 connector_state = state->connector_states[i];
8078 if (connector_state->crtc != crtc_state->base.crtc)
8079 continue;
8080
8081 encoder = to_intel_encoder(connector_state->best_encoder);
8082
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008083 switch (encoder->type) {
8084 case INTEL_OUTPUT_LVDS:
8085 is_lvds = true;
8086 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008087 default:
8088 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008089 }
8090 num_connectors++;
8091 }
8092
8093 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008094 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008095 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008096 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008097 }
8098
8099 return 120000;
8100}
8101
Daniel Vetter6ff93602013-04-19 11:24:36 +02008102static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008103{
8104 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8105 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8106 int pipe = intel_crtc->pipe;
8107 uint32_t val;
8108
Daniel Vetter78114072013-06-13 00:54:57 +02008109 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008110
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008111 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008112 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008113 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008114 break;
8115 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008116 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008117 break;
8118 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008119 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008120 break;
8121 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008122 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008123 break;
8124 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008125 /* Case prevented by intel_choose_pipe_bpp_dither. */
8126 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008127 }
8128
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008129 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008130 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8131
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008132 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008133 val |= PIPECONF_INTERLACED_ILK;
8134 else
8135 val |= PIPECONF_PROGRESSIVE;
8136
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008137 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008138 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008139
Paulo Zanonic8203562012-09-12 10:06:29 -03008140 I915_WRITE(PIPECONF(pipe), val);
8141 POSTING_READ(PIPECONF(pipe));
8142}
8143
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008144/*
8145 * Set up the pipe CSC unit.
8146 *
8147 * Currently only full range RGB to limited range RGB conversion
8148 * is supported, but eventually this should handle various
8149 * RGB<->YCbCr scenarios as well.
8150 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01008151static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008152{
8153 struct drm_device *dev = crtc->dev;
8154 struct drm_i915_private *dev_priv = dev->dev_private;
8155 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8156 int pipe = intel_crtc->pipe;
8157 uint16_t coeff = 0x7800; /* 1.0 */
8158
8159 /*
8160 * TODO: Check what kind of values actually come out of the pipe
8161 * with these coeff/postoff values and adjust to get the best
8162 * accuracy. Perhaps we even need to take the bpc value into
8163 * consideration.
8164 */
8165
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008166 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008167 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8168
8169 /*
8170 * GY/GU and RY/RU should be the other way around according
8171 * to BSpec, but reality doesn't agree. Just set them up in
8172 * a way that results in the correct picture.
8173 */
8174 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8175 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8176
8177 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8178 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8179
8180 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8181 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8182
8183 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8184 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8185 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8186
8187 if (INTEL_INFO(dev)->gen > 6) {
8188 uint16_t postoff = 0;
8189
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008190 if (intel_crtc->config->limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02008191 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008192
8193 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8194 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8195 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8196
8197 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8198 } else {
8199 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8200
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008201 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008202 mode |= CSC_BLACK_SCREEN_OFFSET;
8203
8204 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8205 }
8206}
8207
Daniel Vetter6ff93602013-04-19 11:24:36 +02008208static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008209{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008210 struct drm_device *dev = crtc->dev;
8211 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008212 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008213 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008214 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008215 uint32_t val;
8216
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008217 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008218
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008219 if (IS_HASWELL(dev) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008220 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8221
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008222 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008223 val |= PIPECONF_INTERLACED_ILK;
8224 else
8225 val |= PIPECONF_PROGRESSIVE;
8226
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008227 I915_WRITE(PIPECONF(cpu_transcoder), val);
8228 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008229
8230 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8231 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008232
Satheeshakrishna M3cdf1222014-04-08 15:46:53 +05308233 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008234 val = 0;
8235
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008236 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008237 case 18:
8238 val |= PIPEMISC_DITHER_6_BPC;
8239 break;
8240 case 24:
8241 val |= PIPEMISC_DITHER_8_BPC;
8242 break;
8243 case 30:
8244 val |= PIPEMISC_DITHER_10_BPC;
8245 break;
8246 case 36:
8247 val |= PIPEMISC_DITHER_12_BPC;
8248 break;
8249 default:
8250 /* Case prevented by pipe_config_set_bpp. */
8251 BUG();
8252 }
8253
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008254 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008255 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8256
8257 I915_WRITE(PIPEMISC(pipe), val);
8258 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008259}
8260
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008261static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008262 struct intel_crtc_state *crtc_state,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008263 intel_clock_t *clock,
8264 bool *has_reduced_clock,
8265 intel_clock_t *reduced_clock)
8266{
8267 struct drm_device *dev = crtc->dev;
8268 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008269 int refclk;
8270 const intel_limit_t *limit;
Daniel Vettera16af722013-04-30 14:01:44 +02008271 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008272
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008273 is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008274
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008275 refclk = ironlake_get_refclk(crtc_state);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008276
8277 /*
8278 * Returns a set of divisors for the desired target clock with the given
8279 * refclk, or FALSE. The returned values represent the clock equation:
8280 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8281 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008282 limit = intel_limit(crtc_state, refclk);
8283 ret = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008284 crtc_state->port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008285 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008286 if (!ret)
8287 return false;
8288
8289 if (is_lvds && dev_priv->lvds_downclock_avail) {
8290 /*
8291 * Ensure we match the reduced clock's P to the target clock.
8292 * If the clocks don't match, we can't switch the display clock
8293 * by using the FP0/FP1. In such case we will disable the LVDS
8294 * downclock feature.
8295 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02008296 *has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008297 dev_priv->display.find_dpll(limit, crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008298 dev_priv->lvds_downclock,
8299 refclk, clock,
8300 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008301 }
8302
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008303 return true;
8304}
8305
Paulo Zanonid4b19312012-11-29 11:29:32 -02008306int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8307{
8308 /*
8309 * Account for spread spectrum to avoid
8310 * oversubscribing the link. Max center spread
8311 * is 2.5%; use 5% for safety's sake.
8312 */
8313 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008314 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008315}
8316
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008317static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008318{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008319 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008320}
8321
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008322static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008323 struct intel_crtc_state *crtc_state,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008324 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008325 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008326{
8327 struct drm_crtc *crtc = &intel_crtc->base;
8328 struct drm_device *dev = crtc->dev;
8329 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008330 struct drm_atomic_state *state = crtc_state->base.state;
8331 struct drm_connector_state *connector_state;
8332 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008333 uint32_t dpll;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008334 int factor, num_connectors = 0, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008335 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008336
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008337 for (i = 0; i < state->num_connector; i++) {
8338 if (!state->connectors[i])
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02008339 continue;
8340
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008341 connector_state = state->connector_states[i];
8342 if (connector_state->crtc != crtc_state->base.crtc)
8343 continue;
8344
8345 encoder = to_intel_encoder(connector_state->best_encoder);
8346
8347 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008348 case INTEL_OUTPUT_LVDS:
8349 is_lvds = true;
8350 break;
8351 case INTEL_OUTPUT_SDVO:
8352 case INTEL_OUTPUT_HDMI:
8353 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008354 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008355 default:
8356 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008357 }
8358
8359 num_connectors++;
8360 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008361
Chris Wilsonc1858122010-12-03 21:35:48 +00008362 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008363 factor = 21;
8364 if (is_lvds) {
8365 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008366 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008367 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008368 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008369 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008370 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008371
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008372 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02008373 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00008374
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008375 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8376 *fp2 |= FP_CB_TUNE;
8377
Chris Wilson5eddb702010-09-11 13:48:45 +01008378 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008379
Eric Anholta07d6782011-03-30 13:01:08 -07008380 if (is_lvds)
8381 dpll |= DPLLB_MODE_LVDS;
8382 else
8383 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008384
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008385 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008386 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008387
8388 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008389 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008390 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008391 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008392
Eric Anholta07d6782011-03-30 13:01:08 -07008393 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008394 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008395 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008396 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008397
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008398 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008399 case 5:
8400 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8401 break;
8402 case 7:
8403 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8404 break;
8405 case 10:
8406 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8407 break;
8408 case 14:
8409 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8410 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008411 }
8412
Daniel Vetterb4c09f32013-04-30 14:01:42 +02008413 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008414 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008415 else
8416 dpll |= PLL_REF_INPUT_DREFCLK;
8417
Daniel Vetter959e16d2013-06-05 13:34:21 +02008418 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008419}
8420
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008421static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8422 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008423{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008424 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008425 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008426 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03008427 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01008428 bool is_lvds = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008429 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008430
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03008431 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
Jesse Barnes79e53942008-11-07 14:24:08 -08008432
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008433 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8434 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
8435
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008436 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008437 &has_reduced_clock, &reduced_clock);
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008438 if (!ok && !crtc_state->clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008439 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8440 return -EINVAL;
8441 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01008442 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008443 if (!crtc_state->clock_set) {
8444 crtc_state->dpll.n = clock.n;
8445 crtc_state->dpll.m1 = clock.m1;
8446 crtc_state->dpll.m2 = clock.m2;
8447 crtc_state->dpll.p1 = clock.p1;
8448 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008449 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008450
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008451 /* 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 +02008452 if (crtc_state->has_pch_encoder) {
8453 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008454 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008455 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008456
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008457 dpll = ironlake_compute_dpll(crtc, crtc_state,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008458 &fp, &reduced_clock,
8459 has_reduced_clock ? &fp2 : NULL);
8460
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008461 crtc_state->dpll_hw_state.dpll = dpll;
8462 crtc_state->dpll_hw_state.fp0 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008463 if (has_reduced_clock)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008464 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008465 else
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008466 crtc_state->dpll_hw_state.fp1 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008467
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008468 pll = intel_get_shared_dpll(crtc, crtc_state);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008469 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03008470 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008471 pipe_name(crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07008472 return -EINVAL;
8473 }
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008474 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008475
Rodrigo Viviab585de2015-03-24 12:40:09 -07008476 if (is_lvds && has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008477 crtc->lowfreq_avail = true;
Daniel Vetterbcd644e2013-06-05 13:34:22 +02008478 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008479 crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008480
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008481 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008482}
8483
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008484static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8485 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008486{
8487 struct drm_device *dev = crtc->base.dev;
8488 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008489 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008490
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008491 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8492 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8493 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8494 & ~TU_SIZE_MASK;
8495 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8496 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8497 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8498}
8499
8500static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8501 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008502 struct intel_link_m_n *m_n,
8503 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008504{
8505 struct drm_device *dev = crtc->base.dev;
8506 struct drm_i915_private *dev_priv = dev->dev_private;
8507 enum pipe pipe = crtc->pipe;
8508
8509 if (INTEL_INFO(dev)->gen >= 5) {
8510 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8511 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8512 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8513 & ~TU_SIZE_MASK;
8514 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8515 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8516 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008517 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8518 * gen < 8) and if DRRS is supported (to make sure the
8519 * registers are not unnecessarily read).
8520 */
8521 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008522 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008523 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8524 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8525 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8526 & ~TU_SIZE_MASK;
8527 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8528 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8529 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8530 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008531 } else {
8532 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8533 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8534 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8535 & ~TU_SIZE_MASK;
8536 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8537 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8538 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8539 }
8540}
8541
8542void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008543 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008544{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02008545 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008546 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8547 else
8548 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008549 &pipe_config->dp_m_n,
8550 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008551}
8552
Daniel Vetter72419202013-04-04 13:28:53 +02008553static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008554 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02008555{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008556 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008557 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02008558}
8559
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008560static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008561 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008562{
8563 struct drm_device *dev = crtc->base.dev;
8564 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07008565 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8566 uint32_t ps_ctrl = 0;
8567 int id = -1;
8568 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008569
Chandra Kondurua1b22782015-04-07 15:28:45 -07008570 /* find scaler attached to this pipe */
8571 for (i = 0; i < crtc->num_scalers; i++) {
8572 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8573 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8574 id = i;
8575 pipe_config->pch_pfit.enabled = true;
8576 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8577 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8578 break;
8579 }
8580 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008581
Chandra Kondurua1b22782015-04-07 15:28:45 -07008582 scaler_state->scaler_id = id;
8583 if (id >= 0) {
8584 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8585 } else {
8586 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008587 }
8588}
8589
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008590static void
8591skylake_get_initial_plane_config(struct intel_crtc *crtc,
8592 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008593{
8594 struct drm_device *dev = crtc->base.dev;
8595 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00008596 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008597 int pipe = crtc->pipe;
8598 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008599 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008600 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008601 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008602
Damien Lespiaud9806c92015-01-21 14:07:19 +00008603 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008604 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008605 DRM_DEBUG_KMS("failed to alloc fb\n");
8606 return;
8607 }
8608
Damien Lespiau1b842c82015-01-21 13:50:54 +00008609 fb = &intel_fb->base;
8610
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008611 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00008612 if (!(val & PLANE_CTL_ENABLE))
8613 goto error;
8614
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008615 pixel_format = val & PLANE_CTL_FORMAT_MASK;
8616 fourcc = skl_format_to_fourcc(pixel_format,
8617 val & PLANE_CTL_ORDER_RGBX,
8618 val & PLANE_CTL_ALPHA_MASK);
8619 fb->pixel_format = fourcc;
8620 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
8621
Damien Lespiau40f46282015-02-27 11:15:21 +00008622 tiling = val & PLANE_CTL_TILED_MASK;
8623 switch (tiling) {
8624 case PLANE_CTL_TILED_LINEAR:
8625 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
8626 break;
8627 case PLANE_CTL_TILED_X:
8628 plane_config->tiling = I915_TILING_X;
8629 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8630 break;
8631 case PLANE_CTL_TILED_Y:
8632 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
8633 break;
8634 case PLANE_CTL_TILED_YF:
8635 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
8636 break;
8637 default:
8638 MISSING_CASE(tiling);
8639 goto error;
8640 }
8641
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008642 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
8643 plane_config->base = base;
8644
8645 offset = I915_READ(PLANE_OFFSET(pipe, 0));
8646
8647 val = I915_READ(PLANE_SIZE(pipe, 0));
8648 fb->height = ((val >> 16) & 0xfff) + 1;
8649 fb->width = ((val >> 0) & 0x1fff) + 1;
8650
8651 val = I915_READ(PLANE_STRIDE(pipe, 0));
Damien Lespiau40f46282015-02-27 11:15:21 +00008652 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
8653 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008654 fb->pitches[0] = (val & 0x3ff) * stride_mult;
8655
8656 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008657 fb->pixel_format,
8658 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008659
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008660 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008661
8662 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8663 pipe_name(pipe), fb->width, fb->height,
8664 fb->bits_per_pixel, base, fb->pitches[0],
8665 plane_config->size);
8666
Damien Lespiau2d140302015-02-05 17:22:18 +00008667 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008668 return;
8669
8670error:
8671 kfree(fb);
8672}
8673
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008674static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008675 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008676{
8677 struct drm_device *dev = crtc->base.dev;
8678 struct drm_i915_private *dev_priv = dev->dev_private;
8679 uint32_t tmp;
8680
8681 tmp = I915_READ(PF_CTL(crtc->pipe));
8682
8683 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008684 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008685 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
8686 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02008687
8688 /* We currently do not free assignements of panel fitters on
8689 * ivb/hsw (since we don't use the higher upscaling modes which
8690 * differentiates them) so just WARN about this case for now. */
8691 if (IS_GEN7(dev)) {
8692 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
8693 PF_PIPE_SEL_IVB(crtc->pipe));
8694 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008695 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008696}
8697
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008698static void
8699ironlake_get_initial_plane_config(struct intel_crtc *crtc,
8700 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008701{
8702 struct drm_device *dev = crtc->base.dev;
8703 struct drm_i915_private *dev_priv = dev->dev_private;
8704 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008705 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008706 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008707 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008708 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008709 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008710
Damien Lespiau42a7b082015-02-05 19:35:13 +00008711 val = I915_READ(DSPCNTR(pipe));
8712 if (!(val & DISPLAY_PLANE_ENABLE))
8713 return;
8714
Damien Lespiaud9806c92015-01-21 14:07:19 +00008715 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008716 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008717 DRM_DEBUG_KMS("failed to alloc fb\n");
8718 return;
8719 }
8720
Damien Lespiau1b842c82015-01-21 13:50:54 +00008721 fb = &intel_fb->base;
8722
Daniel Vetter18c52472015-02-10 17:16:09 +00008723 if (INTEL_INFO(dev)->gen >= 4) {
8724 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008725 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008726 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8727 }
8728 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008729
8730 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008731 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008732 fb->pixel_format = fourcc;
8733 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008734
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008735 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008736 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008737 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008738 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00008739 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008740 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008741 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008742 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008743 }
8744 plane_config->base = base;
8745
8746 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008747 fb->width = ((val >> 16) & 0xfff) + 1;
8748 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008749
8750 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008751 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008752
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008753 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008754 fb->pixel_format,
8755 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008756
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008757 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008758
Damien Lespiau2844a922015-01-20 12:51:48 +00008759 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8760 pipe_name(pipe), fb->width, fb->height,
8761 fb->bits_per_pixel, base, fb->pitches[0],
8762 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008763
Damien Lespiau2d140302015-02-05 17:22:18 +00008764 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008765}
8766
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008767static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008768 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008769{
8770 struct drm_device *dev = crtc->base.dev;
8771 struct drm_i915_private *dev_priv = dev->dev_private;
8772 uint32_t tmp;
8773
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008774 if (!intel_display_power_is_enabled(dev_priv,
8775 POWER_DOMAIN_PIPE(crtc->pipe)))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03008776 return false;
8777
Daniel Vettere143a212013-07-04 12:01:15 +02008778 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008779 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02008780
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008781 tmp = I915_READ(PIPECONF(crtc->pipe));
8782 if (!(tmp & PIPECONF_ENABLE))
8783 return false;
8784
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008785 switch (tmp & PIPECONF_BPC_MASK) {
8786 case PIPECONF_6BPC:
8787 pipe_config->pipe_bpp = 18;
8788 break;
8789 case PIPECONF_8BPC:
8790 pipe_config->pipe_bpp = 24;
8791 break;
8792 case PIPECONF_10BPC:
8793 pipe_config->pipe_bpp = 30;
8794 break;
8795 case PIPECONF_12BPC:
8796 pipe_config->pipe_bpp = 36;
8797 break;
8798 default:
8799 break;
8800 }
8801
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008802 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
8803 pipe_config->limited_color_range = true;
8804
Daniel Vetterab9412b2013-05-03 11:49:46 +02008805 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02008806 struct intel_shared_dpll *pll;
8807
Daniel Vetter88adfff2013-03-28 10:42:01 +01008808 pipe_config->has_pch_encoder = true;
8809
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008810 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
8811 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8812 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02008813
8814 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008815
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008816 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02008817 pipe_config->shared_dpll =
8818 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008819 } else {
8820 tmp = I915_READ(PCH_DPLL_SEL);
8821 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8822 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
8823 else
8824 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
8825 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02008826
8827 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
8828
8829 WARN_ON(!pll->get_hw_state(dev_priv, pll,
8830 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02008831
8832 tmp = pipe_config->dpll_hw_state.dpll;
8833 pipe_config->pixel_multiplier =
8834 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
8835 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03008836
8837 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008838 } else {
8839 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008840 }
8841
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008842 intel_get_pipe_timings(crtc, pipe_config);
8843
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008844 ironlake_get_pfit_config(crtc, pipe_config);
8845
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008846 return true;
8847}
8848
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008849static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
8850{
8851 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008852 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008853
Damien Lespiaud3fcc802014-05-13 23:32:22 +01008854 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05008855 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008856 pipe_name(crtc->pipe));
8857
Rob Clarke2c719b2014-12-15 13:56:32 -05008858 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
8859 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
8860 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
8861 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
8862 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
8863 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008864 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03008865 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05008866 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03008867 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008868 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008869 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008870 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008871 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008872 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008873
Paulo Zanoni9926ada2014-04-01 19:39:47 -03008874 /*
8875 * In theory we can still leave IRQs enabled, as long as only the HPD
8876 * interrupts remain enabled. We used to check for that, but since it's
8877 * gen-specific and since we only disable LCPLL after we fully disable
8878 * the interrupts, the check below should be enough.
8879 */
Rob Clarke2c719b2014-12-15 13:56:32 -05008880 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008881}
8882
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008883static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
8884{
8885 struct drm_device *dev = dev_priv->dev;
8886
8887 if (IS_HASWELL(dev))
8888 return I915_READ(D_COMP_HSW);
8889 else
8890 return I915_READ(D_COMP_BDW);
8891}
8892
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008893static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
8894{
8895 struct drm_device *dev = dev_priv->dev;
8896
8897 if (IS_HASWELL(dev)) {
8898 mutex_lock(&dev_priv->rps.hw_lock);
8899 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
8900 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03008901 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008902 mutex_unlock(&dev_priv->rps.hw_lock);
8903 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008904 I915_WRITE(D_COMP_BDW, val);
8905 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008906 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008907}
8908
8909/*
8910 * This function implements pieces of two sequences from BSpec:
8911 * - Sequence for display software to disable LCPLL
8912 * - Sequence for display software to allow package C8+
8913 * The steps implemented here are just the steps that actually touch the LCPLL
8914 * register. Callers should take care of disabling all the display engine
8915 * functions, doing the mode unset, fixing interrupts, etc.
8916 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03008917static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
8918 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008919{
8920 uint32_t val;
8921
8922 assert_can_disable_lcpll(dev_priv);
8923
8924 val = I915_READ(LCPLL_CTL);
8925
8926 if (switch_to_fclk) {
8927 val |= LCPLL_CD_SOURCE_FCLK;
8928 I915_WRITE(LCPLL_CTL, val);
8929
8930 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
8931 LCPLL_CD_SOURCE_FCLK_DONE, 1))
8932 DRM_ERROR("Switching to FCLK failed\n");
8933
8934 val = I915_READ(LCPLL_CTL);
8935 }
8936
8937 val |= LCPLL_PLL_DISABLE;
8938 I915_WRITE(LCPLL_CTL, val);
8939 POSTING_READ(LCPLL_CTL);
8940
8941 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
8942 DRM_ERROR("LCPLL still locked\n");
8943
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008944 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008945 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008946 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008947 ndelay(100);
8948
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008949 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
8950 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008951 DRM_ERROR("D_COMP RCOMP still in progress\n");
8952
8953 if (allow_power_down) {
8954 val = I915_READ(LCPLL_CTL);
8955 val |= LCPLL_POWER_DOWN_ALLOW;
8956 I915_WRITE(LCPLL_CTL, val);
8957 POSTING_READ(LCPLL_CTL);
8958 }
8959}
8960
8961/*
8962 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
8963 * source.
8964 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03008965static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008966{
8967 uint32_t val;
8968
8969 val = I915_READ(LCPLL_CTL);
8970
8971 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
8972 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
8973 return;
8974
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03008975 /*
8976 * Make sure we're not on PC8 state before disabling PC8, otherwise
8977 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03008978 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02008979 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03008980
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008981 if (val & LCPLL_POWER_DOWN_ALLOW) {
8982 val &= ~LCPLL_POWER_DOWN_ALLOW;
8983 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02008984 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008985 }
8986
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008987 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008988 val |= D_COMP_COMP_FORCE;
8989 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008990 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008991
8992 val = I915_READ(LCPLL_CTL);
8993 val &= ~LCPLL_PLL_DISABLE;
8994 I915_WRITE(LCPLL_CTL, val);
8995
8996 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
8997 DRM_ERROR("LCPLL not locked yet\n");
8998
8999 if (val & LCPLL_CD_SOURCE_FCLK) {
9000 val = I915_READ(LCPLL_CTL);
9001 val &= ~LCPLL_CD_SOURCE_FCLK;
9002 I915_WRITE(LCPLL_CTL, val);
9003
9004 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9005 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9006 DRM_ERROR("Switching back to LCPLL failed\n");
9007 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009008
Mika Kuoppala59bad942015-01-16 11:34:40 +02009009 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009010}
9011
Paulo Zanoni765dab672014-03-07 20:08:18 -03009012/*
9013 * Package states C8 and deeper are really deep PC states that can only be
9014 * reached when all the devices on the system allow it, so even if the graphics
9015 * device allows PC8+, it doesn't mean the system will actually get to these
9016 * states. Our driver only allows PC8+ when going into runtime PM.
9017 *
9018 * The requirements for PC8+ are that all the outputs are disabled, the power
9019 * well is disabled and most interrupts are disabled, and these are also
9020 * requirements for runtime PM. When these conditions are met, we manually do
9021 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9022 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9023 * hang the machine.
9024 *
9025 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9026 * the state of some registers, so when we come back from PC8+ we need to
9027 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9028 * need to take care of the registers kept by RC6. Notice that this happens even
9029 * if we don't put the device in PCI D3 state (which is what currently happens
9030 * because of the runtime PM support).
9031 *
9032 * For more, read "Display Sequences for Package C8" on the hardware
9033 * documentation.
9034 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009035void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009036{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009037 struct drm_device *dev = dev_priv->dev;
9038 uint32_t val;
9039
Paulo Zanonic67a4702013-08-19 13:18:09 -03009040 DRM_DEBUG_KMS("Enabling package C8+\n");
9041
Paulo Zanonic67a4702013-08-19 13:18:09 -03009042 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9043 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9044 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9045 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9046 }
9047
9048 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009049 hsw_disable_lcpll(dev_priv, true, true);
9050}
9051
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009052void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009053{
9054 struct drm_device *dev = dev_priv->dev;
9055 uint32_t val;
9056
Paulo Zanonic67a4702013-08-19 13:18:09 -03009057 DRM_DEBUG_KMS("Disabling package C8+\n");
9058
9059 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009060 lpt_init_pch_refclk(dev);
9061
9062 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9063 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9064 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9065 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9066 }
9067
9068 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009069}
9070
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309071static void broxton_modeset_global_resources(struct drm_atomic_state *state)
9072{
9073 struct drm_device *dev = state->dev;
9074 struct drm_i915_private *dev_priv = dev->dev_private;
9075 int max_pixclk = intel_mode_max_pixclk(state);
9076 int req_cdclk;
9077
9078 /* see the comment in valleyview_modeset_global_resources */
9079 if (WARN_ON(max_pixclk < 0))
9080 return;
9081
9082 req_cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
9083
9084 if (req_cdclk != dev_priv->cdclk_freq)
9085 broxton_set_cdclk(dev, req_cdclk);
9086}
9087
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009088static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9089 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009090{
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009091 if (!intel_ddi_pll_select(crtc, crtc_state))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009092 return -EINVAL;
Daniel Vetter716c2e52014-06-25 22:02:02 +03009093
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009094 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009095
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009096 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009097}
9098
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309099static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9100 enum port port,
9101 struct intel_crtc_state *pipe_config)
9102{
9103 switch (port) {
9104 case PORT_A:
9105 pipe_config->ddi_pll_sel = SKL_DPLL0;
9106 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9107 break;
9108 case PORT_B:
9109 pipe_config->ddi_pll_sel = SKL_DPLL1;
9110 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9111 break;
9112 case PORT_C:
9113 pipe_config->ddi_pll_sel = SKL_DPLL2;
9114 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9115 break;
9116 default:
9117 DRM_ERROR("Incorrect port type\n");
9118 }
9119}
9120
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009121static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9122 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009123 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009124{
Damien Lespiau3148ade2014-11-21 16:14:56 +00009125 u32 temp, dpll_ctl1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009126
9127 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9128 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9129
9130 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009131 case SKL_DPLL0:
9132 /*
9133 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9134 * of the shared DPLL framework and thus needs to be read out
9135 * separately
9136 */
9137 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9138 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9139 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009140 case SKL_DPLL1:
9141 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9142 break;
9143 case SKL_DPLL2:
9144 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9145 break;
9146 case SKL_DPLL3:
9147 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9148 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009149 }
9150}
9151
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009152static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9153 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009154 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009155{
9156 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9157
9158 switch (pipe_config->ddi_pll_sel) {
9159 case PORT_CLK_SEL_WRPLL1:
9160 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9161 break;
9162 case PORT_CLK_SEL_WRPLL2:
9163 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9164 break;
9165 }
9166}
9167
Daniel Vetter26804af2014-06-25 22:01:55 +03009168static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009169 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009170{
9171 struct drm_device *dev = crtc->base.dev;
9172 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009173 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009174 enum port port;
9175 uint32_t tmp;
9176
9177 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9178
9179 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9180
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009181 if (IS_SKYLAKE(dev))
9182 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309183 else if (IS_BROXTON(dev))
9184 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009185 else
9186 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009187
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009188 if (pipe_config->shared_dpll >= 0) {
9189 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9190
9191 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9192 &pipe_config->dpll_hw_state));
9193 }
9194
Daniel Vetter26804af2014-06-25 22:01:55 +03009195 /*
9196 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9197 * DDI E. So just check whether this pipe is wired to DDI E and whether
9198 * the PCH transcoder is on.
9199 */
Damien Lespiauca370452013-12-03 13:56:24 +00009200 if (INTEL_INFO(dev)->gen < 9 &&
9201 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009202 pipe_config->has_pch_encoder = true;
9203
9204 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9205 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9206 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9207
9208 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9209 }
9210}
9211
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009212static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009213 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009214{
9215 struct drm_device *dev = crtc->base.dev;
9216 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009217 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009218 uint32_t tmp;
9219
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009220 if (!intel_display_power_is_enabled(dev_priv,
Imre Deakb5482bd2014-03-05 16:20:55 +02009221 POWER_DOMAIN_PIPE(crtc->pipe)))
9222 return false;
9223
Daniel Vettere143a212013-07-04 12:01:15 +02009224 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009225 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9226
Daniel Vettereccb1402013-05-22 00:50:22 +02009227 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9228 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9229 enum pipe trans_edp_pipe;
9230 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9231 default:
9232 WARN(1, "unknown pipe linked to edp transcoder\n");
9233 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9234 case TRANS_DDI_EDP_INPUT_A_ON:
9235 trans_edp_pipe = PIPE_A;
9236 break;
9237 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9238 trans_edp_pipe = PIPE_B;
9239 break;
9240 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9241 trans_edp_pipe = PIPE_C;
9242 break;
9243 }
9244
9245 if (trans_edp_pipe == crtc->pipe)
9246 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9247 }
9248
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009249 if (!intel_display_power_is_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02009250 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03009251 return false;
9252
Daniel Vettereccb1402013-05-22 00:50:22 +02009253 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009254 if (!(tmp & PIPECONF_ENABLE))
9255 return false;
9256
Daniel Vetter26804af2014-06-25 22:01:55 +03009257 haswell_get_ddi_port_state(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009258
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009259 intel_get_pipe_timings(crtc, pipe_config);
9260
Chandra Kondurua1b22782015-04-07 15:28:45 -07009261 if (INTEL_INFO(dev)->gen >= 9) {
9262 skl_init_scalers(dev, crtc, pipe_config);
9263 }
9264
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009265 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009266 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009267 if (INTEL_INFO(dev)->gen == 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009268 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009269 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009270 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009271 else
9272 MISSING_CASE(INTEL_INFO(dev)->gen);
9273
Chandra Kondurua1b22782015-04-07 15:28:45 -07009274 } else {
9275 pipe_config->scaler_state.scaler_id = -1;
9276 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009277 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01009278
Jesse Barnese59150d2014-01-07 13:30:45 -08009279 if (IS_HASWELL(dev))
9280 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9281 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009282
Clint Taylorebb69c92014-09-30 10:30:22 -07009283 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
9284 pipe_config->pixel_multiplier =
9285 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9286 } else {
9287 pipe_config->pixel_multiplier = 1;
9288 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009289
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009290 return true;
9291}
9292
Chris Wilson560b85b2010-08-07 11:01:38 +01009293static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
9294{
9295 struct drm_device *dev = crtc->dev;
9296 struct drm_i915_private *dev_priv = dev->dev_private;
9297 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +03009298 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01009299
Ville Syrjälädc41c152014-08-13 11:57:05 +03009300 if (base) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009301 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
9302 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009303 unsigned int stride = roundup_pow_of_two(width) * 4;
9304
9305 switch (stride) {
9306 default:
9307 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9308 width, stride);
9309 stride = 256;
9310 /* fallthrough */
9311 case 256:
9312 case 512:
9313 case 1024:
9314 case 2048:
9315 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009316 }
9317
Ville Syrjälädc41c152014-08-13 11:57:05 +03009318 cntl |= CURSOR_ENABLE |
9319 CURSOR_GAMMA_ENABLE |
9320 CURSOR_FORMAT_ARGB |
9321 CURSOR_STRIDE(stride);
9322
9323 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009324 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009325
Ville Syrjälädc41c152014-08-13 11:57:05 +03009326 if (intel_crtc->cursor_cntl != 0 &&
9327 (intel_crtc->cursor_base != base ||
9328 intel_crtc->cursor_size != size ||
9329 intel_crtc->cursor_cntl != cntl)) {
9330 /* On these chipsets we can only modify the base/size/stride
9331 * whilst the cursor is disabled.
9332 */
9333 I915_WRITE(_CURACNTR, 0);
9334 POSTING_READ(_CURACNTR);
9335 intel_crtc->cursor_cntl = 0;
9336 }
9337
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009338 if (intel_crtc->cursor_base != base) {
Ville Syrjälädc41c152014-08-13 11:57:05 +03009339 I915_WRITE(_CURABASE, base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009340 intel_crtc->cursor_base = base;
9341 }
Ville Syrjälädc41c152014-08-13 11:57:05 +03009342
9343 if (intel_crtc->cursor_size != size) {
9344 I915_WRITE(CURSIZE, size);
9345 intel_crtc->cursor_size = size;
9346 }
9347
Chris Wilson4b0e3332014-05-30 16:35:26 +03009348 if (intel_crtc->cursor_cntl != cntl) {
9349 I915_WRITE(_CURACNTR, cntl);
9350 POSTING_READ(_CURACNTR);
9351 intel_crtc->cursor_cntl = cntl;
9352 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009353}
9354
9355static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
9356{
9357 struct drm_device *dev = crtc->dev;
9358 struct drm_i915_private *dev_priv = dev->dev_private;
9359 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9360 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009361 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +01009362
Chris Wilson4b0e3332014-05-30 16:35:26 +03009363 cntl = 0;
9364 if (base) {
9365 cntl = MCURSOR_GAMMA_ENABLE;
Matt Roper3dd512f2015-02-27 10:12:00 -08009366 switch (intel_crtc->base.cursor->state->crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309367 case 64:
9368 cntl |= CURSOR_MODE_64_ARGB_AX;
9369 break;
9370 case 128:
9371 cntl |= CURSOR_MODE_128_ARGB_AX;
9372 break;
9373 case 256:
9374 cntl |= CURSOR_MODE_256_ARGB_AX;
9375 break;
9376 default:
Matt Roper3dd512f2015-02-27 10:12:00 -08009377 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309378 return;
Chris Wilson560b85b2010-08-07 11:01:38 +01009379 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009380 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +03009381
9382 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
9383 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson560b85b2010-08-07 11:01:38 +01009384 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009385
Matt Roper8e7d6882015-01-21 16:35:41 -08009386 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009387 cntl |= CURSOR_ROTATE_180;
9388
Chris Wilson4b0e3332014-05-30 16:35:26 +03009389 if (intel_crtc->cursor_cntl != cntl) {
9390 I915_WRITE(CURCNTR(pipe), cntl);
9391 POSTING_READ(CURCNTR(pipe));
9392 intel_crtc->cursor_cntl = cntl;
9393 }
9394
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009395 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009396 I915_WRITE(CURBASE(pipe), base);
9397 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009398
9399 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009400}
9401
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009402/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01009403static void intel_crtc_update_cursor(struct drm_crtc *crtc,
9404 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009405{
9406 struct drm_device *dev = crtc->dev;
9407 struct drm_i915_private *dev_priv = dev->dev_private;
9408 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9409 int pipe = intel_crtc->pipe;
Matt Roper3d7d6512014-06-10 08:28:13 -07009410 int x = crtc->cursor_x;
9411 int y = crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009412 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009413
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009414 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009415 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009416
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009417 if (x >= intel_crtc->config->pipe_src_w)
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009418 base = 0;
9419
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009420 if (y >= intel_crtc->config->pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009421 base = 0;
9422
9423 if (x < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009424 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009425 base = 0;
9426
9427 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
9428 x = -x;
9429 }
9430 pos |= x << CURSOR_X_SHIFT;
9431
9432 if (y < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009433 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009434 base = 0;
9435
9436 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
9437 y = -y;
9438 }
9439 pos |= y << CURSOR_Y_SHIFT;
9440
Chris Wilson4b0e3332014-05-30 16:35:26 +03009441 if (base == 0 && intel_crtc->cursor_base == 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009442 return;
9443
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009444 I915_WRITE(CURPOS(pipe), pos);
9445
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009446 /* ILK+ do this automagically */
9447 if (HAS_GMCH_DISPLAY(dev) &&
Matt Roper8e7d6882015-01-21 16:35:41 -08009448 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009449 base += (intel_crtc->base.cursor->state->crtc_h *
9450 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009451 }
9452
Ville Syrjälä8ac54662014-08-12 19:39:54 +03009453 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009454 i845_update_cursor(crtc, base);
9455 else
9456 i9xx_update_cursor(crtc, base);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009457}
9458
Ville Syrjälädc41c152014-08-13 11:57:05 +03009459static bool cursor_size_ok(struct drm_device *dev,
9460 uint32_t width, uint32_t height)
9461{
9462 if (width == 0 || height == 0)
9463 return false;
9464
9465 /*
9466 * 845g/865g are special in that they are only limited by
9467 * the width of their cursors, the height is arbitrary up to
9468 * the precision of the register. Everything else requires
9469 * square cursors, limited to a few power-of-two sizes.
9470 */
9471 if (IS_845G(dev) || IS_I865G(dev)) {
9472 if ((width & 63) != 0)
9473 return false;
9474
9475 if (width > (IS_845G(dev) ? 64 : 512))
9476 return false;
9477
9478 if (height > 1023)
9479 return false;
9480 } else {
9481 switch (width | height) {
9482 case 256:
9483 case 128:
9484 if (IS_GEN2(dev))
9485 return false;
9486 case 64:
9487 break;
9488 default:
9489 return false;
9490 }
9491 }
9492
9493 return true;
9494}
9495
Jesse Barnes79e53942008-11-07 14:24:08 -08009496static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01009497 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08009498{
James Simmons72034252010-08-03 01:33:19 +01009499 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08009500 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08009501
James Simmons72034252010-08-03 01:33:19 +01009502 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009503 intel_crtc->lut_r[i] = red[i] >> 8;
9504 intel_crtc->lut_g[i] = green[i] >> 8;
9505 intel_crtc->lut_b[i] = blue[i] >> 8;
9506 }
9507
9508 intel_crtc_load_lut(crtc);
9509}
9510
Jesse Barnes79e53942008-11-07 14:24:08 -08009511/* VESA 640x480x72Hz mode to set on the pipe */
9512static struct drm_display_mode load_detect_mode = {
9513 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
9514 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
9515};
9516
Daniel Vettera8bb6812014-02-10 18:00:39 +01009517struct drm_framebuffer *
9518__intel_framebuffer_create(struct drm_device *dev,
9519 struct drm_mode_fb_cmd2 *mode_cmd,
9520 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +01009521{
9522 struct intel_framebuffer *intel_fb;
9523 int ret;
9524
9525 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
9526 if (!intel_fb) {
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +03009527 drm_gem_object_unreference(&obj->base);
Chris Wilsond2dff872011-04-19 08:36:26 +01009528 return ERR_PTR(-ENOMEM);
9529 }
9530
9531 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009532 if (ret)
9533 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +01009534
9535 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009536err:
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +03009537 drm_gem_object_unreference(&obj->base);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009538 kfree(intel_fb);
9539
9540 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +01009541}
9542
Daniel Vetterb5ea6422014-03-02 21:18:00 +01009543static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +01009544intel_framebuffer_create(struct drm_device *dev,
9545 struct drm_mode_fb_cmd2 *mode_cmd,
9546 struct drm_i915_gem_object *obj)
9547{
9548 struct drm_framebuffer *fb;
9549 int ret;
9550
9551 ret = i915_mutex_lock_interruptible(dev);
9552 if (ret)
9553 return ERR_PTR(ret);
9554 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
9555 mutex_unlock(&dev->struct_mutex);
9556
9557 return fb;
9558}
9559
Chris Wilsond2dff872011-04-19 08:36:26 +01009560static u32
9561intel_framebuffer_pitch_for_width(int width, int bpp)
9562{
9563 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
9564 return ALIGN(pitch, 64);
9565}
9566
9567static u32
9568intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
9569{
9570 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +02009571 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +01009572}
9573
9574static struct drm_framebuffer *
9575intel_framebuffer_create_for_mode(struct drm_device *dev,
9576 struct drm_display_mode *mode,
9577 int depth, int bpp)
9578{
9579 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00009580 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01009581
9582 obj = i915_gem_alloc_object(dev,
9583 intel_framebuffer_size_for_mode(mode, bpp));
9584 if (obj == NULL)
9585 return ERR_PTR(-ENOMEM);
9586
9587 mode_cmd.width = mode->hdisplay;
9588 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009589 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
9590 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00009591 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01009592
9593 return intel_framebuffer_create(dev, &mode_cmd, obj);
9594}
9595
9596static struct drm_framebuffer *
9597mode_fits_in_fbdev(struct drm_device *dev,
9598 struct drm_display_mode *mode)
9599{
Daniel Vetter4520f532013-10-09 09:18:51 +02009600#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +01009601 struct drm_i915_private *dev_priv = dev->dev_private;
9602 struct drm_i915_gem_object *obj;
9603 struct drm_framebuffer *fb;
9604
Daniel Vetter4c0e5522014-02-14 16:35:54 +01009605 if (!dev_priv->fbdev)
9606 return NULL;
9607
9608 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +01009609 return NULL;
9610
Jesse Barnes8bcd4552014-02-07 12:10:38 -08009611 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +01009612 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +01009613
Jesse Barnes8bcd4552014-02-07 12:10:38 -08009614 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009615 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
9616 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01009617 return NULL;
9618
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009619 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01009620 return NULL;
9621
9622 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +02009623#else
9624 return NULL;
9625#endif
Chris Wilsond2dff872011-04-19 08:36:26 +01009626}
9627
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009628bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01009629 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -05009630 struct intel_load_detect_pipe *old,
9631 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08009632{
9633 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009634 struct intel_encoder *intel_encoder =
9635 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08009636 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01009637 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08009638 struct drm_crtc *crtc = NULL;
9639 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02009640 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -05009641 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009642 struct drm_atomic_state *state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009643 struct drm_connector_state *connector_state;
Rob Clark51fd3712013-11-19 12:10:12 -05009644 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -08009645
Chris Wilsond2dff872011-04-19 08:36:26 +01009646 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03009647 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03009648 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01009649
Rob Clark51fd3712013-11-19 12:10:12 -05009650retry:
9651 ret = drm_modeset_lock(&config->connection_mutex, ctx);
9652 if (ret)
9653 goto fail_unlock;
Daniel Vetter6e9f7982014-05-29 23:54:47 +02009654
Jesse Barnes79e53942008-11-07 14:24:08 -08009655 /*
9656 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01009657 *
Jesse Barnes79e53942008-11-07 14:24:08 -08009658 * - if the connector already has an assigned crtc, use it (but make
9659 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01009660 *
Jesse Barnes79e53942008-11-07 14:24:08 -08009661 * - try to find the first unused crtc that can drive this connector,
9662 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08009663 */
9664
9665 /* See if we already have a CRTC for this connector */
9666 if (encoder->crtc) {
9667 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01009668
Rob Clark51fd3712013-11-19 12:10:12 -05009669 ret = drm_modeset_lock(&crtc->mutex, ctx);
9670 if (ret)
9671 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01009672 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
9673 if (ret)
9674 goto fail_unlock;
Daniel Vetter7b240562012-12-12 00:35:33 +01009675
Daniel Vetter24218aa2012-08-12 19:27:11 +02009676 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01009677 old->load_detect_temp = false;
9678
9679 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02009680 if (connector->dpms != DRM_MODE_DPMS_ON)
9681 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01009682
Chris Wilson71731882011-04-19 23:10:58 +01009683 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08009684 }
9685
9686 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01009687 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009688 i++;
9689 if (!(encoder->possible_crtcs & (1 << i)))
9690 continue;
Matt Roper83d65732015-02-25 13:12:16 -08009691 if (possible_crtc->state->enable)
Ville Syrjäläa4592492014-08-11 13:15:36 +03009692 continue;
9693 /* This can occur when applying the pipe A quirk on resume. */
9694 if (to_intel_crtc(possible_crtc)->new_enabled)
9695 continue;
9696
9697 crtc = possible_crtc;
9698 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08009699 }
9700
9701 /*
9702 * If we didn't find an unused CRTC, don't use any.
9703 */
9704 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01009705 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Rob Clark51fd3712013-11-19 12:10:12 -05009706 goto fail_unlock;
Jesse Barnes79e53942008-11-07 14:24:08 -08009707 }
9708
Rob Clark51fd3712013-11-19 12:10:12 -05009709 ret = drm_modeset_lock(&crtc->mutex, ctx);
9710 if (ret)
9711 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01009712 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
9713 if (ret)
9714 goto fail_unlock;
Daniel Vetterfc303102012-07-09 10:40:58 +02009715 intel_encoder->new_crtc = to_intel_crtc(crtc);
9716 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08009717
9718 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009719 intel_crtc->new_enabled = true;
Daniel Vetter24218aa2012-08-12 19:27:11 +02009720 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01009721 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01009722 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08009723
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009724 state = drm_atomic_state_alloc(dev);
9725 if (!state)
9726 return false;
9727
9728 state->acquire_ctx = ctx;
9729
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009730 connector_state = drm_atomic_get_connector_state(state, connector);
9731 if (IS_ERR(connector_state)) {
9732 ret = PTR_ERR(connector_state);
9733 goto fail;
9734 }
9735
9736 connector_state->crtc = crtc;
9737 connector_state->best_encoder = &intel_encoder->base;
9738
Chris Wilson64927112011-04-20 07:25:26 +01009739 if (!mode)
9740 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08009741
Chris Wilsond2dff872011-04-19 08:36:26 +01009742 /* We need a framebuffer large enough to accommodate all accesses
9743 * that the plane may generate whilst we perform load detection.
9744 * We can not rely on the fbcon either being present (we get called
9745 * during its initialisation to detect all boot displays, or it may
9746 * not even exist) or that it is large enough to satisfy the
9747 * requested mode.
9748 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02009749 fb = mode_fits_in_fbdev(dev, mode);
9750 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01009751 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02009752 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
9753 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01009754 } else
9755 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02009756 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01009757 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009758 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08009759 }
Chris Wilsond2dff872011-04-19 08:36:26 +01009760
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009761 if (intel_set_mode(crtc, mode, 0, 0, fb, state)) {
Chris Wilson64927112011-04-20 07:25:26 +01009762 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01009763 if (old->release_fb)
9764 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009765 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08009766 }
Daniel Vetter9128b042015-03-03 17:31:21 +01009767 crtc->primary->crtc = crtc;
Chris Wilson71731882011-04-19 23:10:58 +01009768
Jesse Barnes79e53942008-11-07 14:24:08 -08009769 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07009770 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01009771 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009772
9773 fail:
Matt Roper83d65732015-02-25 13:12:16 -08009774 intel_crtc->new_enabled = crtc->state->enable;
Rob Clark51fd3712013-11-19 12:10:12 -05009775fail_unlock:
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009776 if (state) {
9777 drm_atomic_state_free(state);
9778 state = NULL;
9779 }
9780
Rob Clark51fd3712013-11-19 12:10:12 -05009781 if (ret == -EDEADLK) {
9782 drm_modeset_backoff(ctx);
9783 goto retry;
9784 }
9785
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009786 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08009787}
9788
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009789void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +02009790 struct intel_load_detect_pipe *old,
9791 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08009792{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009793 struct drm_device *dev = connector->dev;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009794 struct intel_encoder *intel_encoder =
9795 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01009796 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01009797 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009798 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009799 struct drm_atomic_state *state;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009800 struct drm_connector_state *connector_state;
Jesse Barnes79e53942008-11-07 14:24:08 -08009801
Chris Wilsond2dff872011-04-19 08:36:26 +01009802 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03009803 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03009804 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01009805
Chris Wilson8261b192011-04-19 23:18:09 +01009806 if (old->load_detect_temp) {
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009807 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009808 if (!state)
9809 goto fail;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009810
9811 state->acquire_ctx = ctx;
9812
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009813 connector_state = drm_atomic_get_connector_state(state, connector);
9814 if (IS_ERR(connector_state))
9815 goto fail;
9816
Daniel Vetterfc303102012-07-09 10:40:58 +02009817 to_intel_connector(connector)->new_encoder = NULL;
9818 intel_encoder->new_crtc = NULL;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009819 intel_crtc->new_enabled = false;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009820
9821 connector_state->best_encoder = NULL;
9822 connector_state->crtc = NULL;
9823
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009824 intel_set_mode(crtc, NULL, 0, 0, NULL, state);
9825
9826 drm_atomic_state_free(state);
Chris Wilsond2dff872011-04-19 08:36:26 +01009827
Daniel Vetter36206362012-12-10 20:42:17 +01009828 if (old->release_fb) {
9829 drm_framebuffer_unregister_private(old->release_fb);
9830 drm_framebuffer_unreference(old->release_fb);
9831 }
Chris Wilsond2dff872011-04-19 08:36:26 +01009832
Chris Wilson0622a532011-04-21 09:32:11 +01009833 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08009834 }
9835
Eric Anholtc751ce42010-03-25 11:48:48 -07009836 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02009837 if (old->dpms_mode != DRM_MODE_DPMS_ON)
9838 connector->funcs->dpms(connector, old->dpms_mode);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009839
9840 return;
9841fail:
9842 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
9843 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -08009844}
9845
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009846static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009847 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009848{
9849 struct drm_i915_private *dev_priv = dev->dev_private;
9850 u32 dpll = pipe_config->dpll_hw_state.dpll;
9851
9852 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +02009853 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009854 else if (HAS_PCH_SPLIT(dev))
9855 return 120000;
9856 else if (!IS_GEN2(dev))
9857 return 96000;
9858 else
9859 return 48000;
9860}
9861
Jesse Barnes79e53942008-11-07 14:24:08 -08009862/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009863static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009864 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08009865{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009866 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08009867 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009868 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +03009869 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08009870 u32 fp;
9871 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009872 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -08009873
9874 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +03009875 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009876 else
Ville Syrjälä293623f2013-09-13 16:18:46 +03009877 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -08009878
9879 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05009880 if (IS_PINEVIEW(dev)) {
9881 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
9882 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08009883 } else {
9884 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
9885 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
9886 }
9887
Chris Wilsona6c45cf2010-09-17 00:32:17 +01009888 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05009889 if (IS_PINEVIEW(dev))
9890 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
9891 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08009892 else
9893 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08009894 DPLL_FPA01_P1_POST_DIV_SHIFT);
9895
9896 switch (dpll & DPLL_MODE_MASK) {
9897 case DPLLB_MODE_DAC_SERIAL:
9898 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
9899 5 : 10;
9900 break;
9901 case DPLLB_MODE_LVDS:
9902 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
9903 7 : 14;
9904 break;
9905 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08009906 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08009907 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009908 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08009909 }
9910
Daniel Vetterac58c3f2013-06-01 17:16:17 +02009911 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009912 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +02009913 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009914 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08009915 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +02009916 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02009917 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -08009918
9919 if (is_lvds) {
9920 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
9921 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02009922
9923 if (lvds & LVDS_CLKB_POWER_UP)
9924 clock.p2 = 7;
9925 else
9926 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -08009927 } else {
9928 if (dpll & PLL_P1_DIVIDE_BY_TWO)
9929 clock.p1 = 2;
9930 else {
9931 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
9932 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
9933 }
9934 if (dpll & PLL_P2_DIVIDE_BY_4)
9935 clock.p2 = 4;
9936 else
9937 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -08009938 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009939
9940 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08009941 }
9942
Ville Syrjälä18442d02013-09-13 16:00:08 +03009943 /*
9944 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +01009945 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +03009946 * encoder's get_config() function.
9947 */
9948 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009949}
9950
Ville Syrjälä6878da02013-09-13 15:59:11 +03009951int intel_dotclock_calculate(int link_freq,
9952 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009953{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009954 /*
9955 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +03009956 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009957 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +03009958 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009959 *
9960 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +03009961 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -08009962 */
9963
Ville Syrjälä6878da02013-09-13 15:59:11 +03009964 if (!m_n->link_n)
9965 return 0;
9966
9967 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
9968}
9969
Ville Syrjälä18442d02013-09-13 16:00:08 +03009970static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009971 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +03009972{
9973 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009974
9975 /* read out port_clock from the DPLL */
9976 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +03009977
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009978 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +03009979 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +01009980 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +03009981 * agree once we know their relationship in the encoder's
9982 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009983 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02009984 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +03009985 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
9986 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -08009987}
9988
9989/** Returns the currently programmed mode of the given pipe. */
9990struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
9991 struct drm_crtc *crtc)
9992{
Jesse Barnes548f2452011-02-17 10:40:53 -08009993 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08009994 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009995 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08009996 struct drm_display_mode *mode;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009997 struct intel_crtc_state pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02009998 int htot = I915_READ(HTOTAL(cpu_transcoder));
9999 int hsync = I915_READ(HSYNC(cpu_transcoder));
10000 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10001 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010002 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010003
10004 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10005 if (!mode)
10006 return NULL;
10007
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010008 /*
10009 * Construct a pipe_config sufficient for getting the clock info
10010 * back out of crtc_clock_get.
10011 *
10012 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10013 * to use a real value here instead.
10014 */
Ville Syrjälä293623f2013-09-13 16:18:46 +030010015 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010016 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010017 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10018 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10019 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010020 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10021
Ville Syrjälä773ae032013-09-23 17:48:20 +030010022 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010023 mode->hdisplay = (htot & 0xffff) + 1;
10024 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10025 mode->hsync_start = (hsync & 0xffff) + 1;
10026 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10027 mode->vdisplay = (vtot & 0xffff) + 1;
10028 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10029 mode->vsync_start = (vsync & 0xffff) + 1;
10030 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10031
10032 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010033
10034 return mode;
10035}
10036
Jesse Barnes652c3932009-08-17 13:31:43 -070010037static void intel_decrease_pllclock(struct drm_crtc *crtc)
10038{
10039 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030010040 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -070010041 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -070010042
Sonika Jindalbaff2962014-07-22 11:16:35 +053010043 if (!HAS_GMCH_DISPLAY(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -070010044 return;
10045
10046 if (!dev_priv->lvds_downclock_avail)
10047 return;
10048
10049 /*
10050 * Since this is called by a timer, we should never get here in
10051 * the manual case.
10052 */
10053 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +010010054 int pipe = intel_crtc->pipe;
10055 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +020010056 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +010010057
Zhao Yakui44d98a62009-10-09 11:39:40 +080010058 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -070010059
Sean Paul8ac5a6d2012-02-13 13:14:51 -050010060 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -070010061
Chris Wilson074b5e12012-05-02 12:07:06 +010010062 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -070010063 dpll |= DISPLAY_RATE_SELECT_FPA1;
10064 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010065 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -070010066 dpll = I915_READ(dpll_reg);
10067 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +080010068 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -070010069 }
10070
10071}
10072
Chris Wilsonf047e392012-07-21 12:31:41 +010010073void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010074{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010075 struct drm_i915_private *dev_priv = dev->dev_private;
10076
Chris Wilsonf62a0072014-02-21 17:55:39 +000010077 if (dev_priv->mm.busy)
10078 return;
10079
Paulo Zanoni43694d62014-03-07 20:08:08 -030010080 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010081 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010082 if (INTEL_INFO(dev)->gen >= 6)
10083 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010084 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010085}
10086
10087void intel_mark_idle(struct drm_device *dev)
10088{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010089 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010090 struct drm_crtc *crtc;
10091
Chris Wilsonf62a0072014-02-21 17:55:39 +000010092 if (!dev_priv->mm.busy)
10093 return;
10094
10095 dev_priv->mm.busy = false;
10096
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010097 for_each_crtc(dev, crtc) {
Matt Roperf4510a22014-04-01 15:22:40 -070010098 if (!crtc->primary->fb)
Chris Wilson725a5b52013-01-08 11:02:57 +000010099 continue;
10100
10101 intel_decrease_pllclock(crtc);
10102 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010103
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010104 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010105 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010106
Paulo Zanoni43694d62014-03-07 20:08:08 -030010107 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010108}
10109
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020010110static void intel_crtc_set_state(struct intel_crtc *crtc,
10111 struct intel_crtc_state *crtc_state)
10112{
10113 kfree(crtc->config);
10114 crtc->config = crtc_state;
Ander Conselvan de Oliveira16f3f652015-01-15 14:55:27 +020010115 crtc->base.state = &crtc_state->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020010116}
10117
Jesse Barnes79e53942008-11-07 14:24:08 -080010118static void intel_crtc_destroy(struct drm_crtc *crtc)
10119{
10120 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010121 struct drm_device *dev = crtc->dev;
10122 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010123
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010124 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010125 work = intel_crtc->unpin_work;
10126 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010127 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010128
10129 if (work) {
10130 cancel_work_sync(&work->work);
10131 kfree(work);
10132 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010133
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020010134 intel_crtc_set_state(intel_crtc, NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -080010135 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010136
Jesse Barnes79e53942008-11-07 14:24:08 -080010137 kfree(intel_crtc);
10138}
10139
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010140static void intel_unpin_work_fn(struct work_struct *__work)
10141{
10142 struct intel_unpin_work *work =
10143 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010144 struct drm_device *dev = work->crtc->dev;
Daniel Vetterf99d7062014-06-19 16:01:59 +020010145 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010146
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010147 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000010148 intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
Chris Wilson05394f32010-11-08 19:18:58 +000010149 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010150
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020010151 intel_fbc_update(dev);
John Harrisonf06cc1b2014-11-24 18:49:37 +000010152
10153 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010154 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010155 mutex_unlock(&dev->struct_mutex);
10156
Daniel Vetterf99d7062014-06-19 16:01:59 +020010157 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Chris Wilson89ed88b2015-02-16 14:31:49 +000010158 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010159
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010160 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
10161 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
10162
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010163 kfree(work);
10164}
10165
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010166static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010010167 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010168{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010169 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10170 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010171 unsigned long flags;
10172
10173 /* Ignore early vblank irqs */
10174 if (intel_crtc == NULL)
10175 return;
10176
Daniel Vetterf3260382014-09-15 14:55:23 +020010177 /*
10178 * This is called both by irq handlers and the reset code (to complete
10179 * lost pageflips) so needs the full irqsave spinlocks.
10180 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010181 spin_lock_irqsave(&dev->event_lock, flags);
10182 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000010183
10184 /* Ensure we don't miss a work->pending update ... */
10185 smp_rmb();
10186
10187 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010188 spin_unlock_irqrestore(&dev->event_lock, flags);
10189 return;
10190 }
10191
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010192 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010010193
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010194 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010195}
10196
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010197void intel_finish_page_flip(struct drm_device *dev, int pipe)
10198{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010199 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010200 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10201
Mario Kleiner49b14a52010-12-09 07:00:07 +010010202 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010203}
10204
10205void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10206{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010207 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010208 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10209
Mario Kleiner49b14a52010-12-09 07:00:07 +010010210 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010211}
10212
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010213/* Is 'a' after or equal to 'b'? */
10214static bool g4x_flip_count_after_eq(u32 a, u32 b)
10215{
10216 return !((a - b) & 0x80000000);
10217}
10218
10219static bool page_flip_finished(struct intel_crtc *crtc)
10220{
10221 struct drm_device *dev = crtc->base.dev;
10222 struct drm_i915_private *dev_priv = dev->dev_private;
10223
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030010224 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10225 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10226 return true;
10227
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010228 /*
10229 * The relevant registers doen't exist on pre-ctg.
10230 * As the flip done interrupt doesn't trigger for mmio
10231 * flips on gmch platforms, a flip count check isn't
10232 * really needed there. But since ctg has the registers,
10233 * include it in the check anyway.
10234 */
10235 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10236 return true;
10237
10238 /*
10239 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10240 * used the same base address. In that case the mmio flip might
10241 * have completed, but the CS hasn't even executed the flip yet.
10242 *
10243 * A flip count check isn't enough as the CS might have updated
10244 * the base address just after start of vblank, but before we
10245 * managed to process the interrupt. This means we'd complete the
10246 * CS flip too soon.
10247 *
10248 * Combining both checks should get us a good enough result. It may
10249 * still happen that the CS flip has been executed, but has not
10250 * yet actually completed. But in case the base address is the same
10251 * anyway, we don't really care.
10252 */
10253 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10254 crtc->unpin_work->gtt_offset &&
10255 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
10256 crtc->unpin_work->flip_count);
10257}
10258
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010259void intel_prepare_page_flip(struct drm_device *dev, int plane)
10260{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010261 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010262 struct intel_crtc *intel_crtc =
10263 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10264 unsigned long flags;
10265
Daniel Vetterf3260382014-09-15 14:55:23 +020010266
10267 /*
10268 * This is called both by irq handlers and the reset code (to complete
10269 * lost pageflips) so needs the full irqsave spinlocks.
10270 *
10271 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000010272 * generate a page-flip completion irq, i.e. every modeset
10273 * is also accompanied by a spurious intel_prepare_page_flip().
10274 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010275 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010276 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000010277 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010278 spin_unlock_irqrestore(&dev->event_lock, flags);
10279}
10280
Robin Schroereba905b2014-05-18 02:24:50 +020010281static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
Chris Wilsone7d841c2012-12-03 11:36:30 +000010282{
10283 /* Ensure that the work item is consistent when activating it ... */
10284 smp_wmb();
10285 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
10286 /* and that it is marked active as soon as the irq could fire. */
10287 smp_wmb();
10288}
10289
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010290static int intel_gen2_queue_flip(struct drm_device *dev,
10291 struct drm_crtc *crtc,
10292 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010293 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010294 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010295 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010296{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010297 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010298 u32 flip_mask;
10299 int ret;
10300
Daniel Vetter6d90c952012-04-26 23:28:05 +020010301 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010302 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010303 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010304
10305 /* Can't queue multiple flips, so wait for the previous
10306 * one to finish before executing the next.
10307 */
10308 if (intel_crtc->plane)
10309 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10310 else
10311 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010312 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10313 intel_ring_emit(ring, MI_NOOP);
10314 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10315 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10316 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010317 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010318 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000010319
10320 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010321 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010322 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010323}
10324
10325static int intel_gen3_queue_flip(struct drm_device *dev,
10326 struct drm_crtc *crtc,
10327 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010328 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010329 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010330 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010331{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010332 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010333 u32 flip_mask;
10334 int ret;
10335
Daniel Vetter6d90c952012-04-26 23:28:05 +020010336 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010337 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010338 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010339
10340 if (intel_crtc->plane)
10341 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10342 else
10343 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010344 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10345 intel_ring_emit(ring, MI_NOOP);
10346 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
10347 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10348 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010349 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010350 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010351
Chris Wilsone7d841c2012-12-03 11:36:30 +000010352 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010353 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010354 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010355}
10356
10357static int intel_gen4_queue_flip(struct drm_device *dev,
10358 struct drm_crtc *crtc,
10359 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010360 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010361 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010362 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010363{
10364 struct drm_i915_private *dev_priv = dev->dev_private;
10365 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10366 uint32_t pf, pipesrc;
10367 int ret;
10368
Daniel Vetter6d90c952012-04-26 23:28:05 +020010369 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010370 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010371 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010372
10373 /* i965+ uses the linear or tiled offsets from the
10374 * Display Registers (which do not change across a page-flip)
10375 * so we need only reprogram the base address.
10376 */
Daniel Vetter6d90c952012-04-26 23:28:05 +020010377 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10378 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10379 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010380 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020010381 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010382
10383 /* XXX Enabling the panel-fitter across page-flip is so far
10384 * untested on non-native modes, so ignore it for now.
10385 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
10386 */
10387 pf = 0;
10388 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010389 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010390
10391 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010392 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010393 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010394}
10395
10396static int intel_gen6_queue_flip(struct drm_device *dev,
10397 struct drm_crtc *crtc,
10398 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010399 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010400 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010401 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010402{
10403 struct drm_i915_private *dev_priv = dev->dev_private;
10404 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10405 uint32_t pf, pipesrc;
10406 int ret;
10407
Daniel Vetter6d90c952012-04-26 23:28:05 +020010408 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010409 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010410 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010411
Daniel Vetter6d90c952012-04-26 23:28:05 +020010412 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10413 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10414 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010415 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010416
Chris Wilson99d9acd2012-04-17 20:37:00 +010010417 /* Contrary to the suggestions in the documentation,
10418 * "Enable Panel Fitter" does not seem to be required when page
10419 * flipping with a non-native mode, and worse causes a normal
10420 * modeset to fail.
10421 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
10422 */
10423 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010424 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010425 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010426
10427 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010428 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010429 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010430}
10431
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010432static int intel_gen7_queue_flip(struct drm_device *dev,
10433 struct drm_crtc *crtc,
10434 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010435 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010436 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010437 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010438{
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010439 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010440 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010010441 int len, ret;
10442
Robin Schroereba905b2014-05-18 02:24:50 +020010443 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010444 case PLANE_A:
10445 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
10446 break;
10447 case PLANE_B:
10448 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
10449 break;
10450 case PLANE_C:
10451 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
10452 break;
10453 default:
10454 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010455 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010456 }
10457
Chris Wilsonffe74d72013-08-26 20:58:12 +010010458 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +010010459 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010010460 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010010461 /*
10462 * On Gen 8, SRM is now taking an extra dword to accommodate
10463 * 48bits addresses, and we need a NOOP for the batch size to
10464 * stay even.
10465 */
10466 if (IS_GEN8(dev))
10467 len += 2;
10468 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010010469
Ville Syrjäläf66fab82014-02-11 19:52:06 +020010470 /*
10471 * BSpec MI_DISPLAY_FLIP for IVB:
10472 * "The full packet must be contained within the same cache line."
10473 *
10474 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
10475 * cacheline, if we ever start emitting more commands before
10476 * the MI_DISPLAY_FLIP we may need to first emit everything else,
10477 * then do the cacheline alignment, and finally emit the
10478 * MI_DISPLAY_FLIP.
10479 */
10480 ret = intel_ring_cacheline_align(ring);
10481 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010482 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020010483
Chris Wilsonffe74d72013-08-26 20:58:12 +010010484 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010485 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010486 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010487
Chris Wilsonffe74d72013-08-26 20:58:12 +010010488 /* Unmask the flip-done completion message. Note that the bspec says that
10489 * we should do this for both the BCS and RCS, and that we must not unmask
10490 * more than one flip event at any time (or ensure that one flip message
10491 * can be sent by waiting for flip-done prior to queueing new flips).
10492 * Experimentation says that BCS works despite DERRMR masking all
10493 * flip-done completion events and that unmasking all planes at once
10494 * for the RCS also doesn't appear to drop events. Setting the DERRMR
10495 * to zero does lead to lockups within MI_DISPLAY_FLIP.
10496 */
10497 if (ring->id == RCS) {
10498 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
10499 intel_ring_emit(ring, DERRMR);
10500 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
10501 DERRMR_PIPEB_PRI_FLIP_DONE |
10502 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010010503 if (IS_GEN8(dev))
10504 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
10505 MI_SRM_LRM_GLOBAL_GTT);
10506 else
10507 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
10508 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +010010509 intel_ring_emit(ring, DERRMR);
10510 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010010511 if (IS_GEN8(dev)) {
10512 intel_ring_emit(ring, 0);
10513 intel_ring_emit(ring, MI_NOOP);
10514 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010010515 }
10516
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010517 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010518 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010519 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010520 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000010521
10522 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010523 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010524 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010525}
10526
Sourab Gupta84c33a62014-06-02 16:47:17 +053010527static bool use_mmio_flip(struct intel_engine_cs *ring,
10528 struct drm_i915_gem_object *obj)
10529{
10530 /*
10531 * This is not being used for older platforms, because
10532 * non-availability of flip done interrupt forces us to use
10533 * CS flips. Older platforms derive flip done using some clever
10534 * tricks involving the flip_pending status bits and vblank irqs.
10535 * So using MMIO flips there would disrupt this mechanism.
10536 */
10537
Chris Wilson8e09bf82014-07-08 10:40:30 +010010538 if (ring == NULL)
10539 return true;
10540
Sourab Gupta84c33a62014-06-02 16:47:17 +053010541 if (INTEL_INFO(ring->dev)->gen < 5)
10542 return false;
10543
10544 if (i915.use_mmio_flip < 0)
10545 return false;
10546 else if (i915.use_mmio_flip > 0)
10547 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010010548 else if (i915.enable_execlists)
10549 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053010550 else
John Harrison41c52412014-11-24 18:49:43 +000010551 return ring != i915_gem_request_get_ring(obj->last_read_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010552}
10553
Damien Lespiauff944562014-11-20 14:58:16 +000010554static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
10555{
10556 struct drm_device *dev = intel_crtc->base.dev;
10557 struct drm_i915_private *dev_priv = dev->dev_private;
10558 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000010559 const enum pipe pipe = intel_crtc->pipe;
10560 u32 ctl, stride;
10561
10562 ctl = I915_READ(PLANE_CTL(pipe, 0));
10563 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010010564 switch (fb->modifier[0]) {
10565 case DRM_FORMAT_MOD_NONE:
10566 break;
10567 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000010568 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010010569 break;
10570 case I915_FORMAT_MOD_Y_TILED:
10571 ctl |= PLANE_CTL_TILED_Y;
10572 break;
10573 case I915_FORMAT_MOD_Yf_TILED:
10574 ctl |= PLANE_CTL_TILED_YF;
10575 break;
10576 default:
10577 MISSING_CASE(fb->modifier[0]);
10578 }
Damien Lespiauff944562014-11-20 14:58:16 +000010579
10580 /*
10581 * The stride is either expressed as a multiple of 64 bytes chunks for
10582 * linear buffers or in number of tiles for tiled buffers.
10583 */
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010010584 stride = fb->pitches[0] /
10585 intel_fb_stride_alignment(dev, fb->modifier[0],
10586 fb->pixel_format);
Damien Lespiauff944562014-11-20 14:58:16 +000010587
10588 /*
10589 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
10590 * PLANE_SURF updates, the update is then guaranteed to be atomic.
10591 */
10592 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
10593 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
10594
10595 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
10596 POSTING_READ(PLANE_SURF(pipe, 0));
10597}
10598
10599static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
Sourab Gupta84c33a62014-06-02 16:47:17 +053010600{
10601 struct drm_device *dev = intel_crtc->base.dev;
10602 struct drm_i915_private *dev_priv = dev->dev_private;
10603 struct intel_framebuffer *intel_fb =
10604 to_intel_framebuffer(intel_crtc->base.primary->fb);
10605 struct drm_i915_gem_object *obj = intel_fb->obj;
10606 u32 dspcntr;
10607 u32 reg;
10608
Sourab Gupta84c33a62014-06-02 16:47:17 +053010609 reg = DSPCNTR(intel_crtc->plane);
10610 dspcntr = I915_READ(reg);
10611
Damien Lespiauc5d97472014-10-25 00:11:11 +010010612 if (obj->tiling_mode != I915_TILING_NONE)
10613 dspcntr |= DISPPLANE_TILED;
10614 else
10615 dspcntr &= ~DISPPLANE_TILED;
10616
Sourab Gupta84c33a62014-06-02 16:47:17 +053010617 I915_WRITE(reg, dspcntr);
10618
10619 I915_WRITE(DSPSURF(intel_crtc->plane),
10620 intel_crtc->unpin_work->gtt_offset);
10621 POSTING_READ(DSPSURF(intel_crtc->plane));
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010622
Damien Lespiauff944562014-11-20 14:58:16 +000010623}
10624
10625/*
10626 * XXX: This is the temporary way to update the plane registers until we get
10627 * around to using the usual plane update functions for MMIO flips
10628 */
10629static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
10630{
10631 struct drm_device *dev = intel_crtc->base.dev;
10632 bool atomic_update;
10633 u32 start_vbl_count;
10634
10635 intel_mark_page_flip_active(intel_crtc);
10636
10637 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
10638
10639 if (INTEL_INFO(dev)->gen >= 9)
10640 skl_do_mmio_flip(intel_crtc);
10641 else
10642 /* use_mmio_flip() retricts MMIO flips to ilk+ */
10643 ilk_do_mmio_flip(intel_crtc);
10644
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010645 if (atomic_update)
10646 intel_pipe_update_end(intel_crtc, start_vbl_count);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010647}
10648
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010649static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053010650{
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010651 struct intel_crtc *crtc =
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010652 container_of(work, struct intel_crtc, mmio_flip.work);
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010653 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053010654
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010655 mmio_flip = &crtc->mmio_flip;
10656 if (mmio_flip->req)
John Harrison9c654812014-11-24 18:49:35 +000010657 WARN_ON(__i915_wait_request(mmio_flip->req,
10658 crtc->reset_counter,
10659 false, NULL, NULL) != 0);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010660
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010661 intel_do_mmio_flip(crtc);
10662 if (mmio_flip->req) {
10663 mutex_lock(&crtc->base.dev->struct_mutex);
John Harrison146d84f2014-12-05 13:49:33 +000010664 i915_gem_request_assign(&mmio_flip->req, NULL);
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010665 mutex_unlock(&crtc->base.dev->struct_mutex);
10666 }
Sourab Gupta84c33a62014-06-02 16:47:17 +053010667}
10668
10669static int intel_queue_mmio_flip(struct drm_device *dev,
10670 struct drm_crtc *crtc,
10671 struct drm_framebuffer *fb,
10672 struct drm_i915_gem_object *obj,
10673 struct intel_engine_cs *ring,
10674 uint32_t flags)
10675{
Sourab Gupta84c33a62014-06-02 16:47:17 +053010676 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010677
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010678 i915_gem_request_assign(&intel_crtc->mmio_flip.req,
10679 obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010680
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020010681 schedule_work(&intel_crtc->mmio_flip.work);
10682
Sourab Gupta84c33a62014-06-02 16:47:17 +053010683 return 0;
10684}
10685
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010686static int intel_default_queue_flip(struct drm_device *dev,
10687 struct drm_crtc *crtc,
10688 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010689 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010690 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010691 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010692{
10693 return -ENODEV;
10694}
10695
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010696static bool __intel_pageflip_stall_check(struct drm_device *dev,
10697 struct drm_crtc *crtc)
10698{
10699 struct drm_i915_private *dev_priv = dev->dev_private;
10700 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10701 struct intel_unpin_work *work = intel_crtc->unpin_work;
10702 u32 addr;
10703
10704 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
10705 return true;
10706
10707 if (!work->enable_stall_check)
10708 return false;
10709
10710 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010010711 if (work->flip_queued_req &&
10712 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010713 return false;
10714
Daniel Vetter1e3feef2015-02-13 21:03:45 +010010715 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010716 }
10717
Daniel Vetter1e3feef2015-02-13 21:03:45 +010010718 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010719 return false;
10720
10721 /* Potential stall - if we see that the flip has happened,
10722 * assume a missed interrupt. */
10723 if (INTEL_INFO(dev)->gen >= 4)
10724 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
10725 else
10726 addr = I915_READ(DSPADDR(intel_crtc->plane));
10727
10728 /* There is a potential issue here with a false positive after a flip
10729 * to the same address. We could address this by checking for a
10730 * non-incrementing frame counter.
10731 */
10732 return addr == work->gtt_offset;
10733}
10734
10735void intel_check_page_flip(struct drm_device *dev, int pipe)
10736{
10737 struct drm_i915_private *dev_priv = dev->dev_private;
10738 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10739 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010010740 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020010741
Dave Gordon6c51d462015-03-06 15:34:26 +000010742 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010743
10744 if (crtc == NULL)
10745 return;
10746
Daniel Vetterf3260382014-09-15 14:55:23 +020010747 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010010748 work = intel_crtc->unpin_work;
10749 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010750 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010010751 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010752 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010010753 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010754 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010010755 if (work != NULL &&
10756 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
10757 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020010758 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010759}
10760
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010761static int intel_crtc_page_flip(struct drm_crtc *crtc,
10762 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010763 struct drm_pending_vblank_event *event,
10764 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010765{
10766 struct drm_device *dev = crtc->dev;
10767 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070010768 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070010769 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010770 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080010771 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020010772 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010773 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010774 struct intel_engine_cs *ring;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010010775 bool mmio_flip;
Chris Wilson52e68632010-08-08 10:15:59 +010010776 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010777
Matt Roper2ff8fde2014-07-08 07:50:07 -070010778 /*
10779 * drm_mode_page_flip_ioctl() should already catch this, but double
10780 * check to be safe. In the future we may enable pageflipping from
10781 * a disabled primary plane.
10782 */
10783 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
10784 return -EBUSY;
10785
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030010786 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070010787 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030010788 return -EINVAL;
10789
10790 /*
10791 * TILEOFF/LINOFF registers can't be changed via MI display flips.
10792 * Note that pitch changes could also affect these register.
10793 */
10794 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070010795 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
10796 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030010797 return -EINVAL;
10798
Chris Wilsonf900db42014-02-20 09:26:13 +000010799 if (i915_terminally_wedged(&dev_priv->gpu_error))
10800 goto out_hang;
10801
Daniel Vetterb14c5672013-09-19 12:18:32 +020010802 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010803 if (work == NULL)
10804 return -ENOMEM;
10805
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010806 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010807 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010808 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010809 INIT_WORK(&work->work, intel_unpin_work_fn);
10810
Daniel Vetter87b6b102014-05-15 15:33:46 +020010811 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070010812 if (ret)
10813 goto free_work;
10814
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010815 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010816 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010817 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010818 /* Before declaring the flip queue wedged, check if
10819 * the hardware completed the operation behind our backs.
10820 */
10821 if (__intel_pageflip_stall_check(dev, crtc)) {
10822 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
10823 page_flip_completed(intel_crtc);
10824 } else {
10825 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010826 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010010827
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010828 drm_crtc_vblank_put(crtc);
10829 kfree(work);
10830 return -EBUSY;
10831 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010832 }
10833 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010834 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010835
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010836 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
10837 flush_workqueue(dev_priv->wq);
10838
Jesse Barnes75dfca82010-02-10 15:09:44 -080010839 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010840 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000010841 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010842
Matt Roperf4510a22014-04-01 15:22:40 -070010843 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080010844 update_state_fb(crtc->primary);
Matt Roper1ed1f962015-01-30 16:22:36 -080010845
Chris Wilsone1f99ce2010-10-27 12:45:26 +010010846 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010010847
Chris Wilson89ed88b2015-02-16 14:31:49 +000010848 ret = i915_mutex_lock_interruptible(dev);
10849 if (ret)
10850 goto cleanup;
10851
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010852 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020010853 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010010854
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010855 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Daniel Vettera071fa02014-06-18 23:28:09 +020010856 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010857
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010858 if (IS_VALLEYVIEW(dev)) {
10859 ring = &dev_priv->ring[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010860 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010010861 /* vlv: DISPLAY_FLIP fails to change tiling */
10862 ring = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000010863 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Chris Wilson2a92d5b2014-07-08 10:40:29 +010010864 ring = &dev_priv->ring[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010865 } else if (INTEL_INFO(dev)->gen >= 7) {
John Harrison41c52412014-11-24 18:49:43 +000010866 ring = i915_gem_request_get_ring(obj->last_read_req);
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010867 if (ring == NULL || ring->id != RCS)
10868 ring = &dev_priv->ring[BCS];
10869 } else {
10870 ring = &dev_priv->ring[RCS];
10871 }
10872
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010010873 mmio_flip = use_mmio_flip(ring, obj);
10874
10875 /* When using CS flips, we want to emit semaphores between rings.
10876 * However, when using mmio flips we will create a task to do the
10877 * synchronisation, so all we want here is to pin the framebuffer
10878 * into the display plane and skip any waits.
10879 */
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000010880 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010010881 crtc->primary->state,
10882 mmio_flip ? i915_gem_request_get_ring(obj->last_read_req) : ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010883 if (ret)
10884 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010885
Tvrtko Ursulin121920f2015-03-23 11:10:37 +000010886 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
10887 + intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010888
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010010889 if (mmio_flip) {
Sourab Gupta84c33a62014-06-02 16:47:17 +053010890 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
10891 page_flip_flags);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010892 if (ret)
10893 goto cleanup_unpin;
10894
John Harrisonf06cc1b2014-11-24 18:49:37 +000010895 i915_gem_request_assign(&work->flip_queued_req,
10896 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010897 } else {
Sourab Gupta84c33a62014-06-02 16:47:17 +053010898 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010899 page_flip_flags);
10900 if (ret)
10901 goto cleanup_unpin;
10902
John Harrisonf06cc1b2014-11-24 18:49:37 +000010903 i915_gem_request_assign(&work->flip_queued_req,
10904 intel_ring_get_request(ring));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010905 }
10906
Daniel Vetter1e3feef2015-02-13 21:03:45 +010010907 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010908 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010909
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010910 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Daniel Vettera071fa02014-06-18 23:28:09 +020010911 INTEL_FRONTBUFFER_PRIMARY(pipe));
10912
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020010913 intel_fbc_disable(dev);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010914 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010915 mutex_unlock(&dev->struct_mutex);
10916
Jesse Barnese5510fa2010-07-01 16:48:37 -070010917 trace_i915_flip_request(intel_crtc->plane, obj);
10918
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010919 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010010920
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010921cleanup_unpin:
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000010922 intel_unpin_fb_obj(fb, crtc->primary->state);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010923cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010924 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000010925 mutex_unlock(&dev->struct_mutex);
10926cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070010927 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080010928 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010010929
Chris Wilson89ed88b2015-02-16 14:31:49 +000010930 drm_gem_object_unreference_unlocked(&obj->base);
10931 drm_framebuffer_unreference(work->old_fb);
10932
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010933 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010010934 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010935 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010010936
Daniel Vetter87b6b102014-05-15 15:33:46 +020010937 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070010938free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010010939 kfree(work);
10940
Chris Wilsonf900db42014-02-20 09:26:13 +000010941 if (ret == -EIO) {
10942out_hang:
Matt Roper53a366b2014-12-23 10:41:53 -080010943 ret = intel_plane_restore(primary);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010010944 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010945 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020010946 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010947 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010010948 }
Chris Wilsonf900db42014-02-20 09:26:13 +000010949 }
Chris Wilson96b099f2010-06-07 14:03:04 +010010950 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010951}
10952
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010953static struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010954 .mode_set_base_atomic = intel_pipe_set_base_atomic,
10955 .load_lut = intel_crtc_load_lut,
Matt Roperea2c67b2014-12-23 10:41:52 -080010956 .atomic_begin = intel_begin_crtc_commit,
10957 .atomic_flush = intel_finish_crtc_commit,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010958};
10959
Daniel Vetter9a935852012-07-05 22:34:27 +020010960/**
10961 * intel_modeset_update_staged_output_state
10962 *
10963 * Updates the staged output configuration state, e.g. after we've read out the
10964 * current hw state.
10965 */
10966static void intel_modeset_update_staged_output_state(struct drm_device *dev)
10967{
Ville Syrjälä76688512014-01-10 11:28:06 +020010968 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020010969 struct intel_encoder *encoder;
10970 struct intel_connector *connector;
10971
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020010972 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020010973 connector->new_encoder =
10974 to_intel_encoder(connector->base.encoder);
10975 }
10976
Damien Lespiaub2784e12014-08-05 11:29:37 +010010977 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020010978 encoder->new_crtc =
10979 to_intel_crtc(encoder->base.crtc);
10980 }
Ville Syrjälä76688512014-01-10 11:28:06 +020010981
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010982 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080010983 crtc->new_enabled = crtc->base.state->enable;
Ville Syrjälä76688512014-01-10 11:28:06 +020010984 }
Daniel Vetter9a935852012-07-05 22:34:27 +020010985}
10986
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020010987/* Transitional helper to copy current connector/encoder state to
10988 * connector->state. This is needed so that code that is partially
10989 * converted to atomic does the right thing.
10990 */
10991static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
10992{
10993 struct intel_connector *connector;
10994
10995 for_each_intel_connector(dev, connector) {
10996 if (connector->base.encoder) {
10997 connector->base.state->best_encoder =
10998 connector->base.encoder;
10999 connector->base.state->crtc =
11000 connector->base.encoder->crtc;
11001 } else {
11002 connector->base.state->best_encoder = NULL;
11003 connector->base.state->crtc = NULL;
11004 }
11005 }
11006}
11007
Daniel Vetter9a935852012-07-05 22:34:27 +020011008/**
11009 * intel_modeset_commit_output_state
11010 *
11011 * This function copies the stage display pipe configuration to the real one.
11012 */
11013static void intel_modeset_commit_output_state(struct drm_device *dev)
11014{
Ville Syrjälä76688512014-01-10 11:28:06 +020011015 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011016 struct intel_encoder *encoder;
11017 struct intel_connector *connector;
11018
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011019 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011020 connector->base.encoder = &connector->new_encoder->base;
11021 }
11022
Damien Lespiaub2784e12014-08-05 11:29:37 +010011023 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011024 encoder->base.crtc = &encoder->new_crtc->base;
11025 }
Ville Syrjälä76688512014-01-10 11:28:06 +020011026
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011027 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011028 crtc->base.state->enable = crtc->new_enabled;
Ville Syrjälä76688512014-01-10 11:28:06 +020011029 crtc->base.enabled = crtc->new_enabled;
11030 }
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011031
11032 intel_modeset_update_connector_atomic_state(dev);
Daniel Vetter9a935852012-07-05 22:34:27 +020011033}
11034
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011035static void
Robin Schroereba905b2014-05-18 02:24:50 +020011036connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011037 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011038{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011039 int bpp = pipe_config->pipe_bpp;
11040
11041 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11042 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011043 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011044
11045 /* Don't use an invalid EDID bpc value */
11046 if (connector->base.display_info.bpc &&
11047 connector->base.display_info.bpc * 3 < bpp) {
11048 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11049 bpp, connector->base.display_info.bpc*3);
11050 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11051 }
11052
11053 /* Clamp bpp to 8 on screens without EDID 1.4 */
11054 if (connector->base.display_info.bpc == 0 && bpp > 24) {
11055 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11056 bpp);
11057 pipe_config->pipe_bpp = 24;
11058 }
11059}
11060
11061static int
11062compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011063 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011064{
11065 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011066 struct drm_atomic_state *state;
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011067 struct intel_connector *connector;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011068 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011069
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011070 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011071 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011072 else if (INTEL_INFO(dev)->gen >= 5)
11073 bpp = 12*3;
11074 else
11075 bpp = 8*3;
11076
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011077
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011078 pipe_config->pipe_bpp = bpp;
11079
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011080 state = pipe_config->base.state;
11081
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011082 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011083 for (i = 0; i < state->num_connector; i++) {
11084 if (!state->connectors[i])
11085 continue;
11086
11087 connector = to_intel_connector(state->connectors[i]);
11088 if (state->connector_states[i]->crtc != &crtc->base)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011089 continue;
11090
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011091 connected_sink_compute_bpp(connector, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011092 }
11093
11094 return bpp;
11095}
11096
Daniel Vetter644db712013-09-19 14:53:58 +020011097static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11098{
11099 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11100 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010011101 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020011102 mode->crtc_hdisplay, mode->crtc_hsync_start,
11103 mode->crtc_hsync_end, mode->crtc_htotal,
11104 mode->crtc_vdisplay, mode->crtc_vsync_start,
11105 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11106}
11107
Daniel Vetterc0b03412013-05-28 12:05:54 +020011108static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011109 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020011110 const char *context)
11111{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011112 struct drm_device *dev = crtc->base.dev;
11113 struct drm_plane *plane;
11114 struct intel_plane *intel_plane;
11115 struct intel_plane_state *state;
11116 struct drm_framebuffer *fb;
11117
11118 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
11119 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020011120
11121 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
11122 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
11123 pipe_config->pipe_bpp, pipe_config->dither);
11124 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11125 pipe_config->has_pch_encoder,
11126 pipe_config->fdi_lanes,
11127 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
11128 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
11129 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011130 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11131 pipe_config->has_dp_encoder,
11132 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
11133 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
11134 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011135
11136 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
11137 pipe_config->has_dp_encoder,
11138 pipe_config->dp_m2_n2.gmch_m,
11139 pipe_config->dp_m2_n2.gmch_n,
11140 pipe_config->dp_m2_n2.link_m,
11141 pipe_config->dp_m2_n2.link_n,
11142 pipe_config->dp_m2_n2.tu);
11143
Daniel Vetter55072d12014-11-20 16:10:28 +010011144 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
11145 pipe_config->has_audio,
11146 pipe_config->has_infoframe);
11147
Daniel Vetterc0b03412013-05-28 12:05:54 +020011148 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011149 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011150 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011151 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11152 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030011153 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030011154 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
11155 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011156 DRM_DEBUG_KMS("num_scalers: %d\n", crtc->num_scalers);
11157 DRM_DEBUG_KMS("scaler_users: 0x%x\n", pipe_config->scaler_state.scaler_users);
11158 DRM_DEBUG_KMS("scaler id: %d\n", pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011159 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11160 pipe_config->gmch_pfit.control,
11161 pipe_config->gmch_pfit.pgm_ratios,
11162 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011163 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020011164 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011165 pipe_config->pch_pfit.size,
11166 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030011167 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030011168 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011169
11170 DRM_DEBUG_KMS("planes on this crtc\n");
11171 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
11172 intel_plane = to_intel_plane(plane);
11173 if (intel_plane->pipe != crtc->pipe)
11174 continue;
11175
11176 state = to_intel_plane_state(plane->state);
11177 fb = state->base.fb;
11178 if (!fb) {
11179 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
11180 "disabled, scaler_id = %d\n",
11181 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11182 plane->base.id, intel_plane->pipe,
11183 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
11184 drm_plane_index(plane), state->scaler_id);
11185 continue;
11186 }
11187
11188 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
11189 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11190 plane->base.id, intel_plane->pipe,
11191 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
11192 drm_plane_index(plane));
11193 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
11194 fb->base.id, fb->width, fb->height, fb->pixel_format);
11195 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
11196 state->scaler_id,
11197 state->src.x1 >> 16, state->src.y1 >> 16,
11198 drm_rect_width(&state->src) >> 16,
11199 drm_rect_height(&state->src) >> 16,
11200 state->dst.x1, state->dst.y1,
11201 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
11202 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020011203}
11204
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011205static bool encoders_cloneable(const struct intel_encoder *a,
11206 const struct intel_encoder *b)
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011207{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011208 /* masks could be asymmetric, so check both ways */
11209 return a == b || (a->cloneable & (1 << b->type) &&
11210 b->cloneable & (1 << a->type));
11211}
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011212
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011213static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11214 struct intel_crtc *crtc,
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011215 struct intel_encoder *encoder)
11216{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011217 struct intel_encoder *source_encoder;
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011218 struct drm_connector_state *connector_state;
11219 int i;
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011220
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011221 for (i = 0; i < state->num_connector; i++) {
11222 if (!state->connectors[i])
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011223 continue;
11224
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011225 connector_state = state->connector_states[i];
11226 if (connector_state->crtc != &crtc->base)
11227 continue;
11228
11229 source_encoder =
11230 to_intel_encoder(connector_state->best_encoder);
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011231 if (!encoders_cloneable(encoder, source_encoder))
11232 return false;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011233 }
11234
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011235 return true;
11236}
11237
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011238static bool check_encoder_cloning(struct drm_atomic_state *state,
11239 struct intel_crtc *crtc)
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011240{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011241 struct intel_encoder *encoder;
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011242 struct drm_connector_state *connector_state;
11243 int i;
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011244
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011245 for (i = 0; i < state->num_connector; i++) {
11246 if (!state->connectors[i])
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011247 continue;
11248
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011249 connector_state = state->connector_states[i];
11250 if (connector_state->crtc != &crtc->base)
11251 continue;
11252
11253 encoder = to_intel_encoder(connector_state->best_encoder);
11254 if (!check_single_encoder_cloning(state, crtc, encoder))
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011255 return false;
11256 }
11257
11258 return true;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011259}
11260
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011261static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011262{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011263 struct drm_device *dev = state->dev;
11264 struct intel_encoder *encoder;
11265 struct drm_connector_state *connector_state;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011266 unsigned int used_ports = 0;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011267 int i;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011268
11269 /*
11270 * Walk the connector list instead of the encoder
11271 * list to detect the problem on ddi platforms
11272 * where there's just one encoder per digital port.
11273 */
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011274 for (i = 0; i < state->num_connector; i++) {
11275 if (!state->connectors[i])
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011276 continue;
11277
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011278 connector_state = state->connector_states[i];
11279 if (!connector_state->best_encoder)
11280 continue;
11281
11282 encoder = to_intel_encoder(connector_state->best_encoder);
11283
11284 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011285
11286 switch (encoder->type) {
11287 unsigned int port_mask;
11288 case INTEL_OUTPUT_UNKNOWN:
11289 if (WARN_ON(!HAS_DDI(dev)))
11290 break;
11291 case INTEL_OUTPUT_DISPLAYPORT:
11292 case INTEL_OUTPUT_HDMI:
11293 case INTEL_OUTPUT_EDP:
11294 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
11295
11296 /* the same port mustn't appear more than once */
11297 if (used_ports & port_mask)
11298 return false;
11299
11300 used_ports |= port_mask;
11301 default:
11302 break;
11303 }
11304 }
11305
11306 return true;
11307}
11308
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011309static void
11310clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
11311{
11312 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070011313 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011314
Chandra Konduru663a3642015-04-07 15:28:41 -070011315 /* Clear only the intel specific part of the crtc state excluding scalers */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011316 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070011317 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011318 memset(crtc_state, 0, sizeof *crtc_state);
11319 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070011320 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011321}
11322
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011323static struct intel_crtc_state *
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011324intel_modeset_pipe_config(struct drm_crtc *crtc,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011325 struct drm_display_mode *mode,
11326 struct drm_atomic_state *state)
Daniel Vetter7758a112012-07-08 19:40:39 +020011327{
Daniel Vetter7758a112012-07-08 19:40:39 +020011328 struct intel_encoder *encoder;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011329 struct intel_connector *connector;
11330 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011331 struct intel_crtc_state *pipe_config;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011332 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011333 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010011334 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020011335
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011336 if (!check_encoder_cloning(state, to_intel_crtc(crtc))) {
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011337 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11338 return ERR_PTR(-EINVAL);
11339 }
11340
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011341 if (!check_digital_port_conflicts(state)) {
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011342 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
11343 return ERR_PTR(-EINVAL);
11344 }
11345
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011346 pipe_config = intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));
11347 if (IS_ERR(pipe_config))
11348 return pipe_config;
11349
11350 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020011351
Matt Roper07878242015-02-25 11:43:26 -080011352 pipe_config->base.crtc = crtc;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011353 drm_mode_copy(&pipe_config->base.adjusted_mode, mode);
11354 drm_mode_copy(&pipe_config->base.mode, mode);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030011355
Daniel Vettere143a212013-07-04 12:01:15 +020011356 pipe_config->cpu_transcoder =
11357 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +020011358 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011359
Imre Deak2960bc92013-07-30 13:36:32 +030011360 /*
11361 * Sanitize sync polarity flags based on requested ones. If neither
11362 * positive or negative polarity is requested, treat this as meaning
11363 * negative polarity.
11364 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011365 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030011366 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011367 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030011368
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011369 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030011370 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011371 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030011372
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011373 /* Compute a starting value for pipe_config->pipe_bpp taking the source
11374 * plane pixel format and any sink constraints into account. Returns the
11375 * source plane bpp so that dithering can be selected on mismatches
11376 * after encoders and crtc also have had their say. */
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011377 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
11378 pipe_config);
11379 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011380 goto fail;
11381
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030011382 /*
11383 * Determine the real pipe dimensions. Note that stereo modes can
11384 * increase the actual pipe size due to the frame doubling and
11385 * insertion of additional space for blanks between the frame. This
11386 * is stored in the crtc timings. We use the requested mode to do this
11387 * computation to clearly distinguish it from the adjusted mode, which
11388 * can be changed by the connectors in the below retry loop.
11389 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011390 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080011391 &pipe_config->pipe_src_w,
11392 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030011393
Daniel Vettere29c22c2013-02-21 00:00:16 +010011394encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020011395 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020011396 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020011397 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020011398
Daniel Vetter135c81b2013-07-21 21:37:09 +020011399 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011400 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
11401 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020011402
Daniel Vetter7758a112012-07-08 19:40:39 +020011403 /* Pass our mode to the connectors and the CRTC to give them a chance to
11404 * adjust it according to limitations or connector properties, and also
11405 * a chance to reject the mode entirely.
11406 */
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011407 for (i = 0; i < state->num_connector; i++) {
11408 connector = to_intel_connector(state->connectors[i]);
11409 if (!connector)
Daniel Vetter7758a112012-07-08 19:40:39 +020011410 continue;
Daniel Vetter7ae89232013-03-27 00:44:52 +010011411
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011412 connector_state = state->connector_states[i];
11413 if (connector_state->crtc != crtc)
11414 continue;
11415
11416 encoder = to_intel_encoder(connector_state->best_encoder);
11417
Daniel Vetterefea6e82013-07-21 21:36:59 +020011418 if (!(encoder->compute_config(encoder, pipe_config))) {
11419 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020011420 goto fail;
11421 }
11422 }
11423
Daniel Vetterff9a6752013-06-01 17:16:21 +020011424 /* Set default port clock if not overwritten by the encoder. Needs to be
11425 * done afterwards in case the encoder adjusts the mode. */
11426 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011427 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010011428 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020011429
Daniel Vettera43f6e02013-06-07 23:10:32 +020011430 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010011431 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020011432 DRM_DEBUG_KMS("CRTC fixup failed\n");
11433 goto fail;
11434 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010011435
11436 if (ret == RETRY) {
11437 if (WARN(!retry, "loop in pipe configuration computation\n")) {
11438 ret = -EINVAL;
11439 goto fail;
11440 }
11441
11442 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
11443 retry = false;
11444 goto encoder_retry;
11445 }
11446
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011447 pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011448 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011449 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011450
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011451 return pipe_config;
Daniel Vetter7758a112012-07-08 19:40:39 +020011452fail:
Daniel Vettere29c22c2013-02-21 00:00:16 +010011453 return ERR_PTR(ret);
Daniel Vetter7758a112012-07-08 19:40:39 +020011454}
11455
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011456/* Computes which crtcs are affected and sets the relevant bits in the mask. For
11457 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
11458static void
11459intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
11460 unsigned *prepare_pipes, unsigned *disable_pipes)
11461{
11462 struct intel_crtc *intel_crtc;
11463 struct drm_device *dev = crtc->dev;
11464 struct intel_encoder *encoder;
11465 struct intel_connector *connector;
11466 struct drm_crtc *tmp_crtc;
11467
11468 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
11469
11470 /* Check which crtcs have changed outputs connected to them, these need
11471 * to be part of the prepare_pipes mask. We don't (yet) support global
11472 * modeset across multiple crtcs, so modeset_pipes will only have one
11473 * bit set at most. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011474 for_each_intel_connector(dev, connector) {
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011475 if (connector->base.encoder == &connector->new_encoder->base)
11476 continue;
11477
11478 if (connector->base.encoder) {
11479 tmp_crtc = connector->base.encoder->crtc;
11480
11481 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
11482 }
11483
11484 if (connector->new_encoder)
11485 *prepare_pipes |=
11486 1 << connector->new_encoder->new_crtc->pipe;
11487 }
11488
Damien Lespiaub2784e12014-08-05 11:29:37 +010011489 for_each_intel_encoder(dev, encoder) {
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011490 if (encoder->base.crtc == &encoder->new_crtc->base)
11491 continue;
11492
11493 if (encoder->base.crtc) {
11494 tmp_crtc = encoder->base.crtc;
11495
11496 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
11497 }
11498
11499 if (encoder->new_crtc)
11500 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
11501 }
11502
Ville Syrjälä76688512014-01-10 11:28:06 +020011503 /* Check for pipes that will be enabled/disabled ... */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011504 for_each_intel_crtc(dev, intel_crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011505 if (intel_crtc->base.state->enable == intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011506 continue;
11507
Ville Syrjälä76688512014-01-10 11:28:06 +020011508 if (!intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011509 *disable_pipes |= 1 << intel_crtc->pipe;
Ville Syrjälä76688512014-01-10 11:28:06 +020011510 else
11511 *prepare_pipes |= 1 << intel_crtc->pipe;
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011512 }
11513
11514
11515 /* set_mode is also used to update properties on life display pipes. */
11516 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä76688512014-01-10 11:28:06 +020011517 if (intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011518 *prepare_pipes |= 1 << intel_crtc->pipe;
11519
Daniel Vetterb6c51642013-04-12 18:48:43 +020011520 /*
11521 * For simplicity do a full modeset on any pipe where the output routing
11522 * changed. We could be more clever, but that would require us to be
11523 * more careful with calling the relevant encoder->mode_set functions.
11524 */
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011525 if (*prepare_pipes)
11526 *modeset_pipes = *prepare_pipes;
11527
11528 /* ... and mask these out. */
11529 *modeset_pipes &= ~(*disable_pipes);
11530 *prepare_pipes &= ~(*disable_pipes);
Daniel Vetterb6c51642013-04-12 18:48:43 +020011531
11532 /*
11533 * HACK: We don't (yet) fully support global modesets. intel_set_config
11534 * obies this rule, but the modeset restore mode of
11535 * intel_modeset_setup_hw_state does not.
11536 */
11537 *modeset_pipes &= 1 << intel_crtc->pipe;
11538 *prepare_pipes &= 1 << intel_crtc->pipe;
Daniel Vettere3641d32013-04-11 19:49:07 +020011539
11540 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
11541 *modeset_pipes, *prepare_pipes, *disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011542}
11543
Daniel Vetterea9d7582012-07-10 10:42:52 +020011544static bool intel_crtc_in_use(struct drm_crtc *crtc)
11545{
11546 struct drm_encoder *encoder;
11547 struct drm_device *dev = crtc->dev;
11548
11549 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
11550 if (encoder->crtc == crtc)
11551 return true;
11552
11553 return false;
11554}
11555
11556static void
11557intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
11558{
Daniel Vetterba41c0de2014-11-03 15:04:55 +010011559 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011560 struct intel_encoder *intel_encoder;
11561 struct intel_crtc *intel_crtc;
11562 struct drm_connector *connector;
11563
Daniel Vetterba41c0de2014-11-03 15:04:55 +010011564 intel_shared_dpll_commit(dev_priv);
11565
Damien Lespiaub2784e12014-08-05 11:29:37 +010011566 for_each_intel_encoder(dev, intel_encoder) {
Daniel Vetterea9d7582012-07-10 10:42:52 +020011567 if (!intel_encoder->base.crtc)
11568 continue;
11569
11570 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
11571
11572 if (prepare_pipes & (1 << intel_crtc->pipe))
11573 intel_encoder->connectors_active = false;
11574 }
11575
11576 intel_modeset_commit_output_state(dev);
11577
Ville Syrjälä76688512014-01-10 11:28:06 +020011578 /* Double check state. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011579 for_each_intel_crtc(dev, intel_crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011580 WARN_ON(intel_crtc->base.state->enable != intel_crtc_in_use(&intel_crtc->base));
Daniel Vetterea9d7582012-07-10 10:42:52 +020011581 }
11582
11583 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
11584 if (!connector->encoder || !connector->encoder->crtc)
11585 continue;
11586
11587 intel_crtc = to_intel_crtc(connector->encoder->crtc);
11588
11589 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +020011590 struct drm_property *dpms_property =
11591 dev->mode_config.dpms_property;
11592
Daniel Vetterea9d7582012-07-10 10:42:52 +020011593 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -050011594 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +020011595 dpms_property,
11596 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +020011597
11598 intel_encoder = to_intel_encoder(connector->encoder);
11599 intel_encoder->connectors_active = true;
11600 }
11601 }
11602
11603}
11604
Ville Syrjälä3bd26262013-09-06 23:29:02 +030011605static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011606{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030011607 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011608
11609 if (clock1 == clock2)
11610 return true;
11611
11612 if (!clock1 || !clock2)
11613 return false;
11614
11615 diff = abs(clock1 - clock2);
11616
11617 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
11618 return true;
11619
11620 return false;
11621}
11622
Daniel Vetter25c5b262012-07-08 22:08:04 +020011623#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
11624 list_for_each_entry((intel_crtc), \
11625 &(dev)->mode_config.crtc_list, \
11626 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +020011627 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020011628
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011629static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020011630intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011631 struct intel_crtc_state *current_config,
11632 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011633{
Daniel Vetter66e985c2013-06-05 13:34:20 +020011634#define PIPE_CONF_CHECK_X(name) \
11635 if (current_config->name != pipe_config->name) { \
11636 DRM_ERROR("mismatch in " #name " " \
11637 "(expected 0x%08x, found 0x%08x)\n", \
11638 current_config->name, \
11639 pipe_config->name); \
11640 return false; \
11641 }
11642
Daniel Vetter08a24032013-04-19 11:25:34 +020011643#define PIPE_CONF_CHECK_I(name) \
11644 if (current_config->name != pipe_config->name) { \
11645 DRM_ERROR("mismatch in " #name " " \
11646 "(expected %i, found %i)\n", \
11647 current_config->name, \
11648 pipe_config->name); \
11649 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010011650 }
11651
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011652/* This is required for BDW+ where there is only one set of registers for
11653 * switching between high and low RR.
11654 * This macro can be used whenever a comparison has to be made between one
11655 * hw state and multiple sw state variables.
11656 */
11657#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
11658 if ((current_config->name != pipe_config->name) && \
11659 (current_config->alt_name != pipe_config->name)) { \
11660 DRM_ERROR("mismatch in " #name " " \
11661 "(expected %i or %i, found %i)\n", \
11662 current_config->name, \
11663 current_config->alt_name, \
11664 pipe_config->name); \
11665 return false; \
11666 }
11667
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011668#define PIPE_CONF_CHECK_FLAGS(name, mask) \
11669 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -070011670 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011671 "(expected %i, found %i)\n", \
11672 current_config->name & (mask), \
11673 pipe_config->name & (mask)); \
11674 return false; \
11675 }
11676
Ville Syrjälä5e550652013-09-06 23:29:07 +030011677#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
11678 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
11679 DRM_ERROR("mismatch in " #name " " \
11680 "(expected %i, found %i)\n", \
11681 current_config->name, \
11682 pipe_config->name); \
11683 return false; \
11684 }
11685
Daniel Vetterbb760062013-06-06 14:55:52 +020011686#define PIPE_CONF_QUIRK(quirk) \
11687 ((current_config->quirks | pipe_config->quirks) & (quirk))
11688
Daniel Vettereccb1402013-05-22 00:50:22 +020011689 PIPE_CONF_CHECK_I(cpu_transcoder);
11690
Daniel Vetter08a24032013-04-19 11:25:34 +020011691 PIPE_CONF_CHECK_I(has_pch_encoder);
11692 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +020011693 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
11694 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
11695 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
11696 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
11697 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +020011698
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011699 PIPE_CONF_CHECK_I(has_dp_encoder);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011700
11701 if (INTEL_INFO(dev)->gen < 8) {
11702 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
11703 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
11704 PIPE_CONF_CHECK_I(dp_m_n.link_m);
11705 PIPE_CONF_CHECK_I(dp_m_n.link_n);
11706 PIPE_CONF_CHECK_I(dp_m_n.tu);
11707
11708 if (current_config->has_drrs) {
11709 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
11710 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
11711 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
11712 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
11713 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
11714 }
11715 } else {
11716 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
11717 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
11718 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
11719 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
11720 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
11721 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011722
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011723 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
11724 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
11725 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
11726 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
11727 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
11728 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011729
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011730 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
11731 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
11732 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
11733 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
11734 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
11735 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011736
Daniel Vetterc93f54c2013-06-27 19:47:19 +020011737 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b2014-04-24 23:54:47 +020011738 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020011739 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
11740 IS_VALLEYVIEW(dev))
11741 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080011742 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020011743
Daniel Vetter9ed109a2014-04-24 23:54:52 +020011744 PIPE_CONF_CHECK_I(has_audio);
11745
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011746 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011747 DRM_MODE_FLAG_INTERLACE);
11748
Daniel Vetterbb760062013-06-06 14:55:52 +020011749 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011750 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011751 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011752 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011753 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011754 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011755 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011756 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011757 DRM_MODE_FLAG_NVSYNC);
11758 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070011759
Ville Syrjälä37327ab2013-09-04 18:25:28 +030011760 PIPE_CONF_CHECK_I(pipe_src_w);
11761 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011762
Daniel Vetter99535992014-04-13 12:00:33 +020011763 /*
11764 * FIXME: BIOS likes to set up a cloned config with lvds+external
11765 * screen. Since we don't yet re-compute the pipe config when moving
11766 * just the lvds port away to another pipe the sw tracking won't match.
11767 *
11768 * Proper atomic modesets with recomputed global state will fix this.
11769 * Until then just don't check gmch state for inherited modes.
11770 */
11771 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
11772 PIPE_CONF_CHECK_I(gmch_pfit.control);
11773 /* pfit ratios are autocomputed by the hw on gen4+ */
11774 if (INTEL_INFO(dev)->gen < 4)
11775 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
11776 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
11777 }
11778
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011779 PIPE_CONF_CHECK_I(pch_pfit.enabled);
11780 if (current_config->pch_pfit.enabled) {
11781 PIPE_CONF_CHECK_I(pch_pfit.pos);
11782 PIPE_CONF_CHECK_I(pch_pfit.size);
11783 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020011784
Chandra Kondurua1b22782015-04-07 15:28:45 -070011785 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
11786
Jesse Barnese59150d2014-01-07 13:30:45 -080011787 /* BDW+ don't expose a synchronous way to read the state */
11788 if (IS_HASWELL(dev))
11789 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030011790
Ville Syrjälä282740f2013-09-04 18:30:03 +030011791 PIPE_CONF_CHECK_I(double_wide);
11792
Daniel Vetter26804af2014-06-25 22:01:55 +030011793 PIPE_CONF_CHECK_X(ddi_pll_sel);
11794
Daniel Vetterc0d43d62013-06-07 23:11:08 +020011795 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020011796 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020011797 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020011798 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
11799 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030011800 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000011801 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
11802 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
11803 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020011804
Ville Syrjälä42571ae2013-09-06 23:29:00 +030011805 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
11806 PIPE_CONF_CHECK_I(pipe_bpp);
11807
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011808 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080011809 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030011810
Daniel Vetter66e985c2013-06-05 13:34:20 +020011811#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020011812#undef PIPE_CONF_CHECK_I
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011813#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011814#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030011815#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020011816#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +020011817
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011818 return true;
11819}
11820
Damien Lespiau08db6652014-11-04 17:06:52 +000011821static void check_wm_state(struct drm_device *dev)
11822{
11823 struct drm_i915_private *dev_priv = dev->dev_private;
11824 struct skl_ddb_allocation hw_ddb, *sw_ddb;
11825 struct intel_crtc *intel_crtc;
11826 int plane;
11827
11828 if (INTEL_INFO(dev)->gen < 9)
11829 return;
11830
11831 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
11832 sw_ddb = &dev_priv->wm.skl_hw.ddb;
11833
11834 for_each_intel_crtc(dev, intel_crtc) {
11835 struct skl_ddb_entry *hw_entry, *sw_entry;
11836 const enum pipe pipe = intel_crtc->pipe;
11837
11838 if (!intel_crtc->active)
11839 continue;
11840
11841 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000011842 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000011843 hw_entry = &hw_ddb.plane[pipe][plane];
11844 sw_entry = &sw_ddb->plane[pipe][plane];
11845
11846 if (skl_ddb_entry_equal(hw_entry, sw_entry))
11847 continue;
11848
11849 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
11850 "(expected (%u,%u), found (%u,%u))\n",
11851 pipe_name(pipe), plane + 1,
11852 sw_entry->start, sw_entry->end,
11853 hw_entry->start, hw_entry->end);
11854 }
11855
11856 /* cursor */
11857 hw_entry = &hw_ddb.cursor[pipe];
11858 sw_entry = &sw_ddb->cursor[pipe];
11859
11860 if (skl_ddb_entry_equal(hw_entry, sw_entry))
11861 continue;
11862
11863 DRM_ERROR("mismatch in DDB state pipe %c cursor "
11864 "(expected (%u,%u), found (%u,%u))\n",
11865 pipe_name(pipe),
11866 sw_entry->start, sw_entry->end,
11867 hw_entry->start, hw_entry->end);
11868 }
11869}
11870
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011871static void
11872check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011873{
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011874 struct intel_connector *connector;
11875
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011876 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011877 /* This also checks the encoder/connector hw state with the
11878 * ->get_hw_state callbacks. */
11879 intel_connector_check_state(connector);
11880
Rob Clarke2c719b2014-12-15 13:56:32 -050011881 I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011882 "connector's staged encoder doesn't match current encoder\n");
11883 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011884}
11885
11886static void
11887check_encoder_state(struct drm_device *dev)
11888{
11889 struct intel_encoder *encoder;
11890 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011891
Damien Lespiaub2784e12014-08-05 11:29:37 +010011892 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011893 bool enabled = false;
11894 bool active = false;
11895 enum pipe pipe, tracked_pipe;
11896
11897 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
11898 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030011899 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011900
Rob Clarke2c719b2014-12-15 13:56:32 -050011901 I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011902 "encoder's stage crtc doesn't match current crtc\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050011903 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011904 "encoder's active_connectors set, but no crtc\n");
11905
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011906 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011907 if (connector->base.encoder != &encoder->base)
11908 continue;
11909 enabled = true;
11910 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
11911 active = true;
11912 }
Dave Airlie0e32b392014-05-02 14:02:48 +100011913 /*
11914 * for MST connectors if we unplug the connector is gone
11915 * away but the encoder is still connected to a crtc
11916 * until a modeset happens in response to the hotplug.
11917 */
11918 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
11919 continue;
11920
Rob Clarke2c719b2014-12-15 13:56:32 -050011921 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011922 "encoder's enabled state mismatch "
11923 "(expected %i, found %i)\n",
11924 !!encoder->base.crtc, enabled);
Rob Clarke2c719b2014-12-15 13:56:32 -050011925 I915_STATE_WARN(active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011926 "active encoder with no crtc\n");
11927
Rob Clarke2c719b2014-12-15 13:56:32 -050011928 I915_STATE_WARN(encoder->connectors_active != active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011929 "encoder's computed active state doesn't match tracked active state "
11930 "(expected %i, found %i)\n", active, encoder->connectors_active);
11931
11932 active = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -050011933 I915_STATE_WARN(active != encoder->connectors_active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011934 "encoder's hw state doesn't match sw tracking "
11935 "(expected %i, found %i)\n",
11936 encoder->connectors_active, active);
11937
11938 if (!encoder->base.crtc)
11939 continue;
11940
11941 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
Rob Clarke2c719b2014-12-15 13:56:32 -050011942 I915_STATE_WARN(active && pipe != tracked_pipe,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011943 "active encoder's pipe doesn't match"
11944 "(expected %i, found %i)\n",
11945 tracked_pipe, pipe);
11946
11947 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011948}
11949
11950static void
11951check_crtc_state(struct drm_device *dev)
11952{
Jani Nikulafbee40d2014-03-31 14:27:18 +030011953 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011954 struct intel_crtc *crtc;
11955 struct intel_encoder *encoder;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011956 struct intel_crtc_state pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011957
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011958 for_each_intel_crtc(dev, crtc) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011959 bool enabled = false;
11960 bool active = false;
11961
Jesse Barnes045ac3b2013-05-14 17:08:26 -070011962 memset(&pipe_config, 0, sizeof(pipe_config));
11963
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011964 DRM_DEBUG_KMS("[CRTC:%d]\n",
11965 crtc->base.base.id);
11966
Matt Roper83d65732015-02-25 13:12:16 -080011967 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011968 "active crtc, but not enabled in sw tracking\n");
11969
Damien Lespiaub2784e12014-08-05 11:29:37 +010011970 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011971 if (encoder->base.crtc != &crtc->base)
11972 continue;
11973 enabled = true;
11974 if (encoder->connectors_active)
11975 active = true;
11976 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020011977
Rob Clarke2c719b2014-12-15 13:56:32 -050011978 I915_STATE_WARN(active != crtc->active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011979 "crtc's computed active state doesn't match tracked active state "
11980 "(expected %i, found %i)\n", active, crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080011981 I915_STATE_WARN(enabled != crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011982 "crtc's computed enabled state doesn't match tracked enabled state "
Matt Roper83d65732015-02-25 13:12:16 -080011983 "(expected %i, found %i)\n", enabled,
11984 crtc->base.state->enable);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011985
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011986 active = dev_priv->display.get_pipe_config(crtc,
11987 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +020011988
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030011989 /* hw state is inconsistent with the pipe quirk */
11990 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
11991 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetterd62cf622013-05-29 10:41:29 +020011992 active = crtc->active;
11993
Damien Lespiaub2784e12014-08-05 11:29:37 +010011994 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +030011995 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +020011996 if (encoder->base.crtc != &crtc->base)
11997 continue;
Daniel Vetter1d37b682013-11-18 09:00:59 +010011998 if (encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +020011999 encoder->get_config(encoder, &pipe_config);
12000 }
12001
Rob Clarke2c719b2014-12-15 13:56:32 -050012002 I915_STATE_WARN(crtc->active != active,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012003 "crtc active state doesn't match with hw state "
12004 "(expected %i, found %i)\n", crtc->active, active);
12005
Daniel Vetterc0b03412013-05-28 12:05:54 +020012006 if (active &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012007 !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050012008 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Daniel Vetterc0b03412013-05-28 12:05:54 +020012009 intel_dump_pipe_config(crtc, &pipe_config,
12010 "[hw state]");
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012011 intel_dump_pipe_config(crtc, crtc->config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012012 "[sw state]");
12013 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012014 }
12015}
12016
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012017static void
12018check_shared_dpll_state(struct drm_device *dev)
12019{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012020 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012021 struct intel_crtc *crtc;
12022 struct intel_dpll_hw_state dpll_hw_state;
12023 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020012024
12025 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12026 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12027 int enabled_crtcs = 0, active_crtcs = 0;
12028 bool active;
12029
12030 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12031
12032 DRM_DEBUG_KMS("%s\n", pll->name);
12033
12034 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
12035
Rob Clarke2c719b2014-12-15 13:56:32 -050012036 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
Daniel Vetter53589012013-06-05 13:34:16 +020012037 "more active pll users than references: %i vs %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012038 pll->active, hweight32(pll->config.crtc_mask));
Rob Clarke2c719b2014-12-15 13:56:32 -050012039 I915_STATE_WARN(pll->active && !pll->on,
Daniel Vetter53589012013-06-05 13:34:16 +020012040 "pll in active use but not on in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012041 I915_STATE_WARN(pll->on && !pll->active,
Daniel Vetter35c95372013-07-17 06:55:04 +020012042 "pll in on but not on in use in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012043 I915_STATE_WARN(pll->on != active,
Daniel Vetter53589012013-06-05 13:34:16 +020012044 "pll on state mismatch (expected %i, found %i)\n",
12045 pll->on, active);
12046
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012047 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080012048 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
Daniel Vetter53589012013-06-05 13:34:16 +020012049 enabled_crtcs++;
12050 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12051 active_crtcs++;
12052 }
Rob Clarke2c719b2014-12-15 13:56:32 -050012053 I915_STATE_WARN(pll->active != active_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012054 "pll active crtcs mismatch (expected %i, found %i)\n",
12055 pll->active, active_crtcs);
Rob Clarke2c719b2014-12-15 13:56:32 -050012056 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012057 "pll enabled crtcs mismatch (expected %i, found %i)\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012058 hweight32(pll->config.crtc_mask), enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012059
Rob Clarke2c719b2014-12-15 13:56:32 -050012060 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020012061 sizeof(dpll_hw_state)),
12062 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020012063 }
Daniel Vettera6778b32012-07-02 09:56:42 +020012064}
12065
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012066void
12067intel_modeset_check_state(struct drm_device *dev)
12068{
Damien Lespiau08db6652014-11-04 17:06:52 +000012069 check_wm_state(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012070 check_connector_state(dev);
12071 check_encoder_state(dev);
12072 check_crtc_state(dev);
12073 check_shared_dpll_state(dev);
12074}
12075
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012076void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
Ville Syrjälä18442d02013-09-13 16:00:08 +030012077 int dotclock)
12078{
12079 /*
12080 * FDI already provided one idea for the dotclock.
12081 * Yell if the encoder disagrees.
12082 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012083 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030012084 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012085 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030012086}
12087
Ville Syrjälä80715b22014-05-15 20:23:23 +030012088static void update_scanline_offset(struct intel_crtc *crtc)
12089{
12090 struct drm_device *dev = crtc->base.dev;
12091
12092 /*
12093 * The scanline counter increments at the leading edge of hsync.
12094 *
12095 * On most platforms it starts counting from vtotal-1 on the
12096 * first active line. That means the scanline counter value is
12097 * always one less than what we would expect. Ie. just after
12098 * start of vblank, which also occurs at start of hsync (on the
12099 * last active line), the scanline counter will read vblank_start-1.
12100 *
12101 * On gen2 the scanline counter starts counting from 1 instead
12102 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12103 * to keep the value positive), instead of adding one.
12104 *
12105 * On HSW+ the behaviour of the scanline counter depends on the output
12106 * type. For DP ports it behaves like most other platforms, but on HDMI
12107 * there's an extra 1 line difference. So we need to add two instead of
12108 * one to the value.
12109 */
12110 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012111 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012112 int vtotal;
12113
12114 vtotal = mode->crtc_vtotal;
12115 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
12116 vtotal /= 2;
12117
12118 crtc->scanline_offset = vtotal - 1;
12119 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030012120 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030012121 crtc->scanline_offset = 2;
12122 } else
12123 crtc->scanline_offset = 1;
12124}
12125
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012126static struct intel_crtc_state *
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012127intel_modeset_compute_config(struct drm_crtc *crtc,
12128 struct drm_display_mode *mode,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012129 struct drm_atomic_state *state,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012130 unsigned *modeset_pipes,
12131 unsigned *prepare_pipes,
12132 unsigned *disable_pipes)
12133{
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020012134 struct drm_device *dev = crtc->dev;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012135 struct intel_crtc_state *pipe_config = NULL;
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020012136 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012137 int ret = 0;
12138
12139 ret = drm_atomic_add_affected_connectors(state, crtc);
12140 if (ret)
12141 return ERR_PTR(ret);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012142
12143 intel_modeset_affected_pipes(crtc, modeset_pipes,
12144 prepare_pipes, disable_pipes);
12145
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020012146 for_each_intel_crtc_masked(dev, *disable_pipes, intel_crtc) {
12147 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
12148 if (IS_ERR(pipe_config))
12149 return pipe_config;
12150
12151 pipe_config->base.enable = false;
12152 }
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012153
12154 /*
12155 * Note this needs changes when we start tracking multiple modes
12156 * and crtcs. At that point we'll need to compute the whole config
12157 * (i.e. one pipe_config for each crtc) rather than just the one
12158 * for this crtc.
12159 */
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020012160 for_each_intel_crtc_masked(dev, *modeset_pipes, intel_crtc) {
12161 /* FIXME: For now we still expect modeset_pipes has at most
12162 * one bit set. */
12163 if (WARN_ON(&intel_crtc->base != crtc))
12164 continue;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012165
Daniel Vetter8805aa72015-04-10 16:22:38 +020012166 pipe_config = intel_modeset_pipe_config(crtc, mode, state);
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020012167 if (IS_ERR(pipe_config))
12168 return pipe_config;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012169
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030012170 pipe_config->base.enable = true;
12171
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020012172 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
12173 "[modeset]");
12174 }
12175
12176 return intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012177}
12178
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012179static int __intel_set_mode_setup_plls(struct drm_atomic_state *state,
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012180 unsigned modeset_pipes,
12181 unsigned disable_pipes)
12182{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012183 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012184 struct drm_i915_private *dev_priv = to_i915(dev);
12185 unsigned clear_pipes = modeset_pipes | disable_pipes;
12186 struct intel_crtc *intel_crtc;
12187 int ret = 0;
12188
12189 if (!dev_priv->display.crtc_compute_clock)
12190 return 0;
12191
12192 ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
12193 if (ret)
12194 goto done;
12195
12196 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012197 struct intel_crtc_state *crtc_state =
12198 intel_atomic_get_crtc_state(state, intel_crtc);
12199
12200 /* Modeset pipes should have a new state by now */
12201 if (WARN_ON(IS_ERR(crtc_state)))
12202 continue;
12203
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012204 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012205 crtc_state);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012206 if (ret) {
12207 intel_shared_dpll_abort_config(dev_priv);
12208 goto done;
12209 }
12210 }
12211
12212done:
12213 return ret;
12214}
12215
Daniel Vetterf30da182013-04-11 20:22:50 +020012216static int __intel_set_mode(struct drm_crtc *crtc,
12217 struct drm_display_mode *mode,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012218 int x, int y, struct drm_framebuffer *fb,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012219 struct intel_crtc_state *pipe_config,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012220 unsigned modeset_pipes,
12221 unsigned prepare_pipes,
12222 unsigned disable_pipes)
Daniel Vettera6778b32012-07-02 09:56:42 +020012223{
12224 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030012225 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030012226 struct drm_display_mode *saved_mode;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030012227 struct drm_atomic_state *state = pipe_config->base.state;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012228 struct intel_crtc_state *crtc_state_copy = NULL;
Daniel Vetter25c5b262012-07-08 22:08:04 +020012229 struct intel_crtc *intel_crtc;
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012230 int ret = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020012231
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030012232 saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012233 if (!saved_mode)
12234 return -ENOMEM;
Daniel Vettera6778b32012-07-02 09:56:42 +020012235
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012236 crtc_state_copy = kmalloc(sizeof(*crtc_state_copy), GFP_KERNEL);
12237 if (!crtc_state_copy) {
12238 ret = -ENOMEM;
12239 goto done;
12240 }
12241
Tim Gardner3ac18232012-12-07 07:54:26 -070012242 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +020012243
Jesse Barnes30a970c2013-11-04 13:48:12 -080012244 /*
12245 * See if the config requires any additional preparation, e.g.
12246 * to adjust global state with pipes off. We need to do this
12247 * here so we can get the modeset_pipe updated config for the new
12248 * mode set on this crtc. For other crtcs we need to use the
12249 * adjusted_mode bits in the crtc directly.
12250 */
Vandana Kannanf8437dd12014-11-24 13:37:39 +053012251 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030012252 ret = valleyview_modeset_global_pipes(state, &prepare_pipes);
12253 if (ret)
12254 goto done;
Jesse Barnes30a970c2013-11-04 13:48:12 -080012255
Ville Syrjäläc164f832013-11-05 22:34:12 +020012256 /* may have added more to prepare_pipes than we should */
12257 prepare_pipes &= ~disable_pipes;
12258 }
12259
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012260 ret = __intel_set_mode_setup_plls(state, modeset_pipes, disable_pipes);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012261 if (ret)
12262 goto done;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +020012263
Daniel Vetter460da9162013-03-27 00:44:51 +010012264 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
12265 intel_crtc_disable(&intel_crtc->base);
12266
Daniel Vetterea9d7582012-07-10 10:42:52 +020012267 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080012268 if (intel_crtc->base.state->enable)
Daniel Vetterea9d7582012-07-10 10:42:52 +020012269 dev_priv->display.crtc_disable(&intel_crtc->base);
12270 }
Daniel Vettera6778b32012-07-02 09:56:42 +020012271
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020012272 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
12273 * to set it here already despite that we pass it down the callchain.
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012274 *
12275 * Note we'll need to fix this up when we start tracking multiple
12276 * pipes; here we assume a single modeset_pipe and only track the
12277 * single crtc and mode.
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020012278 */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012279 if (modeset_pipes) {
Daniel Vetter25c5b262012-07-08 22:08:04 +020012280 crtc->mode = *mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012281 /* mode_set/enable/disable functions rely on a correct pipe
12282 * config. */
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020012283 intel_crtc_set_state(to_intel_crtc(crtc), pipe_config);
Ville Syrjäläc326c0a2013-10-28 12:53:41 +020012284
12285 /*
12286 * Calculate and store various constants which
12287 * are later needed by vblank and swap-completion
12288 * timestamping. They are derived from true hwmode.
12289 */
12290 drm_calc_timestamping_constants(crtc,
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012291 &pipe_config->base.adjusted_mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012292 }
Daniel Vetter7758a112012-07-08 19:40:39 +020012293
Daniel Vetterea9d7582012-07-10 10:42:52 +020012294 /* Only after disabling all output pipelines that will be changed can we
12295 * update the the output configuration. */
12296 intel_modeset_update_state(dev, prepare_pipes);
12297
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030012298 modeset_update_crtc_power_domains(state);
Daniel Vetter47fab732012-10-26 10:58:18 +020012299
Daniel Vetter25c5b262012-07-08 22:08:04 +020012300 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Gustavo Padovan455a6802014-12-01 15:40:11 -080012301 struct drm_plane *primary = intel_crtc->base.primary;
12302 int vdisplay, hdisplay;
Daniel Vetter4c107942014-04-24 23:55:05 +020012303
Gustavo Padovan455a6802014-12-01 15:40:11 -080012304 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
Matt Roper70a101f2015-04-08 18:56:53 -070012305 ret = drm_plane_helper_update(primary, &intel_crtc->base,
12306 fb, 0, 0,
12307 hdisplay, vdisplay,
12308 x << 16, y << 16,
12309 hdisplay << 16, vdisplay << 16);
Daniel Vettera6778b32012-07-02 09:56:42 +020012310 }
12311
12312 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ville Syrjälä80715b22014-05-15 20:23:23 +030012313 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
12314 update_scanline_offset(intel_crtc);
12315
Daniel Vetter25c5b262012-07-08 22:08:04 +020012316 dev_priv->display.crtc_enable(&intel_crtc->base);
Ville Syrjälä80715b22014-05-15 20:23:23 +030012317 }
Daniel Vettera6778b32012-07-02 09:56:42 +020012318
Daniel Vettera6778b32012-07-02 09:56:42 +020012319 /* FIXME: add subpixel order */
12320done:
Matt Roper83d65732015-02-25 13:12:16 -080012321 if (ret && crtc->state->enable)
Tim Gardner3ac18232012-12-07 07:54:26 -070012322 crtc->mode = *saved_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +020012323
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012324 if (ret == 0 && pipe_config) {
12325 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12326
12327 /* The pipe_config will be freed with the atomic state, so
12328 * make a copy. */
12329 memcpy(crtc_state_copy, intel_crtc->config,
12330 sizeof *crtc_state_copy);
12331 intel_crtc->config = crtc_state_copy;
12332 intel_crtc->base.state = &crtc_state_copy->base;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012333 } else {
12334 kfree(crtc_state_copy);
12335 }
12336
Tim Gardner3ac18232012-12-07 07:54:26 -070012337 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +020012338 return ret;
12339}
12340
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012341static int intel_set_mode_pipes(struct drm_crtc *crtc,
12342 struct drm_display_mode *mode,
12343 int x, int y, struct drm_framebuffer *fb,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012344 struct intel_crtc_state *pipe_config,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012345 unsigned modeset_pipes,
12346 unsigned prepare_pipes,
12347 unsigned disable_pipes)
12348{
12349 int ret;
12350
12351 ret = __intel_set_mode(crtc, mode, x, y, fb, pipe_config, modeset_pipes,
12352 prepare_pipes, disable_pipes);
12353
12354 if (ret == 0)
12355 intel_modeset_check_state(crtc->dev);
12356
12357 return ret;
12358}
12359
Damien Lespiaue7457a92013-08-08 22:28:59 +010012360static int intel_set_mode(struct drm_crtc *crtc,
12361 struct drm_display_mode *mode,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012362 int x, int y, struct drm_framebuffer *fb,
12363 struct drm_atomic_state *state)
Daniel Vetterf30da182013-04-11 20:22:50 +020012364{
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012365 struct intel_crtc_state *pipe_config;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012366 unsigned modeset_pipes, prepare_pipes, disable_pipes;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012367 int ret = 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020012368
Daniel Vetter8805aa72015-04-10 16:22:38 +020012369 pipe_config = intel_modeset_compute_config(crtc, mode, state,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012370 &modeset_pipes,
12371 &prepare_pipes,
12372 &disable_pipes);
Daniel Vetterf30da182013-04-11 20:22:50 +020012373
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012374 if (IS_ERR(pipe_config)) {
12375 ret = PTR_ERR(pipe_config);
12376 goto out;
12377 }
Daniel Vetterf30da182013-04-11 20:22:50 +020012378
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012379 ret = intel_set_mode_pipes(crtc, mode, x, y, fb, pipe_config,
12380 modeset_pipes, prepare_pipes,
12381 disable_pipes);
12382 if (ret)
12383 goto out;
12384
12385out:
12386 return ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020012387}
12388
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012389void intel_crtc_restore_mode(struct drm_crtc *crtc)
12390{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012391 struct drm_device *dev = crtc->dev;
12392 struct drm_atomic_state *state;
12393 struct intel_encoder *encoder;
12394 struct intel_connector *connector;
12395 struct drm_connector_state *connector_state;
12396
12397 state = drm_atomic_state_alloc(dev);
12398 if (!state) {
12399 DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
12400 crtc->base.id);
12401 return;
12402 }
12403
12404 state->acquire_ctx = dev->mode_config.acquire_ctx;
12405
12406 /* The force restore path in the HW readout code relies on the staged
12407 * config still keeping the user requested config while the actual
12408 * state has been overwritten by the configuration read from HW. We
12409 * need to copy the staged config to the atomic state, otherwise the
12410 * mode set will just reapply the state the HW is already in. */
12411 for_each_intel_encoder(dev, encoder) {
12412 if (&encoder->new_crtc->base != crtc)
12413 continue;
12414
12415 for_each_intel_connector(dev, connector) {
12416 if (connector->new_encoder != encoder)
12417 continue;
12418
12419 connector_state = drm_atomic_get_connector_state(state, &connector->base);
12420 if (IS_ERR(connector_state)) {
12421 DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
12422 connector->base.base.id,
12423 connector->base.name,
12424 PTR_ERR(connector_state));
12425 continue;
12426 }
12427
12428 connector_state->crtc = crtc;
12429 connector_state->best_encoder = &encoder->base;
12430 }
12431 }
12432
12433 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb,
12434 state);
12435
12436 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012437}
12438
Daniel Vetter25c5b262012-07-08 22:08:04 +020012439#undef for_each_intel_crtc_masked
12440
Daniel Vetterd9e55602012-07-04 22:16:09 +020012441static void intel_set_config_free(struct intel_set_config *config)
12442{
12443 if (!config)
12444 return;
12445
Daniel Vetter1aa4b622012-07-05 16:20:48 +020012446 kfree(config->save_connector_encoders);
12447 kfree(config->save_encoder_crtcs);
Ville Syrjälä76688512014-01-10 11:28:06 +020012448 kfree(config->save_crtc_enabled);
Daniel Vetterd9e55602012-07-04 22:16:09 +020012449 kfree(config);
12450}
12451
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012452static int intel_set_config_save_state(struct drm_device *dev,
12453 struct intel_set_config *config)
12454{
Ville Syrjälä76688512014-01-10 11:28:06 +020012455 struct drm_crtc *crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012456 struct drm_encoder *encoder;
12457 struct drm_connector *connector;
12458 int count;
12459
Ville Syrjälä76688512014-01-10 11:28:06 +020012460 config->save_crtc_enabled =
12461 kcalloc(dev->mode_config.num_crtc,
12462 sizeof(bool), GFP_KERNEL);
12463 if (!config->save_crtc_enabled)
12464 return -ENOMEM;
12465
Daniel Vetter1aa4b622012-07-05 16:20:48 +020012466 config->save_encoder_crtcs =
12467 kcalloc(dev->mode_config.num_encoder,
12468 sizeof(struct drm_crtc *), GFP_KERNEL);
12469 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012470 return -ENOMEM;
12471
Daniel Vetter1aa4b622012-07-05 16:20:48 +020012472 config->save_connector_encoders =
12473 kcalloc(dev->mode_config.num_connector,
12474 sizeof(struct drm_encoder *), GFP_KERNEL);
12475 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012476 return -ENOMEM;
12477
12478 /* Copy data. Note that driver private data is not affected.
12479 * Should anything bad happen only the expected state is
12480 * restored, not the drivers personal bookkeeping.
12481 */
12482 count = 0;
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010012483 for_each_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080012484 config->save_crtc_enabled[count++] = crtc->state->enable;
Ville Syrjälä76688512014-01-10 11:28:06 +020012485 }
12486
12487 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012488 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +020012489 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012490 }
12491
12492 count = 0;
12493 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +020012494 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012495 }
12496
12497 return 0;
12498}
12499
12500static void intel_set_config_restore_state(struct drm_device *dev,
12501 struct intel_set_config *config)
12502{
Ville Syrjälä76688512014-01-10 11:28:06 +020012503 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020012504 struct intel_encoder *encoder;
12505 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012506 int count;
12507
12508 count = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012509 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020012510 crtc->new_enabled = config->save_crtc_enabled[count++];
12511 }
12512
12513 count = 0;
Damien Lespiaub2784e12014-08-05 11:29:37 +010012514 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012515 encoder->new_crtc =
12516 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012517 }
12518
12519 count = 0;
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012520 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012521 connector->new_encoder =
12522 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012523 }
12524}
12525
Imre Deake3de42b2013-05-03 19:44:07 +020012526static bool
Chris Wilson2e57f472013-07-17 12:14:40 +010012527is_crtc_connector_off(struct drm_mode_set *set)
Imre Deake3de42b2013-05-03 19:44:07 +020012528{
12529 int i;
12530
Chris Wilson2e57f472013-07-17 12:14:40 +010012531 if (set->num_connectors == 0)
12532 return false;
12533
12534 if (WARN_ON(set->connectors == NULL))
12535 return false;
12536
12537 for (i = 0; i < set->num_connectors; i++)
12538 if (set->connectors[i]->encoder &&
12539 set->connectors[i]->encoder->crtc == set->crtc &&
12540 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
Imre Deake3de42b2013-05-03 19:44:07 +020012541 return true;
12542
12543 return false;
12544}
12545
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012546static void
12547intel_set_config_compute_mode_changes(struct drm_mode_set *set,
12548 struct intel_set_config *config)
12549{
12550
12551 /* We should be able to check here if the fb has the same properties
12552 * and then just flip_or_move it */
Chris Wilson2e57f472013-07-17 12:14:40 +010012553 if (is_crtc_connector_off(set)) {
12554 config->mode_changed = true;
Matt Roperf4510a22014-04-01 15:22:40 -070012555 } else if (set->crtc->primary->fb != set->fb) {
Matt Roper3b150f02014-05-29 08:06:53 -070012556 /*
12557 * If we have no fb, we can only flip as long as the crtc is
12558 * active, otherwise we need a full mode set. The crtc may
12559 * be active if we've only disabled the primary plane, or
12560 * in fastboot situations.
12561 */
Matt Roperf4510a22014-04-01 15:22:40 -070012562 if (set->crtc->primary->fb == NULL) {
Jesse Barnes319d9822013-06-26 01:38:19 +030012563 struct intel_crtc *intel_crtc =
12564 to_intel_crtc(set->crtc);
12565
Matt Roper3b150f02014-05-29 08:06:53 -070012566 if (intel_crtc->active) {
Jesse Barnes319d9822013-06-26 01:38:19 +030012567 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
12568 config->fb_changed = true;
12569 } else {
12570 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
12571 config->mode_changed = true;
12572 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012573 } else if (set->fb == NULL) {
12574 config->mode_changed = true;
Daniel Vetter72f49012013-03-28 16:01:35 +010012575 } else if (set->fb->pixel_format !=
Matt Roperf4510a22014-04-01 15:22:40 -070012576 set->crtc->primary->fb->pixel_format) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012577 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +020012578 } else {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012579 config->fb_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +020012580 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012581 }
12582
Daniel Vetter835c5872012-07-10 18:11:08 +020012583 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012584 config->fb_changed = true;
12585
12586 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
12587 DRM_DEBUG_KMS("modes are different, full mode set\n");
12588 drm_mode_debug_printmodeline(&set->crtc->mode);
12589 drm_mode_debug_printmodeline(set->mode);
12590 config->mode_changed = true;
12591 }
Chris Wilsona1d95702013-08-13 18:48:47 +010012592
12593 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
12594 set->crtc->base.id, config->mode_changed, config->fb_changed);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012595}
12596
Daniel Vetter2e431052012-07-04 22:42:15 +020012597static int
Daniel Vetter9a935852012-07-05 22:34:27 +020012598intel_modeset_stage_output_state(struct drm_device *dev,
12599 struct drm_mode_set *set,
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012600 struct intel_set_config *config,
12601 struct drm_atomic_state *state)
Daniel Vetter50f56112012-07-02 09:35:43 +020012602{
Daniel Vetter9a935852012-07-05 22:34:27 +020012603 struct intel_connector *connector;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012604 struct drm_connector_state *connector_state;
Daniel Vetter9a935852012-07-05 22:34:27 +020012605 struct intel_encoder *encoder;
Ville Syrjälä76688512014-01-10 11:28:06 +020012606 struct intel_crtc *crtc;
Paulo Zanonif3f08572013-08-12 14:56:53 -030012607 int ro;
Daniel Vetter50f56112012-07-02 09:35:43 +020012608
Damien Lespiau9abdda72013-02-13 13:29:23 +000012609 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +020012610 * of connectors. For paranoia, double-check this. */
12611 WARN_ON(!set->fb && (set->num_connectors != 0));
12612 WARN_ON(set->fb && (set->num_connectors == 0));
12613
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012614 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012615 /* Otherwise traverse passed in connector list and get encoders
12616 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +020012617 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012618 if (set->connectors[ro] == &connector->base) {
Dave Airlie0e32b392014-05-02 14:02:48 +100012619 connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
Daniel Vetter50f56112012-07-02 09:35:43 +020012620 break;
12621 }
12622 }
12623
Daniel Vetter9a935852012-07-05 22:34:27 +020012624 /* If we disable the crtc, disable all its connectors. Also, if
12625 * the connector is on the changing crtc but not on the new
12626 * connector list, disable it. */
12627 if ((!set->fb || ro == set->num_connectors) &&
12628 connector->base.encoder &&
12629 connector->base.encoder->crtc == set->crtc) {
12630 connector->new_encoder = NULL;
12631
12632 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
12633 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012634 connector->base.name);
Daniel Vetter9a935852012-07-05 22:34:27 +020012635 }
12636
12637
12638 if (&connector->new_encoder->base != connector->base.encoder) {
Ander Conselvan de Oliveira10634182015-03-03 15:21:57 +020012639 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] encoder changed, full mode switch\n",
12640 connector->base.base.id,
12641 connector->base.name);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012642 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +020012643 }
Daniel Vetter9a935852012-07-05 22:34:27 +020012644 }
12645 /* connector->new_encoder is now updated for all connectors. */
12646
12647 /* Update crtc of enabled connectors. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012648 for_each_intel_connector(dev, connector) {
Ville Syrjälä76688512014-01-10 11:28:06 +020012649 struct drm_crtc *new_crtc;
12650
Daniel Vetter9a935852012-07-05 22:34:27 +020012651 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +020012652 continue;
12653
Daniel Vetter9a935852012-07-05 22:34:27 +020012654 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +020012655
12656 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012657 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +020012658 new_crtc = set->crtc;
12659 }
12660
12661 /* Make sure the new CRTC will work with the encoder */
Thierry Reding14509912014-01-13 12:00:22 +010012662 if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
12663 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012664 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +020012665 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012666 connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
Daniel Vetter9a935852012-07-05 22:34:27 +020012667
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012668 connector_state =
12669 drm_atomic_get_connector_state(state, &connector->base);
12670 if (IS_ERR(connector_state))
12671 return PTR_ERR(connector_state);
12672
12673 connector_state->crtc = new_crtc;
12674 connector_state->best_encoder = &connector->new_encoder->base;
12675
Daniel Vetter9a935852012-07-05 22:34:27 +020012676 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
12677 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012678 connector->base.name,
Daniel Vetter9a935852012-07-05 22:34:27 +020012679 new_crtc->base.id);
12680 }
12681
12682 /* Check for any encoders that needs to be disabled. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010012683 for_each_intel_encoder(dev, encoder) {
Paulo Zanoni5a65f352014-01-07 14:55:53 -020012684 int num_connectors = 0;
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012685 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012686 if (connector->new_encoder == encoder) {
12687 WARN_ON(!connector->new_encoder->new_crtc);
Paulo Zanoni5a65f352014-01-07 14:55:53 -020012688 num_connectors++;
Daniel Vetter9a935852012-07-05 22:34:27 +020012689 }
12690 }
Paulo Zanoni5a65f352014-01-07 14:55:53 -020012691
12692 if (num_connectors == 0)
12693 encoder->new_crtc = NULL;
12694 else if (num_connectors > 1)
12695 return -EINVAL;
12696
Daniel Vetter9a935852012-07-05 22:34:27 +020012697 /* Only now check for crtc changes so we don't miss encoders
12698 * that will be disabled. */
12699 if (&encoder->new_crtc->base != encoder->base.crtc) {
Ander Conselvan de Oliveira10634182015-03-03 15:21:57 +020012700 DRM_DEBUG_KMS("[ENCODER:%d:%s] crtc changed, full mode switch\n",
12701 encoder->base.base.id,
12702 encoder->base.name);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012703 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +020012704 }
12705 }
Daniel Vetter9a935852012-07-05 22:34:27 +020012706 /* Now we've also updated encoder->new_crtc for all encoders. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012707 for_each_intel_connector(dev, connector) {
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012708 connector_state =
12709 drm_atomic_get_connector_state(state, &connector->base);
Ander Conselvan de Oliveira9d918c12015-03-27 15:33:51 +020012710 if (IS_ERR(connector_state))
12711 return PTR_ERR(connector_state);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012712
12713 if (connector->new_encoder) {
Dave Airlie0e32b392014-05-02 14:02:48 +100012714 if (connector->new_encoder != connector->encoder)
12715 connector->encoder = connector->new_encoder;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012716 } else {
12717 connector_state->crtc = NULL;
Ander Conselvan de Oliveiraf61cccf2015-03-31 11:35:00 +030012718 connector_state->best_encoder = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012719 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012720 }
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012721 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020012722 crtc->new_enabled = false;
12723
Damien Lespiaub2784e12014-08-05 11:29:37 +010012724 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä76688512014-01-10 11:28:06 +020012725 if (encoder->new_crtc == crtc) {
12726 crtc->new_enabled = true;
12727 break;
12728 }
12729 }
12730
Matt Roper83d65732015-02-25 13:12:16 -080012731 if (crtc->new_enabled != crtc->base.state->enable) {
Ander Conselvan de Oliveira10634182015-03-03 15:21:57 +020012732 DRM_DEBUG_KMS("[CRTC:%d] %sabled, full mode switch\n",
12733 crtc->base.base.id,
Ville Syrjälä76688512014-01-10 11:28:06 +020012734 crtc->new_enabled ? "en" : "dis");
12735 config->mode_changed = true;
12736 }
12737 }
12738
Daniel Vetter2e431052012-07-04 22:42:15 +020012739 return 0;
12740}
12741
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020012742static void disable_crtc_nofb(struct intel_crtc *crtc)
12743{
12744 struct drm_device *dev = crtc->base.dev;
12745 struct intel_encoder *encoder;
12746 struct intel_connector *connector;
12747
12748 DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
12749 pipe_name(crtc->pipe));
12750
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012751 for_each_intel_connector(dev, connector) {
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020012752 if (connector->new_encoder &&
12753 connector->new_encoder->new_crtc == crtc)
12754 connector->new_encoder = NULL;
12755 }
12756
Damien Lespiaub2784e12014-08-05 11:29:37 +010012757 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020012758 if (encoder->new_crtc == crtc)
12759 encoder->new_crtc = NULL;
12760 }
12761
12762 crtc->new_enabled = false;
12763}
12764
Daniel Vetter2e431052012-07-04 22:42:15 +020012765static int intel_crtc_set_config(struct drm_mode_set *set)
12766{
12767 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +020012768 struct drm_mode_set save_set;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012769 struct drm_atomic_state *state = NULL;
Daniel Vetter2e431052012-07-04 22:42:15 +020012770 struct intel_set_config *config;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012771 struct intel_crtc_state *pipe_config;
Jesse Barnes50f52752014-11-07 13:11:00 -080012772 unsigned modeset_pipes, prepare_pipes, disable_pipes;
Daniel Vetter2e431052012-07-04 22:42:15 +020012773 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +020012774
Daniel Vetter8d3e3752012-07-05 16:09:09 +020012775 BUG_ON(!set);
12776 BUG_ON(!set->crtc);
12777 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +020012778
Daniel Vetter7e53f3a2013-01-21 10:52:17 +010012779 /* Enforce sane interface api - has been abused by the fb helper. */
12780 BUG_ON(!set->mode && set->fb);
12781 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +020012782
Daniel Vetter2e431052012-07-04 22:42:15 +020012783 if (set->fb) {
12784 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
12785 set->crtc->base.id, set->fb->base.id,
12786 (int)set->num_connectors, set->x, set->y);
12787 } else {
12788 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +020012789 }
12790
12791 dev = set->crtc->dev;
12792
12793 ret = -ENOMEM;
12794 config = kzalloc(sizeof(*config), GFP_KERNEL);
12795 if (!config)
12796 goto out_config;
12797
12798 ret = intel_set_config_save_state(dev, config);
12799 if (ret)
12800 goto out_config;
12801
12802 save_set.crtc = set->crtc;
12803 save_set.mode = &set->crtc->mode;
12804 save_set.x = set->crtc->x;
12805 save_set.y = set->crtc->y;
Matt Roperf4510a22014-04-01 15:22:40 -070012806 save_set.fb = set->crtc->primary->fb;
Daniel Vetter2e431052012-07-04 22:42:15 +020012807
12808 /* Compute whether we need a full modeset, only an fb base update or no
12809 * change at all. In the future we might also check whether only the
12810 * mode changed, e.g. for LVDS where we only change the panel fitter in
12811 * such cases. */
12812 intel_set_config_compute_mode_changes(set, config);
12813
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012814 state = drm_atomic_state_alloc(dev);
12815 if (!state) {
12816 ret = -ENOMEM;
12817 goto out_config;
12818 }
12819
12820 state->acquire_ctx = dev->mode_config.acquire_ctx;
12821
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012822 ret = intel_modeset_stage_output_state(dev, set, config, state);
Daniel Vetter2e431052012-07-04 22:42:15 +020012823 if (ret)
12824 goto fail;
12825
Jesse Barnes50f52752014-11-07 13:11:00 -080012826 pipe_config = intel_modeset_compute_config(set->crtc, set->mode,
Daniel Vetter8805aa72015-04-10 16:22:38 +020012827 state,
Jesse Barnes50f52752014-11-07 13:11:00 -080012828 &modeset_pipes,
12829 &prepare_pipes,
12830 &disable_pipes);
Jesse Barnes20664592014-11-05 14:26:09 -080012831 if (IS_ERR(pipe_config)) {
Matt Roper6ac04832014-11-17 09:59:28 -080012832 ret = PTR_ERR(pipe_config);
Jesse Barnes50f52752014-11-07 13:11:00 -080012833 goto fail;
Jesse Barnes20664592014-11-05 14:26:09 -080012834 } else if (pipe_config) {
Ville Syrjäläb9950a12014-11-21 21:00:36 +020012835 if (pipe_config->has_audio !=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012836 to_intel_crtc(set->crtc)->config->has_audio)
Jesse Barnes20664592014-11-05 14:26:09 -080012837 config->mode_changed = true;
12838
Jesse Barnesaf15d2c2014-12-01 09:54:28 -080012839 /*
12840 * Note we have an issue here with infoframes: current code
12841 * only updates them on the full mode set path per hw
12842 * requirements. So here we should be checking for any
12843 * required changes and forcing a mode set.
12844 */
Jesse Barnes20664592014-11-05 14:26:09 -080012845 }
Jesse Barnes50f52752014-11-07 13:11:00 -080012846
Jesse Barnes1f9954d2014-11-05 14:26:10 -080012847 intel_update_pipe_size(to_intel_crtc(set->crtc));
12848
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012849 if (config->mode_changed) {
Jesse Barnes50f52752014-11-07 13:11:00 -080012850 ret = intel_set_mode_pipes(set->crtc, set->mode,
12851 set->x, set->y, set->fb, pipe_config,
12852 modeset_pipes, prepare_pipes,
12853 disable_pipes);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012854 } else if (config->fb_changed) {
Matt Roper3b150f02014-05-29 08:06:53 -070012855 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080012856 struct drm_plane *primary = set->crtc->primary;
12857 int vdisplay, hdisplay;
Matt Roper3b150f02014-05-29 08:06:53 -070012858
Gustavo Padovan455a6802014-12-01 15:40:11 -080012859 drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
Matt Roper70a101f2015-04-08 18:56:53 -070012860 ret = drm_plane_helper_update(primary, set->crtc, set->fb,
12861 0, 0, hdisplay, vdisplay,
12862 set->x << 16, set->y << 16,
12863 hdisplay << 16, vdisplay << 16);
Matt Roper3b150f02014-05-29 08:06:53 -070012864
12865 /*
12866 * We need to make sure the primary plane is re-enabled if it
12867 * has previously been turned off.
12868 */
12869 if (!intel_crtc->primary_enabled && ret == 0) {
12870 WARN_ON(!intel_crtc->active);
Ville Syrjäläfdd508a2014-08-08 21:51:11 +030012871 intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070012872 }
12873
Jesse Barnes7ca51a32014-01-07 13:50:49 -080012874 /*
12875 * In the fastboot case this may be our only check of the
12876 * state after boot. It would be better to only do it on
12877 * the first update, but we don't have a nice way of doing that
12878 * (and really, set_config isn't used much for high freq page
12879 * flipping, so increasing its cost here shouldn't be a big
12880 * deal).
12881 */
Jani Nikulad330a952014-01-21 11:24:25 +020012882 if (i915.fastboot && ret == 0)
Jesse Barnes7ca51a32014-01-07 13:50:49 -080012883 intel_modeset_check_state(set->crtc->dev);
Daniel Vetter50f56112012-07-02 09:35:43 +020012884 }
12885
Chris Wilson2d05eae2013-05-03 17:36:25 +010012886 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +020012887 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
12888 set->crtc->base.id, ret);
Daniel Vetter50f56112012-07-02 09:35:43 +020012889fail:
Chris Wilson2d05eae2013-05-03 17:36:25 +010012890 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +020012891
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012892 drm_atomic_state_clear(state);
12893
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020012894 /*
12895 * HACK: if the pipe was on, but we didn't have a framebuffer,
12896 * force the pipe off to avoid oopsing in the modeset code
12897 * due to fb==NULL. This should only happen during boot since
12898 * we don't yet reconstruct the FB from the hardware state.
12899 */
12900 if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
12901 disable_crtc_nofb(to_intel_crtc(save_set.crtc));
12902
Chris Wilson2d05eae2013-05-03 17:36:25 +010012903 /* Try to restore the config */
12904 if (config->mode_changed &&
12905 intel_set_mode(save_set.crtc, save_set.mode,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012906 save_set.x, save_set.y, save_set.fb,
12907 state))
Chris Wilson2d05eae2013-05-03 17:36:25 +010012908 DRM_ERROR("failed to restore config after modeset failure\n");
12909 }
Daniel Vetter50f56112012-07-02 09:35:43 +020012910
Daniel Vetterd9e55602012-07-04 22:16:09 +020012911out_config:
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012912 if (state)
12913 drm_atomic_state_free(state);
12914
Daniel Vetterd9e55602012-07-04 22:16:09 +020012915 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +020012916 return ret;
12917}
12918
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012919static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012920 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +020012921 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012922 .destroy = intel_crtc_destroy,
12923 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080012924 .atomic_duplicate_state = intel_crtc_duplicate_state,
12925 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012926};
12927
Daniel Vetter53589012013-06-05 13:34:16 +020012928static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
12929 struct intel_shared_dpll *pll,
12930 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012931{
Daniel Vetter53589012013-06-05 13:34:16 +020012932 uint32_t val;
12933
Daniel Vetterf458ebb2014-09-30 10:56:39 +020012934 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030012935 return false;
12936
Daniel Vetter53589012013-06-05 13:34:16 +020012937 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020012938 hw_state->dpll = val;
12939 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
12940 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020012941
12942 return val & DPLL_VCO_ENABLE;
12943}
12944
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012945static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
12946 struct intel_shared_dpll *pll)
12947{
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012948 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
12949 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012950}
12951
Daniel Vettere7b903d2013-06-05 13:34:14 +020012952static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
12953 struct intel_shared_dpll *pll)
12954{
Daniel Vettere7b903d2013-06-05 13:34:14 +020012955 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020012956 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020012957
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012958 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012959
12960 /* Wait for the clocks to stabilize. */
12961 POSTING_READ(PCH_DPLL(pll->id));
12962 udelay(150);
12963
12964 /* The pixel multiplier can only be updated once the
12965 * DPLL is enabled and the clocks are stable.
12966 *
12967 * So write it again.
12968 */
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012969 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012970 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020012971 udelay(200);
12972}
12973
12974static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
12975 struct intel_shared_dpll *pll)
12976{
12977 struct drm_device *dev = dev_priv->dev;
12978 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020012979
12980 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012981 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020012982 if (intel_crtc_to_shared_dpll(crtc) == pll)
12983 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
12984 }
12985
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012986 I915_WRITE(PCH_DPLL(pll->id), 0);
12987 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020012988 udelay(200);
12989}
12990
Daniel Vetter46edb022013-06-05 13:34:12 +020012991static char *ibx_pch_dpll_names[] = {
12992 "PCH DPLL A",
12993 "PCH DPLL B",
12994};
12995
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012996static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012997{
Daniel Vettere7b903d2013-06-05 13:34:14 +020012998 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012999 int i;
13000
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013001 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013002
Daniel Vettere72f9fb2013-06-05 13:34:06 +020013003 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020013004 dev_priv->shared_dplls[i].id = i;
13005 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013006 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013007 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
13008 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020013009 dev_priv->shared_dplls[i].get_hw_state =
13010 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013011 }
13012}
13013
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013014static void intel_shared_dpll_init(struct drm_device *dev)
13015{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013016 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013017
Daniel Vetter9cd86932014-06-25 22:01:57 +030013018 if (HAS_DDI(dev))
13019 intel_ddi_pll_init(dev);
13020 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013021 ibx_pch_dpll_init(dev);
13022 else
13023 dev_priv->num_shared_dpll = 0;
13024
13025 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013026}
13027
Matt Roper6beb8c232014-12-01 15:40:14 -080013028/**
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +000013029 * intel_wm_need_update - Check whether watermarks need updating
13030 * @plane: drm plane
13031 * @state: new plane state
13032 *
13033 * Check current plane state versus the new one to determine whether
13034 * watermarks need to be recalculated.
13035 *
13036 * Returns true or false.
13037 */
13038bool intel_wm_need_update(struct drm_plane *plane,
13039 struct drm_plane_state *state)
13040{
13041 /* Update watermarks on tiling changes. */
13042 if (!plane->state->fb || !state->fb ||
13043 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
13044 plane->state->rotation != state->rotation)
13045 return true;
13046
13047 return false;
13048}
13049
13050/**
Matt Roper6beb8c232014-12-01 15:40:14 -080013051 * intel_prepare_plane_fb - Prepare fb for usage on plane
13052 * @plane: drm plane to prepare for
13053 * @fb: framebuffer to prepare for presentation
13054 *
13055 * Prepares a framebuffer for usage on a display plane. Generally this
13056 * involves pinning the underlying object and updating the frontbuffer tracking
13057 * bits. Some older platforms need special physical address handling for
13058 * cursor planes.
13059 *
13060 * Returns 0 on success, negative error code on failure.
13061 */
13062int
13063intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013064 struct drm_framebuffer *fb,
13065 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013066{
13067 struct drm_device *dev = plane->dev;
Matt Roper6beb8c232014-12-01 15:40:14 -080013068 struct intel_plane *intel_plane = to_intel_plane(plane);
13069 enum pipe pipe = intel_plane->pipe;
13070 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13071 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
13072 unsigned frontbuffer_bits = 0;
13073 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013074
Matt Roperea2c67b2014-12-23 10:41:52 -080013075 if (!obj)
Matt Roper465c1202014-05-29 08:06:54 -070013076 return 0;
13077
Matt Roper6beb8c232014-12-01 15:40:14 -080013078 switch (plane->type) {
13079 case DRM_PLANE_TYPE_PRIMARY:
13080 frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
13081 break;
13082 case DRM_PLANE_TYPE_CURSOR:
13083 frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
13084 break;
13085 case DRM_PLANE_TYPE_OVERLAY:
13086 frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
13087 break;
13088 }
Matt Roper465c1202014-05-29 08:06:54 -070013089
Matt Roper4c345742014-07-09 16:22:10 -070013090 mutex_lock(&dev->struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070013091
Matt Roper6beb8c232014-12-01 15:40:14 -080013092 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13093 INTEL_INFO(dev)->cursor_needs_physical) {
13094 int align = IS_I830(dev) ? 16 * 1024 : 256;
13095 ret = i915_gem_object_attach_phys(obj, align);
13096 if (ret)
13097 DRM_DEBUG_KMS("failed to attach phys object\n");
13098 } else {
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000013099 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
Matt Roper6beb8c232014-12-01 15:40:14 -080013100 }
13101
13102 if (ret == 0)
13103 i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
13104
13105 mutex_unlock(&dev->struct_mutex);
13106
13107 return ret;
13108}
13109
Matt Roper38f3ce32014-12-02 07:45:25 -080013110/**
13111 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13112 * @plane: drm plane to clean up for
13113 * @fb: old framebuffer that was on plane
13114 *
13115 * Cleans up a framebuffer that has just been removed from a plane.
13116 */
13117void
13118intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013119 struct drm_framebuffer *fb,
13120 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013121{
13122 struct drm_device *dev = plane->dev;
13123 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13124
13125 if (WARN_ON(!obj))
13126 return;
13127
13128 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
13129 !INTEL_INFO(dev)->cursor_needs_physical) {
13130 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000013131 intel_unpin_fb_obj(fb, old_state);
Matt Roper38f3ce32014-12-02 07:45:25 -080013132 mutex_unlock(&dev->struct_mutex);
13133 }
Matt Roper465c1202014-05-29 08:06:54 -070013134}
13135
13136static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013137intel_check_primary_plane(struct drm_plane *plane,
13138 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013139{
Matt Roper32b7eee2014-12-24 07:59:06 -080013140 struct drm_device *dev = plane->dev;
13141 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roper2b875c22014-12-01 15:40:13 -080013142 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013143 struct intel_crtc *intel_crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080013144 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013145 struct drm_rect *dest = &state->dst;
13146 struct drm_rect *src = &state->src;
13147 const struct drm_rect *clip = &state->clip;
Sonika Jindald8106362015-04-10 14:37:28 +053013148 bool can_position = false;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013149 int ret;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013150
Matt Roperea2c67b2014-12-23 10:41:52 -080013151 crtc = crtc ? crtc : plane->crtc;
13152 intel_crtc = to_intel_crtc(crtc);
13153
Sonika Jindald8106362015-04-10 14:37:28 +053013154 if (INTEL_INFO(dev)->gen >= 9)
13155 can_position = true;
13156
Matt Roperc59cb172014-12-01 15:40:16 -080013157 ret = drm_plane_helper_check_update(plane, crtc, fb,
13158 src, dest, clip,
13159 DRM_PLANE_HELPER_NO_SCALING,
13160 DRM_PLANE_HELPER_NO_SCALING,
Sonika Jindald8106362015-04-10 14:37:28 +053013161 can_position, true,
13162 &state->visible);
Matt Roperc59cb172014-12-01 15:40:16 -080013163 if (ret)
13164 return ret;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013165
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013166 if (intel_crtc->active) {
Matt Roper32b7eee2014-12-24 07:59:06 -080013167 intel_crtc->atomic.wait_for_flips = true;
13168
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013169 /*
13170 * FBC does not work on some platforms for rotated
13171 * planes, so disable it when rotation is not 0 and
13172 * update it when rotation is set back to 0.
13173 *
13174 * FIXME: This is redundant with the fbc update done in
13175 * the primary plane enable function except that that
13176 * one is done too late. We eventually need to unify
13177 * this.
13178 */
13179 if (intel_crtc->primary_enabled &&
13180 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
Paulo Zanonie35fef22015-02-09 14:46:29 -020013181 dev_priv->fbc.crtc == intel_crtc &&
Matt Roper8e7d6882015-01-21 16:35:41 -080013182 state->base.rotation != BIT(DRM_ROTATE_0)) {
Matt Roper32b7eee2014-12-24 07:59:06 -080013183 intel_crtc->atomic.disable_fbc = true;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013184 }
13185
13186 if (state->visible) {
Matt Roper32b7eee2014-12-24 07:59:06 -080013187 /*
13188 * BDW signals flip done immediately if the plane
13189 * is disabled, even if the plane enable is already
13190 * armed to occur at the next vblank :(
13191 */
13192 if (IS_BROADWELL(dev) && !intel_crtc->primary_enabled)
13193 intel_crtc->atomic.wait_vblank = true;
13194 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013195
Matt Roper32b7eee2014-12-24 07:59:06 -080013196 intel_crtc->atomic.fb_bits |=
13197 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
13198
13199 intel_crtc->atomic.update_fbc = true;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +000013200
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +000013201 if (intel_wm_need_update(plane, &state->base))
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +000013202 intel_crtc->atomic.update_wm = true;
Matt Roperc59cb172014-12-01 15:40:16 -080013203 }
13204
13205 return 0;
Matt Roper465c1202014-05-29 08:06:54 -070013206}
13207
Sonika Jindal48404c12014-08-22 14:06:04 +053013208static void
13209intel_commit_primary_plane(struct drm_plane *plane,
13210 struct intel_plane_state *state)
13211{
Matt Roper2b875c22014-12-01 15:40:13 -080013212 struct drm_crtc *crtc = state->base.crtc;
13213 struct drm_framebuffer *fb = state->base.fb;
13214 struct drm_device *dev = plane->dev;
Sonika Jindal48404c12014-08-22 14:06:04 +053013215 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperea2c67b2014-12-23 10:41:52 -080013216 struct intel_crtc *intel_crtc;
Sonika Jindalce54d852014-08-21 11:44:39 +053013217 struct drm_rect *src = &state->src;
Matt Ropercf4c7c12014-12-04 10:27:42 -080013218
Matt Roperea2c67b2014-12-23 10:41:52 -080013219 crtc = crtc ? crtc : plane->crtc;
13220 intel_crtc = to_intel_crtc(crtc);
13221
Matt Ropercf4c7c12014-12-04 10:27:42 -080013222 plane->fb = fb;
Sonika Jindalce54d852014-08-21 11:44:39 +053013223 crtc->x = src->x1 >> 16;
Matt Roper465c1202014-05-29 08:06:54 -070013224 crtc->y = src->y1 >> 16;
13225
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013226 if (intel_crtc->active) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013227 if (state->visible) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013228 /* FIXME: kill this fastboot hack */
13229 intel_update_pipe_size(intel_crtc);
13230
13231 intel_crtc->primary_enabled = true;
13232
13233 dev_priv->display.update_primary_plane(crtc, plane->fb,
13234 crtc->x, crtc->y);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013235 } else {
13236 /*
13237 * If clipping results in a non-visible primary plane,
13238 * we'll disable the primary plane. Note that this is
13239 * a bit different than what happens if userspace
13240 * explicitly disables the plane by passing fb=0
13241 * because plane->fb still gets set and pinned.
13242 */
13243 intel_disable_primary_hw_plane(plane, crtc);
13244 }
Matt Roper32b7eee2014-12-24 07:59:06 -080013245 }
13246}
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013247
Matt Roper32b7eee2014-12-24 07:59:06 -080013248static void intel_begin_crtc_commit(struct drm_crtc *crtc)
13249{
13250 struct drm_device *dev = crtc->dev;
13251 struct drm_i915_private *dev_priv = dev->dev_private;
13252 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080013253 struct intel_plane *intel_plane;
13254 struct drm_plane *p;
13255 unsigned fb_bits = 0;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013256
Matt Roperea2c67b2014-12-23 10:41:52 -080013257 /* Track fb's for any planes being disabled */
13258 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
13259 intel_plane = to_intel_plane(p);
13260
13261 if (intel_crtc->atomic.disabled_planes &
13262 (1 << drm_plane_index(p))) {
13263 switch (p->type) {
13264 case DRM_PLANE_TYPE_PRIMARY:
13265 fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
13266 break;
13267 case DRM_PLANE_TYPE_CURSOR:
13268 fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
13269 break;
13270 case DRM_PLANE_TYPE_OVERLAY:
13271 fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
13272 break;
13273 }
13274
13275 mutex_lock(&dev->struct_mutex);
13276 i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
13277 mutex_unlock(&dev->struct_mutex);
13278 }
13279 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013280
Matt Roper32b7eee2014-12-24 07:59:06 -080013281 if (intel_crtc->atomic.wait_for_flips)
13282 intel_crtc_wait_for_pending_flips(crtc);
13283
13284 if (intel_crtc->atomic.disable_fbc)
13285 intel_fbc_disable(dev);
13286
13287 if (intel_crtc->atomic.pre_disable_primary)
13288 intel_pre_disable_primary(crtc);
13289
13290 if (intel_crtc->atomic.update_wm)
13291 intel_update_watermarks(crtc);
13292
13293 intel_runtime_pm_get(dev_priv);
Matt Roperc34c9ee2014-12-23 10:41:50 -080013294
13295 /* Perform vblank evasion around commit operation */
13296 if (intel_crtc->active)
13297 intel_crtc->atomic.evade =
13298 intel_pipe_update_start(intel_crtc,
13299 &intel_crtc->atomic.start_vbl_count);
Matt Roper32b7eee2014-12-24 07:59:06 -080013300}
13301
13302static void intel_finish_crtc_commit(struct drm_crtc *crtc)
13303{
13304 struct drm_device *dev = crtc->dev;
13305 struct drm_i915_private *dev_priv = dev->dev_private;
13306 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13307 struct drm_plane *p;
13308
Matt Roperc34c9ee2014-12-23 10:41:50 -080013309 if (intel_crtc->atomic.evade)
13310 intel_pipe_update_end(intel_crtc,
13311 intel_crtc->atomic.start_vbl_count);
13312
Matt Roper32b7eee2014-12-24 07:59:06 -080013313 intel_runtime_pm_put(dev_priv);
13314
13315 if (intel_crtc->atomic.wait_vblank)
13316 intel_wait_for_vblank(dev, intel_crtc->pipe);
13317
13318 intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
13319
13320 if (intel_crtc->atomic.update_fbc) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013321 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020013322 intel_fbc_update(dev);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013323 mutex_unlock(&dev->struct_mutex);
13324 }
Matt Roper465c1202014-05-29 08:06:54 -070013325
Matt Roper32b7eee2014-12-24 07:59:06 -080013326 if (intel_crtc->atomic.post_enable_primary)
13327 intel_post_enable_primary(crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013328
Matt Roper32b7eee2014-12-24 07:59:06 -080013329 drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
13330 if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
13331 intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
13332 false, false);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013333
Matt Roper32b7eee2014-12-24 07:59:06 -080013334 memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013335}
13336
Matt Ropercf4c7c12014-12-04 10:27:42 -080013337/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013338 * intel_plane_destroy - destroy a plane
13339 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013340 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013341 * Common destruction function for all types of planes (primary, cursor,
13342 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013343 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013344void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013345{
13346 struct intel_plane *intel_plane = to_intel_plane(plane);
13347 drm_plane_cleanup(plane);
13348 kfree(intel_plane);
13349}
13350
Matt Roper65a3fea2015-01-21 16:35:42 -080013351const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013352 .update_plane = drm_atomic_helper_update_plane,
13353 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013354 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080013355 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080013356 .atomic_get_property = intel_plane_atomic_get_property,
13357 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013358 .atomic_duplicate_state = intel_plane_duplicate_state,
13359 .atomic_destroy_state = intel_plane_destroy_state,
13360
Matt Roper465c1202014-05-29 08:06:54 -070013361};
13362
13363static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13364 int pipe)
13365{
13366 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080013367 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070013368 const uint32_t *intel_primary_formats;
13369 int num_formats;
13370
13371 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13372 if (primary == NULL)
13373 return NULL;
13374
Matt Roper8e7d6882015-01-21 16:35:41 -080013375 state = intel_create_plane_state(&primary->base);
13376 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013377 kfree(primary);
13378 return NULL;
13379 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013380 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013381
Matt Roper465c1202014-05-29 08:06:54 -070013382 primary->can_scale = false;
13383 primary->max_downscale = 1;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013384 state->scaler_id = -1;
Matt Roper465c1202014-05-29 08:06:54 -070013385 primary->pipe = pipe;
13386 primary->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080013387 primary->check_plane = intel_check_primary_plane;
13388 primary->commit_plane = intel_commit_primary_plane;
Chandra Konduru08e221f2015-04-07 15:28:37 -070013389 primary->ckey.flags = I915_SET_COLORKEY_NONE;
Matt Roper465c1202014-05-29 08:06:54 -070013390 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13391 primary->plane = !pipe;
13392
13393 if (INTEL_INFO(dev)->gen <= 3) {
13394 intel_primary_formats = intel_primary_formats_gen2;
13395 num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
13396 } else {
13397 intel_primary_formats = intel_primary_formats_gen4;
13398 num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
13399 }
13400
13401 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013402 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070013403 intel_primary_formats, num_formats,
13404 DRM_PLANE_TYPE_PRIMARY);
Sonika Jindal48404c12014-08-22 14:06:04 +053013405
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013406 if (INTEL_INFO(dev)->gen >= 4)
13407 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053013408
Matt Roperea2c67b2014-12-23 10:41:52 -080013409 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13410
Matt Roper465c1202014-05-29 08:06:54 -070013411 return &primary->base;
13412}
13413
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013414void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13415{
13416 if (!dev->mode_config.rotation_property) {
13417 unsigned long flags = BIT(DRM_ROTATE_0) |
13418 BIT(DRM_ROTATE_180);
13419
13420 if (INTEL_INFO(dev)->gen >= 9)
13421 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13422
13423 dev->mode_config.rotation_property =
13424 drm_mode_create_rotation_property(dev, flags);
13425 }
13426 if (dev->mode_config.rotation_property)
13427 drm_object_attach_property(&plane->base.base,
13428 dev->mode_config.rotation_property,
13429 plane->base.state->rotation);
13430}
13431
Matt Roper3d7d6512014-06-10 08:28:13 -070013432static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030013433intel_check_cursor_plane(struct drm_plane *plane,
13434 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070013435{
Matt Roper2b875c22014-12-01 15:40:13 -080013436 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013437 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080013438 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013439 struct drm_rect *dest = &state->dst;
13440 struct drm_rect *src = &state->src;
13441 const struct drm_rect *clip = &state->clip;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013442 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Matt Roperea2c67b2014-12-23 10:41:52 -080013443 struct intel_crtc *intel_crtc;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013444 unsigned stride;
13445 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013446
Matt Roperea2c67b2014-12-23 10:41:52 -080013447 crtc = crtc ? crtc : plane->crtc;
13448 intel_crtc = to_intel_crtc(crtc);
13449
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013450 ret = drm_plane_helper_check_update(plane, crtc, fb,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013451 src, dest, clip,
13452 DRM_PLANE_HELPER_NO_SCALING,
13453 DRM_PLANE_HELPER_NO_SCALING,
13454 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013455 if (ret)
13456 return ret;
13457
13458
13459 /* if we want to turn off the cursor ignore width and height */
13460 if (!obj)
Matt Roper32b7eee2014-12-24 07:59:06 -080013461 goto finish;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013462
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013463 /* Check for which cursor types we support */
Matt Roperea2c67b2014-12-23 10:41:52 -080013464 if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
13465 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13466 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013467 return -EINVAL;
13468 }
13469
Matt Roperea2c67b2014-12-23 10:41:52 -080013470 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13471 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013472 DRM_DEBUG_KMS("buffer is too small\n");
13473 return -ENOMEM;
13474 }
13475
Ville Syrjälä3a656b52015-03-09 21:08:37 +020013476 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013477 DRM_DEBUG_KMS("cursor cannot be tiled\n");
13478 ret = -EINVAL;
13479 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013480
Matt Roper32b7eee2014-12-24 07:59:06 -080013481finish:
13482 if (intel_crtc->active) {
Ville Syrjälä3749f462015-03-10 13:15:22 +020013483 if (plane->state->crtc_w != state->base.crtc_w)
Matt Roper32b7eee2014-12-24 07:59:06 -080013484 intel_crtc->atomic.update_wm = true;
13485
13486 intel_crtc->atomic.fb_bits |=
13487 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
13488 }
13489
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013490 return ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013491}
13492
Matt Roperf4a2cf22014-12-01 15:40:12 -080013493static void
Gustavo Padovan852e7872014-09-05 17:22:31 -030013494intel_commit_cursor_plane(struct drm_plane *plane,
13495 struct intel_plane_state *state)
13496{
Matt Roper2b875c22014-12-01 15:40:13 -080013497 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013498 struct drm_device *dev = plane->dev;
13499 struct intel_crtc *intel_crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080013500 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080013501 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070013502
Matt Roperea2c67b2014-12-23 10:41:52 -080013503 crtc = crtc ? crtc : plane->crtc;
13504 intel_crtc = to_intel_crtc(crtc);
Sonika Jindala919db92014-10-23 07:41:33 -070013505
Matt Roperea2c67b2014-12-23 10:41:52 -080013506 plane->fb = state->base.fb;
13507 crtc->cursor_x = state->base.crtc_x;
13508 crtc->cursor_y = state->base.crtc_y;
13509
Gustavo Padovana912f122014-12-01 15:40:10 -080013510 if (intel_crtc->cursor_bo == obj)
13511 goto update;
13512
Matt Roperf4a2cf22014-12-01 15:40:12 -080013513 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080013514 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080013515 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080013516 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080013517 else
Gustavo Padovana912f122014-12-01 15:40:10 -080013518 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080013519
Gustavo Padovana912f122014-12-01 15:40:10 -080013520 intel_crtc->cursor_addr = addr;
13521 intel_crtc->cursor_bo = obj;
13522update:
Gustavo Padovana912f122014-12-01 15:40:10 -080013523
Matt Roper32b7eee2014-12-24 07:59:06 -080013524 if (intel_crtc->active)
Gustavo Padovan852e7872014-09-05 17:22:31 -030013525 intel_crtc_update_cursor(crtc, state->visible);
Matt Roper3d7d6512014-06-10 08:28:13 -070013526}
Gustavo Padovan852e7872014-09-05 17:22:31 -030013527
Matt Roper3d7d6512014-06-10 08:28:13 -070013528static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
13529 int pipe)
13530{
13531 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080013532 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070013533
13534 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
13535 if (cursor == NULL)
13536 return NULL;
13537
Matt Roper8e7d6882015-01-21 16:35:41 -080013538 state = intel_create_plane_state(&cursor->base);
13539 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013540 kfree(cursor);
13541 return NULL;
13542 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013543 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013544
Matt Roper3d7d6512014-06-10 08:28:13 -070013545 cursor->can_scale = false;
13546 cursor->max_downscale = 1;
13547 cursor->pipe = pipe;
13548 cursor->plane = pipe;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013549 state->scaler_id = -1;
Matt Roperc59cb172014-12-01 15:40:16 -080013550 cursor->check_plane = intel_check_cursor_plane;
13551 cursor->commit_plane = intel_commit_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070013552
13553 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013554 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070013555 intel_cursor_formats,
13556 ARRAY_SIZE(intel_cursor_formats),
13557 DRM_PLANE_TYPE_CURSOR);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013558
13559 if (INTEL_INFO(dev)->gen >= 4) {
13560 if (!dev->mode_config.rotation_property)
13561 dev->mode_config.rotation_property =
13562 drm_mode_create_rotation_property(dev,
13563 BIT(DRM_ROTATE_0) |
13564 BIT(DRM_ROTATE_180));
13565 if (dev->mode_config.rotation_property)
13566 drm_object_attach_property(&cursor->base.base,
13567 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080013568 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013569 }
13570
Matt Roperea2c67b2014-12-23 10:41:52 -080013571 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
13572
Matt Roper3d7d6512014-06-10 08:28:13 -070013573 return &cursor->base;
13574}
13575
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013576static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
13577 struct intel_crtc_state *crtc_state)
13578{
13579 int i;
13580 struct intel_scaler *intel_scaler;
13581 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
13582
13583 for (i = 0; i < intel_crtc->num_scalers; i++) {
13584 intel_scaler = &scaler_state->scalers[i];
13585 intel_scaler->in_use = 0;
13586 intel_scaler->id = i;
13587
13588 intel_scaler->mode = PS_SCALER_MODE_DYN;
13589 }
13590
13591 scaler_state->scaler_id = -1;
13592}
13593
Hannes Ederb358d0a2008-12-18 21:18:47 +010013594static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080013595{
Jani Nikulafbee40d2014-03-31 14:27:18 +030013596 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080013597 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013598 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070013599 struct drm_plane *primary = NULL;
13600 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070013601 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080013602
Daniel Vetter955382f2013-09-19 14:05:45 +020013603 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080013604 if (intel_crtc == NULL)
13605 return;
13606
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013607 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
13608 if (!crtc_state)
13609 goto fail;
13610 intel_crtc_set_state(intel_crtc, crtc_state);
Matt Roper07878242015-02-25 11:43:26 -080013611 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013612
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013613 /* initialize shared scalers */
13614 if (INTEL_INFO(dev)->gen >= 9) {
13615 if (pipe == PIPE_C)
13616 intel_crtc->num_scalers = 1;
13617 else
13618 intel_crtc->num_scalers = SKL_NUM_SCALERS;
13619
13620 skl_init_scalers(dev, intel_crtc, crtc_state);
13621 }
13622
Matt Roper465c1202014-05-29 08:06:54 -070013623 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070013624 if (!primary)
13625 goto fail;
13626
13627 cursor = intel_cursor_plane_create(dev, pipe);
13628 if (!cursor)
13629 goto fail;
13630
Matt Roper465c1202014-05-29 08:06:54 -070013631 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Matt Roper3d7d6512014-06-10 08:28:13 -070013632 cursor, &intel_crtc_funcs);
13633 if (ret)
13634 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080013635
13636 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080013637 for (i = 0; i < 256; i++) {
13638 intel_crtc->lut_r[i] = i;
13639 intel_crtc->lut_g[i] = i;
13640 intel_crtc->lut_b[i] = i;
13641 }
13642
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020013643 /*
13644 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020013645 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020013646 */
Jesse Barnes80824002009-09-10 15:28:06 -070013647 intel_crtc->pipe = pipe;
13648 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010013649 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080013650 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010013651 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070013652 }
13653
Chris Wilson4b0e3332014-05-30 16:35:26 +030013654 intel_crtc->cursor_base = ~0;
13655 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030013656 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030013657
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080013658 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
13659 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
13660 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
13661 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
13662
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020013663 INIT_WORK(&intel_crtc->mmio_flip.work, intel_mmio_flip_work_func);
13664
Jesse Barnes79e53942008-11-07 14:24:08 -080013665 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020013666
13667 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070013668 return;
13669
13670fail:
13671 if (primary)
13672 drm_plane_cleanup(primary);
13673 if (cursor)
13674 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013675 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070013676 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080013677}
13678
Jesse Barnes752aa882013-10-31 18:55:49 +020013679enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
13680{
13681 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020013682 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020013683
Rob Clark51fd3712013-11-19 12:10:12 -050013684 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020013685
Ville Syrjäläd3babd32014-11-07 11:16:01 +020013686 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020013687 return INVALID_PIPE;
13688
13689 return to_intel_crtc(encoder->crtc)->pipe;
13690}
13691
Carl Worth08d7b3d2009-04-29 14:43:54 -070013692int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000013693 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070013694{
Carl Worth08d7b3d2009-04-29 14:43:54 -070013695 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040013696 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020013697 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013698
Rob Clark7707e652014-07-17 23:30:04 -040013699 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070013700
Rob Clark7707e652014-07-17 23:30:04 -040013701 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070013702 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030013703 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013704 }
13705
Rob Clark7707e652014-07-17 23:30:04 -040013706 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020013707 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013708
Daniel Vetterc05422d2009-08-11 16:05:30 +020013709 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013710}
13711
Daniel Vetter66a92782012-07-12 20:08:18 +020013712static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080013713{
Daniel Vetter66a92782012-07-12 20:08:18 +020013714 struct drm_device *dev = encoder->base.dev;
13715 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080013716 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080013717 int entry = 0;
13718
Damien Lespiaub2784e12014-08-05 11:29:37 +010013719 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020013720 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020013721 index_mask |= (1 << entry);
13722
Jesse Barnes79e53942008-11-07 14:24:08 -080013723 entry++;
13724 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010013725
Jesse Barnes79e53942008-11-07 14:24:08 -080013726 return index_mask;
13727}
13728
Chris Wilson4d302442010-12-14 19:21:29 +000013729static bool has_edp_a(struct drm_device *dev)
13730{
13731 struct drm_i915_private *dev_priv = dev->dev_private;
13732
13733 if (!IS_MOBILE(dev))
13734 return false;
13735
13736 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
13737 return false;
13738
Damien Lespiaue3589902014-02-07 19:12:50 +000013739 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000013740 return false;
13741
13742 return true;
13743}
13744
Jesse Barnes84b4e042014-06-25 08:24:29 -070013745static bool intel_crt_present(struct drm_device *dev)
13746{
13747 struct drm_i915_private *dev_priv = dev->dev_private;
13748
Damien Lespiau884497e2013-12-03 13:56:23 +000013749 if (INTEL_INFO(dev)->gen >= 9)
13750 return false;
13751
Damien Lespiaucf404ce2014-10-01 20:04:15 +010013752 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070013753 return false;
13754
13755 if (IS_CHERRYVIEW(dev))
13756 return false;
13757
13758 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
13759 return false;
13760
13761 return true;
13762}
13763
Jesse Barnes79e53942008-11-07 14:24:08 -080013764static void intel_setup_outputs(struct drm_device *dev)
13765{
Eric Anholt725e30a2009-01-22 13:01:02 -080013766 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010013767 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013768 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080013769
Daniel Vetterc9093352013-06-06 22:22:47 +020013770 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080013771
Jesse Barnes84b4e042014-06-25 08:24:29 -070013772 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020013773 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013774
Vandana Kannanc776eb22014-08-19 12:05:01 +053013775 if (IS_BROXTON(dev)) {
13776 /*
13777 * FIXME: Broxton doesn't support port detection via the
13778 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
13779 * detect the ports.
13780 */
13781 intel_ddi_init(dev, PORT_A);
13782 intel_ddi_init(dev, PORT_B);
13783 intel_ddi_init(dev, PORT_C);
13784 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013785 int found;
13786
Jesse Barnesde31fac2015-03-06 15:53:32 -080013787 /*
13788 * Haswell uses DDI functions to detect digital outputs.
13789 * On SKL pre-D0 the strap isn't connected, so we assume
13790 * it's there.
13791 */
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013792 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080013793 /* WaIgnoreDDIAStrap: skl */
13794 if (found ||
13795 (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013796 intel_ddi_init(dev, PORT_A);
13797
13798 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
13799 * register */
13800 found = I915_READ(SFUSE_STRAP);
13801
13802 if (found & SFUSE_STRAP_DDIB_DETECTED)
13803 intel_ddi_init(dev, PORT_B);
13804 if (found & SFUSE_STRAP_DDIC_DETECTED)
13805 intel_ddi_init(dev, PORT_C);
13806 if (found & SFUSE_STRAP_DDID_DETECTED)
13807 intel_ddi_init(dev, PORT_D);
13808 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013809 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020013810 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020013811
13812 if (has_edp_a(dev))
13813 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013814
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013815 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080013816 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010013817 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013818 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013819 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013820 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013821 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013822 }
13823
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013824 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013825 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013826
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013827 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013828 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013829
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013830 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013831 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013832
Daniel Vetter270b3042012-10-27 15:52:05 +020013833 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013834 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070013835 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013836 /*
13837 * The DP_DETECTED bit is the latched state of the DDC
13838 * SDA pin at boot. However since eDP doesn't require DDC
13839 * (no way to plug in a DP->HDMI dongle) the DDC pins for
13840 * eDP ports may have been muxed to an alternate function.
13841 * Thus we can't rely on the DP_DETECTED bit alone to detect
13842 * eDP ports. Consult the VBT as well as DP_DETECTED to
13843 * detect eDP ports.
13844 */
Ville Syrjäläd2182a62015-01-09 14:21:14 +020013845 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
13846 !intel_dp_is_edp(dev, PORT_B))
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030013847 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
13848 PORT_B);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013849 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
13850 intel_dp_is_edp(dev, PORT_B))
13851 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030013852
Ville Syrjäläd2182a62015-01-09 14:21:14 +020013853 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
13854 !intel_dp_is_edp(dev, PORT_C))
Jesse Barnes6f6005a2013-08-09 09:34:35 -070013855 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
13856 PORT_C);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013857 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
13858 intel_dp_is_edp(dev, PORT_C))
13859 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053013860
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030013861 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013862 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030013863 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
13864 PORT_D);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013865 /* eDP not supported on port D, so don't check VBT */
13866 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
13867 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030013868 }
13869
Jani Nikula3cfca972013-08-27 15:12:26 +030013870 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +080013871 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080013872 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080013873
Paulo Zanonie2debe92013-02-18 19:00:27 -030013874 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013875 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030013876 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013877 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
13878 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030013879 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013880 }
Ma Ling27185ae2009-08-24 13:50:23 +080013881
Imre Deake7281ea2013-05-08 13:14:08 +030013882 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013883 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080013884 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040013885
13886 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040013887
Paulo Zanonie2debe92013-02-18 19:00:27 -030013888 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013889 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030013890 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013891 }
Ma Ling27185ae2009-08-24 13:50:23 +080013892
Paulo Zanonie2debe92013-02-18 19:00:27 -030013893 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080013894
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013895 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
13896 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030013897 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013898 }
Imre Deake7281ea2013-05-08 13:14:08 +030013899 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013900 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080013901 }
Ma Ling27185ae2009-08-24 13:50:23 +080013902
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013903 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030013904 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013905 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070013906 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080013907 intel_dvo_init(dev);
13908
Zhenyu Wang103a1962009-11-27 11:44:36 +080013909 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080013910 intel_tv_init(dev);
13911
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080013912 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070013913
Damien Lespiaub2784e12014-08-05 11:29:37 +010013914 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010013915 encoder->base.possible_crtcs = encoder->crtc_mask;
13916 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020013917 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080013918 }
Chris Wilson47356eb2011-01-11 17:06:04 +000013919
Paulo Zanonidde86e22012-12-01 12:04:25 -020013920 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020013921
13922 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080013923}
13924
13925static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
13926{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030013927 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080013928 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080013929
Daniel Vetteref2d6332014-02-10 18:00:38 +010013930 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030013931 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010013932 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030013933 drm_gem_object_unreference(&intel_fb->obj->base);
13934 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080013935 kfree(intel_fb);
13936}
13937
13938static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000013939 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080013940 unsigned int *handle)
13941{
13942 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000013943 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080013944
Chris Wilson05394f32010-11-08 19:18:58 +000013945 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080013946}
13947
13948static const struct drm_framebuffer_funcs intel_fb_funcs = {
13949 .destroy = intel_user_framebuffer_destroy,
13950 .create_handle = intel_user_framebuffer_create_handle,
13951};
13952
Damien Lespiaub3218032015-02-27 11:15:18 +000013953static
13954u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
13955 uint32_t pixel_format)
13956{
13957 u32 gen = INTEL_INFO(dev)->gen;
13958
13959 if (gen >= 9) {
13960 /* "The stride in bytes must not exceed the of the size of 8K
13961 * pixels and 32K bytes."
13962 */
13963 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
13964 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
13965 return 32*1024;
13966 } else if (gen >= 4) {
13967 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
13968 return 16*1024;
13969 else
13970 return 32*1024;
13971 } else if (gen >= 3) {
13972 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
13973 return 8*1024;
13974 else
13975 return 16*1024;
13976 } else {
13977 /* XXX DSPC is limited to 4k tiled */
13978 return 8*1024;
13979 }
13980}
13981
Daniel Vetterb5ea6422014-03-02 21:18:00 +010013982static int intel_framebuffer_init(struct drm_device *dev,
13983 struct intel_framebuffer *intel_fb,
13984 struct drm_mode_fb_cmd2 *mode_cmd,
13985 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080013986{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000013987 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080013988 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000013989 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080013990
Daniel Vetterdd4916c2013-10-09 21:23:51 +020013991 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
13992
Daniel Vetter2a80ead2015-02-10 17:16:06 +000013993 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
13994 /* Enforce that fb modifier and tiling mode match, but only for
13995 * X-tiled. This is needed for FBC. */
13996 if (!!(obj->tiling_mode == I915_TILING_X) !=
13997 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
13998 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
13999 return -EINVAL;
14000 }
14001 } else {
14002 if (obj->tiling_mode == I915_TILING_X)
14003 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14004 else if (obj->tiling_mode == I915_TILING_Y) {
14005 DRM_DEBUG("No Y tiling for legacy addfb\n");
14006 return -EINVAL;
14007 }
14008 }
14009
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014010 /* Passed in modifier sanity checking. */
14011 switch (mode_cmd->modifier[0]) {
14012 case I915_FORMAT_MOD_Y_TILED:
14013 case I915_FORMAT_MOD_Yf_TILED:
14014 if (INTEL_INFO(dev)->gen < 9) {
14015 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14016 mode_cmd->modifier[0]);
14017 return -EINVAL;
14018 }
14019 case DRM_FORMAT_MOD_NONE:
14020 case I915_FORMAT_MOD_X_TILED:
14021 break;
14022 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014023 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14024 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014025 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014026 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014027
Damien Lespiaub3218032015-02-27 11:15:18 +000014028 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
14029 mode_cmd->pixel_format);
14030 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14031 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14032 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014033 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014034 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014035
Damien Lespiaub3218032015-02-27 11:15:18 +000014036 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14037 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014038 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014039 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14040 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014041 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014042 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014043 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014044 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014045
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014046 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014047 mode_cmd->pitches[0] != obj->stride) {
14048 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14049 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014050 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014051 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014052
Ville Syrjälä57779d02012-10-31 17:50:14 +020014053 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014054 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014055 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014056 case DRM_FORMAT_RGB565:
14057 case DRM_FORMAT_XRGB8888:
14058 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014059 break;
14060 case DRM_FORMAT_XRGB1555:
14061 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014062 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014063 DRM_DEBUG("unsupported pixel format: %s\n",
14064 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014065 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014066 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014067 break;
14068 case DRM_FORMAT_XBGR8888:
14069 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014070 case DRM_FORMAT_XRGB2101010:
14071 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014072 case DRM_FORMAT_XBGR2101010:
14073 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014074 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014075 DRM_DEBUG("unsupported pixel format: %s\n",
14076 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014077 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014078 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014079 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014080 case DRM_FORMAT_YUYV:
14081 case DRM_FORMAT_UYVY:
14082 case DRM_FORMAT_YVYU:
14083 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014084 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014085 DRM_DEBUG("unsupported pixel format: %s\n",
14086 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014087 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014088 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014089 break;
14090 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014091 DRM_DEBUG("unsupported pixel format: %s\n",
14092 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014093 return -EINVAL;
14094 }
14095
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014096 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14097 if (mode_cmd->offsets[0] != 0)
14098 return -EINVAL;
14099
Damien Lespiauec2c9812015-01-20 12:51:45 +000014100 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014101 mode_cmd->pixel_format,
14102 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014103 /* FIXME drm helper for size checks (especially planar formats)? */
14104 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14105 return -EINVAL;
14106
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014107 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14108 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020014109 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014110
Jesse Barnes79e53942008-11-07 14:24:08 -080014111 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14112 if (ret) {
14113 DRM_ERROR("framebuffer init failed %d\n", ret);
14114 return ret;
14115 }
14116
Jesse Barnes79e53942008-11-07 14:24:08 -080014117 return 0;
14118}
14119
Jesse Barnes79e53942008-11-07 14:24:08 -080014120static struct drm_framebuffer *
14121intel_user_framebuffer_create(struct drm_device *dev,
14122 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014123 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014124{
Chris Wilson05394f32010-11-08 19:18:58 +000014125 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014126
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014127 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
14128 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014129 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014130 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014131
Chris Wilsond2dff872011-04-19 08:36:26 +010014132 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080014133}
14134
Daniel Vetter4520f532013-10-09 09:18:51 +020014135#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020014136static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014137{
14138}
14139#endif
14140
Jesse Barnes79e53942008-11-07 14:24:08 -080014141static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014142 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014143 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014144 .atomic_check = intel_atomic_check,
14145 .atomic_commit = intel_atomic_commit,
Jesse Barnes79e53942008-11-07 14:24:08 -080014146};
14147
Jesse Barnese70236a2009-09-21 10:42:27 -070014148/* Set up chip specific display functions */
14149static void intel_init_display(struct drm_device *dev)
14150{
14151 struct drm_i915_private *dev_priv = dev->dev_private;
14152
Daniel Vetteree9300b2013-06-03 22:40:22 +020014153 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14154 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030014155 else if (IS_CHERRYVIEW(dev))
14156 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020014157 else if (IS_VALLEYVIEW(dev))
14158 dev_priv->display.find_dpll = vlv_find_best_dpll;
14159 else if (IS_PINEVIEW(dev))
14160 dev_priv->display.find_dpll = pnv_find_best_dpll;
14161 else
14162 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14163
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014164 if (INTEL_INFO(dev)->gen >= 9) {
14165 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014166 dev_priv->display.get_initial_plane_config =
14167 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014168 dev_priv->display.crtc_compute_clock =
14169 haswell_crtc_compute_clock;
14170 dev_priv->display.crtc_enable = haswell_crtc_enable;
14171 dev_priv->display.crtc_disable = haswell_crtc_disable;
14172 dev_priv->display.off = ironlake_crtc_off;
14173 dev_priv->display.update_primary_plane =
14174 skylake_update_primary_plane;
14175 } else if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014176 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014177 dev_priv->display.get_initial_plane_config =
14178 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014179 dev_priv->display.crtc_compute_clock =
14180 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014181 dev_priv->display.crtc_enable = haswell_crtc_enable;
14182 dev_priv->display.crtc_disable = haswell_crtc_disable;
Daniel Vetterdf8ad702014-06-25 22:02:03 +030014183 dev_priv->display.off = ironlake_crtc_off;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014184 dev_priv->display.update_primary_plane =
14185 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030014186 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014187 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014188 dev_priv->display.get_initial_plane_config =
14189 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014190 dev_priv->display.crtc_compute_clock =
14191 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014192 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14193 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014194 dev_priv->display.off = ironlake_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070014195 dev_priv->display.update_primary_plane =
14196 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014197 } else if (IS_VALLEYVIEW(dev)) {
14198 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014199 dev_priv->display.get_initial_plane_config =
14200 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014201 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014202 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14203 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14204 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070014205 dev_priv->display.update_primary_plane =
14206 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014207 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014208 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014209 dev_priv->display.get_initial_plane_config =
14210 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014211 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014212 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14213 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014214 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070014215 dev_priv->display.update_primary_plane =
14216 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014217 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014218
Jesse Barnese70236a2009-09-21 10:42:27 -070014219 /* Returns the core display clock speed */
Ville Syrjälä1652d192015-03-31 14:12:01 +030014220 if (IS_SKYLAKE(dev))
14221 dev_priv->display.get_display_clock_speed =
14222 skylake_get_display_clock_speed;
14223 else if (IS_BROADWELL(dev))
14224 dev_priv->display.get_display_clock_speed =
14225 broadwell_get_display_clock_speed;
14226 else if (IS_HASWELL(dev))
14227 dev_priv->display.get_display_clock_speed =
14228 haswell_get_display_clock_speed;
14229 else if (IS_VALLEYVIEW(dev))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070014230 dev_priv->display.get_display_clock_speed =
14231 valleyview_get_display_clock_speed;
Ville Syrjäläb37a6432015-03-31 14:11:54 +030014232 else if (IS_GEN5(dev))
14233 dev_priv->display.get_display_clock_speed =
14234 ilk_get_display_clock_speed;
Ville Syrjäläa7c66cd2015-03-31 14:11:56 +030014235 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
14236 IS_GEN6(dev) || IS_IVYBRIDGE(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -070014237 dev_priv->display.get_display_clock_speed =
14238 i945_get_display_clock_speed;
14239 else if (IS_I915G(dev))
14240 dev_priv->display.get_display_clock_speed =
14241 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014242 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014243 dev_priv->display.get_display_clock_speed =
14244 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014245 else if (IS_PINEVIEW(dev))
14246 dev_priv->display.get_display_clock_speed =
14247 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014248 else if (IS_I915GM(dev))
14249 dev_priv->display.get_display_clock_speed =
14250 i915gm_get_display_clock_speed;
14251 else if (IS_I865G(dev))
14252 dev_priv->display.get_display_clock_speed =
14253 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020014254 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014255 dev_priv->display.get_display_clock_speed =
14256 i855_get_display_clock_speed;
14257 else /* 852, 830 */
14258 dev_priv->display.get_display_clock_speed =
14259 i830_get_display_clock_speed;
14260
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014261 if (IS_GEN5(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014262 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014263 } else if (IS_GEN6(dev)) {
14264 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014265 } else if (IS_IVYBRIDGE(dev)) {
14266 /* FIXME: detect B0+ stepping and use auto training */
14267 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Paulo Zanoni059b2fe2014-09-02 16:53:57 -030014268 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014269 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Jesse Barnes30a970c2013-11-04 13:48:12 -080014270 } else if (IS_VALLEYVIEW(dev)) {
14271 dev_priv->display.modeset_global_resources =
14272 valleyview_modeset_global_resources;
Vandana Kannanf8437dd12014-11-24 13:37:39 +053014273 } else if (IS_BROXTON(dev)) {
14274 dev_priv->display.modeset_global_resources =
14275 broxton_modeset_global_resources;
Jesse Barnese70236a2009-09-21 10:42:27 -070014276 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014277
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014278 switch (INTEL_INFO(dev)->gen) {
14279 case 2:
14280 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14281 break;
14282
14283 case 3:
14284 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14285 break;
14286
14287 case 4:
14288 case 5:
14289 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14290 break;
14291
14292 case 6:
14293 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14294 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014295 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070014296 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014297 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14298 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000014299 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000014300 /* Drop through - unsupported since execlist only. */
14301 default:
14302 /* Default just returns -ENODEV to indicate unsupported */
14303 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014304 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020014305
14306 intel_panel_init_backlight_funcs(dev);
Ville Syrjäläe39b9992014-09-04 14:53:14 +030014307
14308 mutex_init(&dev_priv->pps_mutex);
Jesse Barnese70236a2009-09-21 10:42:27 -070014309}
14310
Jesse Barnesb690e962010-07-19 13:53:12 -070014311/*
14312 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14313 * resume, or other times. This quirk makes sure that's the case for
14314 * affected systems.
14315 */
Akshay Joshi0206e352011-08-16 15:34:10 -040014316static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070014317{
14318 struct drm_i915_private *dev_priv = dev->dev_private;
14319
14320 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014321 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014322}
14323
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014324static void quirk_pipeb_force(struct drm_device *dev)
14325{
14326 struct drm_i915_private *dev_priv = dev->dev_private;
14327
14328 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14329 DRM_INFO("applying pipe b force quirk\n");
14330}
14331
Keith Packard435793d2011-07-12 14:56:22 -070014332/*
14333 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14334 */
14335static void quirk_ssc_force_disable(struct drm_device *dev)
14336{
14337 struct drm_i915_private *dev_priv = dev->dev_private;
14338 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014339 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070014340}
14341
Carsten Emde4dca20e2012-03-15 15:56:26 +010014342/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010014343 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14344 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010014345 */
14346static void quirk_invert_brightness(struct drm_device *dev)
14347{
14348 struct drm_i915_private *dev_priv = dev->dev_private;
14349 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014350 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014351}
14352
Scot Doyle9c72cc62014-07-03 23:27:50 +000014353/* Some VBT's incorrectly indicate no backlight is present */
14354static void quirk_backlight_present(struct drm_device *dev)
14355{
14356 struct drm_i915_private *dev_priv = dev->dev_private;
14357 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14358 DRM_INFO("applying backlight present quirk\n");
14359}
14360
Jesse Barnesb690e962010-07-19 13:53:12 -070014361struct intel_quirk {
14362 int device;
14363 int subsystem_vendor;
14364 int subsystem_device;
14365 void (*hook)(struct drm_device *dev);
14366};
14367
Egbert Eich5f85f172012-10-14 15:46:38 +020014368/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14369struct intel_dmi_quirk {
14370 void (*hook)(struct drm_device *dev);
14371 const struct dmi_system_id (*dmi_id_list)[];
14372};
14373
14374static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14375{
14376 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14377 return 1;
14378}
14379
14380static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14381 {
14382 .dmi_id_list = &(const struct dmi_system_id[]) {
14383 {
14384 .callback = intel_dmi_reverse_brightness,
14385 .ident = "NCR Corporation",
14386 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14387 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14388 },
14389 },
14390 { } /* terminating entry */
14391 },
14392 .hook = quirk_invert_brightness,
14393 },
14394};
14395
Ben Widawskyc43b5632012-04-16 14:07:40 -070014396static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070014397 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -040014398 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -070014399
Jesse Barnesb690e962010-07-19 13:53:12 -070014400 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14401 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14402
Jesse Barnesb690e962010-07-19 13:53:12 -070014403 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14404 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14405
Ville Syrjälä5f080c02014-08-15 01:22:06 +030014406 /* 830 needs to leave pipe A & dpll A up */
14407 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14408
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014409 /* 830 needs to leave pipe B & dpll B up */
14410 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14411
Keith Packard435793d2011-07-12 14:56:22 -070014412 /* Lenovo U160 cannot use SSC on LVDS */
14413 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020014414
14415 /* Sony Vaio Y cannot use SSC on LVDS */
14416 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010014417
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010014418 /* Acer Aspire 5734Z must invert backlight brightness */
14419 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14420
14421 /* Acer/eMachines G725 */
14422 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14423
14424 /* Acer/eMachines e725 */
14425 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14426
14427 /* Acer/Packard Bell NCL20 */
14428 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14429
14430 /* Acer Aspire 4736Z */
14431 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020014432
14433 /* Acer Aspire 5336 */
14434 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000014435
14436 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14437 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000014438
Scot Doyledfb3d47b2014-08-21 16:08:02 +000014439 /* Acer C720 Chromebook (Core i3 4005U) */
14440 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14441
jens steinb2a96012014-10-28 20:25:53 +010014442 /* Apple Macbook 2,1 (Core 2 T7400) */
14443 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14444
Scot Doyled4967d82014-07-03 23:27:52 +000014445 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14446 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000014447
14448 /* HP Chromebook 14 (Celeron 2955U) */
14449 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020014450
14451 /* Dell Chromebook 11 */
14452 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070014453};
14454
14455static void intel_init_quirks(struct drm_device *dev)
14456{
14457 struct pci_dev *d = dev->pdev;
14458 int i;
14459
14460 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14461 struct intel_quirk *q = &intel_quirks[i];
14462
14463 if (d->device == q->device &&
14464 (d->subsystem_vendor == q->subsystem_vendor ||
14465 q->subsystem_vendor == PCI_ANY_ID) &&
14466 (d->subsystem_device == q->subsystem_device ||
14467 q->subsystem_device == PCI_ANY_ID))
14468 q->hook(dev);
14469 }
Egbert Eich5f85f172012-10-14 15:46:38 +020014470 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14471 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14472 intel_dmi_quirks[i].hook(dev);
14473 }
Jesse Barnesb690e962010-07-19 13:53:12 -070014474}
14475
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014476/* Disable the VGA plane that we never use */
14477static void i915_disable_vga(struct drm_device *dev)
14478{
14479 struct drm_i915_private *dev_priv = dev->dev_private;
14480 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020014481 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014482
Ville Syrjälä2b37c612014-01-22 21:32:38 +020014483 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014484 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070014485 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014486 sr1 = inb(VGA_SR_DATA);
14487 outb(sr1 | 1<<5, VGA_SR_DATA);
14488 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
14489 udelay(300);
14490
Ville Syrjälä01f5a622014-12-16 18:38:37 +020014491 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014492 POSTING_READ(vga_reg);
14493}
14494
Daniel Vetterf8175862012-04-10 15:50:11 +020014495void intel_modeset_init_hw(struct drm_device *dev)
14496{
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030014497 intel_prepare_ddi(dev);
14498
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +030014499 if (IS_VALLEYVIEW(dev))
14500 vlv_update_cdclk(dev);
14501
Daniel Vetterf8175862012-04-10 15:50:11 +020014502 intel_init_clock_gating(dev);
14503
Daniel Vetter8090c6b2012-06-24 16:42:32 +020014504 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020014505}
14506
Jesse Barnes79e53942008-11-07 14:24:08 -080014507void intel_modeset_init(struct drm_device *dev)
14508{
Jesse Barnes652c3932009-08-17 13:31:43 -070014509 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000014510 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014511 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080014512 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080014513
14514 drm_mode_config_init(dev);
14515
14516 dev->mode_config.min_width = 0;
14517 dev->mode_config.min_height = 0;
14518
Dave Airlie019d96c2011-09-29 16:20:42 +010014519 dev->mode_config.preferred_depth = 24;
14520 dev->mode_config.prefer_shadow = 1;
14521
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000014522 dev->mode_config.allow_fb_modifiers = true;
14523
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020014524 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080014525
Jesse Barnesb690e962010-07-19 13:53:12 -070014526 intel_init_quirks(dev);
14527
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030014528 intel_init_pm(dev);
14529
Ben Widawskye3c74752013-04-05 13:12:39 -070014530 if (INTEL_INFO(dev)->num_pipes == 0)
14531 return;
14532
Jesse Barnese70236a2009-09-21 10:42:27 -070014533 intel_init_display(dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014534 intel_init_audio(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070014535
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014536 if (IS_GEN2(dev)) {
14537 dev->mode_config.max_width = 2048;
14538 dev->mode_config.max_height = 2048;
14539 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070014540 dev->mode_config.max_width = 4096;
14541 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080014542 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014543 dev->mode_config.max_width = 8192;
14544 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080014545 }
Damien Lespiau068be562014-03-28 14:17:49 +000014546
Ville Syrjälädc41c152014-08-13 11:57:05 +030014547 if (IS_845G(dev) || IS_I865G(dev)) {
14548 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
14549 dev->mode_config.cursor_height = 1023;
14550 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000014551 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
14552 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
14553 } else {
14554 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
14555 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
14556 }
14557
Ben Widawsky5d4545a2013-01-17 12:45:15 -080014558 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080014559
Zhao Yakui28c97732009-10-09 11:39:41 +080014560 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014561 INTEL_INFO(dev)->num_pipes,
14562 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080014563
Damien Lespiau055e3932014-08-18 13:49:10 +010014564 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014565 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000014566 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000014567 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070014568 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030014569 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000014570 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070014571 }
Jesse Barnes79e53942008-11-07 14:24:08 -080014572 }
14573
Jesse Barnesf42bb702013-12-16 16:34:23 -080014574 intel_init_dpio(dev);
14575
Daniel Vettere72f9fb2013-06-05 13:34:06 +020014576 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014577
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014578 /* Just disable it once at startup */
14579 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014580 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000014581
14582 /* Just in case the BIOS is doing something questionable. */
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020014583 intel_fbc_disable(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080014584
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014585 drm_modeset_lock_all(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080014586 intel_modeset_setup_hw_state(dev, false);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014587 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014588
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014589 for_each_intel_crtc(dev, crtc) {
Jesse Barnes46f297f2014-03-07 08:57:48 -080014590 if (!crtc->active)
14591 continue;
14592
Jesse Barnes46f297f2014-03-07 08:57:48 -080014593 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080014594 * Note that reserving the BIOS fb up front prevents us
14595 * from stuffing other stolen allocations like the ring
14596 * on top. This prevents some ugliness at boot time, and
14597 * can even allow for smooth boot transitions if the BIOS
14598 * fb is large enough for the active pipe configuration.
14599 */
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014600 if (dev_priv->display.get_initial_plane_config) {
14601 dev_priv->display.get_initial_plane_config(crtc,
Jesse Barnes46f297f2014-03-07 08:57:48 -080014602 &crtc->plane_config);
14603 /*
14604 * If the fb is shared between multiple heads, we'll
14605 * just get the first one.
14606 */
Daniel Vetterf6936e22015-03-26 12:17:05 +010014607 intel_find_initial_plane_obj(crtc, &crtc->plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014608 }
Jesse Barnes46f297f2014-03-07 08:57:48 -080014609 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010014610}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080014611
Daniel Vetter7fad7982012-07-04 17:51:47 +020014612static void intel_enable_pipe_a(struct drm_device *dev)
14613{
14614 struct intel_connector *connector;
14615 struct drm_connector *crt = NULL;
14616 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030014617 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020014618
14619 /* We can't just switch on the pipe A, we need to set things up with a
14620 * proper mode and output configuration. As a gross hack, enable pipe A
14621 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014622 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020014623 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
14624 crt = &connector->base;
14625 break;
14626 }
14627 }
14628
14629 if (!crt)
14630 return;
14631
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030014632 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020014633 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020014634}
14635
Daniel Vetterfa555832012-10-10 23:14:00 +020014636static bool
14637intel_check_plane_mapping(struct intel_crtc *crtc)
14638{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014639 struct drm_device *dev = crtc->base.dev;
14640 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020014641 u32 reg, val;
14642
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014643 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020014644 return true;
14645
14646 reg = DSPCNTR(!crtc->plane);
14647 val = I915_READ(reg);
14648
14649 if ((val & DISPLAY_PLANE_ENABLE) &&
14650 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
14651 return false;
14652
14653 return true;
14654}
14655
Daniel Vetter24929352012-07-02 20:28:59 +020014656static void intel_sanitize_crtc(struct intel_crtc *crtc)
14657{
14658 struct drm_device *dev = crtc->base.dev;
14659 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020014660 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020014661
Daniel Vetter24929352012-07-02 20:28:59 +020014662 /* Clear any frame start delays used for debugging left by the BIOS */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014663 reg = PIPECONF(crtc->config->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020014664 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
14665
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030014666 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010014667 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030014668 if (crtc->active) {
14669 update_scanline_offset(crtc);
Daniel Vetter96256042015-02-13 21:03:42 +010014670 drm_crtc_vblank_on(&crtc->base);
14671 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030014672
Daniel Vetter24929352012-07-02 20:28:59 +020014673 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020014674 * disable the crtc (and hence change the state) if it is wrong. Note
14675 * that gen4+ has a fixed plane -> pipe mapping. */
14676 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020014677 struct intel_connector *connector;
14678 bool plane;
14679
Daniel Vetter24929352012-07-02 20:28:59 +020014680 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
14681 crtc->base.base.id);
14682
14683 /* Pipe has the wrong plane attached and the plane is active.
14684 * Temporarily change the plane mapping and disable everything
14685 * ... */
14686 plane = crtc->plane;
14687 crtc->plane = !plane;
Daniel Vetter9c8958b2014-07-14 19:35:31 +020014688 crtc->primary_enabled = true;
Daniel Vetter24929352012-07-02 20:28:59 +020014689 dev_priv->display.crtc_disable(&crtc->base);
14690 crtc->plane = plane;
14691
14692 /* ... and break all links. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014693 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020014694 if (connector->encoder->base.crtc != &crtc->base)
14695 continue;
14696
Egbert Eich7f1950f2014-04-25 10:56:22 +020014697 connector->base.dpms = DRM_MODE_DPMS_OFF;
14698 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020014699 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020014700 /* multiple connectors may have the same encoder:
14701 * handle them and break crtc link separately */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014702 for_each_intel_connector(dev, connector)
Egbert Eich7f1950f2014-04-25 10:56:22 +020014703 if (connector->encoder->base.crtc == &crtc->base) {
14704 connector->encoder->base.crtc = NULL;
14705 connector->encoder->connectors_active = false;
14706 }
Daniel Vetter24929352012-07-02 20:28:59 +020014707
14708 WARN_ON(crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080014709 crtc->base.state->enable = false;
Daniel Vetter24929352012-07-02 20:28:59 +020014710 crtc->base.enabled = false;
14711 }
Daniel Vetter24929352012-07-02 20:28:59 +020014712
Daniel Vetter7fad7982012-07-04 17:51:47 +020014713 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
14714 crtc->pipe == PIPE_A && !crtc->active) {
14715 /* BIOS forgot to enable pipe A, this mostly happens after
14716 * resume. Force-enable the pipe to fix this, the update_dpms
14717 * call below we restore the pipe to the right state, but leave
14718 * the required bits on. */
14719 intel_enable_pipe_a(dev);
14720 }
14721
Daniel Vetter24929352012-07-02 20:28:59 +020014722 /* Adjust the state of the output pipe according to whether we
14723 * have active connectors/encoders. */
14724 intel_crtc_update_dpms(&crtc->base);
14725
Matt Roper83d65732015-02-25 13:12:16 -080014726 if (crtc->active != crtc->base.state->enable) {
Daniel Vetter24929352012-07-02 20:28:59 +020014727 struct intel_encoder *encoder;
14728
14729 /* This can happen either due to bugs in the get_hw_state
14730 * functions or because the pipe is force-enabled due to the
14731 * pipe A quirk. */
14732 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
14733 crtc->base.base.id,
Matt Roper83d65732015-02-25 13:12:16 -080014734 crtc->base.state->enable ? "enabled" : "disabled",
Daniel Vetter24929352012-07-02 20:28:59 +020014735 crtc->active ? "enabled" : "disabled");
14736
Matt Roper83d65732015-02-25 13:12:16 -080014737 crtc->base.state->enable = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020014738 crtc->base.enabled = crtc->active;
14739
14740 /* Because we only establish the connector -> encoder ->
14741 * crtc links if something is active, this means the
14742 * crtc is now deactivated. Break the links. connector
14743 * -> encoder links are only establish when things are
14744 * actually up, hence no need to break them. */
14745 WARN_ON(crtc->active);
14746
14747 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
14748 WARN_ON(encoder->connectors_active);
14749 encoder->base.crtc = NULL;
14750 }
14751 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020014752
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030014753 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010014754 /*
14755 * We start out with underrun reporting disabled to avoid races.
14756 * For correct bookkeeping mark this on active crtcs.
14757 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020014758 * Also on gmch platforms we dont have any hardware bits to
14759 * disable the underrun reporting. Which means we need to start
14760 * out with underrun reporting disabled also on inactive pipes,
14761 * since otherwise we'll complain about the garbage we read when
14762 * e.g. coming up after runtime pm.
14763 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010014764 * No protection against concurrent access is required - at
14765 * worst a fifo underrun happens which also sets this to false.
14766 */
14767 crtc->cpu_fifo_underrun_disabled = true;
14768 crtc->pch_fifo_underrun_disabled = true;
14769 }
Daniel Vetter24929352012-07-02 20:28:59 +020014770}
14771
14772static void intel_sanitize_encoder(struct intel_encoder *encoder)
14773{
14774 struct intel_connector *connector;
14775 struct drm_device *dev = encoder->base.dev;
14776
14777 /* We need to check both for a crtc link (meaning that the
14778 * encoder is active and trying to read from a pipe) and the
14779 * pipe itself being active. */
14780 bool has_active_crtc = encoder->base.crtc &&
14781 to_intel_crtc(encoder->base.crtc)->active;
14782
14783 if (encoder->connectors_active && !has_active_crtc) {
14784 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
14785 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014786 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020014787
14788 /* Connector is active, but has no active pipe. This is
14789 * fallout from our resume register restoring. Disable
14790 * the encoder manually again. */
14791 if (encoder->base.crtc) {
14792 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
14793 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014794 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020014795 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030014796 if (encoder->post_disable)
14797 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020014798 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020014799 encoder->base.crtc = NULL;
14800 encoder->connectors_active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020014801
14802 /* Inconsistent output/port/pipe state happens presumably due to
14803 * a bug in one of the get_hw_state functions. Or someplace else
14804 * in our code, like the register restore mess on resume. Clamp
14805 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014806 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020014807 if (connector->encoder != encoder)
14808 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020014809 connector->base.dpms = DRM_MODE_DPMS_OFF;
14810 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020014811 }
14812 }
14813 /* Enabled encoders without active connectors will be fixed in
14814 * the crtc fixup. */
14815}
14816
Imre Deak04098752014-02-18 00:02:16 +020014817void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014818{
14819 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020014820 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014821
Imre Deak04098752014-02-18 00:02:16 +020014822 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
14823 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
14824 i915_disable_vga(dev);
14825 }
14826}
14827
14828void i915_redisable_vga(struct drm_device *dev)
14829{
14830 struct drm_i915_private *dev_priv = dev->dev_private;
14831
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030014832 /* This function can be called both from intel_modeset_setup_hw_state or
14833 * at a very early point in our resume sequence, where the power well
14834 * structures are not yet restored. Since this function is at a very
14835 * paranoid "someone might have enabled VGA while we were not looking"
14836 * level, just check if the power well is enabled instead of trying to
14837 * follow the "don't touch the power well if we don't need it" policy
14838 * the rest of the driver uses. */
Daniel Vetterf458ebb2014-09-30 10:56:39 +020014839 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030014840 return;
14841
Imre Deak04098752014-02-18 00:02:16 +020014842 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014843}
14844
Ville Syrjälä98ec7732014-04-30 17:43:01 +030014845static bool primary_get_hw_state(struct intel_crtc *crtc)
14846{
14847 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
14848
14849 if (!crtc->active)
14850 return false;
14851
14852 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
14853}
14854
Daniel Vetter30e984d2013-06-05 13:34:17 +020014855static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020014856{
14857 struct drm_i915_private *dev_priv = dev->dev_private;
14858 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020014859 struct intel_crtc *crtc;
14860 struct intel_encoder *encoder;
14861 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020014862 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020014863
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014864 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014865 memset(crtc->config, 0, sizeof(*crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +020014866
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014867 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
Daniel Vetter99535992014-04-13 12:00:33 +020014868
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014869 crtc->active = dev_priv->display.get_pipe_config(crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014870 crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020014871
Matt Roper83d65732015-02-25 13:12:16 -080014872 crtc->base.state->enable = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020014873 crtc->base.enabled = crtc->active;
Ville Syrjälä98ec7732014-04-30 17:43:01 +030014874 crtc->primary_enabled = primary_get_hw_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020014875
14876 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
14877 crtc->base.base.id,
14878 crtc->active ? "enabled" : "disabled");
14879 }
14880
Daniel Vetter53589012013-06-05 13:34:16 +020014881 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
14882 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
14883
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020014884 pll->on = pll->get_hw_state(dev_priv, pll,
14885 &pll->config.hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020014886 pll->active = 0;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020014887 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014888 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020014889 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
Daniel Vetter53589012013-06-05 13:34:16 +020014890 pll->active++;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020014891 pll->config.crtc_mask |= 1 << crtc->pipe;
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020014892 }
Daniel Vetter53589012013-06-05 13:34:16 +020014893 }
Daniel Vetter53589012013-06-05 13:34:16 +020014894
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020014895 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020014896 pll->name, pll->config.crtc_mask, pll->on);
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030014897
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020014898 if (pll->config.crtc_mask)
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030014899 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
Daniel Vetter53589012013-06-05 13:34:16 +020014900 }
14901
Damien Lespiaub2784e12014-08-05 11:29:37 +010014902 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020014903 pipe = 0;
14904
14905 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070014906 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
14907 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014908 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020014909 } else {
14910 encoder->base.crtc = NULL;
14911 }
14912
14913 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010014914 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020014915 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014916 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020014917 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010014918 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020014919 }
14920
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014921 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020014922 if (connector->get_hw_state(connector)) {
14923 connector->base.dpms = DRM_MODE_DPMS_ON;
14924 connector->encoder->connectors_active = true;
14925 connector->base.encoder = &connector->encoder->base;
14926 } else {
14927 connector->base.dpms = DRM_MODE_DPMS_OFF;
14928 connector->base.encoder = NULL;
14929 }
14930 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
14931 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030014932 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020014933 connector->base.encoder ? "enabled" : "disabled");
14934 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020014935}
14936
14937/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
14938 * and i915 state tracking structures. */
14939void intel_modeset_setup_hw_state(struct drm_device *dev,
14940 bool force_restore)
14941{
14942 struct drm_i915_private *dev_priv = dev->dev_private;
14943 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020014944 struct intel_crtc *crtc;
14945 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020014946 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020014947
14948 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020014949
Jesse Barnesbabea612013-06-26 18:57:38 +030014950 /*
14951 * Now that we have the config, copy it to each CRTC struct
14952 * Note that this could go away if we move to using crtc_config
14953 * checking everywhere.
14954 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014955 for_each_intel_crtc(dev, crtc) {
Jani Nikulad330a952014-01-21 11:24:25 +020014956 if (crtc->active && i915.fastboot) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014957 intel_mode_from_pipe_config(&crtc->base.mode,
14958 crtc->config);
Jesse Barnesbabea612013-06-26 18:57:38 +030014959 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
14960 crtc->base.base.id);
14961 drm_mode_debug_printmodeline(&crtc->base.mode);
14962 }
14963 }
14964
Daniel Vetter24929352012-07-02 20:28:59 +020014965 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010014966 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020014967 intel_sanitize_encoder(encoder);
14968 }
14969
Damien Lespiau055e3932014-08-18 13:49:10 +010014970 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020014971 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
14972 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014973 intel_dump_pipe_config(crtc, crtc->config,
14974 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020014975 }
Daniel Vetter9a935852012-07-05 22:34:27 +020014976
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020014977 intel_modeset_update_connector_atomic_state(dev);
14978
Daniel Vetter35c95372013-07-17 06:55:04 +020014979 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
14980 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
14981
14982 if (!pll->on || pll->active)
14983 continue;
14984
14985 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
14986
14987 pll->disable(dev_priv, pll);
14988 pll->on = false;
14989 }
14990
Pradeep Bhat30789992014-11-04 17:06:45 +000014991 if (IS_GEN9(dev))
14992 skl_wm_get_hw_state(dev);
14993 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030014994 ilk_wm_get_hw_state(dev);
14995
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010014996 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030014997 i915_redisable_vga(dev);
14998
Daniel Vetterf30da182013-04-11 20:22:50 +020014999 /*
15000 * We need to use raw interfaces for restoring state to avoid
15001 * checking (bogus) intermediate states.
15002 */
Damien Lespiau055e3932014-08-18 13:49:10 +010015003 for_each_pipe(dev_priv, pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070015004 struct drm_crtc *crtc =
15005 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020015006
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020015007 intel_crtc_restore_mode(crtc);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015008 }
15009 } else {
15010 intel_modeset_update_staged_output_state(dev);
15011 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020015012
15013 intel_modeset_check_state(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015014}
15015
15016void intel_modeset_gem_init(struct drm_device *dev)
15017{
Jesse Barnes92122782014-10-09 12:57:42 -070015018 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015019 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070015020 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015021 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015022
Imre Deakae484342014-03-31 15:10:44 +030015023 mutex_lock(&dev->struct_mutex);
15024 intel_init_gt_powersave(dev);
15025 mutex_unlock(&dev->struct_mutex);
15026
Jesse Barnes92122782014-10-09 12:57:42 -070015027 /*
15028 * There may be no VBT; and if the BIOS enabled SSC we can
15029 * just keep using it to avoid unnecessary flicker. Whereas if the
15030 * BIOS isn't using it, don't assume it will work even if the VBT
15031 * indicates as much.
15032 */
15033 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
15034 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15035 DREF_SSC1_ENABLE);
15036
Chris Wilson1833b132012-05-09 11:56:28 +010015037 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020015038
15039 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015040
15041 /*
15042 * Make sure any fbs we allocated at startup are properly
15043 * pinned & fenced. When we do the allocation it's too early
15044 * for this.
15045 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010015046 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070015047 obj = intel_fb_obj(c->primary->fb);
15048 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080015049 continue;
15050
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015051 mutex_lock(&dev->struct_mutex);
15052 ret = intel_pin_and_fence_fb_obj(c->primary,
15053 c->primary->fb,
15054 c->primary->state,
15055 NULL);
15056 mutex_unlock(&dev->struct_mutex);
15057 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080015058 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15059 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100015060 drm_framebuffer_unreference(c->primary->fb);
15061 c->primary->fb = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080015062 update_state_fb(c->primary);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015063 }
15064 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015065
15066 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015067}
15068
Imre Deak4932e2c2014-02-11 17:12:48 +020015069void intel_connector_unregister(struct intel_connector *intel_connector)
15070{
15071 struct drm_connector *connector = &intel_connector->base;
15072
15073 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010015074 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015075}
15076
Jesse Barnes79e53942008-11-07 14:24:08 -080015077void intel_modeset_cleanup(struct drm_device *dev)
15078{
Jesse Barnes652c3932009-08-17 13:31:43 -070015079 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid9255d52013-09-26 20:05:59 -030015080 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070015081
Imre Deak2eb52522014-11-19 15:30:05 +020015082 intel_disable_gt_powersave(dev);
15083
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015084 intel_backlight_unregister(dev);
15085
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015086 /*
15087 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020015088 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015089 * experience fancy races otherwise.
15090 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020015091 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070015092
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015093 /*
15094 * Due to the hpd irq storm handling the hotplug work can re-arm the
15095 * poll handlers. Hence disable polling after hpd handling is shut down.
15096 */
Keith Packardf87ea762010-10-03 19:36:26 -070015097 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015098
Jesse Barnes652c3932009-08-17 13:31:43 -070015099 mutex_lock(&dev->struct_mutex);
15100
Jesse Barnes723bfd72010-10-07 16:01:13 -070015101 intel_unregister_dsm_handler();
15102
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020015103 intel_fbc_disable(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070015104
Kristian Høgsberg69341a52009-11-11 12:19:17 -050015105 mutex_unlock(&dev->struct_mutex);
15106
Chris Wilson1630fe72011-07-08 12:22:42 +010015107 /* flush any delayed tasks or pending work */
15108 flush_scheduled_work();
15109
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015110 /* destroy the backlight and sysfs files before encoders/connectors */
15111 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020015112 struct intel_connector *intel_connector;
15113
15114 intel_connector = to_intel_connector(connector);
15115 intel_connector->unregister(intel_connector);
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015116 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030015117
Jesse Barnes79e53942008-11-07 14:24:08 -080015118 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010015119
15120 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030015121
15122 mutex_lock(&dev->struct_mutex);
15123 intel_cleanup_gt_powersave(dev);
15124 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080015125}
15126
Dave Airlie28d52042009-09-21 14:33:58 +100015127/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080015128 * Return which encoder is currently attached for connector.
15129 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010015130struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080015131{
Chris Wilsondf0e9242010-09-09 16:20:55 +010015132 return &intel_attached_encoder(connector)->base;
15133}
Jesse Barnes79e53942008-11-07 14:24:08 -080015134
Chris Wilsondf0e9242010-09-09 16:20:55 +010015135void intel_connector_attach_encoder(struct intel_connector *connector,
15136 struct intel_encoder *encoder)
15137{
15138 connector->encoder = encoder;
15139 drm_mode_connector_attach_encoder(&connector->base,
15140 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080015141}
Dave Airlie28d52042009-09-21 14:33:58 +100015142
15143/*
15144 * set vga decode state - true == enable VGA decode
15145 */
15146int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15147{
15148 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000015149 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100015150 u16 gmch_ctrl;
15151
Chris Wilson75fa0412014-02-07 18:37:02 -020015152 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15153 DRM_ERROR("failed to read control word\n");
15154 return -EIO;
15155 }
15156
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020015157 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15158 return 0;
15159
Dave Airlie28d52042009-09-21 14:33:58 +100015160 if (state)
15161 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15162 else
15163 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020015164
15165 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15166 DRM_ERROR("failed to write control word\n");
15167 return -EIO;
15168 }
15169
Dave Airlie28d52042009-09-21 14:33:58 +100015170 return 0;
15171}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015172
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015173struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015174
15175 u32 power_well_driver;
15176
Chris Wilson63b66e52013-08-08 15:12:06 +020015177 int num_transcoders;
15178
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015179 struct intel_cursor_error_state {
15180 u32 control;
15181 u32 position;
15182 u32 base;
15183 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010015184 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015185
15186 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015187 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015188 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030015189 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010015190 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015191
15192 struct intel_plane_error_state {
15193 u32 control;
15194 u32 stride;
15195 u32 size;
15196 u32 pos;
15197 u32 addr;
15198 u32 surface;
15199 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010015200 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020015201
15202 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015203 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020015204 enum transcoder cpu_transcoder;
15205
15206 u32 conf;
15207
15208 u32 htotal;
15209 u32 hblank;
15210 u32 hsync;
15211 u32 vtotal;
15212 u32 vblank;
15213 u32 vsync;
15214 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015215};
15216
15217struct intel_display_error_state *
15218intel_display_capture_error_state(struct drm_device *dev)
15219{
Jani Nikulafbee40d2014-03-31 14:27:18 +030015220 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015221 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020015222 int transcoders[] = {
15223 TRANSCODER_A,
15224 TRANSCODER_B,
15225 TRANSCODER_C,
15226 TRANSCODER_EDP,
15227 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015228 int i;
15229
Chris Wilson63b66e52013-08-08 15:12:06 +020015230 if (INTEL_INFO(dev)->num_pipes == 0)
15231 return NULL;
15232
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015233 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015234 if (error == NULL)
15235 return NULL;
15236
Imre Deak190be112013-11-25 17:15:31 +020015237 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015238 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15239
Damien Lespiau055e3932014-08-18 13:49:10 +010015240 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020015241 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015242 __intel_display_power_is_enabled(dev_priv,
15243 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020015244 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015245 continue;
15246
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030015247 error->cursor[i].control = I915_READ(CURCNTR(i));
15248 error->cursor[i].position = I915_READ(CURPOS(i));
15249 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015250
15251 error->plane[i].control = I915_READ(DSPCNTR(i));
15252 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015253 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030015254 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015255 error->plane[i].pos = I915_READ(DSPPOS(i));
15256 }
Paulo Zanonica291362013-03-06 20:03:14 -030015257 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15258 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015259 if (INTEL_INFO(dev)->gen >= 4) {
15260 error->plane[i].surface = I915_READ(DSPSURF(i));
15261 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15262 }
15263
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015264 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030015265
Sonika Jindal3abfce72014-07-21 15:23:43 +053015266 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e2014-04-18 15:55:04 +030015267 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020015268 }
15269
15270 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
15271 if (HAS_DDI(dev_priv->dev))
15272 error->num_transcoders++; /* Account for eDP. */
15273
15274 for (i = 0; i < error->num_transcoders; i++) {
15275 enum transcoder cpu_transcoder = transcoders[i];
15276
Imre Deakddf9c532013-11-27 22:02:02 +020015277 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015278 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020015279 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015280 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015281 continue;
15282
Chris Wilson63b66e52013-08-08 15:12:06 +020015283 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15284
15285 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15286 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15287 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15288 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15289 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15290 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15291 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015292 }
15293
15294 return error;
15295}
15296
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015297#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15298
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015299void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015300intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015301 struct drm_device *dev,
15302 struct intel_display_error_state *error)
15303{
Damien Lespiau055e3932014-08-18 13:49:10 +010015304 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015305 int i;
15306
Chris Wilson63b66e52013-08-08 15:12:06 +020015307 if (!error)
15308 return;
15309
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015310 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020015311 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015312 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015313 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010015314 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015315 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020015316 err_printf(m, " Power: %s\n",
15317 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015318 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030015319 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015320
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015321 err_printf(m, "Plane [%d]:\n", i);
15322 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15323 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015324 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015325 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15326 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015327 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030015328 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015329 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015330 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015331 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15332 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015333 }
15334
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015335 err_printf(m, "Cursor [%d]:\n", i);
15336 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15337 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15338 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015339 }
Chris Wilson63b66e52013-08-08 15:12:06 +020015340
15341 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010015342 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020015343 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015344 err_printf(m, " Power: %s\n",
15345 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020015346 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15347 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15348 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15349 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15350 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15351 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15352 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15353 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015354}
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015355
15356void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
15357{
15358 struct intel_crtc *crtc;
15359
15360 for_each_intel_crtc(dev, crtc) {
15361 struct intel_unpin_work *work;
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015362
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015363 spin_lock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015364
15365 work = crtc->unpin_work;
15366
15367 if (work && work->event &&
15368 work->event->base.file_priv == file) {
15369 kfree(work->event);
15370 work->event = NULL;
15371 }
15372
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015373 spin_unlock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015374 }
15375}