blob: 14709547659f529b953ba83724801ca26cc5478c [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
Chandra Konduru6156a452015-04-27 13:48:39 -07002988u32 skl_plane_ctl_format(uint32_t pixel_format)
2989{
2990 u32 plane_ctl_format = 0;
2991 switch (pixel_format) {
2992 case DRM_FORMAT_RGB565:
2993 plane_ctl_format = PLANE_CTL_FORMAT_RGB_565;
2994 break;
2995 case DRM_FORMAT_XBGR8888:
2996 plane_ctl_format = PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
2997 break;
2998 case DRM_FORMAT_XRGB8888:
2999 plane_ctl_format = PLANE_CTL_FORMAT_XRGB_8888;
3000 break;
3001 /*
3002 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
3003 * to be already pre-multiplied. We need to add a knob (or a different
3004 * DRM_FORMAT) for user-space to configure that.
3005 */
3006 case DRM_FORMAT_ABGR8888:
3007 plane_ctl_format = PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
3008 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
3009 break;
3010 case DRM_FORMAT_ARGB8888:
3011 plane_ctl_format = PLANE_CTL_FORMAT_XRGB_8888 |
3012 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
3013 break;
3014 case DRM_FORMAT_XRGB2101010:
3015 plane_ctl_format = PLANE_CTL_FORMAT_XRGB_2101010;
3016 break;
3017 case DRM_FORMAT_XBGR2101010:
3018 plane_ctl_format = PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
3019 break;
3020 case DRM_FORMAT_YUYV:
3021 plane_ctl_format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
3022 break;
3023 case DRM_FORMAT_YVYU:
3024 plane_ctl_format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
3025 break;
3026 case DRM_FORMAT_UYVY:
3027 plane_ctl_format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
3028 break;
3029 case DRM_FORMAT_VYUY:
3030 plane_ctl_format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
3031 break;
3032 default:
3033 BUG();
3034 }
3035 return plane_ctl_format;
3036}
3037
3038u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
3039{
3040 u32 plane_ctl_tiling = 0;
3041 switch (fb_modifier) {
3042 case DRM_FORMAT_MOD_NONE:
3043 break;
3044 case I915_FORMAT_MOD_X_TILED:
3045 plane_ctl_tiling = PLANE_CTL_TILED_X;
3046 break;
3047 case I915_FORMAT_MOD_Y_TILED:
3048 plane_ctl_tiling = PLANE_CTL_TILED_Y;
3049 break;
3050 case I915_FORMAT_MOD_Yf_TILED:
3051 plane_ctl_tiling = PLANE_CTL_TILED_YF;
3052 break;
3053 default:
3054 MISSING_CASE(fb_modifier);
3055 }
3056 return plane_ctl_tiling;
3057}
3058
3059u32 skl_plane_ctl_rotation(unsigned int rotation)
3060{
3061 u32 plane_ctl_rotation = 0;
3062 switch (rotation) {
3063 case BIT(DRM_ROTATE_0):
3064 break;
3065 case BIT(DRM_ROTATE_90):
3066 plane_ctl_rotation = PLANE_CTL_ROTATE_90;
3067 break;
3068 case BIT(DRM_ROTATE_180):
3069 plane_ctl_rotation = PLANE_CTL_ROTATE_180;
3070 break;
3071 case BIT(DRM_ROTATE_270):
3072 plane_ctl_rotation = PLANE_CTL_ROTATE_270;
3073 break;
3074 default:
3075 MISSING_CASE(rotation);
3076 }
3077
3078 return plane_ctl_rotation;
3079}
3080
Damien Lespiau70d21f02013-07-03 21:06:04 +01003081static void skylake_update_primary_plane(struct drm_crtc *crtc,
3082 struct drm_framebuffer *fb,
3083 int x, int y)
3084{
3085 struct drm_device *dev = crtc->dev;
3086 struct drm_i915_private *dev_priv = dev->dev_private;
3087 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003088 struct drm_i915_gem_object *obj;
3089 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303090 u32 plane_ctl, stride_div, stride;
3091 u32 tile_height, plane_offset, plane_size;
3092 unsigned int rotation;
3093 int x_offset, y_offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003094 unsigned long surf_addr;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303095 struct drm_plane *plane;
Chandra Konduru6156a452015-04-27 13:48:39 -07003096 struct intel_crtc_state *crtc_state = intel_crtc->config;
3097 struct intel_plane_state *plane_state;
3098 int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
3099 int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
3100 int scaler_id = -1;
3101
3102 plane = crtc->primary;
3103 plane_state = to_intel_plane_state(plane->state);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003104
3105 if (!intel_crtc->primary_enabled) {
3106 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3107 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3108 POSTING_READ(PLANE_CTL(pipe, 0));
3109 return;
3110 }
3111
3112 plane_ctl = PLANE_CTL_ENABLE |
3113 PLANE_CTL_PIPE_GAMMA_ENABLE |
3114 PLANE_CTL_PIPE_CSC_ENABLE;
3115
Chandra Konduru6156a452015-04-27 13:48:39 -07003116 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3117 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003118 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303119
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303120 rotation = plane->state->rotation;
Chandra Konduru6156a452015-04-27 13:48:39 -07003121 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003122
Damien Lespiaub3218032015-02-27 11:15:18 +00003123 obj = intel_fb_obj(fb);
3124 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3125 fb->pixel_format);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303126 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
3127
Chandra Konduru6156a452015-04-27 13:48:39 -07003128 /*
3129 * FIXME: intel_plane_state->src, dst aren't set when transitional
3130 * update_plane helpers are called from legacy paths.
3131 * Once full atomic crtc is available, below check can be avoided.
3132 */
3133 if (drm_rect_width(&plane_state->src)) {
3134 scaler_id = plane_state->scaler_id;
3135 src_x = plane_state->src.x1 >> 16;
3136 src_y = plane_state->src.y1 >> 16;
3137 src_w = drm_rect_width(&plane_state->src) >> 16;
3138 src_h = drm_rect_height(&plane_state->src) >> 16;
3139 dst_x = plane_state->dst.x1;
3140 dst_y = plane_state->dst.y1;
3141 dst_w = drm_rect_width(&plane_state->dst);
3142 dst_h = drm_rect_height(&plane_state->dst);
3143
3144 WARN_ON(x != src_x || y != src_y);
3145 } else {
3146 src_w = intel_crtc->config->pipe_src_w;
3147 src_h = intel_crtc->config->pipe_src_h;
3148 }
3149
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303150 if (intel_rotation_90_or_270(rotation)) {
3151 /* stride = Surface height in tiles */
3152 tile_height = intel_tile_height(dev, fb->bits_per_pixel,
3153 fb->modifier[0]);
3154 stride = DIV_ROUND_UP(fb->height, tile_height);
Chandra Konduru6156a452015-04-27 13:48:39 -07003155 x_offset = stride * tile_height - y - src_h;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303156 y_offset = x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003157 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303158 } else {
3159 stride = fb->pitches[0] / stride_div;
3160 x_offset = x;
3161 y_offset = y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003162 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303163 }
3164 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003165
Damien Lespiau70d21f02013-07-03 21:06:04 +01003166 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303167 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3168 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3169 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003170
3171 if (scaler_id >= 0) {
3172 uint32_t ps_ctrl = 0;
3173
3174 WARN_ON(!dst_w || !dst_h);
3175 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3176 crtc_state->scaler_state.scalers[scaler_id].mode;
3177 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3178 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3179 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3180 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3181 I915_WRITE(PLANE_POS(pipe, 0), 0);
3182 } else {
3183 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3184 }
3185
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003186 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003187
3188 POSTING_READ(PLANE_SURF(pipe, 0));
3189}
3190
Jesse Barnes17638cd2011-06-24 12:19:23 -07003191/* Assume fb object is pinned & idle & fenced and just update base pointers */
3192static int
3193intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3194 int x, int y, enum mode_set_atomic state)
3195{
3196 struct drm_device *dev = crtc->dev;
3197 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003198
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01003199 if (dev_priv->display.disable_fbc)
3200 dev_priv->display.disable_fbc(dev);
Jesse Barnes81255562010-08-02 12:07:50 -07003201
Daniel Vetter29b9bde2014-04-24 23:55:01 +02003202 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3203
3204 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07003205}
3206
Ville Syrjälä75147472014-11-24 18:28:11 +02003207static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003208{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003209 struct drm_crtc *crtc;
3210
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003211 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003212 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3213 enum plane plane = intel_crtc->plane;
3214
3215 intel_prepare_page_flip(dev, plane);
3216 intel_finish_page_flip_plane(dev, plane);
3217 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003218}
3219
3220static void intel_update_primary_planes(struct drm_device *dev)
3221{
3222 struct drm_i915_private *dev_priv = dev->dev_private;
3223 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003224
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003225 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003226 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3227
Rob Clark51fd3712013-11-19 12:10:12 -05003228 drm_modeset_lock(&crtc->mutex, NULL);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003229 /*
3230 * FIXME: Once we have proper support for primary planes (and
3231 * disabling them without disabling the entire crtc) allow again
Dave Airlie66e514c2014-04-03 07:51:54 +10003232 * a NULL crtc->primary->fb.
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003233 */
Matt Roperf4510a22014-04-01 15:22:40 -07003234 if (intel_crtc->active && crtc->primary->fb)
Matt Roper262ca2b2014-03-18 17:22:55 -07003235 dev_priv->display.update_primary_plane(crtc,
Dave Airlie66e514c2014-04-03 07:51:54 +10003236 crtc->primary->fb,
Matt Roper262ca2b2014-03-18 17:22:55 -07003237 crtc->x,
3238 crtc->y);
Rob Clark51fd3712013-11-19 12:10:12 -05003239 drm_modeset_unlock(&crtc->mutex);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003240 }
3241}
3242
Ville Syrjälä75147472014-11-24 18:28:11 +02003243void intel_prepare_reset(struct drm_device *dev)
3244{
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003245 struct drm_i915_private *dev_priv = to_i915(dev);
3246 struct intel_crtc *crtc;
3247
Ville Syrjälä75147472014-11-24 18:28:11 +02003248 /* no reset support for gen2 */
3249 if (IS_GEN2(dev))
3250 return;
3251
3252 /* reset doesn't touch the display */
3253 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3254 return;
3255
3256 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003257
3258 /*
3259 * Disabling the crtcs gracefully seems nicer. Also the
3260 * g33 docs say we should at least disable all the planes.
3261 */
3262 for_each_intel_crtc(dev, crtc) {
3263 if (crtc->active)
3264 dev_priv->display.crtc_disable(&crtc->base);
3265 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003266}
3267
3268void intel_finish_reset(struct drm_device *dev)
3269{
3270 struct drm_i915_private *dev_priv = to_i915(dev);
3271
3272 /*
3273 * Flips in the rings will be nuked by the reset,
3274 * so complete all pending flips so that user space
3275 * will get its events and not get stuck.
3276 */
3277 intel_complete_page_flips(dev);
3278
3279 /* no reset support for gen2 */
3280 if (IS_GEN2(dev))
3281 return;
3282
3283 /* reset doesn't touch the display */
3284 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3285 /*
3286 * Flips in the rings have been nuked by the reset,
3287 * so update the base address of all primary
3288 * planes to the the last fb to make sure we're
3289 * showing the correct fb after a reset.
3290 */
3291 intel_update_primary_planes(dev);
3292 return;
3293 }
3294
3295 /*
3296 * The display has been reset as well,
3297 * so need a full re-initialization.
3298 */
3299 intel_runtime_pm_disable_interrupts(dev_priv);
3300 intel_runtime_pm_enable_interrupts(dev_priv);
3301
3302 intel_modeset_init_hw(dev);
3303
3304 spin_lock_irq(&dev_priv->irq_lock);
3305 if (dev_priv->display.hpd_irq_setup)
3306 dev_priv->display.hpd_irq_setup(dev);
3307 spin_unlock_irq(&dev_priv->irq_lock);
3308
3309 intel_modeset_setup_hw_state(dev, true);
3310
3311 intel_hpd_init(dev_priv);
3312
3313 drm_modeset_unlock_all(dev);
3314}
3315
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003316static int
Chris Wilson14667a42012-04-03 17:58:35 +01003317intel_finish_fb(struct drm_framebuffer *old_fb)
3318{
Matt Roper2ff8fde2014-07-08 07:50:07 -07003319 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
Chris Wilson14667a42012-04-03 17:58:35 +01003320 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
3321 bool was_interruptible = dev_priv->mm.interruptible;
3322 int ret;
3323
Chris Wilson14667a42012-04-03 17:58:35 +01003324 /* Big Hammer, we also need to ensure that any pending
3325 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3326 * current scanout is retired before unpinning the old
3327 * framebuffer.
3328 *
3329 * This should only fail upon a hung GPU, in which case we
3330 * can safely continue.
3331 */
3332 dev_priv->mm.interruptible = false;
3333 ret = i915_gem_object_finish_gpu(obj);
3334 dev_priv->mm.interruptible = was_interruptible;
3335
3336 return ret;
3337}
3338
Chris Wilson7d5e3792014-03-04 13:15:08 +00003339static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3340{
3341 struct drm_device *dev = crtc->dev;
3342 struct drm_i915_private *dev_priv = dev->dev_private;
3343 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003344 bool pending;
3345
3346 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3347 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3348 return false;
3349
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003350 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003351 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003352 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003353
3354 return pending;
3355}
3356
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003357static void intel_update_pipe_size(struct intel_crtc *crtc)
3358{
3359 struct drm_device *dev = crtc->base.dev;
3360 struct drm_i915_private *dev_priv = dev->dev_private;
3361 const struct drm_display_mode *adjusted_mode;
3362
3363 if (!i915.fastboot)
3364 return;
3365
3366 /*
3367 * Update pipe size and adjust fitter if needed: the reason for this is
3368 * that in compute_mode_changes we check the native mode (not the pfit
3369 * mode) to see if we can flip rather than do a full mode set. In the
3370 * fastboot case, we'll flip, but if we don't update the pipesrc and
3371 * pfit state, we'll end up with a big fb scanned out into the wrong
3372 * sized surface.
3373 *
3374 * To fix this properly, we need to hoist the checks up into
3375 * compute_mode_changes (or above), check the actual pfit state and
3376 * whether the platform allows pfit disable with pipe active, and only
3377 * then update the pipesrc and pfit state, even on the flip path.
3378 */
3379
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003380 adjusted_mode = &crtc->config->base.adjusted_mode;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003381
3382 I915_WRITE(PIPESRC(crtc->pipe),
3383 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3384 (adjusted_mode->crtc_vdisplay - 1));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003385 if (!crtc->config->pch_pfit.enabled &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03003386 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3387 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003388 I915_WRITE(PF_CTL(crtc->pipe), 0);
3389 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3390 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3391 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003392 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3393 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003394}
3395
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003396static void intel_fdi_normal_train(struct drm_crtc *crtc)
3397{
3398 struct drm_device *dev = crtc->dev;
3399 struct drm_i915_private *dev_priv = dev->dev_private;
3400 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3401 int pipe = intel_crtc->pipe;
3402 u32 reg, temp;
3403
3404 /* enable normal train */
3405 reg = FDI_TX_CTL(pipe);
3406 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003407 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003408 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3409 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003410 } else {
3411 temp &= ~FDI_LINK_TRAIN_NONE;
3412 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003413 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003414 I915_WRITE(reg, temp);
3415
3416 reg = FDI_RX_CTL(pipe);
3417 temp = I915_READ(reg);
3418 if (HAS_PCH_CPT(dev)) {
3419 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3420 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3421 } else {
3422 temp &= ~FDI_LINK_TRAIN_NONE;
3423 temp |= FDI_LINK_TRAIN_NONE;
3424 }
3425 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3426
3427 /* wait one idle pattern time */
3428 POSTING_READ(reg);
3429 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003430
3431 /* IVB wants error correction enabled */
3432 if (IS_IVYBRIDGE(dev))
3433 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3434 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003435}
3436
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003437/* The FDI link training functions for ILK/Ibexpeak. */
3438static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3439{
3440 struct drm_device *dev = crtc->dev;
3441 struct drm_i915_private *dev_priv = dev->dev_private;
3442 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3443 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003444 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003445
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003446 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003447 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003448
Adam Jacksone1a44742010-06-25 15:32:14 -04003449 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3450 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003451 reg = FDI_RX_IMR(pipe);
3452 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003453 temp &= ~FDI_RX_SYMBOL_LOCK;
3454 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003455 I915_WRITE(reg, temp);
3456 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003457 udelay(150);
3458
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003459 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003460 reg = FDI_TX_CTL(pipe);
3461 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003462 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003463 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003464 temp &= ~FDI_LINK_TRAIN_NONE;
3465 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003466 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003467
Chris Wilson5eddb702010-09-11 13:48:45 +01003468 reg = FDI_RX_CTL(pipe);
3469 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003470 temp &= ~FDI_LINK_TRAIN_NONE;
3471 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003472 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3473
3474 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003475 udelay(150);
3476
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003477 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003478 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3479 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3480 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003481
Chris Wilson5eddb702010-09-11 13:48:45 +01003482 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003483 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003484 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003485 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3486
3487 if ((temp & FDI_RX_BIT_LOCK)) {
3488 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003489 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003490 break;
3491 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003492 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003493 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003494 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003495
3496 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003497 reg = FDI_TX_CTL(pipe);
3498 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003499 temp &= ~FDI_LINK_TRAIN_NONE;
3500 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003501 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003502
Chris Wilson5eddb702010-09-11 13:48:45 +01003503 reg = FDI_RX_CTL(pipe);
3504 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003505 temp &= ~FDI_LINK_TRAIN_NONE;
3506 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003507 I915_WRITE(reg, temp);
3508
3509 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003510 udelay(150);
3511
Chris Wilson5eddb702010-09-11 13:48:45 +01003512 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003513 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003514 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003515 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3516
3517 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003518 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003519 DRM_DEBUG_KMS("FDI train 2 done.\n");
3520 break;
3521 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003522 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003523 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003524 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003525
3526 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003527
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003528}
3529
Akshay Joshi0206e352011-08-16 15:34:10 -04003530static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003531 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3532 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3533 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3534 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3535};
3536
3537/* The FDI link training functions for SNB/Cougarpoint. */
3538static void gen6_fdi_link_train(struct drm_crtc *crtc)
3539{
3540 struct drm_device *dev = crtc->dev;
3541 struct drm_i915_private *dev_priv = dev->dev_private;
3542 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3543 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05003544 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003545
Adam Jacksone1a44742010-06-25 15:32:14 -04003546 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3547 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003548 reg = FDI_RX_IMR(pipe);
3549 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003550 temp &= ~FDI_RX_SYMBOL_LOCK;
3551 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003552 I915_WRITE(reg, temp);
3553
3554 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003555 udelay(150);
3556
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003557 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003558 reg = FDI_TX_CTL(pipe);
3559 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003560 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003561 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003562 temp &= ~FDI_LINK_TRAIN_NONE;
3563 temp |= FDI_LINK_TRAIN_PATTERN_1;
3564 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3565 /* SNB-B */
3566 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003567 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003568
Daniel Vetterd74cf322012-10-26 10:58:13 +02003569 I915_WRITE(FDI_RX_MISC(pipe),
3570 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3571
Chris Wilson5eddb702010-09-11 13:48:45 +01003572 reg = FDI_RX_CTL(pipe);
3573 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003574 if (HAS_PCH_CPT(dev)) {
3575 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3576 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3577 } else {
3578 temp &= ~FDI_LINK_TRAIN_NONE;
3579 temp |= FDI_LINK_TRAIN_PATTERN_1;
3580 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003581 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3582
3583 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003584 udelay(150);
3585
Akshay Joshi0206e352011-08-16 15:34:10 -04003586 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003587 reg = FDI_TX_CTL(pipe);
3588 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003589 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3590 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003591 I915_WRITE(reg, temp);
3592
3593 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003594 udelay(500);
3595
Sean Paulfa37d392012-03-02 12:53:39 -05003596 for (retry = 0; retry < 5; retry++) {
3597 reg = FDI_RX_IIR(pipe);
3598 temp = I915_READ(reg);
3599 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3600 if (temp & FDI_RX_BIT_LOCK) {
3601 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3602 DRM_DEBUG_KMS("FDI train 1 done.\n");
3603 break;
3604 }
3605 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003606 }
Sean Paulfa37d392012-03-02 12:53:39 -05003607 if (retry < 5)
3608 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003609 }
3610 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003611 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003612
3613 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003614 reg = FDI_TX_CTL(pipe);
3615 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003616 temp &= ~FDI_LINK_TRAIN_NONE;
3617 temp |= FDI_LINK_TRAIN_PATTERN_2;
3618 if (IS_GEN6(dev)) {
3619 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3620 /* SNB-B */
3621 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3622 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003623 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003624
Chris Wilson5eddb702010-09-11 13:48:45 +01003625 reg = FDI_RX_CTL(pipe);
3626 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003627 if (HAS_PCH_CPT(dev)) {
3628 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3629 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3630 } else {
3631 temp &= ~FDI_LINK_TRAIN_NONE;
3632 temp |= FDI_LINK_TRAIN_PATTERN_2;
3633 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003634 I915_WRITE(reg, temp);
3635
3636 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003637 udelay(150);
3638
Akshay Joshi0206e352011-08-16 15:34:10 -04003639 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003640 reg = FDI_TX_CTL(pipe);
3641 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003642 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3643 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003644 I915_WRITE(reg, temp);
3645
3646 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003647 udelay(500);
3648
Sean Paulfa37d392012-03-02 12:53:39 -05003649 for (retry = 0; retry < 5; retry++) {
3650 reg = FDI_RX_IIR(pipe);
3651 temp = I915_READ(reg);
3652 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3653 if (temp & FDI_RX_SYMBOL_LOCK) {
3654 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3655 DRM_DEBUG_KMS("FDI train 2 done.\n");
3656 break;
3657 }
3658 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003659 }
Sean Paulfa37d392012-03-02 12:53:39 -05003660 if (retry < 5)
3661 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003662 }
3663 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003664 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003665
3666 DRM_DEBUG_KMS("FDI train done.\n");
3667}
3668
Jesse Barnes357555c2011-04-28 15:09:55 -07003669/* Manual link training for Ivy Bridge A0 parts */
3670static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3671{
3672 struct drm_device *dev = crtc->dev;
3673 struct drm_i915_private *dev_priv = dev->dev_private;
3674 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3675 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003676 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003677
3678 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3679 for train result */
3680 reg = FDI_RX_IMR(pipe);
3681 temp = I915_READ(reg);
3682 temp &= ~FDI_RX_SYMBOL_LOCK;
3683 temp &= ~FDI_RX_BIT_LOCK;
3684 I915_WRITE(reg, temp);
3685
3686 POSTING_READ(reg);
3687 udelay(150);
3688
Daniel Vetter01a415f2012-10-27 15:58:40 +02003689 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3690 I915_READ(FDI_RX_IIR(pipe)));
3691
Jesse Barnes139ccd32013-08-19 11:04:55 -07003692 /* Try each vswing and preemphasis setting twice before moving on */
3693 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3694 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003695 reg = FDI_TX_CTL(pipe);
3696 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003697 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3698 temp &= ~FDI_TX_ENABLE;
3699 I915_WRITE(reg, temp);
3700
3701 reg = FDI_RX_CTL(pipe);
3702 temp = I915_READ(reg);
3703 temp &= ~FDI_LINK_TRAIN_AUTO;
3704 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3705 temp &= ~FDI_RX_ENABLE;
3706 I915_WRITE(reg, temp);
3707
3708 /* enable CPU FDI TX and PCH FDI RX */
3709 reg = FDI_TX_CTL(pipe);
3710 temp = I915_READ(reg);
3711 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003712 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003713 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003714 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003715 temp |= snb_b_fdi_train_param[j/2];
3716 temp |= FDI_COMPOSITE_SYNC;
3717 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3718
3719 I915_WRITE(FDI_RX_MISC(pipe),
3720 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3721
3722 reg = FDI_RX_CTL(pipe);
3723 temp = I915_READ(reg);
3724 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3725 temp |= FDI_COMPOSITE_SYNC;
3726 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3727
3728 POSTING_READ(reg);
3729 udelay(1); /* should be 0.5us */
3730
3731 for (i = 0; i < 4; i++) {
3732 reg = FDI_RX_IIR(pipe);
3733 temp = I915_READ(reg);
3734 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3735
3736 if (temp & FDI_RX_BIT_LOCK ||
3737 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3738 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3739 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3740 i);
3741 break;
3742 }
3743 udelay(1); /* should be 0.5us */
3744 }
3745 if (i == 4) {
3746 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3747 continue;
3748 }
3749
3750 /* Train 2 */
3751 reg = FDI_TX_CTL(pipe);
3752 temp = I915_READ(reg);
3753 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3754 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3755 I915_WRITE(reg, temp);
3756
3757 reg = FDI_RX_CTL(pipe);
3758 temp = I915_READ(reg);
3759 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3760 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003761 I915_WRITE(reg, temp);
3762
3763 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003764 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003765
Jesse Barnes139ccd32013-08-19 11:04:55 -07003766 for (i = 0; i < 4; i++) {
3767 reg = FDI_RX_IIR(pipe);
3768 temp = I915_READ(reg);
3769 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003770
Jesse Barnes139ccd32013-08-19 11:04:55 -07003771 if (temp & FDI_RX_SYMBOL_LOCK ||
3772 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3773 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3774 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3775 i);
3776 goto train_done;
3777 }
3778 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003779 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003780 if (i == 4)
3781 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003782 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003783
Jesse Barnes139ccd32013-08-19 11:04:55 -07003784train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003785 DRM_DEBUG_KMS("FDI train done.\n");
3786}
3787
Daniel Vetter88cefb62012-08-12 19:27:14 +02003788static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003789{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003790 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003791 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003792 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003793 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003794
Jesse Barnesc64e3112010-09-10 11:27:03 -07003795
Jesse Barnes0e23b992010-09-10 11:10:00 -07003796 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003797 reg = FDI_RX_CTL(pipe);
3798 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003799 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003800 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003801 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003802 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3803
3804 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003805 udelay(200);
3806
3807 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003808 temp = I915_READ(reg);
3809 I915_WRITE(reg, temp | FDI_PCDCLK);
3810
3811 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003812 udelay(200);
3813
Paulo Zanoni20749732012-11-23 15:30:38 -02003814 /* Enable CPU FDI TX PLL, always on for Ironlake */
3815 reg = FDI_TX_CTL(pipe);
3816 temp = I915_READ(reg);
3817 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3818 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003819
Paulo Zanoni20749732012-11-23 15:30:38 -02003820 POSTING_READ(reg);
3821 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003822 }
3823}
3824
Daniel Vetter88cefb62012-08-12 19:27:14 +02003825static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3826{
3827 struct drm_device *dev = intel_crtc->base.dev;
3828 struct drm_i915_private *dev_priv = dev->dev_private;
3829 int pipe = intel_crtc->pipe;
3830 u32 reg, temp;
3831
3832 /* Switch from PCDclk to Rawclk */
3833 reg = FDI_RX_CTL(pipe);
3834 temp = I915_READ(reg);
3835 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3836
3837 /* Disable CPU FDI TX PLL */
3838 reg = FDI_TX_CTL(pipe);
3839 temp = I915_READ(reg);
3840 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3841
3842 POSTING_READ(reg);
3843 udelay(100);
3844
3845 reg = FDI_RX_CTL(pipe);
3846 temp = I915_READ(reg);
3847 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3848
3849 /* Wait for the clocks to turn off. */
3850 POSTING_READ(reg);
3851 udelay(100);
3852}
3853
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003854static void ironlake_fdi_disable(struct drm_crtc *crtc)
3855{
3856 struct drm_device *dev = crtc->dev;
3857 struct drm_i915_private *dev_priv = dev->dev_private;
3858 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3859 int pipe = intel_crtc->pipe;
3860 u32 reg, temp;
3861
3862 /* disable CPU FDI tx and PCH FDI rx */
3863 reg = FDI_TX_CTL(pipe);
3864 temp = I915_READ(reg);
3865 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3866 POSTING_READ(reg);
3867
3868 reg = FDI_RX_CTL(pipe);
3869 temp = I915_READ(reg);
3870 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003871 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003872 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3873
3874 POSTING_READ(reg);
3875 udelay(100);
3876
3877 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003878 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003879 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003880
3881 /* still set train pattern 1 */
3882 reg = FDI_TX_CTL(pipe);
3883 temp = I915_READ(reg);
3884 temp &= ~FDI_LINK_TRAIN_NONE;
3885 temp |= FDI_LINK_TRAIN_PATTERN_1;
3886 I915_WRITE(reg, temp);
3887
3888 reg = FDI_RX_CTL(pipe);
3889 temp = I915_READ(reg);
3890 if (HAS_PCH_CPT(dev)) {
3891 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3892 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3893 } else {
3894 temp &= ~FDI_LINK_TRAIN_NONE;
3895 temp |= FDI_LINK_TRAIN_PATTERN_1;
3896 }
3897 /* BPC in FDI rx is consistent with that in PIPECONF */
3898 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003899 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003900 I915_WRITE(reg, temp);
3901
3902 POSTING_READ(reg);
3903 udelay(100);
3904}
3905
Chris Wilson5dce5b932014-01-20 10:17:36 +00003906bool intel_has_pending_fb_unpin(struct drm_device *dev)
3907{
3908 struct intel_crtc *crtc;
3909
3910 /* Note that we don't need to be called with mode_config.lock here
3911 * as our list of CRTC objects is static for the lifetime of the
3912 * device and so cannot disappear as we iterate. Similarly, we can
3913 * happily treat the predicates as racy, atomic checks as userspace
3914 * cannot claim and pin a new fb without at least acquring the
3915 * struct_mutex and so serialising with us.
3916 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003917 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003918 if (atomic_read(&crtc->unpin_work_count) == 0)
3919 continue;
3920
3921 if (crtc->unpin_work)
3922 intel_wait_for_vblank(dev, crtc->pipe);
3923
3924 return true;
3925 }
3926
3927 return false;
3928}
3929
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003930static void page_flip_completed(struct intel_crtc *intel_crtc)
3931{
3932 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3933 struct intel_unpin_work *work = intel_crtc->unpin_work;
3934
3935 /* ensure that the unpin work is consistent wrt ->pending. */
3936 smp_rmb();
3937 intel_crtc->unpin_work = NULL;
3938
3939 if (work->event)
3940 drm_send_vblank_event(intel_crtc->base.dev,
3941 intel_crtc->pipe,
3942 work->event);
3943
3944 drm_crtc_vblank_put(&intel_crtc->base);
3945
3946 wake_up_all(&dev_priv->pending_flip_queue);
3947 queue_work(dev_priv->wq, &work->work);
3948
3949 trace_i915_flip_complete(intel_crtc->plane,
3950 work->pending_flip_obj);
3951}
3952
Ville Syrjälä46a55d32014-05-21 14:04:46 +03003953void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003954{
Chris Wilson0f911282012-04-17 10:05:38 +01003955 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003956 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003957
Daniel Vetter2c10d572012-12-20 21:24:07 +01003958 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Chris Wilson9c787942014-09-05 07:13:25 +01003959 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3960 !intel_crtc_has_pending_flip(crtc),
3961 60*HZ) == 0)) {
3962 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003963
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003964 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003965 if (intel_crtc->unpin_work) {
3966 WARN_ONCE(1, "Removing stuck page flip\n");
3967 page_flip_completed(intel_crtc);
3968 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003969 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003970 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003971
Chris Wilson975d5682014-08-20 13:13:34 +01003972 if (crtc->primary->fb) {
3973 mutex_lock(&dev->struct_mutex);
3974 intel_finish_fb(crtc->primary->fb);
3975 mutex_unlock(&dev->struct_mutex);
3976 }
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003977}
3978
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003979/* Program iCLKIP clock to the desired frequency */
3980static void lpt_program_iclkip(struct drm_crtc *crtc)
3981{
3982 struct drm_device *dev = crtc->dev;
3983 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003984 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003985 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3986 u32 temp;
3987
Daniel Vetter09153002012-12-12 14:06:44 +01003988 mutex_lock(&dev_priv->dpio_lock);
3989
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003990 /* It is necessary to ungate the pixclk gate prior to programming
3991 * the divisors, and gate it back when it is done.
3992 */
3993 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3994
3995 /* Disable SSCCTL */
3996 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003997 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3998 SBI_SSCCTL_DISABLE,
3999 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004000
4001 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03004002 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004003 auxdiv = 1;
4004 divsel = 0x41;
4005 phaseinc = 0x20;
4006 } else {
4007 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01004008 * but the adjusted_mode->crtc_clock in in KHz. To get the
4009 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004010 * convert the virtual clock precision to KHz here for higher
4011 * precision.
4012 */
4013 u32 iclk_virtual_root_freq = 172800 * 1000;
4014 u32 iclk_pi_range = 64;
4015 u32 desired_divisor, msb_divisor_value, pi_value;
4016
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03004017 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004018 msb_divisor_value = desired_divisor / iclk_pi_range;
4019 pi_value = desired_divisor % iclk_pi_range;
4020
4021 auxdiv = 0;
4022 divsel = msb_divisor_value - 2;
4023 phaseinc = pi_value;
4024 }
4025
4026 /* This should not happen with any sane values */
4027 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
4028 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
4029 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
4030 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
4031
4032 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03004033 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004034 auxdiv,
4035 divsel,
4036 phasedir,
4037 phaseinc);
4038
4039 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004040 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004041 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4042 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4043 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4044 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4045 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4046 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004047 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004048
4049 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004050 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004051 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4052 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004053 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004054
4055 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004056 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004057 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004058 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004059
4060 /* Wait for initialization time */
4061 udelay(24);
4062
4063 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01004064
4065 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004066}
4067
Daniel Vetter275f01b22013-05-03 11:49:47 +02004068static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4069 enum pipe pch_transcoder)
4070{
4071 struct drm_device *dev = crtc->base.dev;
4072 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004073 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02004074
4075 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4076 I915_READ(HTOTAL(cpu_transcoder)));
4077 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4078 I915_READ(HBLANK(cpu_transcoder)));
4079 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4080 I915_READ(HSYNC(cpu_transcoder)));
4081
4082 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4083 I915_READ(VTOTAL(cpu_transcoder)));
4084 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4085 I915_READ(VBLANK(cpu_transcoder)));
4086 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4087 I915_READ(VSYNC(cpu_transcoder)));
4088 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4089 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4090}
4091
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004092static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004093{
4094 struct drm_i915_private *dev_priv = dev->dev_private;
4095 uint32_t temp;
4096
4097 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004098 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004099 return;
4100
4101 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4102 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4103
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004104 temp &= ~FDI_BC_BIFURCATION_SELECT;
4105 if (enable)
4106 temp |= FDI_BC_BIFURCATION_SELECT;
4107
4108 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004109 I915_WRITE(SOUTH_CHICKEN1, temp);
4110 POSTING_READ(SOUTH_CHICKEN1);
4111}
4112
4113static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4114{
4115 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004116
4117 switch (intel_crtc->pipe) {
4118 case PIPE_A:
4119 break;
4120 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004121 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004122 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004123 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004124 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004125
4126 break;
4127 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004128 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004129
4130 break;
4131 default:
4132 BUG();
4133 }
4134}
4135
Jesse Barnesf67a5592011-01-05 10:31:48 -08004136/*
4137 * Enable PCH resources required for PCH ports:
4138 * - PCH PLLs
4139 * - FDI training & RX/TX
4140 * - update transcoder timings
4141 * - DP transcoding bits
4142 * - transcoder
4143 */
4144static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004145{
4146 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004147 struct drm_i915_private *dev_priv = dev->dev_private;
4148 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4149 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004150 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004151
Daniel Vetterab9412b2013-05-03 11:49:46 +02004152 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004153
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004154 if (IS_IVYBRIDGE(dev))
4155 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4156
Daniel Vettercd986ab2012-10-26 10:58:12 +02004157 /* Write the TU size bits before fdi link training, so that error
4158 * detection works. */
4159 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4160 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4161
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004162 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004163 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004164
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004165 /* We need to program the right clock selection before writing the pixel
4166 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004167 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004168 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004169
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004170 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004171 temp |= TRANS_DPLL_ENABLE(pipe);
4172 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004173 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004174 temp |= sel;
4175 else
4176 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004177 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004178 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004179
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004180 /* XXX: pch pll's can be enabled any time before we enable the PCH
4181 * transcoder, and we actually should do this to not upset any PCH
4182 * transcoder that already use the clock when we share it.
4183 *
4184 * Note that enable_shared_dpll tries to do the right thing, but
4185 * get_shared_dpll unconditionally resets the pll - we need that to have
4186 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004187 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004188
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004189 /* set transcoder timing, panel must allow it */
4190 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004191 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004192
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004193 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004194
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004195 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004196 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004197 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01004198 reg = TRANS_DP_CTL(pipe);
4199 temp = I915_READ(reg);
4200 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004201 TRANS_DP_SYNC_MASK |
4202 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01004203 temp |= (TRANS_DP_OUTPUT_ENABLE |
4204 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004205 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004206
4207 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004208 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004209 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004210 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004211
4212 switch (intel_trans_dp_port_sel(crtc)) {
4213 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004214 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004215 break;
4216 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004217 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004218 break;
4219 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004220 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004221 break;
4222 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004223 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004224 }
4225
Chris Wilson5eddb702010-09-11 13:48:45 +01004226 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004227 }
4228
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004229 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004230}
4231
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004232static void lpt_pch_enable(struct drm_crtc *crtc)
4233{
4234 struct drm_device *dev = crtc->dev;
4235 struct drm_i915_private *dev_priv = dev->dev_private;
4236 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004237 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004238
Daniel Vetterab9412b2013-05-03 11:49:46 +02004239 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004240
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004241 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004242
Paulo Zanoni0540e482012-10-31 18:12:40 -02004243 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004244 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004245
Paulo Zanoni937bb612012-10-31 18:12:47 -02004246 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004247}
4248
Daniel Vetter716c2e52014-06-25 22:02:02 +03004249void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004250{
Daniel Vettere2b78262013-06-07 23:10:03 +02004251 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004252
4253 if (pll == NULL)
4254 return;
4255
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02004256 if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004257 WARN(1, "bad %s crtc mask\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004258 return;
4259 }
4260
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02004261 pll->config.crtc_mask &= ~(1 << crtc->pipe);
4262 if (pll->config.crtc_mask == 0) {
Daniel Vetterf4a091c2013-06-10 17:28:22 +02004263 WARN_ON(pll->on);
4264 WARN_ON(pll->active);
4265 }
4266
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004267 crtc->config->shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004268}
4269
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004270struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4271 struct intel_crtc_state *crtc_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004272{
Daniel Vettere2b78262013-06-07 23:10:03 +02004273 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004274 struct intel_shared_dpll *pll;
Daniel Vettere2b78262013-06-07 23:10:03 +02004275 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004276
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004277 if (HAS_PCH_IBX(dev_priv->dev)) {
4278 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02004279 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004280 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004281
Daniel Vetter46edb022013-06-05 13:34:12 +02004282 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4283 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004284
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004285 WARN_ON(pll->new_config->crtc_mask);
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004286
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004287 goto found;
4288 }
4289
Satheeshakrishna Mbcddf612014-08-22 09:49:10 +05304290 if (IS_BROXTON(dev_priv->dev)) {
4291 /* PLL is attached to port in bxt */
4292 struct intel_encoder *encoder;
4293 struct intel_digital_port *intel_dig_port;
4294
4295 encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
4296 if (WARN_ON(!encoder))
4297 return NULL;
4298
4299 intel_dig_port = enc_to_dig_port(&encoder->base);
4300 /* 1:1 mapping between ports and PLLs */
4301 i = (enum intel_dpll_id)intel_dig_port->port;
4302 pll = &dev_priv->shared_dplls[i];
4303 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4304 crtc->base.base.id, pll->name);
4305 WARN_ON(pll->new_config->crtc_mask);
4306
4307 goto found;
4308 }
4309
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004310 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4311 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004312
4313 /* Only want to check enabled timings first */
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004314 if (pll->new_config->crtc_mask == 0)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004315 continue;
4316
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004317 if (memcmp(&crtc_state->dpll_hw_state,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004318 &pll->new_config->hw_state,
4319 sizeof(pll->new_config->hw_state)) == 0) {
4320 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004321 crtc->base.base.id, pll->name,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004322 pll->new_config->crtc_mask,
4323 pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004324 goto found;
4325 }
4326 }
4327
4328 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004329 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4330 pll = &dev_priv->shared_dplls[i];
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004331 if (pll->new_config->crtc_mask == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02004332 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4333 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004334 goto found;
4335 }
4336 }
4337
4338 return NULL;
4339
4340found:
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004341 if (pll->new_config->crtc_mask == 0)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004342 pll->new_config->hw_state = crtc_state->dpll_hw_state;
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004343
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004344 crtc_state->shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02004345 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4346 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02004347
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004348 pll->new_config->crtc_mask |= 1 << crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004349
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004350 return pll;
4351}
4352
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004353/**
4354 * intel_shared_dpll_start_config - start a new PLL staged config
4355 * @dev_priv: DRM device
4356 * @clear_pipes: mask of pipes that will have their PLLs freed
4357 *
4358 * Starts a new PLL staged config, copying the current config but
4359 * releasing the references of pipes specified in clear_pipes.
4360 */
4361static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
4362 unsigned clear_pipes)
4363{
4364 struct intel_shared_dpll *pll;
4365 enum intel_dpll_id i;
4366
4367 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4368 pll = &dev_priv->shared_dplls[i];
4369
4370 pll->new_config = kmemdup(&pll->config, sizeof pll->config,
4371 GFP_KERNEL);
4372 if (!pll->new_config)
4373 goto cleanup;
4374
4375 pll->new_config->crtc_mask &= ~clear_pipes;
4376 }
4377
4378 return 0;
4379
4380cleanup:
4381 while (--i >= 0) {
4382 pll = &dev_priv->shared_dplls[i];
Ander Conselvan de Oliveiraf354d732014-11-07 14:07:41 +02004383 kfree(pll->new_config);
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004384 pll->new_config = NULL;
4385 }
4386
4387 return -ENOMEM;
4388}
4389
4390static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
4391{
4392 struct intel_shared_dpll *pll;
4393 enum intel_dpll_id i;
4394
4395 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4396 pll = &dev_priv->shared_dplls[i];
4397
4398 WARN_ON(pll->new_config == &pll->config);
4399
4400 pll->config = *pll->new_config;
4401 kfree(pll->new_config);
4402 pll->new_config = NULL;
4403 }
4404}
4405
4406static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
4407{
4408 struct intel_shared_dpll *pll;
4409 enum intel_dpll_id i;
4410
4411 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4412 pll = &dev_priv->shared_dplls[i];
4413
4414 WARN_ON(pll->new_config == &pll->config);
4415
4416 kfree(pll->new_config);
4417 pll->new_config = NULL;
4418 }
4419}
4420
Daniel Vettera1520312013-05-03 11:49:50 +02004421static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004422{
4423 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01004424 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004425 u32 temp;
4426
4427 temp = I915_READ(dslreg);
4428 udelay(500);
4429 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004430 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004431 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004432 }
4433}
4434
Chandra Kondurua1b22782015-04-07 15:28:45 -07004435/**
4436 * skl_update_scaler_users - Stages update to crtc's scaler state
4437 * @intel_crtc: crtc
4438 * @crtc_state: crtc_state
4439 * @plane: plane (NULL indicates crtc is requesting update)
4440 * @plane_state: plane's state
4441 * @force_detach: request unconditional detachment of scaler
4442 *
4443 * This function updates scaler state for requested plane or crtc.
4444 * To request scaler usage update for a plane, caller shall pass plane pointer.
4445 * To request scaler usage update for crtc, caller shall pass plane pointer
4446 * as NULL.
4447 *
4448 * Return
4449 * 0 - scaler_usage updated successfully
4450 * error - requested scaling cannot be supported or other error condition
4451 */
4452int
4453skl_update_scaler_users(
4454 struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state,
4455 struct intel_plane *intel_plane, struct intel_plane_state *plane_state,
4456 int force_detach)
4457{
4458 int need_scaling;
4459 int idx;
4460 int src_w, src_h, dst_w, dst_h;
4461 int *scaler_id;
4462 struct drm_framebuffer *fb;
4463 struct intel_crtc_scaler_state *scaler_state;
Chandra Konduru6156a452015-04-27 13:48:39 -07004464 unsigned int rotation;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004465
4466 if (!intel_crtc || !crtc_state)
4467 return 0;
4468
4469 scaler_state = &crtc_state->scaler_state;
4470
4471 idx = intel_plane ? drm_plane_index(&intel_plane->base) : SKL_CRTC_INDEX;
4472 fb = intel_plane ? plane_state->base.fb : NULL;
4473
4474 if (intel_plane) {
4475 src_w = drm_rect_width(&plane_state->src) >> 16;
4476 src_h = drm_rect_height(&plane_state->src) >> 16;
4477 dst_w = drm_rect_width(&plane_state->dst);
4478 dst_h = drm_rect_height(&plane_state->dst);
4479 scaler_id = &plane_state->scaler_id;
Chandra Konduru6156a452015-04-27 13:48:39 -07004480 rotation = plane_state->base.rotation;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004481 } else {
4482 struct drm_display_mode *adjusted_mode =
4483 &crtc_state->base.adjusted_mode;
4484 src_w = crtc_state->pipe_src_w;
4485 src_h = crtc_state->pipe_src_h;
4486 dst_w = adjusted_mode->hdisplay;
4487 dst_h = adjusted_mode->vdisplay;
4488 scaler_id = &scaler_state->scaler_id;
Chandra Konduru6156a452015-04-27 13:48:39 -07004489 rotation = DRM_ROTATE_0;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004490 }
Chandra Konduru6156a452015-04-27 13:48:39 -07004491
4492 need_scaling = intel_rotation_90_or_270(rotation) ?
4493 (src_h != dst_w || src_w != dst_h):
4494 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004495
4496 /*
4497 * if plane is being disabled or scaler is no more required or force detach
4498 * - free scaler binded to this plane/crtc
4499 * - in order to do this, update crtc->scaler_usage
4500 *
4501 * Here scaler state in crtc_state is set free so that
4502 * scaler can be assigned to other user. Actual register
4503 * update to free the scaler is done in plane/panel-fit programming.
4504 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4505 */
4506 if (force_detach || !need_scaling || (intel_plane &&
4507 (!fb || !plane_state->visible))) {
4508 if (*scaler_id >= 0) {
4509 scaler_state->scaler_users &= ~(1 << idx);
4510 scaler_state->scalers[*scaler_id].in_use = 0;
4511
4512 DRM_DEBUG_KMS("Staged freeing scaler id %d.%d from %s:%d "
4513 "crtc_state = %p scaler_users = 0x%x\n",
4514 intel_crtc->pipe, *scaler_id, intel_plane ? "PLANE" : "CRTC",
4515 intel_plane ? intel_plane->base.base.id :
4516 intel_crtc->base.base.id, crtc_state,
4517 scaler_state->scaler_users);
4518 *scaler_id = -1;
4519 }
4520 return 0;
4521 }
4522
4523 /* range checks */
4524 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4525 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4526
4527 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4528 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
4529 DRM_DEBUG_KMS("%s:%d scaler_user index %u.%u: src %ux%u dst %ux%u "
4530 "size is out of scaler range\n",
4531 intel_plane ? "PLANE" : "CRTC",
4532 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4533 intel_crtc->pipe, idx, src_w, src_h, dst_w, dst_h);
4534 return -EINVAL;
4535 }
4536
4537 /* check colorkey */
4538 if (intel_plane && intel_plane->ckey.flags != I915_SET_COLORKEY_NONE) {
4539 DRM_DEBUG_KMS("PLANE:%d scaling with color key not allowed",
4540 intel_plane->base.base.id);
4541 return -EINVAL;
4542 }
4543
4544 /* Check src format */
4545 if (intel_plane) {
4546 switch (fb->pixel_format) {
4547 case DRM_FORMAT_RGB565:
4548 case DRM_FORMAT_XBGR8888:
4549 case DRM_FORMAT_XRGB8888:
4550 case DRM_FORMAT_ABGR8888:
4551 case DRM_FORMAT_ARGB8888:
4552 case DRM_FORMAT_XRGB2101010:
4553 case DRM_FORMAT_ARGB2101010:
4554 case DRM_FORMAT_XBGR2101010:
4555 case DRM_FORMAT_ABGR2101010:
4556 case DRM_FORMAT_YUYV:
4557 case DRM_FORMAT_YVYU:
4558 case DRM_FORMAT_UYVY:
4559 case DRM_FORMAT_VYUY:
4560 break;
4561 default:
4562 DRM_DEBUG_KMS("PLANE:%d FB:%d unsupported scaling format 0x%x\n",
4563 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4564 return -EINVAL;
4565 }
4566 }
4567
4568 /* mark this plane as a scaler user in crtc_state */
4569 scaler_state->scaler_users |= (1 << idx);
4570 DRM_DEBUG_KMS("%s:%d staged scaling request for %ux%u->%ux%u "
4571 "crtc_state = %p scaler_users = 0x%x\n",
4572 intel_plane ? "PLANE" : "CRTC",
4573 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4574 src_w, src_h, dst_w, dst_h, crtc_state, scaler_state->scaler_users);
4575 return 0;
4576}
4577
4578static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004579{
4580 struct drm_device *dev = crtc->base.dev;
4581 struct drm_i915_private *dev_priv = dev->dev_private;
4582 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004583 struct intel_crtc_scaler_state *scaler_state =
4584 &crtc->config->scaler_state;
4585
4586 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4587
4588 /* To update pfit, first update scaler state */
4589 skl_update_scaler_users(crtc, crtc->config, NULL, NULL, !enable);
4590 intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
4591 skl_detach_scalers(crtc);
4592 if (!enable)
4593 return;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004594
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004595 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004596 int id;
4597
4598 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4599 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4600 return;
4601 }
4602
4603 id = scaler_state->scaler_id;
4604 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4605 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4606 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4607 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4608
4609 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004610 }
4611}
4612
Jesse Barnesb074cec2013-04-25 12:55:02 -07004613static void ironlake_pfit_enable(struct intel_crtc *crtc)
4614{
4615 struct drm_device *dev = crtc->base.dev;
4616 struct drm_i915_private *dev_priv = dev->dev_private;
4617 int pipe = crtc->pipe;
4618
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004619 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004620 /* Force use of hard-coded filter coefficients
4621 * as some pre-programmed values are broken,
4622 * e.g. x201.
4623 */
4624 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4625 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4626 PF_PIPE_SEL_IVB(pipe));
4627 else
4628 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004629 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4630 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004631 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004632}
4633
Matt Roper4a3b8762014-12-23 10:41:51 -08004634static void intel_enable_sprite_planes(struct drm_crtc *crtc)
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004635{
4636 struct drm_device *dev = crtc->dev;
4637 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07004638 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004639 struct intel_plane *intel_plane;
4640
Matt Roperaf2b6532014-04-01 15:22:32 -07004641 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4642 intel_plane = to_intel_plane(plane);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004643 if (intel_plane->pipe == pipe)
4644 intel_plane_restore(&intel_plane->base);
Matt Roperaf2b6532014-04-01 15:22:32 -07004645 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004646}
4647
Matt Roper0d703d42015-03-04 10:49:04 -08004648/*
4649 * Disable a plane internally without actually modifying the plane's state.
4650 * This will allow us to easily restore the plane later by just reprogramming
4651 * its state.
4652 */
4653static void disable_plane_internal(struct drm_plane *plane)
4654{
4655 struct intel_plane *intel_plane = to_intel_plane(plane);
4656 struct drm_plane_state *state =
4657 plane->funcs->atomic_duplicate_state(plane);
4658 struct intel_plane_state *intel_state = to_intel_plane_state(state);
4659
4660 intel_state->visible = false;
4661 intel_plane->commit_plane(plane, intel_state);
4662
4663 intel_plane_destroy_state(plane, state);
4664}
4665
Matt Roper4a3b8762014-12-23 10:41:51 -08004666static void intel_disable_sprite_planes(struct drm_crtc *crtc)
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004667{
4668 struct drm_device *dev = crtc->dev;
4669 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07004670 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004671 struct intel_plane *intel_plane;
4672
Matt Roperaf2b6532014-04-01 15:22:32 -07004673 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4674 intel_plane = to_intel_plane(plane);
Matt Roper0d703d42015-03-04 10:49:04 -08004675 if (plane->fb && intel_plane->pipe == pipe)
4676 disable_plane_internal(plane);
Matt Roperaf2b6532014-04-01 15:22:32 -07004677 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004678}
4679
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004680void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004681{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004682 struct drm_device *dev = crtc->base.dev;
4683 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004684
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004685 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004686 return;
4687
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004688 /* We can only enable IPS after we enable a plane and wait for a vblank */
4689 intel_wait_for_vblank(dev, crtc->pipe);
4690
Paulo Zanonid77e4532013-09-24 13:52:55 -03004691 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004692 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004693 mutex_lock(&dev_priv->rps.hw_lock);
4694 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4695 mutex_unlock(&dev_priv->rps.hw_lock);
4696 /* Quoting Art Runyan: "its not safe to expect any particular
4697 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004698 * mailbox." Moreover, the mailbox may return a bogus state,
4699 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004700 */
4701 } else {
4702 I915_WRITE(IPS_CTL, IPS_ENABLE);
4703 /* The bit only becomes 1 in the next vblank, so this wait here
4704 * is essentially intel_wait_for_vblank. If we don't have this
4705 * and don't wait for vblanks until the end of crtc_enable, then
4706 * the HW state readout code will complain that the expected
4707 * IPS_CTL value is not the one we read. */
4708 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4709 DRM_ERROR("Timed out waiting for IPS enable\n");
4710 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004711}
4712
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004713void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004714{
4715 struct drm_device *dev = crtc->base.dev;
4716 struct drm_i915_private *dev_priv = dev->dev_private;
4717
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004718 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004719 return;
4720
4721 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004722 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004723 mutex_lock(&dev_priv->rps.hw_lock);
4724 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4725 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004726 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4727 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4728 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004729 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004730 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004731 POSTING_READ(IPS_CTL);
4732 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004733
4734 /* We need to wait for a vblank before we can disable the plane. */
4735 intel_wait_for_vblank(dev, crtc->pipe);
4736}
4737
4738/** Loads the palette/gamma unit for the CRTC with the prepared values */
4739static void intel_crtc_load_lut(struct drm_crtc *crtc)
4740{
4741 struct drm_device *dev = crtc->dev;
4742 struct drm_i915_private *dev_priv = dev->dev_private;
4743 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4744 enum pipe pipe = intel_crtc->pipe;
4745 int palreg = PALETTE(pipe);
4746 int i;
4747 bool reenable_ips = false;
4748
4749 /* The clocks have to be on to load the palette. */
Matt Roper83d65732015-02-25 13:12:16 -08004750 if (!crtc->state->enable || !intel_crtc->active)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004751 return;
4752
Imre Deak50360402015-01-16 00:55:16 -08004753 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03004754 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004755 assert_dsi_pll_enabled(dev_priv);
4756 else
4757 assert_pll_enabled(dev_priv, pipe);
4758 }
4759
4760 /* use legacy palette for Ironlake */
Sonika Jindal7a1db492014-07-22 11:18:27 +05304761 if (!HAS_GMCH_DISPLAY(dev))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004762 palreg = LGC_PALETTE(pipe);
4763
4764 /* Workaround : Do not read or write the pipe palette/gamma data while
4765 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4766 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004767 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03004768 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4769 GAMMA_MODE_MODE_SPLIT)) {
4770 hsw_disable_ips(intel_crtc);
4771 reenable_ips = true;
4772 }
4773
4774 for (i = 0; i < 256; i++) {
4775 I915_WRITE(palreg + 4 * i,
4776 (intel_crtc->lut_r[i] << 16) |
4777 (intel_crtc->lut_g[i] << 8) |
4778 intel_crtc->lut_b[i]);
4779 }
4780
4781 if (reenable_ips)
4782 hsw_enable_ips(intel_crtc);
4783}
4784
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004785static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
4786{
4787 if (!enable && intel_crtc->overlay) {
4788 struct drm_device *dev = intel_crtc->base.dev;
4789 struct drm_i915_private *dev_priv = dev->dev_private;
4790
4791 mutex_lock(&dev->struct_mutex);
4792 dev_priv->mm.interruptible = false;
4793 (void) intel_overlay_switch_off(intel_crtc->overlay);
4794 dev_priv->mm.interruptible = true;
4795 mutex_unlock(&dev->struct_mutex);
4796 }
4797
4798 /* Let userspace switch the overlay on again. In most cases userspace
4799 * has to recompute where to put it anyway.
4800 */
4801}
4802
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004803static void intel_crtc_enable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004804{
4805 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004806 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4807 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004808
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03004809 intel_enable_primary_hw_plane(crtc->primary, crtc);
Matt Roper4a3b8762014-12-23 10:41:51 -08004810 intel_enable_sprite_planes(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004811 intel_crtc_update_cursor(crtc, true);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004812 intel_crtc_dpms_overlay(intel_crtc, true);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004813
4814 hsw_enable_ips(intel_crtc);
4815
4816 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004817 intel_fbc_update(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004818 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf99d7062014-06-19 16:01:59 +02004819
4820 /*
4821 * FIXME: Once we grow proper nuclear flip support out of this we need
4822 * to compute the mask of flip planes precisely. For the time being
4823 * consider this a flip from a NULL plane.
4824 */
4825 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004826}
4827
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004828static void intel_crtc_disable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004829{
4830 struct drm_device *dev = crtc->dev;
4831 struct drm_i915_private *dev_priv = dev->dev_private;
4832 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4833 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004834
4835 intel_crtc_wait_for_pending_flips(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004836
Paulo Zanonie35fef22015-02-09 14:46:29 -02004837 if (dev_priv->fbc.crtc == intel_crtc)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004838 intel_fbc_disable(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004839
4840 hsw_disable_ips(intel_crtc);
4841
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004842 intel_crtc_dpms_overlay(intel_crtc, false);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004843 intel_crtc_update_cursor(crtc, false);
Matt Roper4a3b8762014-12-23 10:41:51 -08004844 intel_disable_sprite_planes(crtc);
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03004845 intel_disable_primary_hw_plane(crtc->primary, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004846
Daniel Vetterf99d7062014-06-19 16:01:59 +02004847 /*
4848 * FIXME: Once we grow proper nuclear flip support out of this we need
4849 * to compute the mask of flip planes precisely. For the time being
4850 * consider this a flip to a NULL plane.
4851 */
4852 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004853}
4854
Jesse Barnesf67a5592011-01-05 10:31:48 -08004855static void ironlake_crtc_enable(struct drm_crtc *crtc)
4856{
4857 struct drm_device *dev = crtc->dev;
4858 struct drm_i915_private *dev_priv = dev->dev_private;
4859 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004860 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004861 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004862
Matt Roper83d65732015-02-25 13:12:16 -08004863 WARN_ON(!crtc->state->enable);
Daniel Vetter08a48462012-07-02 11:43:47 +02004864
Jesse Barnesf67a5592011-01-05 10:31:48 -08004865 if (intel_crtc->active)
4866 return;
4867
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004868 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004869 intel_prepare_shared_dpll(intel_crtc);
4870
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004871 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304872 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004873
4874 intel_set_pipe_timings(intel_crtc);
4875
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004876 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004877 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004878 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004879 }
4880
4881 ironlake_set_pipeconf(crtc);
4882
Jesse Barnesf67a5592011-01-05 10:31:48 -08004883 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004884
Daniel Vettera72e4c92014-09-30 10:56:47 +02004885 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4886 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004887
Daniel Vetterf6736a12013-06-05 13:34:30 +02004888 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004889 if (encoder->pre_enable)
4890 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004891
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004892 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004893 /* Note: FDI PLL enabling _must_ be done before we enable the
4894 * cpu pipes, hence this is separate from all the other fdi/pch
4895 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004896 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004897 } else {
4898 assert_fdi_tx_disabled(dev_priv, pipe);
4899 assert_fdi_rx_disabled(dev_priv, pipe);
4900 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004901
Jesse Barnesb074cec2013-04-25 12:55:02 -07004902 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004903
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004904 /*
4905 * On ILK+ LUT must be loaded before the pipe is running but with
4906 * clocks enabled
4907 */
4908 intel_crtc_load_lut(crtc);
4909
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004910 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004911 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004912
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004913 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004914 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004915
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004916 assert_vblank_disabled(crtc);
4917 drm_crtc_vblank_on(crtc);
4918
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004919 for_each_encoder_on_crtc(dev, crtc, encoder)
4920 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004921
4922 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004923 cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02004924
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004925 intel_crtc_enable_planes(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004926}
4927
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004928/* IPS only exists on ULT machines and is tied to pipe A. */
4929static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4930{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004931 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004932}
4933
Paulo Zanonie4916942013-09-20 16:21:19 -03004934/*
4935 * This implements the workaround described in the "notes" section of the mode
4936 * set sequence documentation. When going from no pipes or single pipe to
4937 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4938 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4939 */
4940static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4941{
4942 struct drm_device *dev = crtc->base.dev;
4943 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4944
4945 /* We want to get the other_active_crtc only if there's only 1 other
4946 * active crtc. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004947 for_each_intel_crtc(dev, crtc_it) {
Paulo Zanonie4916942013-09-20 16:21:19 -03004948 if (!crtc_it->active || crtc_it == crtc)
4949 continue;
4950
4951 if (other_active_crtc)
4952 return;
4953
4954 other_active_crtc = crtc_it;
4955 }
4956 if (!other_active_crtc)
4957 return;
4958
4959 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4960 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4961}
4962
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004963static void haswell_crtc_enable(struct drm_crtc *crtc)
4964{
4965 struct drm_device *dev = crtc->dev;
4966 struct drm_i915_private *dev_priv = dev->dev_private;
4967 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4968 struct intel_encoder *encoder;
4969 int pipe = intel_crtc->pipe;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004970
Matt Roper83d65732015-02-25 13:12:16 -08004971 WARN_ON(!crtc->state->enable);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004972
4973 if (intel_crtc->active)
4974 return;
4975
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004976 if (intel_crtc_to_shared_dpll(intel_crtc))
4977 intel_enable_shared_dpll(intel_crtc);
4978
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004979 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304980 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004981
4982 intel_set_pipe_timings(intel_crtc);
4983
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004984 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
4985 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
4986 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004987 }
4988
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004989 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004990 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004991 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004992 }
4993
4994 haswell_set_pipeconf(crtc);
4995
4996 intel_set_pipe_csc(crtc);
4997
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004998 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004999
Daniel Vettera72e4c92014-09-30 10:56:47 +02005000 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005001 for_each_encoder_on_crtc(dev, crtc, encoder)
5002 if (encoder->pre_enable)
5003 encoder->pre_enable(encoder);
5004
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005005 if (intel_crtc->config->has_pch_encoder) {
Daniel Vettera72e4c92014-09-30 10:56:47 +02005006 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5007 true);
Imre Deak4fe94672014-06-25 22:01:49 +03005008 dev_priv->display.fdi_link_train(crtc);
5009 }
5010
Paulo Zanoni1f544382012-10-24 11:32:00 -02005011 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005012
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005013 if (INTEL_INFO(dev)->gen == 9)
Chandra Kondurua1b22782015-04-07 15:28:45 -07005014 skylake_pfit_update(intel_crtc, 1);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005015 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00005016 ironlake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005017 else
5018 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005019
5020 /*
5021 * On ILK+ LUT must be loaded before the pipe is running but with
5022 * clocks enabled
5023 */
5024 intel_crtc_load_lut(crtc);
5025
Paulo Zanoni1f544382012-10-24 11:32:00 -02005026 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00005027 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005028
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005029 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005030 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005031
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005032 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02005033 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005034
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005035 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10005036 intel_ddi_set_vc_payload_alloc(crtc, true);
5037
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005038 assert_vblank_disabled(crtc);
5039 drm_crtc_vblank_on(crtc);
5040
Jani Nikula8807e552013-08-30 19:40:32 +03005041 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005042 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005043 intel_opregion_notify_encoder(encoder, true);
5044 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005045
Paulo Zanonie4916942013-09-20 16:21:19 -03005046 /* If we change the relative order between pipe/planes enabling, we need
5047 * to change the workaround. */
5048 haswell_mode_set_planes_workaround(intel_crtc);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03005049 intel_crtc_enable_planes(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005050}
5051
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005052static void ironlake_pfit_disable(struct intel_crtc *crtc)
5053{
5054 struct drm_device *dev = crtc->base.dev;
5055 struct drm_i915_private *dev_priv = dev->dev_private;
5056 int pipe = crtc->pipe;
5057
5058 /* To avoid upsetting the power well on haswell only disable the pfit if
5059 * it's in use. The hw state code will make sure we get this right. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005060 if (crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005061 I915_WRITE(PF_CTL(pipe), 0);
5062 I915_WRITE(PF_WIN_POS(pipe), 0);
5063 I915_WRITE(PF_WIN_SZ(pipe), 0);
5064 }
5065}
5066
Jesse Barnes6be4a602010-09-10 10:26:01 -07005067static void ironlake_crtc_disable(struct drm_crtc *crtc)
5068{
5069 struct drm_device *dev = crtc->dev;
5070 struct drm_i915_private *dev_priv = dev->dev_private;
5071 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005072 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005073 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01005074 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005075
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005076 if (!intel_crtc->active)
5077 return;
5078
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03005079 intel_crtc_disable_planes(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005080
Daniel Vetterea9d7582012-07-10 10:42:52 +02005081 for_each_encoder_on_crtc(dev, crtc, encoder)
5082 encoder->disable(encoder);
5083
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005084 drm_crtc_vblank_off(crtc);
5085 assert_vblank_disabled(crtc);
5086
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005087 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005088 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Daniel Vetterd925c592013-06-05 13:34:04 +02005089
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005090 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005091
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005092 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005093
Daniel Vetterbf49ec82012-09-06 22:15:40 +02005094 for_each_encoder_on_crtc(dev, crtc, encoder)
5095 if (encoder->post_disable)
5096 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005097
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005098 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005099 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005100
Daniel Vetterd925c592013-06-05 13:34:04 +02005101 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005102
Daniel Vetterd925c592013-06-05 13:34:04 +02005103 if (HAS_PCH_CPT(dev)) {
5104 /* disable TRANS_DP_CTL */
5105 reg = TRANS_DP_CTL(pipe);
5106 temp = I915_READ(reg);
5107 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5108 TRANS_DP_PORT_SEL_MASK);
5109 temp |= TRANS_DP_PORT_SEL_NONE;
5110 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005111
Daniel Vetterd925c592013-06-05 13:34:04 +02005112 /* disable DPLL_SEL */
5113 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005114 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005115 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005116 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005117
5118 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005119 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02005120
5121 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005122 }
5123
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005124 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005125 intel_update_watermarks(crtc);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01005126
5127 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005128 intel_fbc_update(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01005129 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005130}
5131
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005132static void haswell_crtc_disable(struct drm_crtc *crtc)
5133{
5134 struct drm_device *dev = crtc->dev;
5135 struct drm_i915_private *dev_priv = dev->dev_private;
5136 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5137 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005138 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005139
5140 if (!intel_crtc->active)
5141 return;
5142
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03005143 intel_crtc_disable_planes(crtc);
Ville Syrjälädda9a662013-09-19 17:00:37 -03005144
Jani Nikula8807e552013-08-30 19:40:32 +03005145 for_each_encoder_on_crtc(dev, crtc, encoder) {
5146 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005147 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005148 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005149
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005150 drm_crtc_vblank_off(crtc);
5151 assert_vblank_disabled(crtc);
5152
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005153 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005154 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5155 false);
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005156 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005157
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005158 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005159 intel_ddi_set_vc_payload_alloc(crtc, false);
5160
Paulo Zanoniad80a812012-10-24 16:06:19 -02005161 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005162
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005163 if (INTEL_INFO(dev)->gen == 9)
Chandra Kondurua1b22782015-04-07 15:28:45 -07005164 skylake_pfit_update(intel_crtc, 0);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005165 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00005166 ironlake_pfit_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005167 else
5168 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005169
Paulo Zanoni1f544382012-10-24 11:32:00 -02005170 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005171
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005172 if (intel_crtc->config->has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02005173 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02005174 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02005175 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005176
Imre Deak97b040a2014-06-25 22:01:50 +03005177 for_each_encoder_on_crtc(dev, crtc, encoder)
5178 if (encoder->post_disable)
5179 encoder->post_disable(encoder);
5180
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005181 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005182 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005183
5184 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005185 intel_fbc_update(dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005186 mutex_unlock(&dev->struct_mutex);
Daniel Vetterdf8ad702014-06-25 22:02:03 +03005187
5188 if (intel_crtc_to_shared_dpll(intel_crtc))
5189 intel_disable_shared_dpll(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005190}
5191
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005192static void ironlake_crtc_off(struct drm_crtc *crtc)
5193{
5194 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005195 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005196}
5197
Paulo Zanoni6441ab52012-10-05 12:05:58 -03005198
Jesse Barnes2dd24552013-04-25 12:55:01 -07005199static void i9xx_pfit_enable(struct intel_crtc *crtc)
5200{
5201 struct drm_device *dev = crtc->base.dev;
5202 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005203 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005204
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005205 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005206 return;
5207
Daniel Vetterc0b03412013-05-28 12:05:54 +02005208 /*
5209 * The panel fitter should only be adjusted whilst the pipe is disabled,
5210 * according to register description and PRM.
5211 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005212 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5213 assert_pipe_disabled(dev_priv, crtc->pipe);
5214
Jesse Barnesb074cec2013-04-25 12:55:02 -07005215 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5216 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005217
5218 /* Border color in case we don't scale up to the full screen. Black by
5219 * default, change to something else for debugging. */
5220 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005221}
5222
Dave Airlied05410f2014-06-05 13:22:59 +10005223static enum intel_display_power_domain port_to_power_domain(enum port port)
5224{
5225 switch (port) {
5226 case PORT_A:
5227 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
5228 case PORT_B:
5229 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
5230 case PORT_C:
5231 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
5232 case PORT_D:
5233 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
5234 default:
5235 WARN_ON_ONCE(1);
5236 return POWER_DOMAIN_PORT_OTHER;
5237 }
5238}
5239
Imre Deak77d22dc2014-03-05 16:20:52 +02005240#define for_each_power_domain(domain, mask) \
5241 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
5242 if ((1 << (domain)) & (mask))
5243
Imre Deak319be8a2014-03-04 19:22:57 +02005244enum intel_display_power_domain
5245intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005246{
Imre Deak319be8a2014-03-04 19:22:57 +02005247 struct drm_device *dev = intel_encoder->base.dev;
5248 struct intel_digital_port *intel_dig_port;
5249
5250 switch (intel_encoder->type) {
5251 case INTEL_OUTPUT_UNKNOWN:
5252 /* Only DDI platforms should ever use this output type */
5253 WARN_ON_ONCE(!HAS_DDI(dev));
5254 case INTEL_OUTPUT_DISPLAYPORT:
5255 case INTEL_OUTPUT_HDMI:
5256 case INTEL_OUTPUT_EDP:
5257 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005258 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005259 case INTEL_OUTPUT_DP_MST:
5260 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5261 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005262 case INTEL_OUTPUT_ANALOG:
5263 return POWER_DOMAIN_PORT_CRT;
5264 case INTEL_OUTPUT_DSI:
5265 return POWER_DOMAIN_PORT_DSI;
5266 default:
5267 return POWER_DOMAIN_PORT_OTHER;
5268 }
5269}
5270
5271static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
5272{
5273 struct drm_device *dev = crtc->dev;
5274 struct intel_encoder *intel_encoder;
5275 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5276 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005277 unsigned long mask;
5278 enum transcoder transcoder;
5279
5280 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
5281
5282 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5283 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005284 if (intel_crtc->config->pch_pfit.enabled ||
5285 intel_crtc->config->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005286 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5287
Imre Deak319be8a2014-03-04 19:22:57 +02005288 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5289 mask |= BIT(intel_display_port_power_domain(intel_encoder));
5290
Imre Deak77d22dc2014-03-05 16:20:52 +02005291 return mask;
5292}
5293
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005294static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
Imre Deak77d22dc2014-03-05 16:20:52 +02005295{
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005296 struct drm_device *dev = state->dev;
Imre Deak77d22dc2014-03-05 16:20:52 +02005297 struct drm_i915_private *dev_priv = dev->dev_private;
5298 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
5299 struct intel_crtc *crtc;
5300
5301 /*
5302 * First get all needed power domains, then put all unneeded, to avoid
5303 * any unnecessary toggling of the power wells.
5304 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005305 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005306 enum intel_display_power_domain domain;
5307
Matt Roper83d65732015-02-25 13:12:16 -08005308 if (!crtc->base.state->enable)
Imre Deak77d22dc2014-03-05 16:20:52 +02005309 continue;
5310
Imre Deak319be8a2014-03-04 19:22:57 +02005311 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
Imre Deak77d22dc2014-03-05 16:20:52 +02005312
5313 for_each_power_domain(domain, pipe_domains[crtc->pipe])
5314 intel_display_power_get(dev_priv, domain);
5315 }
5316
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005317 if (dev_priv->display.modeset_global_resources)
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005318 dev_priv->display.modeset_global_resources(state);
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005319
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005320 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005321 enum intel_display_power_domain domain;
5322
5323 for_each_power_domain(domain, crtc->enabled_power_domains)
5324 intel_display_power_put(dev_priv, domain);
5325
5326 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
5327 }
5328
5329 intel_display_set_init_power(dev_priv, false);
5330}
5331
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305332void broxton_set_cdclk(struct drm_device *dev, int frequency)
5333{
5334 struct drm_i915_private *dev_priv = dev->dev_private;
5335 uint32_t divider;
5336 uint32_t ratio;
5337 uint32_t current_freq;
5338 int ret;
5339
5340 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5341 switch (frequency) {
5342 case 144000:
5343 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5344 ratio = BXT_DE_PLL_RATIO(60);
5345 break;
5346 case 288000:
5347 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5348 ratio = BXT_DE_PLL_RATIO(60);
5349 break;
5350 case 384000:
5351 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5352 ratio = BXT_DE_PLL_RATIO(60);
5353 break;
5354 case 576000:
5355 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5356 ratio = BXT_DE_PLL_RATIO(60);
5357 break;
5358 case 624000:
5359 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5360 ratio = BXT_DE_PLL_RATIO(65);
5361 break;
5362 case 19200:
5363 /*
5364 * Bypass frequency with DE PLL disabled. Init ratio, divider
5365 * to suppress GCC warning.
5366 */
5367 ratio = 0;
5368 divider = 0;
5369 break;
5370 default:
5371 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5372
5373 return;
5374 }
5375
5376 mutex_lock(&dev_priv->rps.hw_lock);
5377 /* Inform power controller of upcoming frequency change */
5378 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5379 0x80000000);
5380 mutex_unlock(&dev_priv->rps.hw_lock);
5381
5382 if (ret) {
5383 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5384 ret, frequency);
5385 return;
5386 }
5387
5388 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5389 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5390 current_freq = current_freq * 500 + 1000;
5391
5392 /*
5393 * DE PLL has to be disabled when
5394 * - setting to 19.2MHz (bypass, PLL isn't used)
5395 * - before setting to 624MHz (PLL needs toggling)
5396 * - before setting to any frequency from 624MHz (PLL needs toggling)
5397 */
5398 if (frequency == 19200 || frequency == 624000 ||
5399 current_freq == 624000) {
5400 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5401 /* Timeout 200us */
5402 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5403 1))
5404 DRM_ERROR("timout waiting for DE PLL unlock\n");
5405 }
5406
5407 if (frequency != 19200) {
5408 uint32_t val;
5409
5410 val = I915_READ(BXT_DE_PLL_CTL);
5411 val &= ~BXT_DE_PLL_RATIO_MASK;
5412 val |= ratio;
5413 I915_WRITE(BXT_DE_PLL_CTL, val);
5414
5415 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5416 /* Timeout 200us */
5417 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5418 DRM_ERROR("timeout waiting for DE PLL lock\n");
5419
5420 val = I915_READ(CDCLK_CTL);
5421 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5422 val |= divider;
5423 /*
5424 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5425 * enable otherwise.
5426 */
5427 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5428 if (frequency >= 500000)
5429 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5430
5431 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5432 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5433 val |= (frequency - 1000) / 500;
5434 I915_WRITE(CDCLK_CTL, val);
5435 }
5436
5437 mutex_lock(&dev_priv->rps.hw_lock);
5438 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5439 DIV_ROUND_UP(frequency, 25000));
5440 mutex_unlock(&dev_priv->rps.hw_lock);
5441
5442 if (ret) {
5443 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5444 ret, frequency);
5445 return;
5446 }
5447
5448 dev_priv->cdclk_freq = frequency;
5449}
5450
5451void broxton_init_cdclk(struct drm_device *dev)
5452{
5453 struct drm_i915_private *dev_priv = dev->dev_private;
5454 uint32_t val;
5455
5456 /*
5457 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5458 * or else the reset will hang because there is no PCH to respond.
5459 * Move the handshake programming to initialization sequence.
5460 * Previously was left up to BIOS.
5461 */
5462 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5463 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5464 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5465
5466 /* Enable PG1 for cdclk */
5467 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5468
5469 /* check if cd clock is enabled */
5470 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5471 DRM_DEBUG_KMS("Display already initialized\n");
5472 return;
5473 }
5474
5475 /*
5476 * FIXME:
5477 * - The initial CDCLK needs to be read from VBT.
5478 * Need to make this change after VBT has changes for BXT.
5479 * - check if setting the max (or any) cdclk freq is really necessary
5480 * here, it belongs to modeset time
5481 */
5482 broxton_set_cdclk(dev, 624000);
5483
5484 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5485 udelay(10);
5486
5487 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5488 DRM_ERROR("DBuf power enable timeout!\n");
5489}
5490
5491void broxton_uninit_cdclk(struct drm_device *dev)
5492{
5493 struct drm_i915_private *dev_priv = dev->dev_private;
5494
5495 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5496 udelay(10);
5497
5498 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5499 DRM_ERROR("DBuf power disable timeout!\n");
5500
5501 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5502 broxton_set_cdclk(dev, 19200);
5503
5504 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5505}
5506
Ville Syrjälädfcab172014-06-13 13:37:47 +03005507/* returns HPLL frequency in kHz */
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005508static int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005509{
Jesse Barnes586f49d2013-11-04 16:06:59 -08005510 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08005511
Jesse Barnes586f49d2013-11-04 16:06:59 -08005512 /* Obtain SKU information */
5513 mutex_lock(&dev_priv->dpio_lock);
5514 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
5515 CCK_FUSE_HPLL_FREQ_MASK;
5516 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005517
Ville Syrjälädfcab172014-06-13 13:37:47 +03005518 return vco_freq[hpll_freq] * 1000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005519}
5520
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005521static void vlv_update_cdclk(struct drm_device *dev)
5522{
5523 struct drm_i915_private *dev_priv = dev->dev_private;
5524
Vandana Kannan164dfd22014-11-24 13:37:41 +05305525 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
Ville Syrjälä43dc52c2014-10-07 17:41:20 +03005526 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
Vandana Kannan164dfd22014-11-24 13:37:41 +05305527 dev_priv->cdclk_freq);
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005528
5529 /*
5530 * Program the gmbus_freq based on the cdclk frequency.
5531 * BSpec erroneously claims we should aim for 4MHz, but
5532 * in fact 1MHz is the correct frequency.
5533 */
Vandana Kannan164dfd22014-11-24 13:37:41 +05305534 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005535}
5536
Jesse Barnes30a970c2013-11-04 13:48:12 -08005537/* Adjust CDclk dividers to allow high res or save power if possible */
5538static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5539{
5540 struct drm_i915_private *dev_priv = dev->dev_private;
5541 u32 val, cmd;
5542
Vandana Kannan164dfd22014-11-24 13:37:41 +05305543 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5544 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005545
Ville Syrjälädfcab172014-06-13 13:37:47 +03005546 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005547 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005548 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005549 cmd = 1;
5550 else
5551 cmd = 0;
5552
5553 mutex_lock(&dev_priv->rps.hw_lock);
5554 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5555 val &= ~DSPFREQGUAR_MASK;
5556 val |= (cmd << DSPFREQGUAR_SHIFT);
5557 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5558 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5559 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5560 50)) {
5561 DRM_ERROR("timed out waiting for CDclk change\n");
5562 }
5563 mutex_unlock(&dev_priv->rps.hw_lock);
5564
Ville Syrjälädfcab172014-06-13 13:37:47 +03005565 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005566 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005567
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005568 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005569
5570 mutex_lock(&dev_priv->dpio_lock);
5571 /* adjust cdclk divider */
5572 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjälä9cf33db2014-06-13 13:37:48 +03005573 val &= ~DISPLAY_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005574 val |= divider;
5575 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005576
5577 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5578 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5579 50))
5580 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005581 mutex_unlock(&dev_priv->dpio_lock);
5582 }
5583
5584 mutex_lock(&dev_priv->dpio_lock);
5585 /* adjust self-refresh exit latency value */
5586 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5587 val &= ~0x7f;
5588
5589 /*
5590 * For high bandwidth configs, we set a higher latency in the bunit
5591 * so that the core display fetch happens in time to avoid underruns.
5592 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005593 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005594 val |= 4500 / 250; /* 4.5 usec */
5595 else
5596 val |= 3000 / 250; /* 3.0 usec */
5597 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
5598 mutex_unlock(&dev_priv->dpio_lock);
5599
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005600 vlv_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005601}
5602
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005603static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5604{
5605 struct drm_i915_private *dev_priv = dev->dev_private;
5606 u32 val, cmd;
5607
Vandana Kannan164dfd22014-11-24 13:37:41 +05305608 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5609 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005610
5611 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005612 case 333333:
5613 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005614 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005615 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005616 break;
5617 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005618 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005619 return;
5620 }
5621
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005622 /*
5623 * Specs are full of misinformation, but testing on actual
5624 * hardware has shown that we just need to write the desired
5625 * CCK divider into the Punit register.
5626 */
5627 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5628
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005629 mutex_lock(&dev_priv->rps.hw_lock);
5630 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5631 val &= ~DSPFREQGUAR_MASK_CHV;
5632 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5633 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5634 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5635 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5636 50)) {
5637 DRM_ERROR("timed out waiting for CDclk change\n");
5638 }
5639 mutex_unlock(&dev_priv->rps.hw_lock);
5640
5641 vlv_update_cdclk(dev);
5642}
5643
Jesse Barnes30a970c2013-11-04 13:48:12 -08005644static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5645 int max_pixclk)
5646{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005647 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005648 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005649
Jesse Barnes30a970c2013-11-04 13:48:12 -08005650 /*
5651 * Really only a few cases to deal with, as only 4 CDclks are supported:
5652 * 200MHz
5653 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005654 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005655 * 400MHz (VLV only)
5656 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5657 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005658 *
5659 * We seem to get an unstable or solid color picture at 200MHz.
5660 * Not sure what's wrong. For now use 200MHz only when all pipes
5661 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005662 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005663 if (!IS_CHERRYVIEW(dev_priv) &&
5664 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005665 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005666 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005667 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005668 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005669 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005670 else
5671 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005672}
5673
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305674static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5675 int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005676{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305677 /*
5678 * FIXME:
5679 * - remove the guardband, it's not needed on BXT
5680 * - set 19.2MHz bypass frequency if there are no active pipes
5681 */
5682 if (max_pixclk > 576000*9/10)
5683 return 624000;
5684 else if (max_pixclk > 384000*9/10)
5685 return 576000;
5686 else if (max_pixclk > 288000*9/10)
5687 return 384000;
5688 else if (max_pixclk > 144000*9/10)
5689 return 288000;
5690 else
5691 return 144000;
5692}
5693
Jesse Barnes30a970c2013-11-04 13:48:12 -08005694/* compute the max pixel clock for new configuration */
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005695static int intel_mode_max_pixclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005696{
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005697 struct drm_device *dev = state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005698 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005699 struct intel_crtc_state *crtc_state;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005700 int max_pixclk = 0;
5701
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005702 for_each_intel_crtc(dev, intel_crtc) {
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005703 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
5704 if (IS_ERR(crtc_state))
5705 return PTR_ERR(crtc_state);
5706
5707 if (!crtc_state->base.enable)
5708 continue;
5709
5710 max_pixclk = max(max_pixclk,
5711 crtc_state->base.adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005712 }
5713
5714 return max_pixclk;
5715}
5716
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005717static int valleyview_modeset_global_pipes(struct drm_atomic_state *state,
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005718 unsigned *prepare_pipes)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005719{
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005720 struct drm_i915_private *dev_priv = to_i915(state->dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005721 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005722 int max_pixclk = intel_mode_max_pixclk(state);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305723 int cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005724
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005725 if (max_pixclk < 0)
5726 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005727
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305728 if (IS_VALLEYVIEW(dev_priv))
5729 cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
5730 else
5731 cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
5732
5733 if (cdclk == dev_priv->cdclk_freq)
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005734 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005735
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005736 /* disable/enable all currently active pipes while we change cdclk */
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005737 for_each_intel_crtc(state->dev, intel_crtc)
Matt Roper83d65732015-02-25 13:12:16 -08005738 if (intel_crtc->base.state->enable)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005739 *prepare_pipes |= (1 << intel_crtc->pipe);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005740
5741 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005742}
5743
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005744static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5745{
5746 unsigned int credits, default_credits;
5747
5748 if (IS_CHERRYVIEW(dev_priv))
5749 default_credits = PFI_CREDIT(12);
5750 else
5751 default_credits = PFI_CREDIT(8);
5752
Vandana Kannan164dfd22014-11-24 13:37:41 +05305753 if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005754 /* CHV suggested value is 31 or 63 */
5755 if (IS_CHERRYVIEW(dev_priv))
5756 credits = PFI_CREDIT_31;
5757 else
5758 credits = PFI_CREDIT(15);
5759 } else {
5760 credits = default_credits;
5761 }
5762
5763 /*
5764 * WA - write default credits before re-programming
5765 * FIXME: should we also set the resend bit here?
5766 */
5767 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5768 default_credits);
5769
5770 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5771 credits | PFI_CREDIT_RESEND);
5772
5773 /*
5774 * FIXME is this guaranteed to clear
5775 * immediately or should we poll for it?
5776 */
5777 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
5778}
5779
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005780static void valleyview_modeset_global_resources(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005781{
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005782 struct drm_device *dev = state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005783 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005784 int max_pixclk = intel_mode_max_pixclk(state);
5785 int req_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005786
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005787 /* The only reason this can fail is if we fail to add the crtc_state
5788 * to the atomic state. But that can't happen since the call to
5789 * intel_mode_max_pixclk() in valleyview_modeset_global_pipes() (which
5790 * can't have failed otherwise the mode set would be aborted) added all
5791 * the states already. */
5792 if (WARN_ON(max_pixclk < 0))
5793 return;
5794
5795 req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005796
Vandana Kannan164dfd22014-11-24 13:37:41 +05305797 if (req_cdclk != dev_priv->cdclk_freq) {
Imre Deak738c05c2014-11-19 16:25:37 +02005798 /*
5799 * FIXME: We can end up here with all power domains off, yet
5800 * with a CDCLK frequency other than the minimum. To account
5801 * for this take the PIPE-A power domain, which covers the HW
5802 * blocks needed for the following programming. This can be
5803 * removed once it's guaranteed that we get here either with
5804 * the minimum CDCLK set, or the required power domains
5805 * enabled.
5806 */
5807 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
5808
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005809 if (IS_CHERRYVIEW(dev))
5810 cherryview_set_cdclk(dev, req_cdclk);
5811 else
5812 valleyview_set_cdclk(dev, req_cdclk);
Imre Deak738c05c2014-11-19 16:25:37 +02005813
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005814 vlv_program_pfi_credits(dev_priv);
5815
Imre Deak738c05c2014-11-19 16:25:37 +02005816 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005817 }
Jesse Barnes30a970c2013-11-04 13:48:12 -08005818}
5819
Jesse Barnes89b667f2013-04-18 14:51:36 -07005820static void valleyview_crtc_enable(struct drm_crtc *crtc)
5821{
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 Barnes89b667f2013-04-18 14:51:36 -07005824 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5825 struct intel_encoder *encoder;
5826 int pipe = intel_crtc->pipe;
Jani Nikula23538ef2013-08-27 15:12:22 +03005827 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005828
Matt Roper83d65732015-02-25 13:12:16 -08005829 WARN_ON(!crtc->state->enable);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005830
5831 if (intel_crtc->active)
5832 return;
5833
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005834 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
Shobhit Kumar8525a232014-06-25 12:20:39 +05305835
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005836 if (!is_dsi) {
5837 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005838 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005839 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005840 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005841 }
Daniel Vetter5b18e572014-04-24 23:55:06 +02005842
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005843 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05305844 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005845
5846 intel_set_pipe_timings(intel_crtc);
5847
Ville Syrjäläc14b0482014-10-16 20:52:34 +03005848 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
5849 struct drm_i915_private *dev_priv = dev->dev_private;
5850
5851 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
5852 I915_WRITE(CHV_CANVAS(pipe), 0);
5853 }
5854
Daniel Vetter5b18e572014-04-24 23:55:06 +02005855 i9xx_set_pipeconf(intel_crtc);
5856
Jesse Barnes89b667f2013-04-18 14:51:36 -07005857 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005858
Daniel Vettera72e4c92014-09-30 10:56:47 +02005859 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005860
Jesse Barnes89b667f2013-04-18 14:51:36 -07005861 for_each_encoder_on_crtc(dev, crtc, encoder)
5862 if (encoder->pre_pll_enable)
5863 encoder->pre_pll_enable(encoder);
5864
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005865 if (!is_dsi) {
5866 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005867 chv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005868 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005869 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005870 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07005871
5872 for_each_encoder_on_crtc(dev, crtc, encoder)
5873 if (encoder->pre_enable)
5874 encoder->pre_enable(encoder);
5875
Jesse Barnes2dd24552013-04-25 12:55:01 -07005876 i9xx_pfit_enable(intel_crtc);
5877
Ville Syrjälä63cbb072013-06-04 13:48:59 +03005878 intel_crtc_load_lut(crtc);
5879
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005880 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005881 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02005882
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005883 assert_vblank_disabled(crtc);
5884 drm_crtc_vblank_on(crtc);
5885
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005886 for_each_encoder_on_crtc(dev, crtc, encoder)
5887 encoder->enable(encoder);
5888
Ville Syrjälä9ab04602014-05-08 19:23:14 +03005889 intel_crtc_enable_planes(crtc);
Daniel Vetterd40d9182014-05-21 11:45:40 +02005890
Ville Syrjälä56b80e12014-05-16 19:40:22 +03005891 /* Underruns don't raise interrupts, so check manually. */
Daniel Vettera72e4c92014-09-30 10:56:47 +02005892 i9xx_check_fifo_underruns(dev_priv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005893}
5894
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005895static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
5896{
5897 struct drm_device *dev = crtc->base.dev;
5898 struct drm_i915_private *dev_priv = dev->dev_private;
5899
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005900 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
5901 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005902}
5903
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005904static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005905{
5906 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02005907 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08005908 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005909 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08005910 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08005911
Matt Roper83d65732015-02-25 13:12:16 -08005912 WARN_ON(!crtc->state->enable);
Daniel Vetter08a48462012-07-02 11:43:47 +02005913
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005914 if (intel_crtc->active)
5915 return;
5916
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005917 i9xx_set_pll_dividers(intel_crtc);
5918
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005919 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05305920 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005921
5922 intel_set_pipe_timings(intel_crtc);
5923
Daniel Vetter5b18e572014-04-24 23:55:06 +02005924 i9xx_set_pipeconf(intel_crtc);
5925
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005926 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01005927
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005928 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005929 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005930
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02005931 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02005932 if (encoder->pre_enable)
5933 encoder->pre_enable(encoder);
5934
Daniel Vetterf6736a12013-06-05 13:34:30 +02005935 i9xx_enable_pll(intel_crtc);
5936
Jesse Barnes2dd24552013-04-25 12:55:01 -07005937 i9xx_pfit_enable(intel_crtc);
5938
Ville Syrjälä63cbb072013-06-04 13:48:59 +03005939 intel_crtc_load_lut(crtc);
5940
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005941 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005942 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02005943
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005944 assert_vblank_disabled(crtc);
5945 drm_crtc_vblank_on(crtc);
5946
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005947 for_each_encoder_on_crtc(dev, crtc, encoder)
5948 encoder->enable(encoder);
5949
Ville Syrjälä9ab04602014-05-08 19:23:14 +03005950 intel_crtc_enable_planes(crtc);
Daniel Vetterd40d9182014-05-21 11:45:40 +02005951
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005952 /*
5953 * Gen2 reports pipe underruns whenever all planes are disabled.
5954 * So don't enable underrun reporting before at least some planes
5955 * are enabled.
5956 * FIXME: Need to fix the logic to work when we turn off all planes
5957 * but leave the pipe running.
5958 */
5959 if (IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005960 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005961
Ville Syrjälä56b80e12014-05-16 19:40:22 +03005962 /* Underruns don't raise interrupts, so check manually. */
Daniel Vettera72e4c92014-09-30 10:56:47 +02005963 i9xx_check_fifo_underruns(dev_priv);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005964}
5965
Daniel Vetter87476d62013-04-11 16:29:06 +02005966static void i9xx_pfit_disable(struct intel_crtc *crtc)
5967{
5968 struct drm_device *dev = crtc->base.dev;
5969 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02005970
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005971 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02005972 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02005973
5974 assert_pipe_disabled(dev_priv, crtc->pipe);
5975
Daniel Vetter328d8e82013-05-08 10:36:31 +02005976 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
5977 I915_READ(PFIT_CONTROL));
5978 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02005979}
5980
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005981static void i9xx_crtc_disable(struct drm_crtc *crtc)
5982{
5983 struct drm_device *dev = crtc->dev;
5984 struct drm_i915_private *dev_priv = dev->dev_private;
5985 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005986 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005987 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005988
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005989 if (!intel_crtc->active)
5990 return;
5991
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005992 /*
5993 * Gen2 reports pipe underruns whenever all planes are disabled.
5994 * So diasble underrun reporting before all the planes get disabled.
5995 * FIXME: Need to fix the logic to work when we turn off all planes
5996 * but leave the pipe running.
5997 */
5998 if (IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005999 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006000
Imre Deak564ed192014-06-13 14:54:21 +03006001 /*
6002 * Vblank time updates from the shadow to live plane control register
6003 * are blocked if the memory self-refresh mode is active at that
6004 * moment. So to make sure the plane gets truly disabled, disable
6005 * first the self-refresh mode. The self-refresh enable bit in turn
6006 * will be checked/applied by the HW only at the next frame start
6007 * event which is after the vblank start event, so we need to have a
6008 * wait-for-vblank between disabling the plane and the pipe.
6009 */
6010 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä9ab04602014-05-08 19:23:14 +03006011 intel_crtc_disable_planes(crtc);
6012
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006013 /*
6014 * On gen2 planes are double buffered but the pipe isn't, so we must
6015 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03006016 * We also need to wait on all gmch platforms because of the
6017 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006018 */
Imre Deak564ed192014-06-13 14:54:21 +03006019 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006020
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006021 for_each_encoder_on_crtc(dev, crtc, encoder)
6022 encoder->disable(encoder);
6023
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006024 drm_crtc_vblank_off(crtc);
6025 assert_vblank_disabled(crtc);
6026
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006027 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006028
Daniel Vetter87476d62013-04-11 16:29:06 +02006029 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006030
Jesse Barnes89b667f2013-04-18 14:51:36 -07006031 for_each_encoder_on_crtc(dev, crtc, encoder)
6032 if (encoder->post_disable)
6033 encoder->post_disable(encoder);
6034
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006035 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006036 if (IS_CHERRYVIEW(dev))
6037 chv_disable_pll(dev_priv, pipe);
6038 else if (IS_VALLEYVIEW(dev))
6039 vlv_disable_pll(dev_priv, pipe);
6040 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006041 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006042 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006043
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006044 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006045 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006046
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006047 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03006048 intel_update_watermarks(crtc);
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006049
Daniel Vetterefa96242014-04-24 23:55:02 +02006050 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02006051 intel_fbc_update(dev);
Daniel Vetterefa96242014-04-24 23:55:02 +02006052 mutex_unlock(&dev->struct_mutex);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006053}
6054
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006055static void i9xx_crtc_off(struct drm_crtc *crtc)
6056{
6057}
6058
Borun Fub04c5bd2014-07-12 10:02:27 +05306059/* Master function to enable/disable CRTC and corresponding power wells */
6060void intel_crtc_control(struct drm_crtc *crtc, bool enable)
Chris Wilsoncdd59982010-09-08 16:30:16 +01006061{
Chris Wilsoncdd59982010-09-08 16:30:16 +01006062 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006063 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006064 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006065 enum intel_display_power_domain domain;
6066 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006067
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006068 if (enable) {
6069 if (!intel_crtc->active) {
Daniel Vettere1e9fb82014-06-25 22:02:04 +03006070 domains = get_crtc_power_domains(crtc);
6071 for_each_power_domain(domain, domains)
6072 intel_display_power_get(dev_priv, domain);
6073 intel_crtc->enabled_power_domains = domains;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006074
6075 dev_priv->display.crtc_enable(crtc);
6076 }
6077 } else {
6078 if (intel_crtc->active) {
6079 dev_priv->display.crtc_disable(crtc);
6080
Daniel Vettere1e9fb82014-06-25 22:02:04 +03006081 domains = intel_crtc->enabled_power_domains;
6082 for_each_power_domain(domain, domains)
6083 intel_display_power_put(dev_priv, domain);
6084 intel_crtc->enabled_power_domains = 0;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006085 }
6086 }
Borun Fub04c5bd2014-07-12 10:02:27 +05306087}
6088
6089/**
6090 * Sets the power management mode of the pipe and plane.
6091 */
6092void intel_crtc_update_dpms(struct drm_crtc *crtc)
6093{
6094 struct drm_device *dev = crtc->dev;
6095 struct intel_encoder *intel_encoder;
6096 bool enable = false;
6097
6098 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
6099 enable |= intel_encoder->connectors_active;
6100
6101 intel_crtc_control(crtc, enable);
Daniel Vetter976f8a22012-07-08 22:34:21 +02006102}
6103
Daniel Vetter976f8a22012-07-08 22:34:21 +02006104static void intel_crtc_disable(struct drm_crtc *crtc)
6105{
6106 struct drm_device *dev = crtc->dev;
6107 struct drm_connector *connector;
6108 struct drm_i915_private *dev_priv = dev->dev_private;
6109
6110 /* crtc should still be enabled when we disable it. */
Matt Roper83d65732015-02-25 13:12:16 -08006111 WARN_ON(!crtc->state->enable);
Daniel Vetter976f8a22012-07-08 22:34:21 +02006112
6113 dev_priv->display.crtc_disable(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006114 dev_priv->display.off(crtc);
6115
Matt Roper70a101f2015-04-08 18:56:53 -07006116 drm_plane_helper_disable(crtc->primary);
Daniel Vetter976f8a22012-07-08 22:34:21 +02006117
6118 /* Update computed state. */
6119 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
6120 if (!connector->encoder || !connector->encoder->crtc)
6121 continue;
6122
6123 if (connector->encoder->crtc != crtc)
6124 continue;
6125
6126 connector->dpms = DRM_MODE_DPMS_OFF;
6127 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01006128 }
6129}
6130
Chris Wilsonea5b2132010-08-04 13:50:23 +01006131void intel_encoder_destroy(struct drm_encoder *encoder)
6132{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006133 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006134
Chris Wilsonea5b2132010-08-04 13:50:23 +01006135 drm_encoder_cleanup(encoder);
6136 kfree(intel_encoder);
6137}
6138
Damien Lespiau92373292013-08-08 22:28:57 +01006139/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006140 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
6141 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01006142static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006143{
6144 if (mode == DRM_MODE_DPMS_ON) {
6145 encoder->connectors_active = true;
6146
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006147 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006148 } else {
6149 encoder->connectors_active = false;
6150
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006151 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006152 }
6153}
6154
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006155/* Cross check the actual hw state with our own modeset state tracking (and it's
6156 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02006157static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006158{
6159 if (connector->get_hw_state(connector)) {
6160 struct intel_encoder *encoder = connector->encoder;
6161 struct drm_crtc *crtc;
6162 bool encoder_enabled;
6163 enum pipe pipe;
6164
6165 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6166 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03006167 connector->base.name);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006168
Dave Airlie0e32b392014-05-02 14:02:48 +10006169 /* there is no real hw state for MST connectors */
6170 if (connector->mst_port)
6171 return;
6172
Rob Clarke2c719b2014-12-15 13:56:32 -05006173 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006174 "wrong connector dpms state\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006175 I915_STATE_WARN(connector->base.encoder != &encoder->base,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006176 "active connector not linked to encoder\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006177
Dave Airlie36cd7442014-05-02 13:44:18 +10006178 if (encoder) {
Rob Clarke2c719b2014-12-15 13:56:32 -05006179 I915_STATE_WARN(!encoder->connectors_active,
Dave Airlie36cd7442014-05-02 13:44:18 +10006180 "encoder->connectors_active not set\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006181
Dave Airlie36cd7442014-05-02 13:44:18 +10006182 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -05006183 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
6184 if (I915_STATE_WARN_ON(!encoder->base.crtc))
Dave Airlie36cd7442014-05-02 13:44:18 +10006185 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006186
Dave Airlie36cd7442014-05-02 13:44:18 +10006187 crtc = encoder->base.crtc;
6188
Matt Roper83d65732015-02-25 13:12:16 -08006189 I915_STATE_WARN(!crtc->state->enable,
6190 "crtc not enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006191 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
6192 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
Dave Airlie36cd7442014-05-02 13:44:18 +10006193 "encoder active on the wrong pipe\n");
6194 }
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006195 }
6196}
6197
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006198int intel_connector_init(struct intel_connector *connector)
6199{
6200 struct drm_connector_state *connector_state;
6201
6202 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6203 if (!connector_state)
6204 return -ENOMEM;
6205
6206 connector->base.state = connector_state;
6207 return 0;
6208}
6209
6210struct intel_connector *intel_connector_alloc(void)
6211{
6212 struct intel_connector *connector;
6213
6214 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6215 if (!connector)
6216 return NULL;
6217
6218 if (intel_connector_init(connector) < 0) {
6219 kfree(connector);
6220 return NULL;
6221 }
6222
6223 return connector;
6224}
6225
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006226/* Even simpler default implementation, if there's really no special case to
6227 * consider. */
6228void intel_connector_dpms(struct drm_connector *connector, int mode)
6229{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006230 /* All the simple cases only support two dpms states. */
6231 if (mode != DRM_MODE_DPMS_ON)
6232 mode = DRM_MODE_DPMS_OFF;
6233
6234 if (mode == connector->dpms)
6235 return;
6236
6237 connector->dpms = mode;
6238
6239 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dcf2013-09-29 19:15:07 +01006240 if (connector->encoder)
6241 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006242
Daniel Vetterb9805142012-08-31 17:37:33 +02006243 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006244}
6245
Daniel Vetterf0947c32012-07-02 13:10:34 +02006246/* Simple connector->get_hw_state implementation for encoders that support only
6247 * one connector and no cloning and hence the encoder state determines the state
6248 * of the connector. */
6249bool intel_connector_get_hw_state(struct intel_connector *connector)
6250{
Daniel Vetter24929352012-07-02 20:28:59 +02006251 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006252 struct intel_encoder *encoder = connector->encoder;
6253
6254 return encoder->get_hw_state(encoder, &pipe);
6255}
6256
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006257static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006258{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006259 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6260 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006261
6262 return 0;
6263}
6264
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006265static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006266 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006267{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006268 struct drm_atomic_state *state = pipe_config->base.state;
6269 struct intel_crtc *other_crtc;
6270 struct intel_crtc_state *other_crtc_state;
6271
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006272 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6273 pipe_name(pipe), pipe_config->fdi_lanes);
6274 if (pipe_config->fdi_lanes > 4) {
6275 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6276 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006277 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006278 }
6279
Paulo Zanonibafb6552013-11-02 21:07:44 -07006280 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006281 if (pipe_config->fdi_lanes > 2) {
6282 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6283 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006284 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006285 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006286 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006287 }
6288 }
6289
6290 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006291 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006292
6293 /* Ivybridge 3 pipe is really complicated */
6294 switch (pipe) {
6295 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006296 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006297 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006298 if (pipe_config->fdi_lanes <= 2)
6299 return 0;
6300
6301 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6302 other_crtc_state =
6303 intel_atomic_get_crtc_state(state, other_crtc);
6304 if (IS_ERR(other_crtc_state))
6305 return PTR_ERR(other_crtc_state);
6306
6307 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006308 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6309 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006310 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006311 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006312 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006313 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006314 if (pipe_config->fdi_lanes > 2) {
6315 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6316 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006317 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006318 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006319
6320 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6321 other_crtc_state =
6322 intel_atomic_get_crtc_state(state, other_crtc);
6323 if (IS_ERR(other_crtc_state))
6324 return PTR_ERR(other_crtc_state);
6325
6326 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006327 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006328 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006329 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006330 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006331 default:
6332 BUG();
6333 }
6334}
6335
Daniel Vettere29c22c2013-02-21 00:00:16 +01006336#define RETRY 1
6337static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006338 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006339{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006340 struct drm_device *dev = intel_crtc->base.dev;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006341 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006342 int lane, link_bw, fdi_dotclock, ret;
6343 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006344
Daniel Vettere29c22c2013-02-21 00:00:16 +01006345retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006346 /* FDI is a binary signal running at ~2.7GHz, encoding
6347 * each output octet as 10 bits. The actual frequency
6348 * is stored as a divider into a 100MHz clock, and the
6349 * mode pixel clock is stored in units of 1KHz.
6350 * Hence the bw of each lane in terms of the mode signal
6351 * is:
6352 */
6353 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6354
Damien Lespiau241bfc32013-09-25 16:45:37 +01006355 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006356
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006357 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006358 pipe_config->pipe_bpp);
6359
6360 pipe_config->fdi_lanes = lane;
6361
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006362 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006363 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006364
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006365 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6366 intel_crtc->pipe, pipe_config);
6367 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006368 pipe_config->pipe_bpp -= 2*3;
6369 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6370 pipe_config->pipe_bpp);
6371 needs_recompute = true;
6372 pipe_config->bw_constrained = true;
6373
6374 goto retry;
6375 }
6376
6377 if (needs_recompute)
6378 return RETRY;
6379
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006380 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006381}
6382
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006383static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006384 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006385{
Jani Nikulad330a952014-01-21 11:24:25 +02006386 pipe_config->ips_enabled = i915.enable_ips &&
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03006387 hsw_crtc_supports_ips(crtc) &&
Jesse Barnesb6dfdc92013-07-25 10:06:50 -07006388 pipe_config->pipe_bpp <= 24;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006389}
6390
Daniel Vettera43f6e02013-06-07 23:10:32 +02006391static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006392 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006393{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006394 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006395 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006396 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chandra Kondurud03c93d2015-04-09 16:42:46 -07006397 int ret;
Chris Wilson89749352010-09-12 18:25:19 +01006398
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006399 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006400 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006401 int clock_limit =
6402 dev_priv->display.get_display_clock_speed(dev);
6403
6404 /*
6405 * Enable pixel doubling when the dot clock
6406 * is > 90% of the (display) core speed.
6407 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03006408 * GDG double wide on either pipe,
6409 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006410 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03006411 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01006412 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006413 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006414 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006415 }
6416
Damien Lespiau241bfc32013-09-25 16:45:37 +01006417 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006418 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006419 }
Chris Wilson89749352010-09-12 18:25:19 +01006420
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006421 /*
6422 * Pipe horizontal size must be even in:
6423 * - DVO ganged mode
6424 * - LVDS dual channel mode
6425 * - Double wide pipe
6426 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006427 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006428 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6429 pipe_config->pipe_src_w &= ~1;
6430
Damien Lespiau8693a822013-05-03 18:48:11 +01006431 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6432 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006433 */
6434 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
6435 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006436 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006437
Damien Lespiauf5adf942013-06-24 18:29:34 +01006438 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006439 hsw_compute_ips_config(crtc, pipe_config);
6440
Daniel Vetter877d48d2013-04-19 11:24:43 +02006441 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006442 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006443
Chandra Kondurud03c93d2015-04-09 16:42:46 -07006444 /* FIXME: remove below call once atomic mode set is place and all crtc
6445 * related checks called from atomic_crtc_check function */
6446 ret = 0;
6447 DRM_DEBUG_KMS("intel_crtc = %p drm_state (pipe_config->base.state) = %p\n",
6448 crtc, pipe_config->base.state);
6449 ret = intel_atomic_setup_scalers(dev, crtc, pipe_config);
6450
6451 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006452}
6453
Ville Syrjälä1652d192015-03-31 14:12:01 +03006454static int skylake_get_display_clock_speed(struct drm_device *dev)
6455{
6456 struct drm_i915_private *dev_priv = to_i915(dev);
6457 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6458 uint32_t cdctl = I915_READ(CDCLK_CTL);
6459 uint32_t linkrate;
6460
6461 if (!(lcpll1 & LCPLL_PLL_ENABLE)) {
6462 WARN(1, "LCPLL1 not enabled\n");
6463 return 24000; /* 24MHz is the cd freq with NSSC ref */
6464 }
6465
6466 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6467 return 540000;
6468
6469 linkrate = (I915_READ(DPLL_CTRL1) &
6470 DPLL_CRTL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
6471
6472 if (linkrate == DPLL_CRTL1_LINK_RATE_2160 ||
6473 linkrate == DPLL_CRTL1_LINK_RATE_1080) {
6474 /* vco 8640 */
6475 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6476 case CDCLK_FREQ_450_432:
6477 return 432000;
6478 case CDCLK_FREQ_337_308:
6479 return 308570;
6480 case CDCLK_FREQ_675_617:
6481 return 617140;
6482 default:
6483 WARN(1, "Unknown cd freq selection\n");
6484 }
6485 } else {
6486 /* vco 8100 */
6487 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6488 case CDCLK_FREQ_450_432:
6489 return 450000;
6490 case CDCLK_FREQ_337_308:
6491 return 337500;
6492 case CDCLK_FREQ_675_617:
6493 return 675000;
6494 default:
6495 WARN(1, "Unknown cd freq selection\n");
6496 }
6497 }
6498
6499 /* error case, do as if DPLL0 isn't enabled */
6500 return 24000;
6501}
6502
6503static int broadwell_get_display_clock_speed(struct drm_device *dev)
6504{
6505 struct drm_i915_private *dev_priv = dev->dev_private;
6506 uint32_t lcpll = I915_READ(LCPLL_CTL);
6507 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6508
6509 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6510 return 800000;
6511 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6512 return 450000;
6513 else if (freq == LCPLL_CLK_FREQ_450)
6514 return 450000;
6515 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6516 return 540000;
6517 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6518 return 337500;
6519 else
6520 return 675000;
6521}
6522
6523static int haswell_get_display_clock_speed(struct drm_device *dev)
6524{
6525 struct drm_i915_private *dev_priv = dev->dev_private;
6526 uint32_t lcpll = I915_READ(LCPLL_CTL);
6527 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6528
6529 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6530 return 800000;
6531 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6532 return 450000;
6533 else if (freq == LCPLL_CLK_FREQ_450)
6534 return 450000;
6535 else if (IS_HSW_ULT(dev))
6536 return 337500;
6537 else
6538 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006539}
6540
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006541static int valleyview_get_display_clock_speed(struct drm_device *dev)
6542{
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006543 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006544 u32 val;
6545 int divider;
6546
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006547 if (dev_priv->hpll_freq == 0)
6548 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
6549
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006550 mutex_lock(&dev_priv->dpio_lock);
6551 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
6552 mutex_unlock(&dev_priv->dpio_lock);
6553
6554 divider = val & DISPLAY_FREQUENCY_VALUES;
6555
Ville Syrjälä7d007f42014-06-13 13:37:53 +03006556 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
6557 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
6558 "cdclk change in progress\n");
6559
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006560 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006561}
6562
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006563static int ilk_get_display_clock_speed(struct drm_device *dev)
6564{
6565 return 450000;
6566}
6567
Jesse Barnese70236a2009-09-21 10:42:27 -07006568static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006569{
Jesse Barnese70236a2009-09-21 10:42:27 -07006570 return 400000;
6571}
Jesse Barnes79e53942008-11-07 14:24:08 -08006572
Jesse Barnese70236a2009-09-21 10:42:27 -07006573static int i915_get_display_clock_speed(struct drm_device *dev)
6574{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006575 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006576}
Jesse Barnes79e53942008-11-07 14:24:08 -08006577
Jesse Barnese70236a2009-09-21 10:42:27 -07006578static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6579{
6580 return 200000;
6581}
Jesse Barnes79e53942008-11-07 14:24:08 -08006582
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006583static int pnv_get_display_clock_speed(struct drm_device *dev)
6584{
6585 u16 gcfgc = 0;
6586
6587 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6588
6589 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6590 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006591 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006592 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006593 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006594 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006595 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006596 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6597 return 200000;
6598 default:
6599 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6600 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006601 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006602 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006603 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006604 }
6605}
6606
Jesse Barnese70236a2009-09-21 10:42:27 -07006607static int i915gm_get_display_clock_speed(struct drm_device *dev)
6608{
6609 u16 gcfgc = 0;
6610
6611 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6612
6613 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006614 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006615 else {
6616 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6617 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006618 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006619 default:
6620 case GC_DISPLAY_CLOCK_190_200_MHZ:
6621 return 190000;
6622 }
6623 }
6624}
Jesse Barnes79e53942008-11-07 14:24:08 -08006625
Jesse Barnese70236a2009-09-21 10:42:27 -07006626static int i865_get_display_clock_speed(struct drm_device *dev)
6627{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006628 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006629}
6630
6631static int i855_get_display_clock_speed(struct drm_device *dev)
6632{
6633 u16 hpllcc = 0;
6634 /* Assume that the hardware is in the high speed state. This
6635 * should be the default.
6636 */
6637 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6638 case GC_CLOCK_133_200:
6639 case GC_CLOCK_100_200:
6640 return 200000;
6641 case GC_CLOCK_166_250:
6642 return 250000;
6643 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006644 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006645 }
6646
6647 /* Shouldn't happen */
6648 return 0;
6649}
6650
6651static int i830_get_display_clock_speed(struct drm_device *dev)
6652{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006653 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006654}
6655
Zhenyu Wang2c072452009-06-05 15:38:42 +08006656static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006657intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006658{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006659 while (*num > DATA_LINK_M_N_MASK ||
6660 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08006661 *num >>= 1;
6662 *den >>= 1;
6663 }
6664}
6665
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006666static void compute_m_n(unsigned int m, unsigned int n,
6667 uint32_t *ret_m, uint32_t *ret_n)
6668{
6669 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
6670 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6671 intel_reduce_m_n_ratio(ret_m, ret_n);
6672}
6673
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006674void
6675intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6676 int pixel_clock, int link_clock,
6677 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006678{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006679 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006680
6681 compute_m_n(bits_per_pixel * pixel_clock,
6682 link_clock * nlanes * 8,
6683 &m_n->gmch_m, &m_n->gmch_n);
6684
6685 compute_m_n(pixel_clock, link_clock,
6686 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08006687}
6688
Chris Wilsona7615032011-01-12 17:04:08 +00006689static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
6690{
Jani Nikulad330a952014-01-21 11:24:25 +02006691 if (i915.panel_use_ssc >= 0)
6692 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006693 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07006694 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00006695}
6696
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006697static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
6698 int num_connectors)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006699{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006700 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006701 struct drm_i915_private *dev_priv = dev->dev_private;
6702 int refclk;
6703
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006704 WARN_ON(!crtc_state->base.state);
6705
Imre Deak5ab7b0b2015-03-06 03:29:25 +02006706 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02006707 refclk = 100000;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006708 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006709 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02006710 refclk = dev_priv->vbt.lvds_ssc_freq;
6711 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006712 } else if (!IS_GEN2(dev)) {
6713 refclk = 96000;
6714 } else {
6715 refclk = 48000;
6716 }
6717
6718 return refclk;
6719}
6720
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006721static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006722{
Daniel Vetter7df00d72013-05-21 21:54:55 +02006723 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006724}
Daniel Vetterf47709a2013-03-28 10:42:02 +01006725
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006726static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
6727{
6728 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006729}
6730
Daniel Vetterf47709a2013-03-28 10:42:02 +01006731static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006732 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08006733 intel_clock_t *reduced_clock)
6734{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006735 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006736 u32 fp, fp2 = 0;
6737
6738 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006739 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006740 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006741 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006742 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006743 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006744 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006745 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006746 }
6747
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006748 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006749
Daniel Vetterf47709a2013-03-28 10:42:02 +01006750 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006751 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07006752 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006753 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01006754 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006755 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006756 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006757 }
6758}
6759
Chon Ming Lee5e69f972013-09-05 20:41:49 +08006760static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
6761 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07006762{
6763 u32 reg_val;
6764
6765 /*
6766 * PLLB opamp always calibrates to max value of 0x3f, force enable it
6767 * and set it to a reasonable value instead.
6768 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006769 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006770 reg_val &= 0xffffff00;
6771 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006772 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006773
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006774 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006775 reg_val &= 0x8cffffff;
6776 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006777 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006778
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006779 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006780 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006781 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006782
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006783 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006784 reg_val &= 0x00ffffff;
6785 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006786 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006787}
6788
Daniel Vetterb5518422013-05-03 11:49:48 +02006789static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
6790 struct intel_link_m_n *m_n)
6791{
6792 struct drm_device *dev = crtc->base.dev;
6793 struct drm_i915_private *dev_priv = dev->dev_private;
6794 int pipe = crtc->pipe;
6795
Daniel Vettere3b95f12013-05-03 11:49:49 +02006796 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6797 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
6798 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
6799 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02006800}
6801
6802static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07006803 struct intel_link_m_n *m_n,
6804 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02006805{
6806 struct drm_device *dev = crtc->base.dev;
6807 struct drm_i915_private *dev_priv = dev->dev_private;
6808 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006809 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02006810
6811 if (INTEL_INFO(dev)->gen >= 5) {
6812 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
6813 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
6814 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
6815 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07006816 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
6817 * for gen < 8) and if DRRS is supported (to make sure the
6818 * registers are not unnecessarily accessed).
6819 */
Durgadoss R44395bf2015-02-13 15:33:02 +05306820 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006821 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07006822 I915_WRITE(PIPE_DATA_M2(transcoder),
6823 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
6824 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
6825 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
6826 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
6827 }
Daniel Vetterb5518422013-05-03 11:49:48 +02006828 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02006829 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6830 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
6831 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
6832 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02006833 }
6834}
6835
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306836void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006837{
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306838 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
6839
6840 if (m_n == M1_N1) {
6841 dp_m_n = &crtc->config->dp_m_n;
6842 dp_m2_n2 = &crtc->config->dp_m2_n2;
6843 } else if (m_n == M2_N2) {
6844
6845 /*
6846 * M2_N2 registers are not supported. Hence m2_n2 divider value
6847 * needs to be programmed into M1_N1.
6848 */
6849 dp_m_n = &crtc->config->dp_m2_n2;
6850 } else {
6851 DRM_ERROR("Unsupported divider value\n");
6852 return;
6853 }
6854
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006855 if (crtc->config->has_pch_encoder)
6856 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006857 else
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306858 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006859}
6860
Ville Syrjäläd288f652014-10-28 13:20:22 +02006861static void vlv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006862 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006863{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006864 u32 dpll, dpll_md;
6865
6866 /*
6867 * Enable DPIO clock input. We should never disable the reference
6868 * clock for pipe B, since VGA hotplug / manual detection depends
6869 * on it.
6870 */
6871 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
6872 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
6873 /* We should never disable this, set it here for state tracking */
6874 if (crtc->pipe == PIPE_B)
6875 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
6876 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02006877 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006878
Ville Syrjäläd288f652014-10-28 13:20:22 +02006879 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006880 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02006881 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006882}
6883
Ville Syrjäläd288f652014-10-28 13:20:22 +02006884static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006885 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006886{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006887 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006888 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01006889 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006890 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006891 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006892 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006893
Daniel Vetter09153002012-12-12 14:06:44 +01006894 mutex_lock(&dev_priv->dpio_lock);
6895
Ville Syrjäläd288f652014-10-28 13:20:22 +02006896 bestn = pipe_config->dpll.n;
6897 bestm1 = pipe_config->dpll.m1;
6898 bestm2 = pipe_config->dpll.m2;
6899 bestp1 = pipe_config->dpll.p1;
6900 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006901
Jesse Barnes89b667f2013-04-18 14:51:36 -07006902 /* See eDP HDMI DPIO driver vbios notes doc */
6903
6904 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006905 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08006906 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006907
6908 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006909 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006910
6911 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006912 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006913 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006914 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006915
6916 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006917 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006918
6919 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006920 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
6921 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
6922 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006923 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07006924
6925 /*
6926 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
6927 * but we don't support that).
6928 * Note: don't use the DAC post divider as it seems unstable.
6929 */
6930 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006931 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006932
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006933 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006934 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006935
Jesse Barnes89b667f2013-04-18 14:51:36 -07006936 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02006937 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006938 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
6939 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006940 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03006941 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006942 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006943 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006944 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006945
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02006946 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07006947 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006948 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006949 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006950 0x0df40000);
6951 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006952 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006953 0x0df70000);
6954 } else { /* HDMI or VGA */
6955 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006956 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006957 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006958 0x0df70000);
6959 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006960 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006961 0x0df40000);
6962 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006963
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006964 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006965 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006966 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
6967 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006968 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006969 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006970
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006971 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Daniel Vetter09153002012-12-12 14:06:44 +01006972 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006973}
6974
Ville Syrjäläd288f652014-10-28 13:20:22 +02006975static void chv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006976 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006977{
Ville Syrjäläd288f652014-10-28 13:20:22 +02006978 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006979 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
6980 DPLL_VCO_ENABLE;
6981 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02006982 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006983
Ville Syrjäläd288f652014-10-28 13:20:22 +02006984 pipe_config->dpll_hw_state.dpll_md =
6985 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006986}
6987
Ville Syrjäläd288f652014-10-28 13:20:22 +02006988static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006989 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006990{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006991 struct drm_device *dev = crtc->base.dev;
6992 struct drm_i915_private *dev_priv = dev->dev_private;
6993 int pipe = crtc->pipe;
6994 int dpll_reg = DPLL(crtc->pipe);
6995 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306996 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006997 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05306998 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306999 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007000
Ville Syrjäläd288f652014-10-28 13:20:22 +02007001 bestn = pipe_config->dpll.n;
7002 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7003 bestm1 = pipe_config->dpll.m1;
7004 bestm2 = pipe_config->dpll.m2 >> 22;
7005 bestp1 = pipe_config->dpll.p1;
7006 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307007 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307008 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307009 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007010
7011 /*
7012 * Enable Refclk and SSC
7013 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007014 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007015 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007016
7017 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007018
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007019 /* p1 and p2 divider */
7020 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7021 5 << DPIO_CHV_S1_DIV_SHIFT |
7022 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7023 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7024 1 << DPIO_CHV_K_DIV_SHIFT);
7025
7026 /* Feedback post-divider - m2 */
7027 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7028
7029 /* Feedback refclk divider - n and m1 */
7030 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7031 DPIO_CHV_M1_DIV_BY_2 |
7032 1 << DPIO_CHV_N_DIV_SHIFT);
7033
7034 /* M2 fraction division */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307035 if (bestm2_frac)
7036 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007037
7038 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307039 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7040 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7041 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7042 if (bestm2_frac)
7043 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7044 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007045
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307046 /* Program digital lock detect threshold */
7047 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7048 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7049 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7050 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7051 if (!bestm2_frac)
7052 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7053 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7054
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007055 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307056 if (vco == 5400000) {
7057 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7058 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7059 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7060 tribuf_calcntr = 0x9;
7061 } else if (vco <= 6200000) {
7062 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7063 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7064 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7065 tribuf_calcntr = 0x9;
7066 } else if (vco <= 6480000) {
7067 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7068 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7069 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7070 tribuf_calcntr = 0x8;
7071 } else {
7072 /* Not supported. Apply the same limits as in the max case */
7073 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7074 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7075 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7076 tribuf_calcntr = 0;
7077 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007078 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7079
Ville Syrjälä968040b2015-03-11 22:52:08 +02007080 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307081 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7082 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7083 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7084
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007085 /* AFC Recal */
7086 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7087 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7088 DPIO_AFC_RECAL);
7089
7090 mutex_unlock(&dev_priv->dpio_lock);
7091}
7092
Ville Syrjäläd288f652014-10-28 13:20:22 +02007093/**
7094 * vlv_force_pll_on - forcibly enable just the PLL
7095 * @dev_priv: i915 private structure
7096 * @pipe: pipe PLL to enable
7097 * @dpll: PLL configuration
7098 *
7099 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7100 * in cases where we need the PLL enabled even when @pipe is not going to
7101 * be enabled.
7102 */
7103void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7104 const struct dpll *dpll)
7105{
7106 struct intel_crtc *crtc =
7107 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007108 struct intel_crtc_state pipe_config = {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007109 .base.crtc = &crtc->base,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007110 .pixel_multiplier = 1,
7111 .dpll = *dpll,
7112 };
7113
7114 if (IS_CHERRYVIEW(dev)) {
7115 chv_update_pll(crtc, &pipe_config);
7116 chv_prepare_pll(crtc, &pipe_config);
7117 chv_enable_pll(crtc, &pipe_config);
7118 } else {
7119 vlv_update_pll(crtc, &pipe_config);
7120 vlv_prepare_pll(crtc, &pipe_config);
7121 vlv_enable_pll(crtc, &pipe_config);
7122 }
7123}
7124
7125/**
7126 * vlv_force_pll_off - forcibly disable just the PLL
7127 * @dev_priv: i915 private structure
7128 * @pipe: pipe PLL to disable
7129 *
7130 * Disable the PLL for @pipe. To be used in cases where we need
7131 * the PLL enabled even when @pipe is not going to be enabled.
7132 */
7133void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7134{
7135 if (IS_CHERRYVIEW(dev))
7136 chv_disable_pll(to_i915(dev), pipe);
7137 else
7138 vlv_disable_pll(to_i915(dev), pipe);
7139}
7140
Daniel Vetterf47709a2013-03-28 10:42:02 +01007141static void i9xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007142 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01007143 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007144 int num_connectors)
7145{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007146 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007147 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007148 u32 dpll;
7149 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007150 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007151
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007152 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307153
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007154 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7155 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007156
7157 dpll = DPLL_VGA_MODE_DIS;
7158
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007159 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007160 dpll |= DPLLB_MODE_LVDS;
7161 else
7162 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007163
Daniel Vetteref1b4602013-06-01 17:17:04 +02007164 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007165 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007166 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007167 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007168
7169 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007170 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007171
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007172 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007173 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007174
7175 /* compute bitmask from p1 value */
7176 if (IS_PINEVIEW(dev))
7177 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7178 else {
7179 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7180 if (IS_G4X(dev) && reduced_clock)
7181 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7182 }
7183 switch (clock->p2) {
7184 case 5:
7185 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7186 break;
7187 case 7:
7188 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7189 break;
7190 case 10:
7191 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7192 break;
7193 case 14:
7194 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7195 break;
7196 }
7197 if (INTEL_INFO(dev)->gen >= 4)
7198 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7199
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007200 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007201 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007202 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007203 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7204 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7205 else
7206 dpll |= PLL_REF_INPUT_DREFCLK;
7207
7208 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007209 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007210
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007211 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007212 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007213 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007214 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007215 }
7216}
7217
Daniel Vetterf47709a2013-03-28 10:42:02 +01007218static void i8xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007219 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01007220 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007221 int num_connectors)
7222{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007223 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007224 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007225 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007226 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007227
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007228 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307229
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007230 dpll = DPLL_VGA_MODE_DIS;
7231
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007232 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007233 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7234 } else {
7235 if (clock->p1 == 2)
7236 dpll |= PLL_P1_DIVIDE_BY_TWO;
7237 else
7238 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7239 if (clock->p2 == 4)
7240 dpll |= PLL_P2_DIVIDE_BY_4;
7241 }
7242
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007243 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007244 dpll |= DPLL_DVO_2X_MODE;
7245
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007246 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007247 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7248 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7249 else
7250 dpll |= PLL_REF_INPUT_DREFCLK;
7251
7252 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007253 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007254}
7255
Daniel Vetter8a654f32013-06-01 17:16:22 +02007256static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007257{
7258 struct drm_device *dev = intel_crtc->base.dev;
7259 struct drm_i915_private *dev_priv = dev->dev_private;
7260 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007261 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02007262 struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007263 &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007264 uint32_t crtc_vtotal, crtc_vblank_end;
7265 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007266
7267 /* We need to be careful not to changed the adjusted mode, for otherwise
7268 * the hw state checker will get angry at the mismatch. */
7269 crtc_vtotal = adjusted_mode->crtc_vtotal;
7270 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007271
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007272 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007273 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007274 crtc_vtotal -= 1;
7275 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007276
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007277 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007278 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7279 else
7280 vsyncshift = adjusted_mode->crtc_hsync_start -
7281 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007282 if (vsyncshift < 0)
7283 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007284 }
7285
7286 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007287 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007288
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007289 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007290 (adjusted_mode->crtc_hdisplay - 1) |
7291 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007292 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007293 (adjusted_mode->crtc_hblank_start - 1) |
7294 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007295 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007296 (adjusted_mode->crtc_hsync_start - 1) |
7297 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7298
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007299 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007300 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007301 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007302 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007303 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007304 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007305 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007306 (adjusted_mode->crtc_vsync_start - 1) |
7307 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7308
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007309 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7310 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7311 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7312 * bits. */
7313 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7314 (pipe == PIPE_B || pipe == PIPE_C))
7315 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7316
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007317 /* pipesrc controls the size that is scaled from, which should
7318 * always be the user's requested size.
7319 */
7320 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007321 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7322 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007323}
7324
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007325static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007326 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007327{
7328 struct drm_device *dev = crtc->base.dev;
7329 struct drm_i915_private *dev_priv = dev->dev_private;
7330 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7331 uint32_t tmp;
7332
7333 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007334 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7335 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007336 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007337 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7338 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007339 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007340 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7341 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007342
7343 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007344 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7345 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007346 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007347 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7348 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007349 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007350 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7351 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007352
7353 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007354 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7355 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7356 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007357 }
7358
7359 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007360 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7361 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7362
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007363 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7364 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007365}
7366
Daniel Vetterf6a83282014-02-11 15:28:57 -08007367void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007368 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007369{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007370 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7371 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7372 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7373 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007374
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007375 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7376 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7377 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7378 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007379
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007380 mode->flags = pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007381
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007382 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7383 mode->flags |= pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007384}
7385
Daniel Vetter84b046f2013-02-19 18:48:54 +01007386static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7387{
7388 struct drm_device *dev = intel_crtc->base.dev;
7389 struct drm_i915_private *dev_priv = dev->dev_private;
7390 uint32_t pipeconf;
7391
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007392 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007393
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007394 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7395 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7396 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007397
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007398 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007399 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007400
Daniel Vetterff9ce462013-04-24 14:57:17 +02007401 /* only g4x and later have fancy bpc/dither controls */
7402 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007403 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007404 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007405 pipeconf |= PIPECONF_DITHER_EN |
7406 PIPECONF_DITHER_TYPE_SP;
7407
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007408 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007409 case 18:
7410 pipeconf |= PIPECONF_6BPC;
7411 break;
7412 case 24:
7413 pipeconf |= PIPECONF_8BPC;
7414 break;
7415 case 30:
7416 pipeconf |= PIPECONF_10BPC;
7417 break;
7418 default:
7419 /* Case prevented by intel_choose_pipe_bpp_dither. */
7420 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007421 }
7422 }
7423
7424 if (HAS_PIPE_CXSR(dev)) {
7425 if (intel_crtc->lowfreq_avail) {
7426 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7427 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7428 } else {
7429 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007430 }
7431 }
7432
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007433 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007434 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007435 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007436 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7437 else
7438 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7439 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007440 pipeconf |= PIPECONF_PROGRESSIVE;
7441
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007442 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007443 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007444
Daniel Vetter84b046f2013-02-19 18:48:54 +01007445 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7446 POSTING_READ(PIPECONF(intel_crtc->pipe));
7447}
7448
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007449static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7450 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007451{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007452 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007453 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc751ce42010-03-25 11:48:48 -07007454 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07007455 intel_clock_t clock, reduced_clock;
Daniel Vettera16af722013-04-30 14:01:44 +02007456 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007457 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01007458 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08007459 const intel_limit_t *limit;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007460 struct drm_atomic_state *state = crtc_state->base.state;
7461 struct drm_connector_state *connector_state;
7462 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -08007463
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007464 for (i = 0; i < state->num_connector; i++) {
7465 if (!state->connectors[i])
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02007466 continue;
7467
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007468 connector_state = state->connector_states[i];
7469 if (connector_state->crtc != &crtc->base)
7470 continue;
7471
7472 encoder = to_intel_encoder(connector_state->best_encoder);
7473
Chris Wilson5eddb702010-09-11 13:48:45 +01007474 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007475 case INTEL_OUTPUT_LVDS:
7476 is_lvds = true;
7477 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007478 case INTEL_OUTPUT_DSI:
7479 is_dsi = true;
7480 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007481 default:
7482 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007483 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05007484
Eric Anholtc751ce42010-03-25 11:48:48 -07007485 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08007486 }
7487
Jani Nikulaf2335332013-09-13 11:03:09 +03007488 if (is_dsi)
Daniel Vetter5b18e572014-04-24 23:55:06 +02007489 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007490
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007491 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007492 refclk = i9xx_get_refclk(crtc_state, num_connectors);
Jani Nikulaf2335332013-09-13 11:03:09 +03007493
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007494 /*
7495 * Returns a set of divisors for the desired target clock with
7496 * the given refclk, or FALSE. The returned values represent
7497 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7498 * 2) / p1 / p2.
7499 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007500 limit = intel_limit(crtc_state, refclk);
7501 ok = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007502 crtc_state->port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007503 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03007504 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007505 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7506 return -EINVAL;
7507 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007508
Jani Nikulaf2335332013-09-13 11:03:09 +03007509 if (is_lvds && dev_priv->lvds_downclock_avail) {
7510 /*
7511 * Ensure we match the reduced clock's P to the target
7512 * clock. If the clocks don't match, we can't switch
7513 * the display clock by using the FP0/FP1. In such case
7514 * we will disable the LVDS downclock feature.
7515 */
7516 has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007517 dev_priv->display.find_dpll(limit, crtc_state,
Jani Nikulaf2335332013-09-13 11:03:09 +03007518 dev_priv->lvds_downclock,
7519 refclk, &clock,
7520 &reduced_clock);
7521 }
7522 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007523 crtc_state->dpll.n = clock.n;
7524 crtc_state->dpll.m1 = clock.m1;
7525 crtc_state->dpll.m2 = clock.m2;
7526 crtc_state->dpll.p1 = clock.p1;
7527 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007528 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007529
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007530 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007531 i8xx_update_pll(crtc, crtc_state,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307532 has_reduced_clock ? &reduced_clock : NULL,
7533 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007534 } else if (IS_CHERRYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007535 chv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007536 } else if (IS_VALLEYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007537 vlv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007538 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007539 i9xx_update_pll(crtc, crtc_state,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007540 has_reduced_clock ? &reduced_clock : NULL,
Robin Schroereba905b2014-05-18 02:24:50 +02007541 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007542 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007543
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007544 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007545}
7546
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007547static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007548 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007549{
7550 struct drm_device *dev = crtc->base.dev;
7551 struct drm_i915_private *dev_priv = dev->dev_private;
7552 uint32_t tmp;
7553
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007554 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7555 return;
7556
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007557 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007558 if (!(tmp & PFIT_ENABLE))
7559 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007560
Daniel Vetter06922822013-07-11 13:35:40 +02007561 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007562 if (INTEL_INFO(dev)->gen < 4) {
7563 if (crtc->pipe != PIPE_B)
7564 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007565 } else {
7566 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7567 return;
7568 }
7569
Daniel Vetter06922822013-07-11 13:35:40 +02007570 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007571 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7572 if (INTEL_INFO(dev)->gen < 5)
7573 pipe_config->gmch_pfit.lvds_border_bits =
7574 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7575}
7576
Jesse Barnesacbec812013-09-20 11:29:32 -07007577static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007578 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07007579{
7580 struct drm_device *dev = crtc->base.dev;
7581 struct drm_i915_private *dev_priv = dev->dev_private;
7582 int pipe = pipe_config->cpu_transcoder;
7583 intel_clock_t clock;
7584 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07007585 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07007586
Shobhit Kumarf573de52014-07-30 20:32:37 +05307587 /* In case of MIPI DPLL will not even be used */
7588 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7589 return;
7590
Jesse Barnesacbec812013-09-20 11:29:32 -07007591 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007592 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Jesse Barnesacbec812013-09-20 11:29:32 -07007593 mutex_unlock(&dev_priv->dpio_lock);
7594
7595 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7596 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7597 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7598 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7599 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7600
Ville Syrjäläf6466282013-10-14 14:50:31 +03007601 vlv_clock(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007602
Ville Syrjäläf6466282013-10-14 14:50:31 +03007603 /* clock.dot is the fast clock */
7604 pipe_config->port_clock = clock.dot / 5;
Jesse Barnesacbec812013-09-20 11:29:32 -07007605}
7606
Damien Lespiau5724dbd2015-01-20 12:51:52 +00007607static void
7608i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7609 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007610{
7611 struct drm_device *dev = crtc->base.dev;
7612 struct drm_i915_private *dev_priv = dev->dev_private;
7613 u32 val, base, offset;
7614 int pipe = crtc->pipe, plane = crtc->plane;
7615 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00007616 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007617 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00007618 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007619
Damien Lespiau42a7b082015-02-05 19:35:13 +00007620 val = I915_READ(DSPCNTR(plane));
7621 if (!(val & DISPLAY_PLANE_ENABLE))
7622 return;
7623
Damien Lespiaud9806c92015-01-21 14:07:19 +00007624 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00007625 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007626 DRM_DEBUG_KMS("failed to alloc fb\n");
7627 return;
7628 }
7629
Damien Lespiau1b842c82015-01-21 13:50:54 +00007630 fb = &intel_fb->base;
7631
Daniel Vetter18c52472015-02-10 17:16:09 +00007632 if (INTEL_INFO(dev)->gen >= 4) {
7633 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007634 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00007635 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
7636 }
7637 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007638
7639 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00007640 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007641 fb->pixel_format = fourcc;
7642 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007643
7644 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007645 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007646 offset = I915_READ(DSPTILEOFF(plane));
7647 else
7648 offset = I915_READ(DSPLINOFF(plane));
7649 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7650 } else {
7651 base = I915_READ(DSPADDR(plane));
7652 }
7653 plane_config->base = base;
7654
7655 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007656 fb->width = ((val >> 16) & 0xfff) + 1;
7657 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007658
7659 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007660 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007661
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007662 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00007663 fb->pixel_format,
7664 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007665
Daniel Vetterf37b5c22015-02-10 23:12:27 +01007666 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007667
Damien Lespiau2844a922015-01-20 12:51:48 +00007668 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7669 pipe_name(pipe), plane, fb->width, fb->height,
7670 fb->bits_per_pixel, base, fb->pitches[0],
7671 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007672
Damien Lespiau2d140302015-02-05 17:22:18 +00007673 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007674}
7675
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007676static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007677 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007678{
7679 struct drm_device *dev = crtc->base.dev;
7680 struct drm_i915_private *dev_priv = dev->dev_private;
7681 int pipe = pipe_config->cpu_transcoder;
7682 enum dpio_channel port = vlv_pipe_to_channel(pipe);
7683 intel_clock_t clock;
7684 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
7685 int refclk = 100000;
7686
7687 mutex_lock(&dev_priv->dpio_lock);
7688 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
7689 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
7690 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
7691 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
7692 mutex_unlock(&dev_priv->dpio_lock);
7693
7694 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
7695 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
7696 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
7697 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
7698 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
7699
7700 chv_clock(refclk, &clock);
7701
7702 /* clock.dot is the fast clock */
7703 pipe_config->port_clock = clock.dot / 5;
7704}
7705
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007706static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007707 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007708{
7709 struct drm_device *dev = crtc->base.dev;
7710 struct drm_i915_private *dev_priv = dev->dev_private;
7711 uint32_t tmp;
7712
Daniel Vetterf458ebb2014-09-30 10:56:39 +02007713 if (!intel_display_power_is_enabled(dev_priv,
7714 POWER_DOMAIN_PIPE(crtc->pipe)))
Imre Deakb5482bd2014-03-05 16:20:55 +02007715 return false;
7716
Daniel Vettere143a212013-07-04 12:01:15 +02007717 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007718 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02007719
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007720 tmp = I915_READ(PIPECONF(crtc->pipe));
7721 if (!(tmp & PIPECONF_ENABLE))
7722 return false;
7723
Ville Syrjälä42571ae2013-09-06 23:29:00 +03007724 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
7725 switch (tmp & PIPECONF_BPC_MASK) {
7726 case PIPECONF_6BPC:
7727 pipe_config->pipe_bpp = 18;
7728 break;
7729 case PIPECONF_8BPC:
7730 pipe_config->pipe_bpp = 24;
7731 break;
7732 case PIPECONF_10BPC:
7733 pipe_config->pipe_bpp = 30;
7734 break;
7735 default:
7736 break;
7737 }
7738 }
7739
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02007740 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
7741 pipe_config->limited_color_range = true;
7742
Ville Syrjälä282740f2013-09-04 18:30:03 +03007743 if (INTEL_INFO(dev)->gen < 4)
7744 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
7745
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007746 intel_get_pipe_timings(crtc, pipe_config);
7747
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007748 i9xx_get_pfit_config(crtc, pipe_config);
7749
Daniel Vetter6c49f242013-06-06 12:45:25 +02007750 if (INTEL_INFO(dev)->gen >= 4) {
7751 tmp = I915_READ(DPLL_MD(crtc->pipe));
7752 pipe_config->pixel_multiplier =
7753 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
7754 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007755 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02007756 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
7757 tmp = I915_READ(DPLL(crtc->pipe));
7758 pipe_config->pixel_multiplier =
7759 ((tmp & SDVO_MULTIPLIER_MASK)
7760 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
7761 } else {
7762 /* Note that on i915G/GM the pixel multiplier is in the sdvo
7763 * port and will be fixed up in the encoder->get_config
7764 * function. */
7765 pipe_config->pixel_multiplier = 1;
7766 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007767 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
7768 if (!IS_VALLEYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03007769 /*
7770 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
7771 * on 830. Filter it out here so that we don't
7772 * report errors due to that.
7773 */
7774 if (IS_I830(dev))
7775 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
7776
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007777 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
7778 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03007779 } else {
7780 /* Mask out read-only status bits. */
7781 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
7782 DPLL_PORTC_READY_MASK |
7783 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007784 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02007785
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007786 if (IS_CHERRYVIEW(dev))
7787 chv_crtc_clock_get(crtc, pipe_config);
7788 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07007789 vlv_crtc_clock_get(crtc, pipe_config);
7790 else
7791 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03007792
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007793 return true;
7794}
7795
Paulo Zanonidde86e22012-12-01 12:04:25 -02007796static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07007797{
7798 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007799 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007800 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007801 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07007802 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07007803 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07007804 bool has_ck505 = false;
7805 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007806
7807 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01007808 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07007809 switch (encoder->type) {
7810 case INTEL_OUTPUT_LVDS:
7811 has_panel = true;
7812 has_lvds = true;
7813 break;
7814 case INTEL_OUTPUT_EDP:
7815 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03007816 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07007817 has_cpu_edp = true;
7818 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007819 default:
7820 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007821 }
7822 }
7823
Keith Packard99eb6a02011-09-26 14:29:12 -07007824 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007825 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07007826 can_ssc = has_ck505;
7827 } else {
7828 has_ck505 = false;
7829 can_ssc = true;
7830 }
7831
Imre Deak2de69052013-05-08 13:14:04 +03007832 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
7833 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007834
7835 /* Ironlake: try to setup display ref clock before DPLL
7836 * enabling. This is only under driver's control after
7837 * PCH B stepping, previous chipset stepping should be
7838 * ignoring this setting.
7839 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007840 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007841
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007842 /* As we must carefully and slowly disable/enable each source in turn,
7843 * compute the final state we want first and check if we need to
7844 * make any changes at all.
7845 */
7846 final = val;
7847 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07007848 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007849 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07007850 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007851 final |= DREF_NONSPREAD_SOURCE_ENABLE;
7852
7853 final &= ~DREF_SSC_SOURCE_MASK;
7854 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
7855 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007856
Keith Packard199e5d72011-09-22 12:01:57 -07007857 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007858 final |= DREF_SSC_SOURCE_ENABLE;
7859
7860 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7861 final |= DREF_SSC1_ENABLE;
7862
7863 if (has_cpu_edp) {
7864 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7865 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
7866 else
7867 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
7868 } else
7869 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
7870 } else {
7871 final |= DREF_SSC_SOURCE_DISABLE;
7872 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
7873 }
7874
7875 if (final == val)
7876 return;
7877
7878 /* Always enable nonspread source */
7879 val &= ~DREF_NONSPREAD_SOURCE_MASK;
7880
7881 if (has_ck505)
7882 val |= DREF_NONSPREAD_CK505_ENABLE;
7883 else
7884 val |= DREF_NONSPREAD_SOURCE_ENABLE;
7885
7886 if (has_panel) {
7887 val &= ~DREF_SSC_SOURCE_MASK;
7888 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007889
Keith Packard199e5d72011-09-22 12:01:57 -07007890 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07007891 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07007892 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007893 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02007894 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007895 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007896
7897 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007898 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007899 POSTING_READ(PCH_DREF_CONTROL);
7900 udelay(200);
7901
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007902 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007903
7904 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07007905 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07007906 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07007907 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007908 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02007909 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007910 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07007911 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007912 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007913
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007914 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007915 POSTING_READ(PCH_DREF_CONTROL);
7916 udelay(200);
7917 } else {
7918 DRM_DEBUG_KMS("Disabling SSC entirely\n");
7919
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007920 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07007921
7922 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007923 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007924
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007925 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007926 POSTING_READ(PCH_DREF_CONTROL);
7927 udelay(200);
7928
7929 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007930 val &= ~DREF_SSC_SOURCE_MASK;
7931 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007932
7933 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007934 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007935
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007936 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007937 POSTING_READ(PCH_DREF_CONTROL);
7938 udelay(200);
7939 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007940
7941 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007942}
7943
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007944static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02007945{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007946 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02007947
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007948 tmp = I915_READ(SOUTH_CHICKEN2);
7949 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
7950 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007951
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007952 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
7953 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
7954 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02007955
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007956 tmp = I915_READ(SOUTH_CHICKEN2);
7957 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
7958 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007959
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007960 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
7961 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
7962 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007963}
7964
7965/* WaMPhyProgramming:hsw */
7966static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
7967{
7968 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02007969
7970 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
7971 tmp &= ~(0xFF << 24);
7972 tmp |= (0x12 << 24);
7973 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
7974
Paulo Zanonidde86e22012-12-01 12:04:25 -02007975 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
7976 tmp |= (1 << 11);
7977 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
7978
7979 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
7980 tmp |= (1 << 11);
7981 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
7982
Paulo Zanonidde86e22012-12-01 12:04:25 -02007983 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
7984 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7985 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
7986
7987 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
7988 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7989 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
7990
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007991 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
7992 tmp &= ~(7 << 13);
7993 tmp |= (5 << 13);
7994 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007995
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007996 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
7997 tmp &= ~(7 << 13);
7998 tmp |= (5 << 13);
7999 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008000
8001 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8002 tmp &= ~0xFF;
8003 tmp |= 0x1C;
8004 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8005
8006 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8007 tmp &= ~0xFF;
8008 tmp |= 0x1C;
8009 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8010
8011 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8012 tmp &= ~(0xFF << 16);
8013 tmp |= (0x1C << 16);
8014 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8015
8016 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8017 tmp &= ~(0xFF << 16);
8018 tmp |= (0x1C << 16);
8019 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8020
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008021 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8022 tmp |= (1 << 27);
8023 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008024
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008025 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8026 tmp |= (1 << 27);
8027 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008028
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008029 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8030 tmp &= ~(0xF << 28);
8031 tmp |= (4 << 28);
8032 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008033
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008034 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8035 tmp &= ~(0xF << 28);
8036 tmp |= (4 << 28);
8037 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008038}
8039
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008040/* Implements 3 different sequences from BSpec chapter "Display iCLK
8041 * Programming" based on the parameters passed:
8042 * - Sequence to enable CLKOUT_DP
8043 * - Sequence to enable CLKOUT_DP without spread
8044 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8045 */
8046static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8047 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008048{
8049 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008050 uint32_t reg, tmp;
8051
8052 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8053 with_spread = true;
8054 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
8055 with_fdi, "LP PCH doesn't have FDI\n"))
8056 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008057
8058 mutex_lock(&dev_priv->dpio_lock);
8059
8060 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8061 tmp &= ~SBI_SSCCTL_DISABLE;
8062 tmp |= SBI_SSCCTL_PATHALT;
8063 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8064
8065 udelay(24);
8066
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008067 if (with_spread) {
8068 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8069 tmp &= ~SBI_SSCCTL_PATHALT;
8070 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008071
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008072 if (with_fdi) {
8073 lpt_reset_fdi_mphy(dev_priv);
8074 lpt_program_fdi_mphy(dev_priv);
8075 }
8076 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008077
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008078 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8079 SBI_GEN0 : SBI_DBUFF0;
8080 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8081 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8082 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008083
8084 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008085}
8086
Paulo Zanoni47701c32013-07-23 11:19:25 -03008087/* Sequence to disable CLKOUT_DP */
8088static void lpt_disable_clkout_dp(struct drm_device *dev)
8089{
8090 struct drm_i915_private *dev_priv = dev->dev_private;
8091 uint32_t reg, tmp;
8092
8093 mutex_lock(&dev_priv->dpio_lock);
8094
8095 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8096 SBI_GEN0 : SBI_DBUFF0;
8097 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8098 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8099 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8100
8101 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8102 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8103 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8104 tmp |= SBI_SSCCTL_PATHALT;
8105 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8106 udelay(32);
8107 }
8108 tmp |= SBI_SSCCTL_DISABLE;
8109 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8110 }
8111
8112 mutex_unlock(&dev_priv->dpio_lock);
8113}
8114
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008115static void lpt_init_pch_refclk(struct drm_device *dev)
8116{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008117 struct intel_encoder *encoder;
8118 bool has_vga = false;
8119
Damien Lespiaub2784e12014-08-05 11:29:37 +01008120 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008121 switch (encoder->type) {
8122 case INTEL_OUTPUT_ANALOG:
8123 has_vga = true;
8124 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008125 default:
8126 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008127 }
8128 }
8129
Paulo Zanoni47701c32013-07-23 11:19:25 -03008130 if (has_vga)
8131 lpt_enable_clkout_dp(dev, true, true);
8132 else
8133 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008134}
8135
Paulo Zanonidde86e22012-12-01 12:04:25 -02008136/*
8137 * Initialize reference clocks when the driver loads
8138 */
8139void intel_init_pch_refclk(struct drm_device *dev)
8140{
8141 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8142 ironlake_init_pch_refclk(dev);
8143 else if (HAS_PCH_LPT(dev))
8144 lpt_init_pch_refclk(dev);
8145}
8146
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008147static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008148{
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008149 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008150 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008151 struct drm_atomic_state *state = crtc_state->base.state;
8152 struct drm_connector_state *connector_state;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008153 struct intel_encoder *encoder;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008154 int num_connectors = 0, i;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008155 bool is_lvds = false;
8156
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008157 for (i = 0; i < state->num_connector; i++) {
8158 if (!state->connectors[i])
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02008159 continue;
8160
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008161 connector_state = state->connector_states[i];
8162 if (connector_state->crtc != crtc_state->base.crtc)
8163 continue;
8164
8165 encoder = to_intel_encoder(connector_state->best_encoder);
8166
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008167 switch (encoder->type) {
8168 case INTEL_OUTPUT_LVDS:
8169 is_lvds = true;
8170 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008171 default:
8172 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008173 }
8174 num_connectors++;
8175 }
8176
8177 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008178 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008179 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008180 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008181 }
8182
8183 return 120000;
8184}
8185
Daniel Vetter6ff93602013-04-19 11:24:36 +02008186static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008187{
8188 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8189 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8190 int pipe = intel_crtc->pipe;
8191 uint32_t val;
8192
Daniel Vetter78114072013-06-13 00:54:57 +02008193 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008194
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008195 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008196 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008197 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008198 break;
8199 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008200 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008201 break;
8202 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008203 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008204 break;
8205 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008206 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008207 break;
8208 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008209 /* Case prevented by intel_choose_pipe_bpp_dither. */
8210 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008211 }
8212
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008213 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008214 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8215
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008216 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008217 val |= PIPECONF_INTERLACED_ILK;
8218 else
8219 val |= PIPECONF_PROGRESSIVE;
8220
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008221 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008222 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008223
Paulo Zanonic8203562012-09-12 10:06:29 -03008224 I915_WRITE(PIPECONF(pipe), val);
8225 POSTING_READ(PIPECONF(pipe));
8226}
8227
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008228/*
8229 * Set up the pipe CSC unit.
8230 *
8231 * Currently only full range RGB to limited range RGB conversion
8232 * is supported, but eventually this should handle various
8233 * RGB<->YCbCr scenarios as well.
8234 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01008235static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008236{
8237 struct drm_device *dev = crtc->dev;
8238 struct drm_i915_private *dev_priv = dev->dev_private;
8239 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8240 int pipe = intel_crtc->pipe;
8241 uint16_t coeff = 0x7800; /* 1.0 */
8242
8243 /*
8244 * TODO: Check what kind of values actually come out of the pipe
8245 * with these coeff/postoff values and adjust to get the best
8246 * accuracy. Perhaps we even need to take the bpc value into
8247 * consideration.
8248 */
8249
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008250 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008251 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8252
8253 /*
8254 * GY/GU and RY/RU should be the other way around according
8255 * to BSpec, but reality doesn't agree. Just set them up in
8256 * a way that results in the correct picture.
8257 */
8258 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8259 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8260
8261 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8262 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8263
8264 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8265 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8266
8267 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8268 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8269 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8270
8271 if (INTEL_INFO(dev)->gen > 6) {
8272 uint16_t postoff = 0;
8273
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008274 if (intel_crtc->config->limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02008275 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008276
8277 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8278 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8279 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8280
8281 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8282 } else {
8283 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8284
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008285 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008286 mode |= CSC_BLACK_SCREEN_OFFSET;
8287
8288 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8289 }
8290}
8291
Daniel Vetter6ff93602013-04-19 11:24:36 +02008292static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008293{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008294 struct drm_device *dev = crtc->dev;
8295 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008296 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008297 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008298 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008299 uint32_t val;
8300
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008301 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008302
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008303 if (IS_HASWELL(dev) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008304 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8305
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008306 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008307 val |= PIPECONF_INTERLACED_ILK;
8308 else
8309 val |= PIPECONF_PROGRESSIVE;
8310
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008311 I915_WRITE(PIPECONF(cpu_transcoder), val);
8312 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008313
8314 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8315 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008316
Satheeshakrishna M3cdf1222014-04-08 15:46:53 +05308317 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008318 val = 0;
8319
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008320 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008321 case 18:
8322 val |= PIPEMISC_DITHER_6_BPC;
8323 break;
8324 case 24:
8325 val |= PIPEMISC_DITHER_8_BPC;
8326 break;
8327 case 30:
8328 val |= PIPEMISC_DITHER_10_BPC;
8329 break;
8330 case 36:
8331 val |= PIPEMISC_DITHER_12_BPC;
8332 break;
8333 default:
8334 /* Case prevented by pipe_config_set_bpp. */
8335 BUG();
8336 }
8337
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008338 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008339 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8340
8341 I915_WRITE(PIPEMISC(pipe), val);
8342 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008343}
8344
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008345static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008346 struct intel_crtc_state *crtc_state,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008347 intel_clock_t *clock,
8348 bool *has_reduced_clock,
8349 intel_clock_t *reduced_clock)
8350{
8351 struct drm_device *dev = crtc->dev;
8352 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008353 int refclk;
8354 const intel_limit_t *limit;
Daniel Vettera16af722013-04-30 14:01:44 +02008355 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008356
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008357 is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008358
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008359 refclk = ironlake_get_refclk(crtc_state);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008360
8361 /*
8362 * Returns a set of divisors for the desired target clock with the given
8363 * refclk, or FALSE. The returned values represent the clock equation:
8364 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8365 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008366 limit = intel_limit(crtc_state, refclk);
8367 ret = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008368 crtc_state->port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008369 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008370 if (!ret)
8371 return false;
8372
8373 if (is_lvds && dev_priv->lvds_downclock_avail) {
8374 /*
8375 * Ensure we match the reduced clock's P to the target clock.
8376 * If the clocks don't match, we can't switch the display clock
8377 * by using the FP0/FP1. In such case we will disable the LVDS
8378 * downclock feature.
8379 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02008380 *has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008381 dev_priv->display.find_dpll(limit, crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008382 dev_priv->lvds_downclock,
8383 refclk, clock,
8384 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008385 }
8386
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008387 return true;
8388}
8389
Paulo Zanonid4b19312012-11-29 11:29:32 -02008390int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8391{
8392 /*
8393 * Account for spread spectrum to avoid
8394 * oversubscribing the link. Max center spread
8395 * is 2.5%; use 5% for safety's sake.
8396 */
8397 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008398 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008399}
8400
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008401static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008402{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008403 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008404}
8405
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008406static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008407 struct intel_crtc_state *crtc_state,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008408 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008409 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008410{
8411 struct drm_crtc *crtc = &intel_crtc->base;
8412 struct drm_device *dev = crtc->dev;
8413 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008414 struct drm_atomic_state *state = crtc_state->base.state;
8415 struct drm_connector_state *connector_state;
8416 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008417 uint32_t dpll;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008418 int factor, num_connectors = 0, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008419 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008420
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008421 for (i = 0; i < state->num_connector; i++) {
8422 if (!state->connectors[i])
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02008423 continue;
8424
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008425 connector_state = state->connector_states[i];
8426 if (connector_state->crtc != crtc_state->base.crtc)
8427 continue;
8428
8429 encoder = to_intel_encoder(connector_state->best_encoder);
8430
8431 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008432 case INTEL_OUTPUT_LVDS:
8433 is_lvds = true;
8434 break;
8435 case INTEL_OUTPUT_SDVO:
8436 case INTEL_OUTPUT_HDMI:
8437 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008438 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008439 default:
8440 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008441 }
8442
8443 num_connectors++;
8444 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008445
Chris Wilsonc1858122010-12-03 21:35:48 +00008446 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008447 factor = 21;
8448 if (is_lvds) {
8449 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008450 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008451 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008452 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008453 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008454 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008455
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008456 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02008457 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00008458
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008459 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8460 *fp2 |= FP_CB_TUNE;
8461
Chris Wilson5eddb702010-09-11 13:48:45 +01008462 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008463
Eric Anholta07d6782011-03-30 13:01:08 -07008464 if (is_lvds)
8465 dpll |= DPLLB_MODE_LVDS;
8466 else
8467 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008468
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008469 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008470 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008471
8472 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008473 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008474 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008475 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008476
Eric Anholta07d6782011-03-30 13:01:08 -07008477 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008478 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008479 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008480 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008481
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008482 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008483 case 5:
8484 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8485 break;
8486 case 7:
8487 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8488 break;
8489 case 10:
8490 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8491 break;
8492 case 14:
8493 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8494 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008495 }
8496
Daniel Vetterb4c09f32013-04-30 14:01:42 +02008497 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008498 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008499 else
8500 dpll |= PLL_REF_INPUT_DREFCLK;
8501
Daniel Vetter959e16d2013-06-05 13:34:21 +02008502 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008503}
8504
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008505static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8506 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008507{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008508 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008509 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008510 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03008511 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01008512 bool is_lvds = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008513 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008514
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03008515 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
Jesse Barnes79e53942008-11-07 14:24:08 -08008516
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008517 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8518 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
8519
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008520 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008521 &has_reduced_clock, &reduced_clock);
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008522 if (!ok && !crtc_state->clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008523 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8524 return -EINVAL;
8525 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01008526 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008527 if (!crtc_state->clock_set) {
8528 crtc_state->dpll.n = clock.n;
8529 crtc_state->dpll.m1 = clock.m1;
8530 crtc_state->dpll.m2 = clock.m2;
8531 crtc_state->dpll.p1 = clock.p1;
8532 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008533 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008534
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008535 /* 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 +02008536 if (crtc_state->has_pch_encoder) {
8537 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008538 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008539 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008540
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008541 dpll = ironlake_compute_dpll(crtc, crtc_state,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008542 &fp, &reduced_clock,
8543 has_reduced_clock ? &fp2 : NULL);
8544
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008545 crtc_state->dpll_hw_state.dpll = dpll;
8546 crtc_state->dpll_hw_state.fp0 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008547 if (has_reduced_clock)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008548 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008549 else
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008550 crtc_state->dpll_hw_state.fp1 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008551
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008552 pll = intel_get_shared_dpll(crtc, crtc_state);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008553 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03008554 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008555 pipe_name(crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07008556 return -EINVAL;
8557 }
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008558 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008559
Rodrigo Viviab585de2015-03-24 12:40:09 -07008560 if (is_lvds && has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008561 crtc->lowfreq_avail = true;
Daniel Vetterbcd644e2013-06-05 13:34:22 +02008562 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008563 crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008564
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008565 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008566}
8567
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008568static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8569 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008570{
8571 struct drm_device *dev = crtc->base.dev;
8572 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008573 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008574
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008575 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8576 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8577 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8578 & ~TU_SIZE_MASK;
8579 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8580 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8581 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8582}
8583
8584static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8585 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008586 struct intel_link_m_n *m_n,
8587 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008588{
8589 struct drm_device *dev = crtc->base.dev;
8590 struct drm_i915_private *dev_priv = dev->dev_private;
8591 enum pipe pipe = crtc->pipe;
8592
8593 if (INTEL_INFO(dev)->gen >= 5) {
8594 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8595 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8596 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8597 & ~TU_SIZE_MASK;
8598 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8599 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8600 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008601 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8602 * gen < 8) and if DRRS is supported (to make sure the
8603 * registers are not unnecessarily read).
8604 */
8605 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008606 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008607 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8608 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8609 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8610 & ~TU_SIZE_MASK;
8611 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8612 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8613 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8614 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008615 } else {
8616 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8617 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8618 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8619 & ~TU_SIZE_MASK;
8620 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8621 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8622 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8623 }
8624}
8625
8626void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008627 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008628{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02008629 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008630 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8631 else
8632 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008633 &pipe_config->dp_m_n,
8634 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008635}
8636
Daniel Vetter72419202013-04-04 13:28:53 +02008637static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008638 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02008639{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008640 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008641 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02008642}
8643
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008644static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008645 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008646{
8647 struct drm_device *dev = crtc->base.dev;
8648 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07008649 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8650 uint32_t ps_ctrl = 0;
8651 int id = -1;
8652 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008653
Chandra Kondurua1b22782015-04-07 15:28:45 -07008654 /* find scaler attached to this pipe */
8655 for (i = 0; i < crtc->num_scalers; i++) {
8656 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8657 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8658 id = i;
8659 pipe_config->pch_pfit.enabled = true;
8660 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8661 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8662 break;
8663 }
8664 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008665
Chandra Kondurua1b22782015-04-07 15:28:45 -07008666 scaler_state->scaler_id = id;
8667 if (id >= 0) {
8668 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8669 } else {
8670 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008671 }
8672}
8673
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008674static void
8675skylake_get_initial_plane_config(struct intel_crtc *crtc,
8676 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008677{
8678 struct drm_device *dev = crtc->base.dev;
8679 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00008680 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008681 int pipe = crtc->pipe;
8682 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008683 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008684 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008685 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008686
Damien Lespiaud9806c92015-01-21 14:07:19 +00008687 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008688 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008689 DRM_DEBUG_KMS("failed to alloc fb\n");
8690 return;
8691 }
8692
Damien Lespiau1b842c82015-01-21 13:50:54 +00008693 fb = &intel_fb->base;
8694
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008695 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00008696 if (!(val & PLANE_CTL_ENABLE))
8697 goto error;
8698
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008699 pixel_format = val & PLANE_CTL_FORMAT_MASK;
8700 fourcc = skl_format_to_fourcc(pixel_format,
8701 val & PLANE_CTL_ORDER_RGBX,
8702 val & PLANE_CTL_ALPHA_MASK);
8703 fb->pixel_format = fourcc;
8704 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
8705
Damien Lespiau40f46282015-02-27 11:15:21 +00008706 tiling = val & PLANE_CTL_TILED_MASK;
8707 switch (tiling) {
8708 case PLANE_CTL_TILED_LINEAR:
8709 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
8710 break;
8711 case PLANE_CTL_TILED_X:
8712 plane_config->tiling = I915_TILING_X;
8713 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8714 break;
8715 case PLANE_CTL_TILED_Y:
8716 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
8717 break;
8718 case PLANE_CTL_TILED_YF:
8719 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
8720 break;
8721 default:
8722 MISSING_CASE(tiling);
8723 goto error;
8724 }
8725
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008726 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
8727 plane_config->base = base;
8728
8729 offset = I915_READ(PLANE_OFFSET(pipe, 0));
8730
8731 val = I915_READ(PLANE_SIZE(pipe, 0));
8732 fb->height = ((val >> 16) & 0xfff) + 1;
8733 fb->width = ((val >> 0) & 0x1fff) + 1;
8734
8735 val = I915_READ(PLANE_STRIDE(pipe, 0));
Damien Lespiau40f46282015-02-27 11:15:21 +00008736 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
8737 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008738 fb->pitches[0] = (val & 0x3ff) * stride_mult;
8739
8740 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008741 fb->pixel_format,
8742 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008743
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008744 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008745
8746 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8747 pipe_name(pipe), fb->width, fb->height,
8748 fb->bits_per_pixel, base, fb->pitches[0],
8749 plane_config->size);
8750
Damien Lespiau2d140302015-02-05 17:22:18 +00008751 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008752 return;
8753
8754error:
8755 kfree(fb);
8756}
8757
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008758static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008759 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008760{
8761 struct drm_device *dev = crtc->base.dev;
8762 struct drm_i915_private *dev_priv = dev->dev_private;
8763 uint32_t tmp;
8764
8765 tmp = I915_READ(PF_CTL(crtc->pipe));
8766
8767 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008768 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008769 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
8770 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02008771
8772 /* We currently do not free assignements of panel fitters on
8773 * ivb/hsw (since we don't use the higher upscaling modes which
8774 * differentiates them) so just WARN about this case for now. */
8775 if (IS_GEN7(dev)) {
8776 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
8777 PF_PIPE_SEL_IVB(crtc->pipe));
8778 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008779 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008780}
8781
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008782static void
8783ironlake_get_initial_plane_config(struct intel_crtc *crtc,
8784 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008785{
8786 struct drm_device *dev = crtc->base.dev;
8787 struct drm_i915_private *dev_priv = dev->dev_private;
8788 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008789 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008790 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008791 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008792 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008793 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008794
Damien Lespiau42a7b082015-02-05 19:35:13 +00008795 val = I915_READ(DSPCNTR(pipe));
8796 if (!(val & DISPLAY_PLANE_ENABLE))
8797 return;
8798
Damien Lespiaud9806c92015-01-21 14:07:19 +00008799 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008800 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008801 DRM_DEBUG_KMS("failed to alloc fb\n");
8802 return;
8803 }
8804
Damien Lespiau1b842c82015-01-21 13:50:54 +00008805 fb = &intel_fb->base;
8806
Daniel Vetter18c52472015-02-10 17:16:09 +00008807 if (INTEL_INFO(dev)->gen >= 4) {
8808 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008809 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008810 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8811 }
8812 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008813
8814 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008815 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008816 fb->pixel_format = fourcc;
8817 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008818
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008819 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008820 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008821 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008822 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00008823 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008824 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008825 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008826 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008827 }
8828 plane_config->base = base;
8829
8830 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008831 fb->width = ((val >> 16) & 0xfff) + 1;
8832 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008833
8834 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008835 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008836
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008837 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008838 fb->pixel_format,
8839 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008840
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008841 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008842
Damien Lespiau2844a922015-01-20 12:51:48 +00008843 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8844 pipe_name(pipe), fb->width, fb->height,
8845 fb->bits_per_pixel, base, fb->pitches[0],
8846 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008847
Damien Lespiau2d140302015-02-05 17:22:18 +00008848 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008849}
8850
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008851static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008852 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008853{
8854 struct drm_device *dev = crtc->base.dev;
8855 struct drm_i915_private *dev_priv = dev->dev_private;
8856 uint32_t tmp;
8857
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008858 if (!intel_display_power_is_enabled(dev_priv,
8859 POWER_DOMAIN_PIPE(crtc->pipe)))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03008860 return false;
8861
Daniel Vettere143a212013-07-04 12:01:15 +02008862 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008863 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02008864
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008865 tmp = I915_READ(PIPECONF(crtc->pipe));
8866 if (!(tmp & PIPECONF_ENABLE))
8867 return false;
8868
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008869 switch (tmp & PIPECONF_BPC_MASK) {
8870 case PIPECONF_6BPC:
8871 pipe_config->pipe_bpp = 18;
8872 break;
8873 case PIPECONF_8BPC:
8874 pipe_config->pipe_bpp = 24;
8875 break;
8876 case PIPECONF_10BPC:
8877 pipe_config->pipe_bpp = 30;
8878 break;
8879 case PIPECONF_12BPC:
8880 pipe_config->pipe_bpp = 36;
8881 break;
8882 default:
8883 break;
8884 }
8885
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008886 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
8887 pipe_config->limited_color_range = true;
8888
Daniel Vetterab9412b2013-05-03 11:49:46 +02008889 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02008890 struct intel_shared_dpll *pll;
8891
Daniel Vetter88adfff2013-03-28 10:42:01 +01008892 pipe_config->has_pch_encoder = true;
8893
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008894 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
8895 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8896 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02008897
8898 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008899
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008900 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02008901 pipe_config->shared_dpll =
8902 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008903 } else {
8904 tmp = I915_READ(PCH_DPLL_SEL);
8905 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8906 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
8907 else
8908 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
8909 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02008910
8911 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
8912
8913 WARN_ON(!pll->get_hw_state(dev_priv, pll,
8914 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02008915
8916 tmp = pipe_config->dpll_hw_state.dpll;
8917 pipe_config->pixel_multiplier =
8918 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
8919 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03008920
8921 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008922 } else {
8923 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008924 }
8925
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008926 intel_get_pipe_timings(crtc, pipe_config);
8927
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008928 ironlake_get_pfit_config(crtc, pipe_config);
8929
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008930 return true;
8931}
8932
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008933static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
8934{
8935 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008936 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008937
Damien Lespiaud3fcc802014-05-13 23:32:22 +01008938 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05008939 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008940 pipe_name(crtc->pipe));
8941
Rob Clarke2c719b2014-12-15 13:56:32 -05008942 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
8943 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
8944 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
8945 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
8946 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
8947 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008948 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03008949 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05008950 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03008951 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008952 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008953 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008954 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008955 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008956 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008957
Paulo Zanoni9926ada2014-04-01 19:39:47 -03008958 /*
8959 * In theory we can still leave IRQs enabled, as long as only the HPD
8960 * interrupts remain enabled. We used to check for that, but since it's
8961 * gen-specific and since we only disable LCPLL after we fully disable
8962 * the interrupts, the check below should be enough.
8963 */
Rob Clarke2c719b2014-12-15 13:56:32 -05008964 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008965}
8966
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008967static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
8968{
8969 struct drm_device *dev = dev_priv->dev;
8970
8971 if (IS_HASWELL(dev))
8972 return I915_READ(D_COMP_HSW);
8973 else
8974 return I915_READ(D_COMP_BDW);
8975}
8976
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008977static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
8978{
8979 struct drm_device *dev = dev_priv->dev;
8980
8981 if (IS_HASWELL(dev)) {
8982 mutex_lock(&dev_priv->rps.hw_lock);
8983 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
8984 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03008985 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008986 mutex_unlock(&dev_priv->rps.hw_lock);
8987 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008988 I915_WRITE(D_COMP_BDW, val);
8989 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008990 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008991}
8992
8993/*
8994 * This function implements pieces of two sequences from BSpec:
8995 * - Sequence for display software to disable LCPLL
8996 * - Sequence for display software to allow package C8+
8997 * The steps implemented here are just the steps that actually touch the LCPLL
8998 * register. Callers should take care of disabling all the display engine
8999 * functions, doing the mode unset, fixing interrupts, etc.
9000 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009001static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9002 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009003{
9004 uint32_t val;
9005
9006 assert_can_disable_lcpll(dev_priv);
9007
9008 val = I915_READ(LCPLL_CTL);
9009
9010 if (switch_to_fclk) {
9011 val |= LCPLL_CD_SOURCE_FCLK;
9012 I915_WRITE(LCPLL_CTL, val);
9013
9014 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9015 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9016 DRM_ERROR("Switching to FCLK failed\n");
9017
9018 val = I915_READ(LCPLL_CTL);
9019 }
9020
9021 val |= LCPLL_PLL_DISABLE;
9022 I915_WRITE(LCPLL_CTL, val);
9023 POSTING_READ(LCPLL_CTL);
9024
9025 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9026 DRM_ERROR("LCPLL still locked\n");
9027
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009028 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009029 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009030 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009031 ndelay(100);
9032
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009033 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9034 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009035 DRM_ERROR("D_COMP RCOMP still in progress\n");
9036
9037 if (allow_power_down) {
9038 val = I915_READ(LCPLL_CTL);
9039 val |= LCPLL_POWER_DOWN_ALLOW;
9040 I915_WRITE(LCPLL_CTL, val);
9041 POSTING_READ(LCPLL_CTL);
9042 }
9043}
9044
9045/*
9046 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9047 * source.
9048 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009049static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009050{
9051 uint32_t val;
9052
9053 val = I915_READ(LCPLL_CTL);
9054
9055 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9056 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9057 return;
9058
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009059 /*
9060 * Make sure we're not on PC8 state before disabling PC8, otherwise
9061 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009062 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009063 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009064
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009065 if (val & LCPLL_POWER_DOWN_ALLOW) {
9066 val &= ~LCPLL_POWER_DOWN_ALLOW;
9067 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009068 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009069 }
9070
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009071 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009072 val |= D_COMP_COMP_FORCE;
9073 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009074 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009075
9076 val = I915_READ(LCPLL_CTL);
9077 val &= ~LCPLL_PLL_DISABLE;
9078 I915_WRITE(LCPLL_CTL, val);
9079
9080 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9081 DRM_ERROR("LCPLL not locked yet\n");
9082
9083 if (val & LCPLL_CD_SOURCE_FCLK) {
9084 val = I915_READ(LCPLL_CTL);
9085 val &= ~LCPLL_CD_SOURCE_FCLK;
9086 I915_WRITE(LCPLL_CTL, val);
9087
9088 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9089 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9090 DRM_ERROR("Switching back to LCPLL failed\n");
9091 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009092
Mika Kuoppala59bad942015-01-16 11:34:40 +02009093 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009094}
9095
Paulo Zanoni765dab672014-03-07 20:08:18 -03009096/*
9097 * Package states C8 and deeper are really deep PC states that can only be
9098 * reached when all the devices on the system allow it, so even if the graphics
9099 * device allows PC8+, it doesn't mean the system will actually get to these
9100 * states. Our driver only allows PC8+ when going into runtime PM.
9101 *
9102 * The requirements for PC8+ are that all the outputs are disabled, the power
9103 * well is disabled and most interrupts are disabled, and these are also
9104 * requirements for runtime PM. When these conditions are met, we manually do
9105 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9106 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9107 * hang the machine.
9108 *
9109 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9110 * the state of some registers, so when we come back from PC8+ we need to
9111 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9112 * need to take care of the registers kept by RC6. Notice that this happens even
9113 * if we don't put the device in PCI D3 state (which is what currently happens
9114 * because of the runtime PM support).
9115 *
9116 * For more, read "Display Sequences for Package C8" on the hardware
9117 * documentation.
9118 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009119void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009120{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009121 struct drm_device *dev = dev_priv->dev;
9122 uint32_t val;
9123
Paulo Zanonic67a4702013-08-19 13:18:09 -03009124 DRM_DEBUG_KMS("Enabling package C8+\n");
9125
Paulo Zanonic67a4702013-08-19 13:18:09 -03009126 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9127 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9128 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9129 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9130 }
9131
9132 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009133 hsw_disable_lcpll(dev_priv, true, true);
9134}
9135
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009136void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009137{
9138 struct drm_device *dev = dev_priv->dev;
9139 uint32_t val;
9140
Paulo Zanonic67a4702013-08-19 13:18:09 -03009141 DRM_DEBUG_KMS("Disabling package C8+\n");
9142
9143 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009144 lpt_init_pch_refclk(dev);
9145
9146 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9147 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9148 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9149 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9150 }
9151
9152 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009153}
9154
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309155static void broxton_modeset_global_resources(struct drm_atomic_state *state)
9156{
9157 struct drm_device *dev = state->dev;
9158 struct drm_i915_private *dev_priv = dev->dev_private;
9159 int max_pixclk = intel_mode_max_pixclk(state);
9160 int req_cdclk;
9161
9162 /* see the comment in valleyview_modeset_global_resources */
9163 if (WARN_ON(max_pixclk < 0))
9164 return;
9165
9166 req_cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
9167
9168 if (req_cdclk != dev_priv->cdclk_freq)
9169 broxton_set_cdclk(dev, req_cdclk);
9170}
9171
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009172static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9173 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009174{
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009175 if (!intel_ddi_pll_select(crtc, crtc_state))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009176 return -EINVAL;
Daniel Vetter716c2e52014-06-25 22:02:02 +03009177
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009178 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009179
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009180 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009181}
9182
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309183static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9184 enum port port,
9185 struct intel_crtc_state *pipe_config)
9186{
9187 switch (port) {
9188 case PORT_A:
9189 pipe_config->ddi_pll_sel = SKL_DPLL0;
9190 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9191 break;
9192 case PORT_B:
9193 pipe_config->ddi_pll_sel = SKL_DPLL1;
9194 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9195 break;
9196 case PORT_C:
9197 pipe_config->ddi_pll_sel = SKL_DPLL2;
9198 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9199 break;
9200 default:
9201 DRM_ERROR("Incorrect port type\n");
9202 }
9203}
9204
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009205static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9206 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009207 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009208{
Damien Lespiau3148ade2014-11-21 16:14:56 +00009209 u32 temp, dpll_ctl1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009210
9211 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9212 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9213
9214 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009215 case SKL_DPLL0:
9216 /*
9217 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9218 * of the shared DPLL framework and thus needs to be read out
9219 * separately
9220 */
9221 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9222 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9223 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009224 case SKL_DPLL1:
9225 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9226 break;
9227 case SKL_DPLL2:
9228 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9229 break;
9230 case SKL_DPLL3:
9231 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9232 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009233 }
9234}
9235
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009236static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9237 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009238 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009239{
9240 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9241
9242 switch (pipe_config->ddi_pll_sel) {
9243 case PORT_CLK_SEL_WRPLL1:
9244 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9245 break;
9246 case PORT_CLK_SEL_WRPLL2:
9247 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9248 break;
9249 }
9250}
9251
Daniel Vetter26804af2014-06-25 22:01:55 +03009252static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009253 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009254{
9255 struct drm_device *dev = crtc->base.dev;
9256 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009257 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009258 enum port port;
9259 uint32_t tmp;
9260
9261 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9262
9263 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9264
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009265 if (IS_SKYLAKE(dev))
9266 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309267 else if (IS_BROXTON(dev))
9268 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009269 else
9270 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009271
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009272 if (pipe_config->shared_dpll >= 0) {
9273 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9274
9275 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9276 &pipe_config->dpll_hw_state));
9277 }
9278
Daniel Vetter26804af2014-06-25 22:01:55 +03009279 /*
9280 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9281 * DDI E. So just check whether this pipe is wired to DDI E and whether
9282 * the PCH transcoder is on.
9283 */
Damien Lespiauca370452013-12-03 13:56:24 +00009284 if (INTEL_INFO(dev)->gen < 9 &&
9285 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009286 pipe_config->has_pch_encoder = true;
9287
9288 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9289 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9290 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9291
9292 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9293 }
9294}
9295
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009296static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009297 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009298{
9299 struct drm_device *dev = crtc->base.dev;
9300 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009301 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009302 uint32_t tmp;
9303
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009304 if (!intel_display_power_is_enabled(dev_priv,
Imre Deakb5482bd2014-03-05 16:20:55 +02009305 POWER_DOMAIN_PIPE(crtc->pipe)))
9306 return false;
9307
Daniel Vettere143a212013-07-04 12:01:15 +02009308 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009309 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9310
Daniel Vettereccb1402013-05-22 00:50:22 +02009311 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9312 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9313 enum pipe trans_edp_pipe;
9314 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9315 default:
9316 WARN(1, "unknown pipe linked to edp transcoder\n");
9317 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9318 case TRANS_DDI_EDP_INPUT_A_ON:
9319 trans_edp_pipe = PIPE_A;
9320 break;
9321 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9322 trans_edp_pipe = PIPE_B;
9323 break;
9324 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9325 trans_edp_pipe = PIPE_C;
9326 break;
9327 }
9328
9329 if (trans_edp_pipe == crtc->pipe)
9330 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9331 }
9332
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009333 if (!intel_display_power_is_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02009334 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03009335 return false;
9336
Daniel Vettereccb1402013-05-22 00:50:22 +02009337 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009338 if (!(tmp & PIPECONF_ENABLE))
9339 return false;
9340
Daniel Vetter26804af2014-06-25 22:01:55 +03009341 haswell_get_ddi_port_state(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009342
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009343 intel_get_pipe_timings(crtc, pipe_config);
9344
Chandra Kondurua1b22782015-04-07 15:28:45 -07009345 if (INTEL_INFO(dev)->gen >= 9) {
9346 skl_init_scalers(dev, crtc, pipe_config);
9347 }
9348
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009349 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009350 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009351 if (INTEL_INFO(dev)->gen == 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009352 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009353 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009354 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009355 else
9356 MISSING_CASE(INTEL_INFO(dev)->gen);
9357
Chandra Kondurua1b22782015-04-07 15:28:45 -07009358 } else {
9359 pipe_config->scaler_state.scaler_id = -1;
9360 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009361 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01009362
Jesse Barnese59150d2014-01-07 13:30:45 -08009363 if (IS_HASWELL(dev))
9364 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9365 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009366
Clint Taylorebb69c92014-09-30 10:30:22 -07009367 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
9368 pipe_config->pixel_multiplier =
9369 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9370 } else {
9371 pipe_config->pixel_multiplier = 1;
9372 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009373
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009374 return true;
9375}
9376
Chris Wilson560b85b2010-08-07 11:01:38 +01009377static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
9378{
9379 struct drm_device *dev = crtc->dev;
9380 struct drm_i915_private *dev_priv = dev->dev_private;
9381 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +03009382 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01009383
Ville Syrjälädc41c152014-08-13 11:57:05 +03009384 if (base) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009385 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
9386 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009387 unsigned int stride = roundup_pow_of_two(width) * 4;
9388
9389 switch (stride) {
9390 default:
9391 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9392 width, stride);
9393 stride = 256;
9394 /* fallthrough */
9395 case 256:
9396 case 512:
9397 case 1024:
9398 case 2048:
9399 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009400 }
9401
Ville Syrjälädc41c152014-08-13 11:57:05 +03009402 cntl |= CURSOR_ENABLE |
9403 CURSOR_GAMMA_ENABLE |
9404 CURSOR_FORMAT_ARGB |
9405 CURSOR_STRIDE(stride);
9406
9407 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009408 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009409
Ville Syrjälädc41c152014-08-13 11:57:05 +03009410 if (intel_crtc->cursor_cntl != 0 &&
9411 (intel_crtc->cursor_base != base ||
9412 intel_crtc->cursor_size != size ||
9413 intel_crtc->cursor_cntl != cntl)) {
9414 /* On these chipsets we can only modify the base/size/stride
9415 * whilst the cursor is disabled.
9416 */
9417 I915_WRITE(_CURACNTR, 0);
9418 POSTING_READ(_CURACNTR);
9419 intel_crtc->cursor_cntl = 0;
9420 }
9421
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009422 if (intel_crtc->cursor_base != base) {
Ville Syrjälädc41c152014-08-13 11:57:05 +03009423 I915_WRITE(_CURABASE, base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009424 intel_crtc->cursor_base = base;
9425 }
Ville Syrjälädc41c152014-08-13 11:57:05 +03009426
9427 if (intel_crtc->cursor_size != size) {
9428 I915_WRITE(CURSIZE, size);
9429 intel_crtc->cursor_size = size;
9430 }
9431
Chris Wilson4b0e3332014-05-30 16:35:26 +03009432 if (intel_crtc->cursor_cntl != cntl) {
9433 I915_WRITE(_CURACNTR, cntl);
9434 POSTING_READ(_CURACNTR);
9435 intel_crtc->cursor_cntl = cntl;
9436 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009437}
9438
9439static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
9440{
9441 struct drm_device *dev = crtc->dev;
9442 struct drm_i915_private *dev_priv = dev->dev_private;
9443 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9444 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009445 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +01009446
Chris Wilson4b0e3332014-05-30 16:35:26 +03009447 cntl = 0;
9448 if (base) {
9449 cntl = MCURSOR_GAMMA_ENABLE;
Matt Roper3dd512f2015-02-27 10:12:00 -08009450 switch (intel_crtc->base.cursor->state->crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309451 case 64:
9452 cntl |= CURSOR_MODE_64_ARGB_AX;
9453 break;
9454 case 128:
9455 cntl |= CURSOR_MODE_128_ARGB_AX;
9456 break;
9457 case 256:
9458 cntl |= CURSOR_MODE_256_ARGB_AX;
9459 break;
9460 default:
Matt Roper3dd512f2015-02-27 10:12:00 -08009461 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309462 return;
Chris Wilson560b85b2010-08-07 11:01:38 +01009463 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009464 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +03009465
9466 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
9467 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson560b85b2010-08-07 11:01:38 +01009468 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009469
Matt Roper8e7d6882015-01-21 16:35:41 -08009470 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009471 cntl |= CURSOR_ROTATE_180;
9472
Chris Wilson4b0e3332014-05-30 16:35:26 +03009473 if (intel_crtc->cursor_cntl != cntl) {
9474 I915_WRITE(CURCNTR(pipe), cntl);
9475 POSTING_READ(CURCNTR(pipe));
9476 intel_crtc->cursor_cntl = cntl;
9477 }
9478
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009479 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009480 I915_WRITE(CURBASE(pipe), base);
9481 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009482
9483 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009484}
9485
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009486/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01009487static void intel_crtc_update_cursor(struct drm_crtc *crtc,
9488 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009489{
9490 struct drm_device *dev = crtc->dev;
9491 struct drm_i915_private *dev_priv = dev->dev_private;
9492 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9493 int pipe = intel_crtc->pipe;
Matt Roper3d7d6512014-06-10 08:28:13 -07009494 int x = crtc->cursor_x;
9495 int y = crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009496 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009497
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009498 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009499 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009500
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009501 if (x >= intel_crtc->config->pipe_src_w)
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009502 base = 0;
9503
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009504 if (y >= intel_crtc->config->pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009505 base = 0;
9506
9507 if (x < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009508 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009509 base = 0;
9510
9511 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
9512 x = -x;
9513 }
9514 pos |= x << CURSOR_X_SHIFT;
9515
9516 if (y < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009517 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009518 base = 0;
9519
9520 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
9521 y = -y;
9522 }
9523 pos |= y << CURSOR_Y_SHIFT;
9524
Chris Wilson4b0e3332014-05-30 16:35:26 +03009525 if (base == 0 && intel_crtc->cursor_base == 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009526 return;
9527
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009528 I915_WRITE(CURPOS(pipe), pos);
9529
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009530 /* ILK+ do this automagically */
9531 if (HAS_GMCH_DISPLAY(dev) &&
Matt Roper8e7d6882015-01-21 16:35:41 -08009532 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009533 base += (intel_crtc->base.cursor->state->crtc_h *
9534 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009535 }
9536
Ville Syrjälä8ac54662014-08-12 19:39:54 +03009537 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009538 i845_update_cursor(crtc, base);
9539 else
9540 i9xx_update_cursor(crtc, base);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009541}
9542
Ville Syrjälädc41c152014-08-13 11:57:05 +03009543static bool cursor_size_ok(struct drm_device *dev,
9544 uint32_t width, uint32_t height)
9545{
9546 if (width == 0 || height == 0)
9547 return false;
9548
9549 /*
9550 * 845g/865g are special in that they are only limited by
9551 * the width of their cursors, the height is arbitrary up to
9552 * the precision of the register. Everything else requires
9553 * square cursors, limited to a few power-of-two sizes.
9554 */
9555 if (IS_845G(dev) || IS_I865G(dev)) {
9556 if ((width & 63) != 0)
9557 return false;
9558
9559 if (width > (IS_845G(dev) ? 64 : 512))
9560 return false;
9561
9562 if (height > 1023)
9563 return false;
9564 } else {
9565 switch (width | height) {
9566 case 256:
9567 case 128:
9568 if (IS_GEN2(dev))
9569 return false;
9570 case 64:
9571 break;
9572 default:
9573 return false;
9574 }
9575 }
9576
9577 return true;
9578}
9579
Jesse Barnes79e53942008-11-07 14:24:08 -08009580static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01009581 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08009582{
James Simmons72034252010-08-03 01:33:19 +01009583 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08009584 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08009585
James Simmons72034252010-08-03 01:33:19 +01009586 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009587 intel_crtc->lut_r[i] = red[i] >> 8;
9588 intel_crtc->lut_g[i] = green[i] >> 8;
9589 intel_crtc->lut_b[i] = blue[i] >> 8;
9590 }
9591
9592 intel_crtc_load_lut(crtc);
9593}
9594
Jesse Barnes79e53942008-11-07 14:24:08 -08009595/* VESA 640x480x72Hz mode to set on the pipe */
9596static struct drm_display_mode load_detect_mode = {
9597 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
9598 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
9599};
9600
Daniel Vettera8bb6812014-02-10 18:00:39 +01009601struct drm_framebuffer *
9602__intel_framebuffer_create(struct drm_device *dev,
9603 struct drm_mode_fb_cmd2 *mode_cmd,
9604 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +01009605{
9606 struct intel_framebuffer *intel_fb;
9607 int ret;
9608
9609 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
9610 if (!intel_fb) {
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +03009611 drm_gem_object_unreference(&obj->base);
Chris Wilsond2dff872011-04-19 08:36:26 +01009612 return ERR_PTR(-ENOMEM);
9613 }
9614
9615 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009616 if (ret)
9617 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +01009618
9619 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009620err:
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +03009621 drm_gem_object_unreference(&obj->base);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009622 kfree(intel_fb);
9623
9624 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +01009625}
9626
Daniel Vetterb5ea6422014-03-02 21:18:00 +01009627static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +01009628intel_framebuffer_create(struct drm_device *dev,
9629 struct drm_mode_fb_cmd2 *mode_cmd,
9630 struct drm_i915_gem_object *obj)
9631{
9632 struct drm_framebuffer *fb;
9633 int ret;
9634
9635 ret = i915_mutex_lock_interruptible(dev);
9636 if (ret)
9637 return ERR_PTR(ret);
9638 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
9639 mutex_unlock(&dev->struct_mutex);
9640
9641 return fb;
9642}
9643
Chris Wilsond2dff872011-04-19 08:36:26 +01009644static u32
9645intel_framebuffer_pitch_for_width(int width, int bpp)
9646{
9647 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
9648 return ALIGN(pitch, 64);
9649}
9650
9651static u32
9652intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
9653{
9654 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +02009655 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +01009656}
9657
9658static struct drm_framebuffer *
9659intel_framebuffer_create_for_mode(struct drm_device *dev,
9660 struct drm_display_mode *mode,
9661 int depth, int bpp)
9662{
9663 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00009664 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01009665
9666 obj = i915_gem_alloc_object(dev,
9667 intel_framebuffer_size_for_mode(mode, bpp));
9668 if (obj == NULL)
9669 return ERR_PTR(-ENOMEM);
9670
9671 mode_cmd.width = mode->hdisplay;
9672 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009673 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
9674 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00009675 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01009676
9677 return intel_framebuffer_create(dev, &mode_cmd, obj);
9678}
9679
9680static struct drm_framebuffer *
9681mode_fits_in_fbdev(struct drm_device *dev,
9682 struct drm_display_mode *mode)
9683{
Daniel Vetter4520f532013-10-09 09:18:51 +02009684#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +01009685 struct drm_i915_private *dev_priv = dev->dev_private;
9686 struct drm_i915_gem_object *obj;
9687 struct drm_framebuffer *fb;
9688
Daniel Vetter4c0e5522014-02-14 16:35:54 +01009689 if (!dev_priv->fbdev)
9690 return NULL;
9691
9692 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +01009693 return NULL;
9694
Jesse Barnes8bcd4552014-02-07 12:10:38 -08009695 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +01009696 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +01009697
Jesse Barnes8bcd4552014-02-07 12:10:38 -08009698 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009699 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
9700 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01009701 return NULL;
9702
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009703 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01009704 return NULL;
9705
9706 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +02009707#else
9708 return NULL;
9709#endif
Chris Wilsond2dff872011-04-19 08:36:26 +01009710}
9711
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009712bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01009713 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -05009714 struct intel_load_detect_pipe *old,
9715 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08009716{
9717 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009718 struct intel_encoder *intel_encoder =
9719 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08009720 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01009721 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08009722 struct drm_crtc *crtc = NULL;
9723 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02009724 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -05009725 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009726 struct drm_atomic_state *state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009727 struct drm_connector_state *connector_state;
Rob Clark51fd3712013-11-19 12:10:12 -05009728 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -08009729
Chris Wilsond2dff872011-04-19 08:36:26 +01009730 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03009731 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03009732 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01009733
Rob Clark51fd3712013-11-19 12:10:12 -05009734retry:
9735 ret = drm_modeset_lock(&config->connection_mutex, ctx);
9736 if (ret)
9737 goto fail_unlock;
Daniel Vetter6e9f7982014-05-29 23:54:47 +02009738
Jesse Barnes79e53942008-11-07 14:24:08 -08009739 /*
9740 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01009741 *
Jesse Barnes79e53942008-11-07 14:24:08 -08009742 * - if the connector already has an assigned crtc, use it (but make
9743 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01009744 *
Jesse Barnes79e53942008-11-07 14:24:08 -08009745 * - try to find the first unused crtc that can drive this connector,
9746 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08009747 */
9748
9749 /* See if we already have a CRTC for this connector */
9750 if (encoder->crtc) {
9751 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01009752
Rob Clark51fd3712013-11-19 12:10:12 -05009753 ret = drm_modeset_lock(&crtc->mutex, ctx);
9754 if (ret)
9755 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01009756 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
9757 if (ret)
9758 goto fail_unlock;
Daniel Vetter7b240562012-12-12 00:35:33 +01009759
Daniel Vetter24218aa2012-08-12 19:27:11 +02009760 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01009761 old->load_detect_temp = false;
9762
9763 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02009764 if (connector->dpms != DRM_MODE_DPMS_ON)
9765 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01009766
Chris Wilson71731882011-04-19 23:10:58 +01009767 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08009768 }
9769
9770 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01009771 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009772 i++;
9773 if (!(encoder->possible_crtcs & (1 << i)))
9774 continue;
Matt Roper83d65732015-02-25 13:12:16 -08009775 if (possible_crtc->state->enable)
Ville Syrjäläa4592492014-08-11 13:15:36 +03009776 continue;
9777 /* This can occur when applying the pipe A quirk on resume. */
9778 if (to_intel_crtc(possible_crtc)->new_enabled)
9779 continue;
9780
9781 crtc = possible_crtc;
9782 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08009783 }
9784
9785 /*
9786 * If we didn't find an unused CRTC, don't use any.
9787 */
9788 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01009789 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Rob Clark51fd3712013-11-19 12:10:12 -05009790 goto fail_unlock;
Jesse Barnes79e53942008-11-07 14:24:08 -08009791 }
9792
Rob Clark51fd3712013-11-19 12:10:12 -05009793 ret = drm_modeset_lock(&crtc->mutex, ctx);
9794 if (ret)
9795 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01009796 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
9797 if (ret)
9798 goto fail_unlock;
Daniel Vetterfc303102012-07-09 10:40:58 +02009799 intel_encoder->new_crtc = to_intel_crtc(crtc);
9800 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08009801
9802 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009803 intel_crtc->new_enabled = true;
Daniel Vetter24218aa2012-08-12 19:27:11 +02009804 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01009805 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01009806 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08009807
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009808 state = drm_atomic_state_alloc(dev);
9809 if (!state)
9810 return false;
9811
9812 state->acquire_ctx = ctx;
9813
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009814 connector_state = drm_atomic_get_connector_state(state, connector);
9815 if (IS_ERR(connector_state)) {
9816 ret = PTR_ERR(connector_state);
9817 goto fail;
9818 }
9819
9820 connector_state->crtc = crtc;
9821 connector_state->best_encoder = &intel_encoder->base;
9822
Chris Wilson64927112011-04-20 07:25:26 +01009823 if (!mode)
9824 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08009825
Chris Wilsond2dff872011-04-19 08:36:26 +01009826 /* We need a framebuffer large enough to accommodate all accesses
9827 * that the plane may generate whilst we perform load detection.
9828 * We can not rely on the fbcon either being present (we get called
9829 * during its initialisation to detect all boot displays, or it may
9830 * not even exist) or that it is large enough to satisfy the
9831 * requested mode.
9832 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02009833 fb = mode_fits_in_fbdev(dev, mode);
9834 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01009835 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02009836 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
9837 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01009838 } else
9839 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02009840 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01009841 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009842 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08009843 }
Chris Wilsond2dff872011-04-19 08:36:26 +01009844
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009845 if (intel_set_mode(crtc, mode, 0, 0, fb, state)) {
Chris Wilson64927112011-04-20 07:25:26 +01009846 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01009847 if (old->release_fb)
9848 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009849 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08009850 }
Daniel Vetter9128b042015-03-03 17:31:21 +01009851 crtc->primary->crtc = crtc;
Chris Wilson71731882011-04-19 23:10:58 +01009852
Jesse Barnes79e53942008-11-07 14:24:08 -08009853 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07009854 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01009855 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009856
9857 fail:
Matt Roper83d65732015-02-25 13:12:16 -08009858 intel_crtc->new_enabled = crtc->state->enable;
Rob Clark51fd3712013-11-19 12:10:12 -05009859fail_unlock:
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009860 if (state) {
9861 drm_atomic_state_free(state);
9862 state = NULL;
9863 }
9864
Rob Clark51fd3712013-11-19 12:10:12 -05009865 if (ret == -EDEADLK) {
9866 drm_modeset_backoff(ctx);
9867 goto retry;
9868 }
9869
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009870 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08009871}
9872
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009873void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +02009874 struct intel_load_detect_pipe *old,
9875 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08009876{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009877 struct drm_device *dev = connector->dev;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009878 struct intel_encoder *intel_encoder =
9879 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01009880 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01009881 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009882 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009883 struct drm_atomic_state *state;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009884 struct drm_connector_state *connector_state;
Jesse Barnes79e53942008-11-07 14:24:08 -08009885
Chris Wilsond2dff872011-04-19 08:36:26 +01009886 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03009887 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03009888 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01009889
Chris Wilson8261b192011-04-19 23:18:09 +01009890 if (old->load_detect_temp) {
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009891 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009892 if (!state)
9893 goto fail;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009894
9895 state->acquire_ctx = ctx;
9896
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009897 connector_state = drm_atomic_get_connector_state(state, connector);
9898 if (IS_ERR(connector_state))
9899 goto fail;
9900
Daniel Vetterfc303102012-07-09 10:40:58 +02009901 to_intel_connector(connector)->new_encoder = NULL;
9902 intel_encoder->new_crtc = NULL;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009903 intel_crtc->new_enabled = false;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009904
9905 connector_state->best_encoder = NULL;
9906 connector_state->crtc = NULL;
9907
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009908 intel_set_mode(crtc, NULL, 0, 0, NULL, state);
9909
9910 drm_atomic_state_free(state);
Chris Wilsond2dff872011-04-19 08:36:26 +01009911
Daniel Vetter36206362012-12-10 20:42:17 +01009912 if (old->release_fb) {
9913 drm_framebuffer_unregister_private(old->release_fb);
9914 drm_framebuffer_unreference(old->release_fb);
9915 }
Chris Wilsond2dff872011-04-19 08:36:26 +01009916
Chris Wilson0622a532011-04-21 09:32:11 +01009917 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08009918 }
9919
Eric Anholtc751ce42010-03-25 11:48:48 -07009920 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02009921 if (old->dpms_mode != DRM_MODE_DPMS_ON)
9922 connector->funcs->dpms(connector, old->dpms_mode);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009923
9924 return;
9925fail:
9926 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
9927 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -08009928}
9929
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009930static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009931 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009932{
9933 struct drm_i915_private *dev_priv = dev->dev_private;
9934 u32 dpll = pipe_config->dpll_hw_state.dpll;
9935
9936 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +02009937 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009938 else if (HAS_PCH_SPLIT(dev))
9939 return 120000;
9940 else if (!IS_GEN2(dev))
9941 return 96000;
9942 else
9943 return 48000;
9944}
9945
Jesse Barnes79e53942008-11-07 14:24:08 -08009946/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009947static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009948 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08009949{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009950 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08009951 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009952 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +03009953 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08009954 u32 fp;
9955 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009956 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -08009957
9958 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +03009959 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009960 else
Ville Syrjälä293623f2013-09-13 16:18:46 +03009961 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -08009962
9963 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05009964 if (IS_PINEVIEW(dev)) {
9965 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
9966 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08009967 } else {
9968 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
9969 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
9970 }
9971
Chris Wilsona6c45cf2010-09-17 00:32:17 +01009972 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05009973 if (IS_PINEVIEW(dev))
9974 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
9975 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08009976 else
9977 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08009978 DPLL_FPA01_P1_POST_DIV_SHIFT);
9979
9980 switch (dpll & DPLL_MODE_MASK) {
9981 case DPLLB_MODE_DAC_SERIAL:
9982 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
9983 5 : 10;
9984 break;
9985 case DPLLB_MODE_LVDS:
9986 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
9987 7 : 14;
9988 break;
9989 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08009990 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08009991 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009992 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08009993 }
9994
Daniel Vetterac58c3f2013-06-01 17:16:17 +02009995 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009996 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +02009997 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009998 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08009999 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010000 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010001 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010002
10003 if (is_lvds) {
10004 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10005 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010006
10007 if (lvds & LVDS_CLKB_POWER_UP)
10008 clock.p2 = 7;
10009 else
10010 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010011 } else {
10012 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10013 clock.p1 = 2;
10014 else {
10015 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10016 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10017 }
10018 if (dpll & PLL_P2_DIVIDE_BY_4)
10019 clock.p2 = 4;
10020 else
10021 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010022 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010023
10024 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010025 }
10026
Ville Syrjälä18442d02013-09-13 16:00:08 +030010027 /*
10028 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010029 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010030 * encoder's get_config() function.
10031 */
10032 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010033}
10034
Ville Syrjälä6878da02013-09-13 15:59:11 +030010035int intel_dotclock_calculate(int link_freq,
10036 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010037{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010038 /*
10039 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010040 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010041 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010042 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010043 *
10044 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010045 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010046 */
10047
Ville Syrjälä6878da02013-09-13 15:59:11 +030010048 if (!m_n->link_n)
10049 return 0;
10050
10051 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10052}
10053
Ville Syrjälä18442d02013-09-13 16:00:08 +030010054static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010055 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010056{
10057 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +030010058
10059 /* read out port_clock from the DPLL */
10060 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010061
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010062 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +030010063 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +010010064 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +030010065 * agree once we know their relationship in the encoder's
10066 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010067 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010068 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +030010069 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10070 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010071}
10072
10073/** Returns the currently programmed mode of the given pipe. */
10074struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10075 struct drm_crtc *crtc)
10076{
Jesse Barnes548f2452011-02-17 10:40:53 -080010077 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010078 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010079 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010080 struct drm_display_mode *mode;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010081 struct intel_crtc_state pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010082 int htot = I915_READ(HTOTAL(cpu_transcoder));
10083 int hsync = I915_READ(HSYNC(cpu_transcoder));
10084 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10085 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010086 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010087
10088 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10089 if (!mode)
10090 return NULL;
10091
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010092 /*
10093 * Construct a pipe_config sufficient for getting the clock info
10094 * back out of crtc_clock_get.
10095 *
10096 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10097 * to use a real value here instead.
10098 */
Ville Syrjälä293623f2013-09-13 16:18:46 +030010099 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010100 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010101 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10102 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10103 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010104 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10105
Ville Syrjälä773ae032013-09-23 17:48:20 +030010106 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010107 mode->hdisplay = (htot & 0xffff) + 1;
10108 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10109 mode->hsync_start = (hsync & 0xffff) + 1;
10110 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10111 mode->vdisplay = (vtot & 0xffff) + 1;
10112 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10113 mode->vsync_start = (vsync & 0xffff) + 1;
10114 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10115
10116 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010117
10118 return mode;
10119}
10120
Jesse Barnes652c3932009-08-17 13:31:43 -070010121static void intel_decrease_pllclock(struct drm_crtc *crtc)
10122{
10123 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030010124 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -070010125 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -070010126
Sonika Jindalbaff2962014-07-22 11:16:35 +053010127 if (!HAS_GMCH_DISPLAY(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -070010128 return;
10129
10130 if (!dev_priv->lvds_downclock_avail)
10131 return;
10132
10133 /*
10134 * Since this is called by a timer, we should never get here in
10135 * the manual case.
10136 */
10137 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +010010138 int pipe = intel_crtc->pipe;
10139 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +020010140 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +010010141
Zhao Yakui44d98a62009-10-09 11:39:40 +080010142 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -070010143
Sean Paul8ac5a6d2012-02-13 13:14:51 -050010144 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -070010145
Chris Wilson074b5e12012-05-02 12:07:06 +010010146 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -070010147 dpll |= DISPLAY_RATE_SELECT_FPA1;
10148 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010149 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -070010150 dpll = I915_READ(dpll_reg);
10151 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +080010152 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -070010153 }
10154
10155}
10156
Chris Wilsonf047e392012-07-21 12:31:41 +010010157void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010158{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010159 struct drm_i915_private *dev_priv = dev->dev_private;
10160
Chris Wilsonf62a0072014-02-21 17:55:39 +000010161 if (dev_priv->mm.busy)
10162 return;
10163
Paulo Zanoni43694d62014-03-07 20:08:08 -030010164 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010165 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010166 if (INTEL_INFO(dev)->gen >= 6)
10167 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010168 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010169}
10170
10171void intel_mark_idle(struct drm_device *dev)
10172{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010173 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010174 struct drm_crtc *crtc;
10175
Chris Wilsonf62a0072014-02-21 17:55:39 +000010176 if (!dev_priv->mm.busy)
10177 return;
10178
10179 dev_priv->mm.busy = false;
10180
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010181 for_each_crtc(dev, crtc) {
Matt Roperf4510a22014-04-01 15:22:40 -070010182 if (!crtc->primary->fb)
Chris Wilson725a5b52013-01-08 11:02:57 +000010183 continue;
10184
10185 intel_decrease_pllclock(crtc);
10186 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010187
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010188 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010189 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010190
Paulo Zanoni43694d62014-03-07 20:08:08 -030010191 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010192}
10193
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020010194static void intel_crtc_set_state(struct intel_crtc *crtc,
10195 struct intel_crtc_state *crtc_state)
10196{
10197 kfree(crtc->config);
10198 crtc->config = crtc_state;
Ander Conselvan de Oliveira16f3f652015-01-15 14:55:27 +020010199 crtc->base.state = &crtc_state->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020010200}
10201
Jesse Barnes79e53942008-11-07 14:24:08 -080010202static void intel_crtc_destroy(struct drm_crtc *crtc)
10203{
10204 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010205 struct drm_device *dev = crtc->dev;
10206 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010207
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010208 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010209 work = intel_crtc->unpin_work;
10210 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010211 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010212
10213 if (work) {
10214 cancel_work_sync(&work->work);
10215 kfree(work);
10216 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010217
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020010218 intel_crtc_set_state(intel_crtc, NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -080010219 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010220
Jesse Barnes79e53942008-11-07 14:24:08 -080010221 kfree(intel_crtc);
10222}
10223
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010224static void intel_unpin_work_fn(struct work_struct *__work)
10225{
10226 struct intel_unpin_work *work =
10227 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010228 struct drm_device *dev = work->crtc->dev;
Daniel Vetterf99d7062014-06-19 16:01:59 +020010229 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010230
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010231 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000010232 intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
Chris Wilson05394f32010-11-08 19:18:58 +000010233 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010234
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020010235 intel_fbc_update(dev);
John Harrisonf06cc1b2014-11-24 18:49:37 +000010236
10237 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010238 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010239 mutex_unlock(&dev->struct_mutex);
10240
Daniel Vetterf99d7062014-06-19 16:01:59 +020010241 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Chris Wilson89ed88b2015-02-16 14:31:49 +000010242 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010243
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010244 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
10245 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
10246
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010247 kfree(work);
10248}
10249
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010250static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010010251 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010252{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010253 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10254 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010255 unsigned long flags;
10256
10257 /* Ignore early vblank irqs */
10258 if (intel_crtc == NULL)
10259 return;
10260
Daniel Vetterf3260382014-09-15 14:55:23 +020010261 /*
10262 * This is called both by irq handlers and the reset code (to complete
10263 * lost pageflips) so needs the full irqsave spinlocks.
10264 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010265 spin_lock_irqsave(&dev->event_lock, flags);
10266 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000010267
10268 /* Ensure we don't miss a work->pending update ... */
10269 smp_rmb();
10270
10271 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010272 spin_unlock_irqrestore(&dev->event_lock, flags);
10273 return;
10274 }
10275
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010276 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010010277
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010278 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010279}
10280
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010281void intel_finish_page_flip(struct drm_device *dev, int pipe)
10282{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010283 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010284 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10285
Mario Kleiner49b14a52010-12-09 07:00:07 +010010286 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010287}
10288
10289void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10290{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010291 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010292 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10293
Mario Kleiner49b14a52010-12-09 07:00:07 +010010294 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010295}
10296
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010297/* Is 'a' after or equal to 'b'? */
10298static bool g4x_flip_count_after_eq(u32 a, u32 b)
10299{
10300 return !((a - b) & 0x80000000);
10301}
10302
10303static bool page_flip_finished(struct intel_crtc *crtc)
10304{
10305 struct drm_device *dev = crtc->base.dev;
10306 struct drm_i915_private *dev_priv = dev->dev_private;
10307
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030010308 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10309 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10310 return true;
10311
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010312 /*
10313 * The relevant registers doen't exist on pre-ctg.
10314 * As the flip done interrupt doesn't trigger for mmio
10315 * flips on gmch platforms, a flip count check isn't
10316 * really needed there. But since ctg has the registers,
10317 * include it in the check anyway.
10318 */
10319 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10320 return true;
10321
10322 /*
10323 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10324 * used the same base address. In that case the mmio flip might
10325 * have completed, but the CS hasn't even executed the flip yet.
10326 *
10327 * A flip count check isn't enough as the CS might have updated
10328 * the base address just after start of vblank, but before we
10329 * managed to process the interrupt. This means we'd complete the
10330 * CS flip too soon.
10331 *
10332 * Combining both checks should get us a good enough result. It may
10333 * still happen that the CS flip has been executed, but has not
10334 * yet actually completed. But in case the base address is the same
10335 * anyway, we don't really care.
10336 */
10337 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10338 crtc->unpin_work->gtt_offset &&
10339 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
10340 crtc->unpin_work->flip_count);
10341}
10342
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010343void intel_prepare_page_flip(struct drm_device *dev, int plane)
10344{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010345 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010346 struct intel_crtc *intel_crtc =
10347 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10348 unsigned long flags;
10349
Daniel Vetterf3260382014-09-15 14:55:23 +020010350
10351 /*
10352 * This is called both by irq handlers and the reset code (to complete
10353 * lost pageflips) so needs the full irqsave spinlocks.
10354 *
10355 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000010356 * generate a page-flip completion irq, i.e. every modeset
10357 * is also accompanied by a spurious intel_prepare_page_flip().
10358 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010359 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010360 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000010361 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010362 spin_unlock_irqrestore(&dev->event_lock, flags);
10363}
10364
Robin Schroereba905b2014-05-18 02:24:50 +020010365static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
Chris Wilsone7d841c2012-12-03 11:36:30 +000010366{
10367 /* Ensure that the work item is consistent when activating it ... */
10368 smp_wmb();
10369 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
10370 /* and that it is marked active as soon as the irq could fire. */
10371 smp_wmb();
10372}
10373
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010374static int intel_gen2_queue_flip(struct drm_device *dev,
10375 struct drm_crtc *crtc,
10376 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010377 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010378 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010379 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010380{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010381 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010382 u32 flip_mask;
10383 int ret;
10384
Daniel Vetter6d90c952012-04-26 23:28:05 +020010385 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010386 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010387 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010388
10389 /* Can't queue multiple flips, so wait for the previous
10390 * one to finish before executing the next.
10391 */
10392 if (intel_crtc->plane)
10393 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10394 else
10395 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010396 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10397 intel_ring_emit(ring, MI_NOOP);
10398 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10399 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10400 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010401 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010402 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000010403
10404 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010405 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010406 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010407}
10408
10409static int intel_gen3_queue_flip(struct drm_device *dev,
10410 struct drm_crtc *crtc,
10411 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010412 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010413 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010414 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010415{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010416 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010417 u32 flip_mask;
10418 int ret;
10419
Daniel Vetter6d90c952012-04-26 23:28:05 +020010420 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010421 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010422 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010423
10424 if (intel_crtc->plane)
10425 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10426 else
10427 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010428 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10429 intel_ring_emit(ring, MI_NOOP);
10430 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
10431 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10432 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010433 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010434 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010435
Chris Wilsone7d841c2012-12-03 11:36:30 +000010436 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010437 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010438 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010439}
10440
10441static int intel_gen4_queue_flip(struct drm_device *dev,
10442 struct drm_crtc *crtc,
10443 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010444 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010445 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010446 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010447{
10448 struct drm_i915_private *dev_priv = dev->dev_private;
10449 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10450 uint32_t pf, pipesrc;
10451 int ret;
10452
Daniel Vetter6d90c952012-04-26 23:28:05 +020010453 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010454 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010455 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010456
10457 /* i965+ uses the linear or tiled offsets from the
10458 * Display Registers (which do not change across a page-flip)
10459 * so we need only reprogram the base address.
10460 */
Daniel Vetter6d90c952012-04-26 23:28:05 +020010461 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10462 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10463 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010464 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020010465 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010466
10467 /* XXX Enabling the panel-fitter across page-flip is so far
10468 * untested on non-native modes, so ignore it for now.
10469 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
10470 */
10471 pf = 0;
10472 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010473 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010474
10475 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010476 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010477 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010478}
10479
10480static int intel_gen6_queue_flip(struct drm_device *dev,
10481 struct drm_crtc *crtc,
10482 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010483 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010484 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010485 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010486{
10487 struct drm_i915_private *dev_priv = dev->dev_private;
10488 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10489 uint32_t pf, pipesrc;
10490 int ret;
10491
Daniel Vetter6d90c952012-04-26 23:28:05 +020010492 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010493 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010494 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010495
Daniel Vetter6d90c952012-04-26 23:28:05 +020010496 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10497 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10498 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010499 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010500
Chris Wilson99d9acd2012-04-17 20:37:00 +010010501 /* Contrary to the suggestions in the documentation,
10502 * "Enable Panel Fitter" does not seem to be required when page
10503 * flipping with a non-native mode, and worse causes a normal
10504 * modeset to fail.
10505 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
10506 */
10507 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010508 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010509 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010510
10511 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010512 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010513 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010514}
10515
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010516static int intel_gen7_queue_flip(struct drm_device *dev,
10517 struct drm_crtc *crtc,
10518 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010519 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010520 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010521 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010522{
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010523 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010524 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010010525 int len, ret;
10526
Robin Schroereba905b2014-05-18 02:24:50 +020010527 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010528 case PLANE_A:
10529 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
10530 break;
10531 case PLANE_B:
10532 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
10533 break;
10534 case PLANE_C:
10535 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
10536 break;
10537 default:
10538 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010539 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010540 }
10541
Chris Wilsonffe74d72013-08-26 20:58:12 +010010542 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +010010543 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010010544 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010010545 /*
10546 * On Gen 8, SRM is now taking an extra dword to accommodate
10547 * 48bits addresses, and we need a NOOP for the batch size to
10548 * stay even.
10549 */
10550 if (IS_GEN8(dev))
10551 len += 2;
10552 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010010553
Ville Syrjäläf66fab82014-02-11 19:52:06 +020010554 /*
10555 * BSpec MI_DISPLAY_FLIP for IVB:
10556 * "The full packet must be contained within the same cache line."
10557 *
10558 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
10559 * cacheline, if we ever start emitting more commands before
10560 * the MI_DISPLAY_FLIP we may need to first emit everything else,
10561 * then do the cacheline alignment, and finally emit the
10562 * MI_DISPLAY_FLIP.
10563 */
10564 ret = intel_ring_cacheline_align(ring);
10565 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010566 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020010567
Chris Wilsonffe74d72013-08-26 20:58:12 +010010568 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010569 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010570 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010571
Chris Wilsonffe74d72013-08-26 20:58:12 +010010572 /* Unmask the flip-done completion message. Note that the bspec says that
10573 * we should do this for both the BCS and RCS, and that we must not unmask
10574 * more than one flip event at any time (or ensure that one flip message
10575 * can be sent by waiting for flip-done prior to queueing new flips).
10576 * Experimentation says that BCS works despite DERRMR masking all
10577 * flip-done completion events and that unmasking all planes at once
10578 * for the RCS also doesn't appear to drop events. Setting the DERRMR
10579 * to zero does lead to lockups within MI_DISPLAY_FLIP.
10580 */
10581 if (ring->id == RCS) {
10582 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
10583 intel_ring_emit(ring, DERRMR);
10584 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
10585 DERRMR_PIPEB_PRI_FLIP_DONE |
10586 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010010587 if (IS_GEN8(dev))
10588 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
10589 MI_SRM_LRM_GLOBAL_GTT);
10590 else
10591 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
10592 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +010010593 intel_ring_emit(ring, DERRMR);
10594 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010010595 if (IS_GEN8(dev)) {
10596 intel_ring_emit(ring, 0);
10597 intel_ring_emit(ring, MI_NOOP);
10598 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010010599 }
10600
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010601 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010602 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010603 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010604 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000010605
10606 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010607 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010608 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010609}
10610
Sourab Gupta84c33a62014-06-02 16:47:17 +053010611static bool use_mmio_flip(struct intel_engine_cs *ring,
10612 struct drm_i915_gem_object *obj)
10613{
10614 /*
10615 * This is not being used for older platforms, because
10616 * non-availability of flip done interrupt forces us to use
10617 * CS flips. Older platforms derive flip done using some clever
10618 * tricks involving the flip_pending status bits and vblank irqs.
10619 * So using MMIO flips there would disrupt this mechanism.
10620 */
10621
Chris Wilson8e09bf82014-07-08 10:40:30 +010010622 if (ring == NULL)
10623 return true;
10624
Sourab Gupta84c33a62014-06-02 16:47:17 +053010625 if (INTEL_INFO(ring->dev)->gen < 5)
10626 return false;
10627
10628 if (i915.use_mmio_flip < 0)
10629 return false;
10630 else if (i915.use_mmio_flip > 0)
10631 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010010632 else if (i915.enable_execlists)
10633 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053010634 else
John Harrison41c52412014-11-24 18:49:43 +000010635 return ring != i915_gem_request_get_ring(obj->last_read_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010636}
10637
Damien Lespiauff944562014-11-20 14:58:16 +000010638static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
10639{
10640 struct drm_device *dev = intel_crtc->base.dev;
10641 struct drm_i915_private *dev_priv = dev->dev_private;
10642 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000010643 const enum pipe pipe = intel_crtc->pipe;
10644 u32 ctl, stride;
10645
10646 ctl = I915_READ(PLANE_CTL(pipe, 0));
10647 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010010648 switch (fb->modifier[0]) {
10649 case DRM_FORMAT_MOD_NONE:
10650 break;
10651 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000010652 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010010653 break;
10654 case I915_FORMAT_MOD_Y_TILED:
10655 ctl |= PLANE_CTL_TILED_Y;
10656 break;
10657 case I915_FORMAT_MOD_Yf_TILED:
10658 ctl |= PLANE_CTL_TILED_YF;
10659 break;
10660 default:
10661 MISSING_CASE(fb->modifier[0]);
10662 }
Damien Lespiauff944562014-11-20 14:58:16 +000010663
10664 /*
10665 * The stride is either expressed as a multiple of 64 bytes chunks for
10666 * linear buffers or in number of tiles for tiled buffers.
10667 */
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010010668 stride = fb->pitches[0] /
10669 intel_fb_stride_alignment(dev, fb->modifier[0],
10670 fb->pixel_format);
Damien Lespiauff944562014-11-20 14:58:16 +000010671
10672 /*
10673 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
10674 * PLANE_SURF updates, the update is then guaranteed to be atomic.
10675 */
10676 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
10677 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
10678
10679 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
10680 POSTING_READ(PLANE_SURF(pipe, 0));
10681}
10682
10683static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
Sourab Gupta84c33a62014-06-02 16:47:17 +053010684{
10685 struct drm_device *dev = intel_crtc->base.dev;
10686 struct drm_i915_private *dev_priv = dev->dev_private;
10687 struct intel_framebuffer *intel_fb =
10688 to_intel_framebuffer(intel_crtc->base.primary->fb);
10689 struct drm_i915_gem_object *obj = intel_fb->obj;
10690 u32 dspcntr;
10691 u32 reg;
10692
Sourab Gupta84c33a62014-06-02 16:47:17 +053010693 reg = DSPCNTR(intel_crtc->plane);
10694 dspcntr = I915_READ(reg);
10695
Damien Lespiauc5d97472014-10-25 00:11:11 +010010696 if (obj->tiling_mode != I915_TILING_NONE)
10697 dspcntr |= DISPPLANE_TILED;
10698 else
10699 dspcntr &= ~DISPPLANE_TILED;
10700
Sourab Gupta84c33a62014-06-02 16:47:17 +053010701 I915_WRITE(reg, dspcntr);
10702
10703 I915_WRITE(DSPSURF(intel_crtc->plane),
10704 intel_crtc->unpin_work->gtt_offset);
10705 POSTING_READ(DSPSURF(intel_crtc->plane));
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010706
Damien Lespiauff944562014-11-20 14:58:16 +000010707}
10708
10709/*
10710 * XXX: This is the temporary way to update the plane registers until we get
10711 * around to using the usual plane update functions for MMIO flips
10712 */
10713static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
10714{
10715 struct drm_device *dev = intel_crtc->base.dev;
10716 bool atomic_update;
10717 u32 start_vbl_count;
10718
10719 intel_mark_page_flip_active(intel_crtc);
10720
10721 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
10722
10723 if (INTEL_INFO(dev)->gen >= 9)
10724 skl_do_mmio_flip(intel_crtc);
10725 else
10726 /* use_mmio_flip() retricts MMIO flips to ilk+ */
10727 ilk_do_mmio_flip(intel_crtc);
10728
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010729 if (atomic_update)
10730 intel_pipe_update_end(intel_crtc, start_vbl_count);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010731}
10732
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010733static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053010734{
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010735 struct intel_crtc *crtc =
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010736 container_of(work, struct intel_crtc, mmio_flip.work);
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010737 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053010738
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010739 mmio_flip = &crtc->mmio_flip;
10740 if (mmio_flip->req)
John Harrison9c654812014-11-24 18:49:35 +000010741 WARN_ON(__i915_wait_request(mmio_flip->req,
10742 crtc->reset_counter,
10743 false, NULL, NULL) != 0);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010744
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010745 intel_do_mmio_flip(crtc);
10746 if (mmio_flip->req) {
10747 mutex_lock(&crtc->base.dev->struct_mutex);
John Harrison146d84f2014-12-05 13:49:33 +000010748 i915_gem_request_assign(&mmio_flip->req, NULL);
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010749 mutex_unlock(&crtc->base.dev->struct_mutex);
10750 }
Sourab Gupta84c33a62014-06-02 16:47:17 +053010751}
10752
10753static int intel_queue_mmio_flip(struct drm_device *dev,
10754 struct drm_crtc *crtc,
10755 struct drm_framebuffer *fb,
10756 struct drm_i915_gem_object *obj,
10757 struct intel_engine_cs *ring,
10758 uint32_t flags)
10759{
Sourab Gupta84c33a62014-06-02 16:47:17 +053010760 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010761
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010762 i915_gem_request_assign(&intel_crtc->mmio_flip.req,
10763 obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010764
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020010765 schedule_work(&intel_crtc->mmio_flip.work);
10766
Sourab Gupta84c33a62014-06-02 16:47:17 +053010767 return 0;
10768}
10769
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010770static int intel_default_queue_flip(struct drm_device *dev,
10771 struct drm_crtc *crtc,
10772 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010773 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010774 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010775 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010776{
10777 return -ENODEV;
10778}
10779
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010780static bool __intel_pageflip_stall_check(struct drm_device *dev,
10781 struct drm_crtc *crtc)
10782{
10783 struct drm_i915_private *dev_priv = dev->dev_private;
10784 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10785 struct intel_unpin_work *work = intel_crtc->unpin_work;
10786 u32 addr;
10787
10788 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
10789 return true;
10790
10791 if (!work->enable_stall_check)
10792 return false;
10793
10794 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010010795 if (work->flip_queued_req &&
10796 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010797 return false;
10798
Daniel Vetter1e3feef2015-02-13 21:03:45 +010010799 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010800 }
10801
Daniel Vetter1e3feef2015-02-13 21:03:45 +010010802 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010803 return false;
10804
10805 /* Potential stall - if we see that the flip has happened,
10806 * assume a missed interrupt. */
10807 if (INTEL_INFO(dev)->gen >= 4)
10808 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
10809 else
10810 addr = I915_READ(DSPADDR(intel_crtc->plane));
10811
10812 /* There is a potential issue here with a false positive after a flip
10813 * to the same address. We could address this by checking for a
10814 * non-incrementing frame counter.
10815 */
10816 return addr == work->gtt_offset;
10817}
10818
10819void intel_check_page_flip(struct drm_device *dev, int pipe)
10820{
10821 struct drm_i915_private *dev_priv = dev->dev_private;
10822 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10823 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010010824 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020010825
Dave Gordon6c51d462015-03-06 15:34:26 +000010826 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010827
10828 if (crtc == NULL)
10829 return;
10830
Daniel Vetterf3260382014-09-15 14:55:23 +020010831 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010010832 work = intel_crtc->unpin_work;
10833 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010834 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010010835 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010836 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010010837 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010838 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010010839 if (work != NULL &&
10840 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
10841 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020010842 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010843}
10844
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010845static int intel_crtc_page_flip(struct drm_crtc *crtc,
10846 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010847 struct drm_pending_vblank_event *event,
10848 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010849{
10850 struct drm_device *dev = crtc->dev;
10851 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070010852 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070010853 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010854 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080010855 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020010856 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010857 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010858 struct intel_engine_cs *ring;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010010859 bool mmio_flip;
Chris Wilson52e68632010-08-08 10:15:59 +010010860 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010861
Matt Roper2ff8fde2014-07-08 07:50:07 -070010862 /*
10863 * drm_mode_page_flip_ioctl() should already catch this, but double
10864 * check to be safe. In the future we may enable pageflipping from
10865 * a disabled primary plane.
10866 */
10867 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
10868 return -EBUSY;
10869
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030010870 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070010871 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030010872 return -EINVAL;
10873
10874 /*
10875 * TILEOFF/LINOFF registers can't be changed via MI display flips.
10876 * Note that pitch changes could also affect these register.
10877 */
10878 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070010879 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
10880 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030010881 return -EINVAL;
10882
Chris Wilsonf900db42014-02-20 09:26:13 +000010883 if (i915_terminally_wedged(&dev_priv->gpu_error))
10884 goto out_hang;
10885
Daniel Vetterb14c5672013-09-19 12:18:32 +020010886 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010887 if (work == NULL)
10888 return -ENOMEM;
10889
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010890 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010891 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010892 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010893 INIT_WORK(&work->work, intel_unpin_work_fn);
10894
Daniel Vetter87b6b102014-05-15 15:33:46 +020010895 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070010896 if (ret)
10897 goto free_work;
10898
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010899 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010900 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010901 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010902 /* Before declaring the flip queue wedged, check if
10903 * the hardware completed the operation behind our backs.
10904 */
10905 if (__intel_pageflip_stall_check(dev, crtc)) {
10906 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
10907 page_flip_completed(intel_crtc);
10908 } else {
10909 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010910 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010010911
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010912 drm_crtc_vblank_put(crtc);
10913 kfree(work);
10914 return -EBUSY;
10915 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010916 }
10917 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010918 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010919
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010920 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
10921 flush_workqueue(dev_priv->wq);
10922
Jesse Barnes75dfca82010-02-10 15:09:44 -080010923 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010924 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000010925 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010926
Matt Roperf4510a22014-04-01 15:22:40 -070010927 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080010928 update_state_fb(crtc->primary);
Matt Roper1ed1f962015-01-30 16:22:36 -080010929
Chris Wilsone1f99ce2010-10-27 12:45:26 +010010930 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010010931
Chris Wilson89ed88b2015-02-16 14:31:49 +000010932 ret = i915_mutex_lock_interruptible(dev);
10933 if (ret)
10934 goto cleanup;
10935
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010936 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020010937 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010010938
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010939 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Daniel Vettera071fa02014-06-18 23:28:09 +020010940 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010941
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010942 if (IS_VALLEYVIEW(dev)) {
10943 ring = &dev_priv->ring[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010944 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010010945 /* vlv: DISPLAY_FLIP fails to change tiling */
10946 ring = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000010947 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Chris Wilson2a92d5b2014-07-08 10:40:29 +010010948 ring = &dev_priv->ring[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010949 } else if (INTEL_INFO(dev)->gen >= 7) {
John Harrison41c52412014-11-24 18:49:43 +000010950 ring = i915_gem_request_get_ring(obj->last_read_req);
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010951 if (ring == NULL || ring->id != RCS)
10952 ring = &dev_priv->ring[BCS];
10953 } else {
10954 ring = &dev_priv->ring[RCS];
10955 }
10956
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010010957 mmio_flip = use_mmio_flip(ring, obj);
10958
10959 /* When using CS flips, we want to emit semaphores between rings.
10960 * However, when using mmio flips we will create a task to do the
10961 * synchronisation, so all we want here is to pin the framebuffer
10962 * into the display plane and skip any waits.
10963 */
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000010964 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010010965 crtc->primary->state,
10966 mmio_flip ? i915_gem_request_get_ring(obj->last_read_req) : ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010967 if (ret)
10968 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010969
Tvrtko Ursulin121920f2015-03-23 11:10:37 +000010970 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
10971 + intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010972
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010010973 if (mmio_flip) {
Sourab Gupta84c33a62014-06-02 16:47:17 +053010974 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
10975 page_flip_flags);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010976 if (ret)
10977 goto cleanup_unpin;
10978
John Harrisonf06cc1b2014-11-24 18:49:37 +000010979 i915_gem_request_assign(&work->flip_queued_req,
10980 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010981 } else {
Sourab Gupta84c33a62014-06-02 16:47:17 +053010982 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010983 page_flip_flags);
10984 if (ret)
10985 goto cleanup_unpin;
10986
John Harrisonf06cc1b2014-11-24 18:49:37 +000010987 i915_gem_request_assign(&work->flip_queued_req,
10988 intel_ring_get_request(ring));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010989 }
10990
Daniel Vetter1e3feef2015-02-13 21:03:45 +010010991 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010992 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010993
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010994 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Daniel Vettera071fa02014-06-18 23:28:09 +020010995 INTEL_FRONTBUFFER_PRIMARY(pipe));
10996
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020010997 intel_fbc_disable(dev);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010998 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010999 mutex_unlock(&dev->struct_mutex);
11000
Jesse Barnese5510fa2010-07-01 16:48:37 -070011001 trace_i915_flip_request(intel_crtc->plane, obj);
11002
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011003 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010011004
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011005cleanup_unpin:
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011006 intel_unpin_fb_obj(fb, crtc->primary->state);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011007cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011008 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000011009 mutex_unlock(&dev->struct_mutex);
11010cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070011011 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011012 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010011013
Chris Wilson89ed88b2015-02-16 14:31:49 +000011014 drm_gem_object_unreference_unlocked(&obj->base);
11015 drm_framebuffer_unreference(work->old_fb);
11016
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011017 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011018 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011019 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011020
Daniel Vetter87b6b102014-05-15 15:33:46 +020011021 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011022free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010011023 kfree(work);
11024
Chris Wilsonf900db42014-02-20 09:26:13 +000011025 if (ret == -EIO) {
11026out_hang:
Matt Roper53a366b2014-12-23 10:41:53 -080011027 ret = intel_plane_restore(primary);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011028 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011029 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020011030 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011031 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011032 }
Chris Wilsonf900db42014-02-20 09:26:13 +000011033 }
Chris Wilson96b099f2010-06-07 14:03:04 +010011034 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011035}
11036
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011037static struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011038 .mode_set_base_atomic = intel_pipe_set_base_atomic,
11039 .load_lut = intel_crtc_load_lut,
Matt Roperea2c67b2014-12-23 10:41:52 -080011040 .atomic_begin = intel_begin_crtc_commit,
11041 .atomic_flush = intel_finish_crtc_commit,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011042};
11043
Daniel Vetter9a935852012-07-05 22:34:27 +020011044/**
11045 * intel_modeset_update_staged_output_state
11046 *
11047 * Updates the staged output configuration state, e.g. after we've read out the
11048 * current hw state.
11049 */
11050static void intel_modeset_update_staged_output_state(struct drm_device *dev)
11051{
Ville Syrjälä76688512014-01-10 11:28:06 +020011052 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011053 struct intel_encoder *encoder;
11054 struct intel_connector *connector;
11055
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011056 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011057 connector->new_encoder =
11058 to_intel_encoder(connector->base.encoder);
11059 }
11060
Damien Lespiaub2784e12014-08-05 11:29:37 +010011061 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011062 encoder->new_crtc =
11063 to_intel_crtc(encoder->base.crtc);
11064 }
Ville Syrjälä76688512014-01-10 11:28:06 +020011065
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011066 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011067 crtc->new_enabled = crtc->base.state->enable;
Ville Syrjälä76688512014-01-10 11:28:06 +020011068 }
Daniel Vetter9a935852012-07-05 22:34:27 +020011069}
11070
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011071/* Transitional helper to copy current connector/encoder state to
11072 * connector->state. This is needed so that code that is partially
11073 * converted to atomic does the right thing.
11074 */
11075static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11076{
11077 struct intel_connector *connector;
11078
11079 for_each_intel_connector(dev, connector) {
11080 if (connector->base.encoder) {
11081 connector->base.state->best_encoder =
11082 connector->base.encoder;
11083 connector->base.state->crtc =
11084 connector->base.encoder->crtc;
11085 } else {
11086 connector->base.state->best_encoder = NULL;
11087 connector->base.state->crtc = NULL;
11088 }
11089 }
11090}
11091
Daniel Vetter9a935852012-07-05 22:34:27 +020011092/**
11093 * intel_modeset_commit_output_state
11094 *
11095 * This function copies the stage display pipe configuration to the real one.
11096 */
11097static void intel_modeset_commit_output_state(struct drm_device *dev)
11098{
Ville Syrjälä76688512014-01-10 11:28:06 +020011099 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011100 struct intel_encoder *encoder;
11101 struct intel_connector *connector;
11102
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011103 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011104 connector->base.encoder = &connector->new_encoder->base;
11105 }
11106
Damien Lespiaub2784e12014-08-05 11:29:37 +010011107 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011108 encoder->base.crtc = &encoder->new_crtc->base;
11109 }
Ville Syrjälä76688512014-01-10 11:28:06 +020011110
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011111 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011112 crtc->base.state->enable = crtc->new_enabled;
Ville Syrjälä76688512014-01-10 11:28:06 +020011113 crtc->base.enabled = crtc->new_enabled;
11114 }
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011115
11116 intel_modeset_update_connector_atomic_state(dev);
Daniel Vetter9a935852012-07-05 22:34:27 +020011117}
11118
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011119static void
Robin Schroereba905b2014-05-18 02:24:50 +020011120connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011121 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011122{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011123 int bpp = pipe_config->pipe_bpp;
11124
11125 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11126 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011127 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011128
11129 /* Don't use an invalid EDID bpc value */
11130 if (connector->base.display_info.bpc &&
11131 connector->base.display_info.bpc * 3 < bpp) {
11132 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11133 bpp, connector->base.display_info.bpc*3);
11134 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11135 }
11136
11137 /* Clamp bpp to 8 on screens without EDID 1.4 */
11138 if (connector->base.display_info.bpc == 0 && bpp > 24) {
11139 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11140 bpp);
11141 pipe_config->pipe_bpp = 24;
11142 }
11143}
11144
11145static int
11146compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011147 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011148{
11149 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011150 struct drm_atomic_state *state;
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011151 struct intel_connector *connector;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011152 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011153
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011154 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011155 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011156 else if (INTEL_INFO(dev)->gen >= 5)
11157 bpp = 12*3;
11158 else
11159 bpp = 8*3;
11160
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011161
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011162 pipe_config->pipe_bpp = bpp;
11163
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011164 state = pipe_config->base.state;
11165
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011166 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011167 for (i = 0; i < state->num_connector; i++) {
11168 if (!state->connectors[i])
11169 continue;
11170
11171 connector = to_intel_connector(state->connectors[i]);
11172 if (state->connector_states[i]->crtc != &crtc->base)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011173 continue;
11174
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011175 connected_sink_compute_bpp(connector, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011176 }
11177
11178 return bpp;
11179}
11180
Daniel Vetter644db712013-09-19 14:53:58 +020011181static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11182{
11183 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11184 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010011185 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020011186 mode->crtc_hdisplay, mode->crtc_hsync_start,
11187 mode->crtc_hsync_end, mode->crtc_htotal,
11188 mode->crtc_vdisplay, mode->crtc_vsync_start,
11189 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11190}
11191
Daniel Vetterc0b03412013-05-28 12:05:54 +020011192static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011193 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020011194 const char *context)
11195{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011196 struct drm_device *dev = crtc->base.dev;
11197 struct drm_plane *plane;
11198 struct intel_plane *intel_plane;
11199 struct intel_plane_state *state;
11200 struct drm_framebuffer *fb;
11201
11202 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
11203 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020011204
11205 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
11206 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
11207 pipe_config->pipe_bpp, pipe_config->dither);
11208 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11209 pipe_config->has_pch_encoder,
11210 pipe_config->fdi_lanes,
11211 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
11212 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
11213 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011214 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11215 pipe_config->has_dp_encoder,
11216 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
11217 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
11218 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011219
11220 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
11221 pipe_config->has_dp_encoder,
11222 pipe_config->dp_m2_n2.gmch_m,
11223 pipe_config->dp_m2_n2.gmch_n,
11224 pipe_config->dp_m2_n2.link_m,
11225 pipe_config->dp_m2_n2.link_n,
11226 pipe_config->dp_m2_n2.tu);
11227
Daniel Vetter55072d12014-11-20 16:10:28 +010011228 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
11229 pipe_config->has_audio,
11230 pipe_config->has_infoframe);
11231
Daniel Vetterc0b03412013-05-28 12:05:54 +020011232 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011233 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011234 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011235 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11236 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030011237 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030011238 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
11239 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011240 DRM_DEBUG_KMS("num_scalers: %d\n", crtc->num_scalers);
11241 DRM_DEBUG_KMS("scaler_users: 0x%x\n", pipe_config->scaler_state.scaler_users);
11242 DRM_DEBUG_KMS("scaler id: %d\n", pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011243 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11244 pipe_config->gmch_pfit.control,
11245 pipe_config->gmch_pfit.pgm_ratios,
11246 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011247 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020011248 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011249 pipe_config->pch_pfit.size,
11250 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030011251 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030011252 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011253
11254 DRM_DEBUG_KMS("planes on this crtc\n");
11255 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
11256 intel_plane = to_intel_plane(plane);
11257 if (intel_plane->pipe != crtc->pipe)
11258 continue;
11259
11260 state = to_intel_plane_state(plane->state);
11261 fb = state->base.fb;
11262 if (!fb) {
11263 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
11264 "disabled, scaler_id = %d\n",
11265 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11266 plane->base.id, intel_plane->pipe,
11267 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
11268 drm_plane_index(plane), state->scaler_id);
11269 continue;
11270 }
11271
11272 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
11273 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11274 plane->base.id, intel_plane->pipe,
11275 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
11276 drm_plane_index(plane));
11277 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
11278 fb->base.id, fb->width, fb->height, fb->pixel_format);
11279 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
11280 state->scaler_id,
11281 state->src.x1 >> 16, state->src.y1 >> 16,
11282 drm_rect_width(&state->src) >> 16,
11283 drm_rect_height(&state->src) >> 16,
11284 state->dst.x1, state->dst.y1,
11285 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
11286 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020011287}
11288
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011289static bool encoders_cloneable(const struct intel_encoder *a,
11290 const struct intel_encoder *b)
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011291{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011292 /* masks could be asymmetric, so check both ways */
11293 return a == b || (a->cloneable & (1 << b->type) &&
11294 b->cloneable & (1 << a->type));
11295}
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011296
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011297static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11298 struct intel_crtc *crtc,
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011299 struct intel_encoder *encoder)
11300{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011301 struct intel_encoder *source_encoder;
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011302 struct drm_connector_state *connector_state;
11303 int i;
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011304
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011305 for (i = 0; i < state->num_connector; i++) {
11306 if (!state->connectors[i])
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011307 continue;
11308
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011309 connector_state = state->connector_states[i];
11310 if (connector_state->crtc != &crtc->base)
11311 continue;
11312
11313 source_encoder =
11314 to_intel_encoder(connector_state->best_encoder);
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011315 if (!encoders_cloneable(encoder, source_encoder))
11316 return false;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011317 }
11318
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011319 return true;
11320}
11321
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011322static bool check_encoder_cloning(struct drm_atomic_state *state,
11323 struct intel_crtc *crtc)
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011324{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011325 struct intel_encoder *encoder;
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011326 struct drm_connector_state *connector_state;
11327 int i;
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011328
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011329 for (i = 0; i < state->num_connector; i++) {
11330 if (!state->connectors[i])
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011331 continue;
11332
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011333 connector_state = state->connector_states[i];
11334 if (connector_state->crtc != &crtc->base)
11335 continue;
11336
11337 encoder = to_intel_encoder(connector_state->best_encoder);
11338 if (!check_single_encoder_cloning(state, crtc, encoder))
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011339 return false;
11340 }
11341
11342 return true;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011343}
11344
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011345static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011346{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011347 struct drm_device *dev = state->dev;
11348 struct intel_encoder *encoder;
11349 struct drm_connector_state *connector_state;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011350 unsigned int used_ports = 0;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011351 int i;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011352
11353 /*
11354 * Walk the connector list instead of the encoder
11355 * list to detect the problem on ddi platforms
11356 * where there's just one encoder per digital port.
11357 */
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011358 for (i = 0; i < state->num_connector; i++) {
11359 if (!state->connectors[i])
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011360 continue;
11361
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011362 connector_state = state->connector_states[i];
11363 if (!connector_state->best_encoder)
11364 continue;
11365
11366 encoder = to_intel_encoder(connector_state->best_encoder);
11367
11368 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011369
11370 switch (encoder->type) {
11371 unsigned int port_mask;
11372 case INTEL_OUTPUT_UNKNOWN:
11373 if (WARN_ON(!HAS_DDI(dev)))
11374 break;
11375 case INTEL_OUTPUT_DISPLAYPORT:
11376 case INTEL_OUTPUT_HDMI:
11377 case INTEL_OUTPUT_EDP:
11378 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
11379
11380 /* the same port mustn't appear more than once */
11381 if (used_ports & port_mask)
11382 return false;
11383
11384 used_ports |= port_mask;
11385 default:
11386 break;
11387 }
11388 }
11389
11390 return true;
11391}
11392
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011393static void
11394clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
11395{
11396 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070011397 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011398
Chandra Konduru663a3642015-04-07 15:28:41 -070011399 /* Clear only the intel specific part of the crtc state excluding scalers */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011400 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070011401 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011402 memset(crtc_state, 0, sizeof *crtc_state);
11403 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070011404 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011405}
11406
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011407static struct intel_crtc_state *
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011408intel_modeset_pipe_config(struct drm_crtc *crtc,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011409 struct drm_display_mode *mode,
11410 struct drm_atomic_state *state)
Daniel Vetter7758a112012-07-08 19:40:39 +020011411{
Daniel Vetter7758a112012-07-08 19:40:39 +020011412 struct intel_encoder *encoder;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011413 struct intel_connector *connector;
11414 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011415 struct intel_crtc_state *pipe_config;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011416 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011417 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010011418 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020011419
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011420 if (!check_encoder_cloning(state, to_intel_crtc(crtc))) {
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011421 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11422 return ERR_PTR(-EINVAL);
11423 }
11424
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011425 if (!check_digital_port_conflicts(state)) {
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011426 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
11427 return ERR_PTR(-EINVAL);
11428 }
11429
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011430 pipe_config = intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));
11431 if (IS_ERR(pipe_config))
11432 return pipe_config;
11433
11434 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020011435
Matt Roper07878242015-02-25 11:43:26 -080011436 pipe_config->base.crtc = crtc;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011437 drm_mode_copy(&pipe_config->base.adjusted_mode, mode);
11438 drm_mode_copy(&pipe_config->base.mode, mode);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030011439
Daniel Vettere143a212013-07-04 12:01:15 +020011440 pipe_config->cpu_transcoder =
11441 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +020011442 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011443
Imre Deak2960bc92013-07-30 13:36:32 +030011444 /*
11445 * Sanitize sync polarity flags based on requested ones. If neither
11446 * positive or negative polarity is requested, treat this as meaning
11447 * negative polarity.
11448 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011449 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030011450 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011451 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030011452
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011453 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030011454 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011455 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030011456
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011457 /* Compute a starting value for pipe_config->pipe_bpp taking the source
11458 * plane pixel format and any sink constraints into account. Returns the
11459 * source plane bpp so that dithering can be selected on mismatches
11460 * after encoders and crtc also have had their say. */
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011461 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
11462 pipe_config);
11463 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011464 goto fail;
11465
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030011466 /*
11467 * Determine the real pipe dimensions. Note that stereo modes can
11468 * increase the actual pipe size due to the frame doubling and
11469 * insertion of additional space for blanks between the frame. This
11470 * is stored in the crtc timings. We use the requested mode to do this
11471 * computation to clearly distinguish it from the adjusted mode, which
11472 * can be changed by the connectors in the below retry loop.
11473 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011474 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080011475 &pipe_config->pipe_src_w,
11476 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030011477
Daniel Vettere29c22c2013-02-21 00:00:16 +010011478encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020011479 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020011480 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020011481 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020011482
Daniel Vetter135c81b2013-07-21 21:37:09 +020011483 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011484 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
11485 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020011486
Daniel Vetter7758a112012-07-08 19:40:39 +020011487 /* Pass our mode to the connectors and the CRTC to give them a chance to
11488 * adjust it according to limitations or connector properties, and also
11489 * a chance to reject the mode entirely.
11490 */
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011491 for (i = 0; i < state->num_connector; i++) {
11492 connector = to_intel_connector(state->connectors[i]);
11493 if (!connector)
Daniel Vetter7758a112012-07-08 19:40:39 +020011494 continue;
Daniel Vetter7ae89232013-03-27 00:44:52 +010011495
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011496 connector_state = state->connector_states[i];
11497 if (connector_state->crtc != crtc)
11498 continue;
11499
11500 encoder = to_intel_encoder(connector_state->best_encoder);
11501
Daniel Vetterefea6e82013-07-21 21:36:59 +020011502 if (!(encoder->compute_config(encoder, pipe_config))) {
11503 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020011504 goto fail;
11505 }
11506 }
11507
Daniel Vetterff9a6752013-06-01 17:16:21 +020011508 /* Set default port clock if not overwritten by the encoder. Needs to be
11509 * done afterwards in case the encoder adjusts the mode. */
11510 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011511 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010011512 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020011513
Daniel Vettera43f6e02013-06-07 23:10:32 +020011514 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010011515 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020011516 DRM_DEBUG_KMS("CRTC fixup failed\n");
11517 goto fail;
11518 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010011519
11520 if (ret == RETRY) {
11521 if (WARN(!retry, "loop in pipe configuration computation\n")) {
11522 ret = -EINVAL;
11523 goto fail;
11524 }
11525
11526 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
11527 retry = false;
11528 goto encoder_retry;
11529 }
11530
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011531 pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011532 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011533 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011534
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011535 return pipe_config;
Daniel Vetter7758a112012-07-08 19:40:39 +020011536fail:
Daniel Vettere29c22c2013-02-21 00:00:16 +010011537 return ERR_PTR(ret);
Daniel Vetter7758a112012-07-08 19:40:39 +020011538}
11539
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011540/* Computes which crtcs are affected and sets the relevant bits in the mask. For
11541 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
11542static void
11543intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
11544 unsigned *prepare_pipes, unsigned *disable_pipes)
11545{
11546 struct intel_crtc *intel_crtc;
11547 struct drm_device *dev = crtc->dev;
11548 struct intel_encoder *encoder;
11549 struct intel_connector *connector;
11550 struct drm_crtc *tmp_crtc;
11551
11552 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
11553
11554 /* Check which crtcs have changed outputs connected to them, these need
11555 * to be part of the prepare_pipes mask. We don't (yet) support global
11556 * modeset across multiple crtcs, so modeset_pipes will only have one
11557 * bit set at most. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011558 for_each_intel_connector(dev, connector) {
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011559 if (connector->base.encoder == &connector->new_encoder->base)
11560 continue;
11561
11562 if (connector->base.encoder) {
11563 tmp_crtc = connector->base.encoder->crtc;
11564
11565 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
11566 }
11567
11568 if (connector->new_encoder)
11569 *prepare_pipes |=
11570 1 << connector->new_encoder->new_crtc->pipe;
11571 }
11572
Damien Lespiaub2784e12014-08-05 11:29:37 +010011573 for_each_intel_encoder(dev, encoder) {
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011574 if (encoder->base.crtc == &encoder->new_crtc->base)
11575 continue;
11576
11577 if (encoder->base.crtc) {
11578 tmp_crtc = encoder->base.crtc;
11579
11580 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
11581 }
11582
11583 if (encoder->new_crtc)
11584 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
11585 }
11586
Ville Syrjälä76688512014-01-10 11:28:06 +020011587 /* Check for pipes that will be enabled/disabled ... */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011588 for_each_intel_crtc(dev, intel_crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011589 if (intel_crtc->base.state->enable == intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011590 continue;
11591
Ville Syrjälä76688512014-01-10 11:28:06 +020011592 if (!intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011593 *disable_pipes |= 1 << intel_crtc->pipe;
Ville Syrjälä76688512014-01-10 11:28:06 +020011594 else
11595 *prepare_pipes |= 1 << intel_crtc->pipe;
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011596 }
11597
11598
11599 /* set_mode is also used to update properties on life display pipes. */
11600 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä76688512014-01-10 11:28:06 +020011601 if (intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011602 *prepare_pipes |= 1 << intel_crtc->pipe;
11603
Daniel Vetterb6c51642013-04-12 18:48:43 +020011604 /*
11605 * For simplicity do a full modeset on any pipe where the output routing
11606 * changed. We could be more clever, but that would require us to be
11607 * more careful with calling the relevant encoder->mode_set functions.
11608 */
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011609 if (*prepare_pipes)
11610 *modeset_pipes = *prepare_pipes;
11611
11612 /* ... and mask these out. */
11613 *modeset_pipes &= ~(*disable_pipes);
11614 *prepare_pipes &= ~(*disable_pipes);
Daniel Vetterb6c51642013-04-12 18:48:43 +020011615
11616 /*
11617 * HACK: We don't (yet) fully support global modesets. intel_set_config
11618 * obies this rule, but the modeset restore mode of
11619 * intel_modeset_setup_hw_state does not.
11620 */
11621 *modeset_pipes &= 1 << intel_crtc->pipe;
11622 *prepare_pipes &= 1 << intel_crtc->pipe;
Daniel Vettere3641d32013-04-11 19:49:07 +020011623
11624 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
11625 *modeset_pipes, *prepare_pipes, *disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011626}
11627
Daniel Vetterea9d7582012-07-10 10:42:52 +020011628static bool intel_crtc_in_use(struct drm_crtc *crtc)
11629{
11630 struct drm_encoder *encoder;
11631 struct drm_device *dev = crtc->dev;
11632
11633 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
11634 if (encoder->crtc == crtc)
11635 return true;
11636
11637 return false;
11638}
11639
11640static void
11641intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
11642{
Daniel Vetterba41c0de2014-11-03 15:04:55 +010011643 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011644 struct intel_encoder *intel_encoder;
11645 struct intel_crtc *intel_crtc;
11646 struct drm_connector *connector;
11647
Daniel Vetterba41c0de2014-11-03 15:04:55 +010011648 intel_shared_dpll_commit(dev_priv);
11649
Damien Lespiaub2784e12014-08-05 11:29:37 +010011650 for_each_intel_encoder(dev, intel_encoder) {
Daniel Vetterea9d7582012-07-10 10:42:52 +020011651 if (!intel_encoder->base.crtc)
11652 continue;
11653
11654 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
11655
11656 if (prepare_pipes & (1 << intel_crtc->pipe))
11657 intel_encoder->connectors_active = false;
11658 }
11659
11660 intel_modeset_commit_output_state(dev);
11661
Ville Syrjälä76688512014-01-10 11:28:06 +020011662 /* Double check state. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011663 for_each_intel_crtc(dev, intel_crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011664 WARN_ON(intel_crtc->base.state->enable != intel_crtc_in_use(&intel_crtc->base));
Daniel Vetterea9d7582012-07-10 10:42:52 +020011665 }
11666
11667 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
11668 if (!connector->encoder || !connector->encoder->crtc)
11669 continue;
11670
11671 intel_crtc = to_intel_crtc(connector->encoder->crtc);
11672
11673 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +020011674 struct drm_property *dpms_property =
11675 dev->mode_config.dpms_property;
11676
Daniel Vetterea9d7582012-07-10 10:42:52 +020011677 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -050011678 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +020011679 dpms_property,
11680 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +020011681
11682 intel_encoder = to_intel_encoder(connector->encoder);
11683 intel_encoder->connectors_active = true;
11684 }
11685 }
11686
11687}
11688
Ville Syrjälä3bd26262013-09-06 23:29:02 +030011689static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011690{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030011691 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011692
11693 if (clock1 == clock2)
11694 return true;
11695
11696 if (!clock1 || !clock2)
11697 return false;
11698
11699 diff = abs(clock1 - clock2);
11700
11701 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
11702 return true;
11703
11704 return false;
11705}
11706
Daniel Vetter25c5b262012-07-08 22:08:04 +020011707#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
11708 list_for_each_entry((intel_crtc), \
11709 &(dev)->mode_config.crtc_list, \
11710 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +020011711 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020011712
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011713static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020011714intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011715 struct intel_crtc_state *current_config,
11716 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011717{
Daniel Vetter66e985c2013-06-05 13:34:20 +020011718#define PIPE_CONF_CHECK_X(name) \
11719 if (current_config->name != pipe_config->name) { \
11720 DRM_ERROR("mismatch in " #name " " \
11721 "(expected 0x%08x, found 0x%08x)\n", \
11722 current_config->name, \
11723 pipe_config->name); \
11724 return false; \
11725 }
11726
Daniel Vetter08a24032013-04-19 11:25:34 +020011727#define PIPE_CONF_CHECK_I(name) \
11728 if (current_config->name != pipe_config->name) { \
11729 DRM_ERROR("mismatch in " #name " " \
11730 "(expected %i, found %i)\n", \
11731 current_config->name, \
11732 pipe_config->name); \
11733 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010011734 }
11735
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011736/* This is required for BDW+ where there is only one set of registers for
11737 * switching between high and low RR.
11738 * This macro can be used whenever a comparison has to be made between one
11739 * hw state and multiple sw state variables.
11740 */
11741#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
11742 if ((current_config->name != pipe_config->name) && \
11743 (current_config->alt_name != pipe_config->name)) { \
11744 DRM_ERROR("mismatch in " #name " " \
11745 "(expected %i or %i, found %i)\n", \
11746 current_config->name, \
11747 current_config->alt_name, \
11748 pipe_config->name); \
11749 return false; \
11750 }
11751
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011752#define PIPE_CONF_CHECK_FLAGS(name, mask) \
11753 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -070011754 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011755 "(expected %i, found %i)\n", \
11756 current_config->name & (mask), \
11757 pipe_config->name & (mask)); \
11758 return false; \
11759 }
11760
Ville Syrjälä5e550652013-09-06 23:29:07 +030011761#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
11762 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
11763 DRM_ERROR("mismatch in " #name " " \
11764 "(expected %i, found %i)\n", \
11765 current_config->name, \
11766 pipe_config->name); \
11767 return false; \
11768 }
11769
Daniel Vetterbb760062013-06-06 14:55:52 +020011770#define PIPE_CONF_QUIRK(quirk) \
11771 ((current_config->quirks | pipe_config->quirks) & (quirk))
11772
Daniel Vettereccb1402013-05-22 00:50:22 +020011773 PIPE_CONF_CHECK_I(cpu_transcoder);
11774
Daniel Vetter08a24032013-04-19 11:25:34 +020011775 PIPE_CONF_CHECK_I(has_pch_encoder);
11776 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +020011777 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
11778 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
11779 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
11780 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
11781 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +020011782
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011783 PIPE_CONF_CHECK_I(has_dp_encoder);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011784
11785 if (INTEL_INFO(dev)->gen < 8) {
11786 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
11787 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
11788 PIPE_CONF_CHECK_I(dp_m_n.link_m);
11789 PIPE_CONF_CHECK_I(dp_m_n.link_n);
11790 PIPE_CONF_CHECK_I(dp_m_n.tu);
11791
11792 if (current_config->has_drrs) {
11793 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
11794 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
11795 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
11796 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
11797 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
11798 }
11799 } else {
11800 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
11801 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
11802 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
11803 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
11804 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
11805 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011806
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011807 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
11808 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
11809 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
11810 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
11811 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
11812 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011813
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011814 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
11815 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
11816 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
11817 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
11818 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
11819 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011820
Daniel Vetterc93f54c2013-06-27 19:47:19 +020011821 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b2014-04-24 23:54:47 +020011822 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020011823 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
11824 IS_VALLEYVIEW(dev))
11825 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080011826 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020011827
Daniel Vetter9ed109a2014-04-24 23:54:52 +020011828 PIPE_CONF_CHECK_I(has_audio);
11829
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011830 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011831 DRM_MODE_FLAG_INTERLACE);
11832
Daniel Vetterbb760062013-06-06 14:55:52 +020011833 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011834 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011835 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011836 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011837 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011838 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011839 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011840 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011841 DRM_MODE_FLAG_NVSYNC);
11842 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070011843
Ville Syrjälä37327ab2013-09-04 18:25:28 +030011844 PIPE_CONF_CHECK_I(pipe_src_w);
11845 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011846
Daniel Vetter99535992014-04-13 12:00:33 +020011847 /*
11848 * FIXME: BIOS likes to set up a cloned config with lvds+external
11849 * screen. Since we don't yet re-compute the pipe config when moving
11850 * just the lvds port away to another pipe the sw tracking won't match.
11851 *
11852 * Proper atomic modesets with recomputed global state will fix this.
11853 * Until then just don't check gmch state for inherited modes.
11854 */
11855 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
11856 PIPE_CONF_CHECK_I(gmch_pfit.control);
11857 /* pfit ratios are autocomputed by the hw on gen4+ */
11858 if (INTEL_INFO(dev)->gen < 4)
11859 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
11860 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
11861 }
11862
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011863 PIPE_CONF_CHECK_I(pch_pfit.enabled);
11864 if (current_config->pch_pfit.enabled) {
11865 PIPE_CONF_CHECK_I(pch_pfit.pos);
11866 PIPE_CONF_CHECK_I(pch_pfit.size);
11867 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020011868
Chandra Kondurua1b22782015-04-07 15:28:45 -070011869 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
11870
Jesse Barnese59150d2014-01-07 13:30:45 -080011871 /* BDW+ don't expose a synchronous way to read the state */
11872 if (IS_HASWELL(dev))
11873 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030011874
Ville Syrjälä282740f2013-09-04 18:30:03 +030011875 PIPE_CONF_CHECK_I(double_wide);
11876
Daniel Vetter26804af2014-06-25 22:01:55 +030011877 PIPE_CONF_CHECK_X(ddi_pll_sel);
11878
Daniel Vetterc0d43d62013-06-07 23:11:08 +020011879 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020011880 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020011881 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020011882 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
11883 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030011884 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000011885 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
11886 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
11887 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020011888
Ville Syrjälä42571ae2013-09-06 23:29:00 +030011889 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
11890 PIPE_CONF_CHECK_I(pipe_bpp);
11891
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011892 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080011893 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030011894
Daniel Vetter66e985c2013-06-05 13:34:20 +020011895#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020011896#undef PIPE_CONF_CHECK_I
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011897#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011898#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030011899#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020011900#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +020011901
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011902 return true;
11903}
11904
Damien Lespiau08db6652014-11-04 17:06:52 +000011905static void check_wm_state(struct drm_device *dev)
11906{
11907 struct drm_i915_private *dev_priv = dev->dev_private;
11908 struct skl_ddb_allocation hw_ddb, *sw_ddb;
11909 struct intel_crtc *intel_crtc;
11910 int plane;
11911
11912 if (INTEL_INFO(dev)->gen < 9)
11913 return;
11914
11915 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
11916 sw_ddb = &dev_priv->wm.skl_hw.ddb;
11917
11918 for_each_intel_crtc(dev, intel_crtc) {
11919 struct skl_ddb_entry *hw_entry, *sw_entry;
11920 const enum pipe pipe = intel_crtc->pipe;
11921
11922 if (!intel_crtc->active)
11923 continue;
11924
11925 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000011926 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000011927 hw_entry = &hw_ddb.plane[pipe][plane];
11928 sw_entry = &sw_ddb->plane[pipe][plane];
11929
11930 if (skl_ddb_entry_equal(hw_entry, sw_entry))
11931 continue;
11932
11933 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
11934 "(expected (%u,%u), found (%u,%u))\n",
11935 pipe_name(pipe), plane + 1,
11936 sw_entry->start, sw_entry->end,
11937 hw_entry->start, hw_entry->end);
11938 }
11939
11940 /* cursor */
11941 hw_entry = &hw_ddb.cursor[pipe];
11942 sw_entry = &sw_ddb->cursor[pipe];
11943
11944 if (skl_ddb_entry_equal(hw_entry, sw_entry))
11945 continue;
11946
11947 DRM_ERROR("mismatch in DDB state pipe %c cursor "
11948 "(expected (%u,%u), found (%u,%u))\n",
11949 pipe_name(pipe),
11950 sw_entry->start, sw_entry->end,
11951 hw_entry->start, hw_entry->end);
11952 }
11953}
11954
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011955static void
11956check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011957{
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011958 struct intel_connector *connector;
11959
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011960 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011961 /* This also checks the encoder/connector hw state with the
11962 * ->get_hw_state callbacks. */
11963 intel_connector_check_state(connector);
11964
Rob Clarke2c719b2014-12-15 13:56:32 -050011965 I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011966 "connector's staged encoder doesn't match current encoder\n");
11967 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011968}
11969
11970static void
11971check_encoder_state(struct drm_device *dev)
11972{
11973 struct intel_encoder *encoder;
11974 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011975
Damien Lespiaub2784e12014-08-05 11:29:37 +010011976 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011977 bool enabled = false;
11978 bool active = false;
11979 enum pipe pipe, tracked_pipe;
11980
11981 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
11982 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030011983 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011984
Rob Clarke2c719b2014-12-15 13:56:32 -050011985 I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011986 "encoder's stage crtc doesn't match current crtc\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050011987 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011988 "encoder's active_connectors set, but no crtc\n");
11989
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011990 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011991 if (connector->base.encoder != &encoder->base)
11992 continue;
11993 enabled = true;
11994 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
11995 active = true;
11996 }
Dave Airlie0e32b392014-05-02 14:02:48 +100011997 /*
11998 * for MST connectors if we unplug the connector is gone
11999 * away but the encoder is still connected to a crtc
12000 * until a modeset happens in response to the hotplug.
12001 */
12002 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
12003 continue;
12004
Rob Clarke2c719b2014-12-15 13:56:32 -050012005 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012006 "encoder's enabled state mismatch "
12007 "(expected %i, found %i)\n",
12008 !!encoder->base.crtc, enabled);
Rob Clarke2c719b2014-12-15 13:56:32 -050012009 I915_STATE_WARN(active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012010 "active encoder with no crtc\n");
12011
Rob Clarke2c719b2014-12-15 13:56:32 -050012012 I915_STATE_WARN(encoder->connectors_active != active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012013 "encoder's computed active state doesn't match tracked active state "
12014 "(expected %i, found %i)\n", active, encoder->connectors_active);
12015
12016 active = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -050012017 I915_STATE_WARN(active != encoder->connectors_active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012018 "encoder's hw state doesn't match sw tracking "
12019 "(expected %i, found %i)\n",
12020 encoder->connectors_active, active);
12021
12022 if (!encoder->base.crtc)
12023 continue;
12024
12025 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
Rob Clarke2c719b2014-12-15 13:56:32 -050012026 I915_STATE_WARN(active && pipe != tracked_pipe,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012027 "active encoder's pipe doesn't match"
12028 "(expected %i, found %i)\n",
12029 tracked_pipe, pipe);
12030
12031 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012032}
12033
12034static void
12035check_crtc_state(struct drm_device *dev)
12036{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012037 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012038 struct intel_crtc *crtc;
12039 struct intel_encoder *encoder;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012040 struct intel_crtc_state pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012041
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012042 for_each_intel_crtc(dev, crtc) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012043 bool enabled = false;
12044 bool active = false;
12045
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012046 memset(&pipe_config, 0, sizeof(pipe_config));
12047
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012048 DRM_DEBUG_KMS("[CRTC:%d]\n",
12049 crtc->base.base.id);
12050
Matt Roper83d65732015-02-25 13:12:16 -080012051 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012052 "active crtc, but not enabled in sw tracking\n");
12053
Damien Lespiaub2784e12014-08-05 11:29:37 +010012054 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012055 if (encoder->base.crtc != &crtc->base)
12056 continue;
12057 enabled = true;
12058 if (encoder->connectors_active)
12059 active = true;
12060 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020012061
Rob Clarke2c719b2014-12-15 13:56:32 -050012062 I915_STATE_WARN(active != crtc->active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012063 "crtc's computed active state doesn't match tracked active state "
12064 "(expected %i, found %i)\n", active, crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080012065 I915_STATE_WARN(enabled != crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012066 "crtc's computed enabled state doesn't match tracked enabled state "
Matt Roper83d65732015-02-25 13:12:16 -080012067 "(expected %i, found %i)\n", enabled,
12068 crtc->base.state->enable);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012069
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012070 active = dev_priv->display.get_pipe_config(crtc,
12071 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +020012072
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030012073 /* hw state is inconsistent with the pipe quirk */
12074 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12075 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetterd62cf622013-05-29 10:41:29 +020012076 active = crtc->active;
12077
Damien Lespiaub2784e12014-08-05 11:29:37 +010012078 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +030012079 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +020012080 if (encoder->base.crtc != &crtc->base)
12081 continue;
Daniel Vetter1d37b682013-11-18 09:00:59 +010012082 if (encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +020012083 encoder->get_config(encoder, &pipe_config);
12084 }
12085
Rob Clarke2c719b2014-12-15 13:56:32 -050012086 I915_STATE_WARN(crtc->active != active,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012087 "crtc active state doesn't match with hw state "
12088 "(expected %i, found %i)\n", crtc->active, active);
12089
Daniel Vetterc0b03412013-05-28 12:05:54 +020012090 if (active &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012091 !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050012092 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Daniel Vetterc0b03412013-05-28 12:05:54 +020012093 intel_dump_pipe_config(crtc, &pipe_config,
12094 "[hw state]");
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012095 intel_dump_pipe_config(crtc, crtc->config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012096 "[sw state]");
12097 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012098 }
12099}
12100
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012101static void
12102check_shared_dpll_state(struct drm_device *dev)
12103{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012104 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012105 struct intel_crtc *crtc;
12106 struct intel_dpll_hw_state dpll_hw_state;
12107 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020012108
12109 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12110 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12111 int enabled_crtcs = 0, active_crtcs = 0;
12112 bool active;
12113
12114 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12115
12116 DRM_DEBUG_KMS("%s\n", pll->name);
12117
12118 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
12119
Rob Clarke2c719b2014-12-15 13:56:32 -050012120 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
Daniel Vetter53589012013-06-05 13:34:16 +020012121 "more active pll users than references: %i vs %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012122 pll->active, hweight32(pll->config.crtc_mask));
Rob Clarke2c719b2014-12-15 13:56:32 -050012123 I915_STATE_WARN(pll->active && !pll->on,
Daniel Vetter53589012013-06-05 13:34:16 +020012124 "pll in active use but not on in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012125 I915_STATE_WARN(pll->on && !pll->active,
Daniel Vetter35c95372013-07-17 06:55:04 +020012126 "pll in on but not on in use in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012127 I915_STATE_WARN(pll->on != active,
Daniel Vetter53589012013-06-05 13:34:16 +020012128 "pll on state mismatch (expected %i, found %i)\n",
12129 pll->on, active);
12130
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012131 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080012132 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
Daniel Vetter53589012013-06-05 13:34:16 +020012133 enabled_crtcs++;
12134 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12135 active_crtcs++;
12136 }
Rob Clarke2c719b2014-12-15 13:56:32 -050012137 I915_STATE_WARN(pll->active != active_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012138 "pll active crtcs mismatch (expected %i, found %i)\n",
12139 pll->active, active_crtcs);
Rob Clarke2c719b2014-12-15 13:56:32 -050012140 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012141 "pll enabled crtcs mismatch (expected %i, found %i)\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012142 hweight32(pll->config.crtc_mask), enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012143
Rob Clarke2c719b2014-12-15 13:56:32 -050012144 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020012145 sizeof(dpll_hw_state)),
12146 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020012147 }
Daniel Vettera6778b32012-07-02 09:56:42 +020012148}
12149
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012150void
12151intel_modeset_check_state(struct drm_device *dev)
12152{
Damien Lespiau08db6652014-11-04 17:06:52 +000012153 check_wm_state(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012154 check_connector_state(dev);
12155 check_encoder_state(dev);
12156 check_crtc_state(dev);
12157 check_shared_dpll_state(dev);
12158}
12159
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012160void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
Ville Syrjälä18442d02013-09-13 16:00:08 +030012161 int dotclock)
12162{
12163 /*
12164 * FDI already provided one idea for the dotclock.
12165 * Yell if the encoder disagrees.
12166 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012167 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030012168 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012169 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030012170}
12171
Ville Syrjälä80715b22014-05-15 20:23:23 +030012172static void update_scanline_offset(struct intel_crtc *crtc)
12173{
12174 struct drm_device *dev = crtc->base.dev;
12175
12176 /*
12177 * The scanline counter increments at the leading edge of hsync.
12178 *
12179 * On most platforms it starts counting from vtotal-1 on the
12180 * first active line. That means the scanline counter value is
12181 * always one less than what we would expect. Ie. just after
12182 * start of vblank, which also occurs at start of hsync (on the
12183 * last active line), the scanline counter will read vblank_start-1.
12184 *
12185 * On gen2 the scanline counter starts counting from 1 instead
12186 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12187 * to keep the value positive), instead of adding one.
12188 *
12189 * On HSW+ the behaviour of the scanline counter depends on the output
12190 * type. For DP ports it behaves like most other platforms, but on HDMI
12191 * there's an extra 1 line difference. So we need to add two instead of
12192 * one to the value.
12193 */
12194 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012195 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012196 int vtotal;
12197
12198 vtotal = mode->crtc_vtotal;
12199 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
12200 vtotal /= 2;
12201
12202 crtc->scanline_offset = vtotal - 1;
12203 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030012204 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030012205 crtc->scanline_offset = 2;
12206 } else
12207 crtc->scanline_offset = 1;
12208}
12209
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012210static struct intel_crtc_state *
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012211intel_modeset_compute_config(struct drm_crtc *crtc,
12212 struct drm_display_mode *mode,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012213 struct drm_atomic_state *state,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012214 unsigned *modeset_pipes,
12215 unsigned *prepare_pipes,
12216 unsigned *disable_pipes)
12217{
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020012218 struct drm_device *dev = crtc->dev;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012219 struct intel_crtc_state *pipe_config = NULL;
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020012220 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012221 int ret = 0;
12222
12223 ret = drm_atomic_add_affected_connectors(state, crtc);
12224 if (ret)
12225 return ERR_PTR(ret);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012226
12227 intel_modeset_affected_pipes(crtc, modeset_pipes,
12228 prepare_pipes, disable_pipes);
12229
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020012230 for_each_intel_crtc_masked(dev, *disable_pipes, intel_crtc) {
12231 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
12232 if (IS_ERR(pipe_config))
12233 return pipe_config;
12234
12235 pipe_config->base.enable = false;
12236 }
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012237
12238 /*
12239 * Note this needs changes when we start tracking multiple modes
12240 * and crtcs. At that point we'll need to compute the whole config
12241 * (i.e. one pipe_config for each crtc) rather than just the one
12242 * for this crtc.
12243 */
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020012244 for_each_intel_crtc_masked(dev, *modeset_pipes, intel_crtc) {
12245 /* FIXME: For now we still expect modeset_pipes has at most
12246 * one bit set. */
12247 if (WARN_ON(&intel_crtc->base != crtc))
12248 continue;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012249
Daniel Vetter8805aa72015-04-10 16:22:38 +020012250 pipe_config = intel_modeset_pipe_config(crtc, mode, state);
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020012251 if (IS_ERR(pipe_config))
12252 return pipe_config;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012253
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030012254 pipe_config->base.enable = true;
12255
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020012256 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
12257 "[modeset]");
12258 }
12259
12260 return intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012261}
12262
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012263static int __intel_set_mode_setup_plls(struct drm_atomic_state *state,
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012264 unsigned modeset_pipes,
12265 unsigned disable_pipes)
12266{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012267 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012268 struct drm_i915_private *dev_priv = to_i915(dev);
12269 unsigned clear_pipes = modeset_pipes | disable_pipes;
12270 struct intel_crtc *intel_crtc;
12271 int ret = 0;
12272
12273 if (!dev_priv->display.crtc_compute_clock)
12274 return 0;
12275
12276 ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
12277 if (ret)
12278 goto done;
12279
12280 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012281 struct intel_crtc_state *crtc_state =
12282 intel_atomic_get_crtc_state(state, intel_crtc);
12283
12284 /* Modeset pipes should have a new state by now */
12285 if (WARN_ON(IS_ERR(crtc_state)))
12286 continue;
12287
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012288 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012289 crtc_state);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012290 if (ret) {
12291 intel_shared_dpll_abort_config(dev_priv);
12292 goto done;
12293 }
12294 }
12295
12296done:
12297 return ret;
12298}
12299
Daniel Vetterf30da182013-04-11 20:22:50 +020012300static int __intel_set_mode(struct drm_crtc *crtc,
12301 struct drm_display_mode *mode,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012302 int x, int y, struct drm_framebuffer *fb,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012303 struct intel_crtc_state *pipe_config,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012304 unsigned modeset_pipes,
12305 unsigned prepare_pipes,
12306 unsigned disable_pipes)
Daniel Vettera6778b32012-07-02 09:56:42 +020012307{
12308 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030012309 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030012310 struct drm_display_mode *saved_mode;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030012311 struct drm_atomic_state *state = pipe_config->base.state;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012312 struct intel_crtc_state *crtc_state_copy = NULL;
Daniel Vetter25c5b262012-07-08 22:08:04 +020012313 struct intel_crtc *intel_crtc;
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012314 int ret = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020012315
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030012316 saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012317 if (!saved_mode)
12318 return -ENOMEM;
Daniel Vettera6778b32012-07-02 09:56:42 +020012319
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012320 crtc_state_copy = kmalloc(sizeof(*crtc_state_copy), GFP_KERNEL);
12321 if (!crtc_state_copy) {
12322 ret = -ENOMEM;
12323 goto done;
12324 }
12325
Tim Gardner3ac18232012-12-07 07:54:26 -070012326 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +020012327
Jesse Barnes30a970c2013-11-04 13:48:12 -080012328 /*
12329 * See if the config requires any additional preparation, e.g.
12330 * to adjust global state with pipes off. We need to do this
12331 * here so we can get the modeset_pipe updated config for the new
12332 * mode set on this crtc. For other crtcs we need to use the
12333 * adjusted_mode bits in the crtc directly.
12334 */
Vandana Kannanf8437dd12014-11-24 13:37:39 +053012335 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030012336 ret = valleyview_modeset_global_pipes(state, &prepare_pipes);
12337 if (ret)
12338 goto done;
Jesse Barnes30a970c2013-11-04 13:48:12 -080012339
Ville Syrjäläc164f832013-11-05 22:34:12 +020012340 /* may have added more to prepare_pipes than we should */
12341 prepare_pipes &= ~disable_pipes;
12342 }
12343
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012344 ret = __intel_set_mode_setup_plls(state, modeset_pipes, disable_pipes);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012345 if (ret)
12346 goto done;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +020012347
Daniel Vetter460da9162013-03-27 00:44:51 +010012348 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
12349 intel_crtc_disable(&intel_crtc->base);
12350
Daniel Vetterea9d7582012-07-10 10:42:52 +020012351 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080012352 if (intel_crtc->base.state->enable)
Daniel Vetterea9d7582012-07-10 10:42:52 +020012353 dev_priv->display.crtc_disable(&intel_crtc->base);
12354 }
Daniel Vettera6778b32012-07-02 09:56:42 +020012355
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020012356 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
12357 * to set it here already despite that we pass it down the callchain.
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012358 *
12359 * Note we'll need to fix this up when we start tracking multiple
12360 * pipes; here we assume a single modeset_pipe and only track the
12361 * single crtc and mode.
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020012362 */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012363 if (modeset_pipes) {
Daniel Vetter25c5b262012-07-08 22:08:04 +020012364 crtc->mode = *mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012365 /* mode_set/enable/disable functions rely on a correct pipe
12366 * config. */
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020012367 intel_crtc_set_state(to_intel_crtc(crtc), pipe_config);
Ville Syrjäläc326c0a2013-10-28 12:53:41 +020012368
12369 /*
12370 * Calculate and store various constants which
12371 * are later needed by vblank and swap-completion
12372 * timestamping. They are derived from true hwmode.
12373 */
12374 drm_calc_timestamping_constants(crtc,
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012375 &pipe_config->base.adjusted_mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012376 }
Daniel Vetter7758a112012-07-08 19:40:39 +020012377
Daniel Vetterea9d7582012-07-10 10:42:52 +020012378 /* Only after disabling all output pipelines that will be changed can we
12379 * update the the output configuration. */
12380 intel_modeset_update_state(dev, prepare_pipes);
12381
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030012382 modeset_update_crtc_power_domains(state);
Daniel Vetter47fab732012-10-26 10:58:18 +020012383
Daniel Vetter25c5b262012-07-08 22:08:04 +020012384 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Gustavo Padovan455a6802014-12-01 15:40:11 -080012385 struct drm_plane *primary = intel_crtc->base.primary;
12386 int vdisplay, hdisplay;
Daniel Vetter4c107942014-04-24 23:55:05 +020012387
Gustavo Padovan455a6802014-12-01 15:40:11 -080012388 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
Matt Roper70a101f2015-04-08 18:56:53 -070012389 ret = drm_plane_helper_update(primary, &intel_crtc->base,
12390 fb, 0, 0,
12391 hdisplay, vdisplay,
12392 x << 16, y << 16,
12393 hdisplay << 16, vdisplay << 16);
Daniel Vettera6778b32012-07-02 09:56:42 +020012394 }
12395
12396 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ville Syrjälä80715b22014-05-15 20:23:23 +030012397 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
12398 update_scanline_offset(intel_crtc);
12399
Daniel Vetter25c5b262012-07-08 22:08:04 +020012400 dev_priv->display.crtc_enable(&intel_crtc->base);
Ville Syrjälä80715b22014-05-15 20:23:23 +030012401 }
Daniel Vettera6778b32012-07-02 09:56:42 +020012402
Daniel Vettera6778b32012-07-02 09:56:42 +020012403 /* FIXME: add subpixel order */
12404done:
Matt Roper83d65732015-02-25 13:12:16 -080012405 if (ret && crtc->state->enable)
Tim Gardner3ac18232012-12-07 07:54:26 -070012406 crtc->mode = *saved_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +020012407
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012408 if (ret == 0 && pipe_config) {
12409 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12410
12411 /* The pipe_config will be freed with the atomic state, so
12412 * make a copy. */
12413 memcpy(crtc_state_copy, intel_crtc->config,
12414 sizeof *crtc_state_copy);
12415 intel_crtc->config = crtc_state_copy;
12416 intel_crtc->base.state = &crtc_state_copy->base;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012417 } else {
12418 kfree(crtc_state_copy);
12419 }
12420
Tim Gardner3ac18232012-12-07 07:54:26 -070012421 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +020012422 return ret;
12423}
12424
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012425static int intel_set_mode_pipes(struct drm_crtc *crtc,
12426 struct drm_display_mode *mode,
12427 int x, int y, struct drm_framebuffer *fb,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012428 struct intel_crtc_state *pipe_config,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012429 unsigned modeset_pipes,
12430 unsigned prepare_pipes,
12431 unsigned disable_pipes)
12432{
12433 int ret;
12434
12435 ret = __intel_set_mode(crtc, mode, x, y, fb, pipe_config, modeset_pipes,
12436 prepare_pipes, disable_pipes);
12437
12438 if (ret == 0)
12439 intel_modeset_check_state(crtc->dev);
12440
12441 return ret;
12442}
12443
Damien Lespiaue7457a92013-08-08 22:28:59 +010012444static int intel_set_mode(struct drm_crtc *crtc,
12445 struct drm_display_mode *mode,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012446 int x, int y, struct drm_framebuffer *fb,
12447 struct drm_atomic_state *state)
Daniel Vetterf30da182013-04-11 20:22:50 +020012448{
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012449 struct intel_crtc_state *pipe_config;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012450 unsigned modeset_pipes, prepare_pipes, disable_pipes;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012451 int ret = 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020012452
Daniel Vetter8805aa72015-04-10 16:22:38 +020012453 pipe_config = intel_modeset_compute_config(crtc, mode, state,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012454 &modeset_pipes,
12455 &prepare_pipes,
12456 &disable_pipes);
Daniel Vetterf30da182013-04-11 20:22:50 +020012457
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012458 if (IS_ERR(pipe_config)) {
12459 ret = PTR_ERR(pipe_config);
12460 goto out;
12461 }
Daniel Vetterf30da182013-04-11 20:22:50 +020012462
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012463 ret = intel_set_mode_pipes(crtc, mode, x, y, fb, pipe_config,
12464 modeset_pipes, prepare_pipes,
12465 disable_pipes);
12466 if (ret)
12467 goto out;
12468
12469out:
12470 return ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020012471}
12472
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012473void intel_crtc_restore_mode(struct drm_crtc *crtc)
12474{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012475 struct drm_device *dev = crtc->dev;
12476 struct drm_atomic_state *state;
12477 struct intel_encoder *encoder;
12478 struct intel_connector *connector;
12479 struct drm_connector_state *connector_state;
12480
12481 state = drm_atomic_state_alloc(dev);
12482 if (!state) {
12483 DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
12484 crtc->base.id);
12485 return;
12486 }
12487
12488 state->acquire_ctx = dev->mode_config.acquire_ctx;
12489
12490 /* The force restore path in the HW readout code relies on the staged
12491 * config still keeping the user requested config while the actual
12492 * state has been overwritten by the configuration read from HW. We
12493 * need to copy the staged config to the atomic state, otherwise the
12494 * mode set will just reapply the state the HW is already in. */
12495 for_each_intel_encoder(dev, encoder) {
12496 if (&encoder->new_crtc->base != crtc)
12497 continue;
12498
12499 for_each_intel_connector(dev, connector) {
12500 if (connector->new_encoder != encoder)
12501 continue;
12502
12503 connector_state = drm_atomic_get_connector_state(state, &connector->base);
12504 if (IS_ERR(connector_state)) {
12505 DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
12506 connector->base.base.id,
12507 connector->base.name,
12508 PTR_ERR(connector_state));
12509 continue;
12510 }
12511
12512 connector_state->crtc = crtc;
12513 connector_state->best_encoder = &encoder->base;
12514 }
12515 }
12516
12517 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb,
12518 state);
12519
12520 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012521}
12522
Daniel Vetter25c5b262012-07-08 22:08:04 +020012523#undef for_each_intel_crtc_masked
12524
Daniel Vetterd9e55602012-07-04 22:16:09 +020012525static void intel_set_config_free(struct intel_set_config *config)
12526{
12527 if (!config)
12528 return;
12529
Daniel Vetter1aa4b622012-07-05 16:20:48 +020012530 kfree(config->save_connector_encoders);
12531 kfree(config->save_encoder_crtcs);
Ville Syrjälä76688512014-01-10 11:28:06 +020012532 kfree(config->save_crtc_enabled);
Daniel Vetterd9e55602012-07-04 22:16:09 +020012533 kfree(config);
12534}
12535
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012536static int intel_set_config_save_state(struct drm_device *dev,
12537 struct intel_set_config *config)
12538{
Ville Syrjälä76688512014-01-10 11:28:06 +020012539 struct drm_crtc *crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012540 struct drm_encoder *encoder;
12541 struct drm_connector *connector;
12542 int count;
12543
Ville Syrjälä76688512014-01-10 11:28:06 +020012544 config->save_crtc_enabled =
12545 kcalloc(dev->mode_config.num_crtc,
12546 sizeof(bool), GFP_KERNEL);
12547 if (!config->save_crtc_enabled)
12548 return -ENOMEM;
12549
Daniel Vetter1aa4b622012-07-05 16:20:48 +020012550 config->save_encoder_crtcs =
12551 kcalloc(dev->mode_config.num_encoder,
12552 sizeof(struct drm_crtc *), GFP_KERNEL);
12553 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012554 return -ENOMEM;
12555
Daniel Vetter1aa4b622012-07-05 16:20:48 +020012556 config->save_connector_encoders =
12557 kcalloc(dev->mode_config.num_connector,
12558 sizeof(struct drm_encoder *), GFP_KERNEL);
12559 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012560 return -ENOMEM;
12561
12562 /* Copy data. Note that driver private data is not affected.
12563 * Should anything bad happen only the expected state is
12564 * restored, not the drivers personal bookkeeping.
12565 */
12566 count = 0;
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010012567 for_each_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080012568 config->save_crtc_enabled[count++] = crtc->state->enable;
Ville Syrjälä76688512014-01-10 11:28:06 +020012569 }
12570
12571 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012572 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +020012573 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012574 }
12575
12576 count = 0;
12577 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +020012578 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012579 }
12580
12581 return 0;
12582}
12583
12584static void intel_set_config_restore_state(struct drm_device *dev,
12585 struct intel_set_config *config)
12586{
Ville Syrjälä76688512014-01-10 11:28:06 +020012587 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020012588 struct intel_encoder *encoder;
12589 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012590 int count;
12591
12592 count = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012593 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020012594 crtc->new_enabled = config->save_crtc_enabled[count++];
12595 }
12596
12597 count = 0;
Damien Lespiaub2784e12014-08-05 11:29:37 +010012598 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012599 encoder->new_crtc =
12600 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012601 }
12602
12603 count = 0;
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012604 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012605 connector->new_encoder =
12606 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012607 }
12608}
12609
Imre Deake3de42b2013-05-03 19:44:07 +020012610static bool
Chris Wilson2e57f472013-07-17 12:14:40 +010012611is_crtc_connector_off(struct drm_mode_set *set)
Imre Deake3de42b2013-05-03 19:44:07 +020012612{
12613 int i;
12614
Chris Wilson2e57f472013-07-17 12:14:40 +010012615 if (set->num_connectors == 0)
12616 return false;
12617
12618 if (WARN_ON(set->connectors == NULL))
12619 return false;
12620
12621 for (i = 0; i < set->num_connectors; i++)
12622 if (set->connectors[i]->encoder &&
12623 set->connectors[i]->encoder->crtc == set->crtc &&
12624 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
Imre Deake3de42b2013-05-03 19:44:07 +020012625 return true;
12626
12627 return false;
12628}
12629
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012630static void
12631intel_set_config_compute_mode_changes(struct drm_mode_set *set,
12632 struct intel_set_config *config)
12633{
12634
12635 /* We should be able to check here if the fb has the same properties
12636 * and then just flip_or_move it */
Chris Wilson2e57f472013-07-17 12:14:40 +010012637 if (is_crtc_connector_off(set)) {
12638 config->mode_changed = true;
Matt Roperf4510a22014-04-01 15:22:40 -070012639 } else if (set->crtc->primary->fb != set->fb) {
Matt Roper3b150f02014-05-29 08:06:53 -070012640 /*
12641 * If we have no fb, we can only flip as long as the crtc is
12642 * active, otherwise we need a full mode set. The crtc may
12643 * be active if we've only disabled the primary plane, or
12644 * in fastboot situations.
12645 */
Matt Roperf4510a22014-04-01 15:22:40 -070012646 if (set->crtc->primary->fb == NULL) {
Jesse Barnes319d9822013-06-26 01:38:19 +030012647 struct intel_crtc *intel_crtc =
12648 to_intel_crtc(set->crtc);
12649
Matt Roper3b150f02014-05-29 08:06:53 -070012650 if (intel_crtc->active) {
Jesse Barnes319d9822013-06-26 01:38:19 +030012651 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
12652 config->fb_changed = true;
12653 } else {
12654 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
12655 config->mode_changed = true;
12656 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012657 } else if (set->fb == NULL) {
12658 config->mode_changed = true;
Daniel Vetter72f49012013-03-28 16:01:35 +010012659 } else if (set->fb->pixel_format !=
Matt Roperf4510a22014-04-01 15:22:40 -070012660 set->crtc->primary->fb->pixel_format) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012661 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +020012662 } else {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012663 config->fb_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +020012664 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012665 }
12666
Daniel Vetter835c5872012-07-10 18:11:08 +020012667 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012668 config->fb_changed = true;
12669
12670 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
12671 DRM_DEBUG_KMS("modes are different, full mode set\n");
12672 drm_mode_debug_printmodeline(&set->crtc->mode);
12673 drm_mode_debug_printmodeline(set->mode);
12674 config->mode_changed = true;
12675 }
Chris Wilsona1d95702013-08-13 18:48:47 +010012676
12677 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
12678 set->crtc->base.id, config->mode_changed, config->fb_changed);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012679}
12680
Daniel Vetter2e431052012-07-04 22:42:15 +020012681static int
Daniel Vetter9a935852012-07-05 22:34:27 +020012682intel_modeset_stage_output_state(struct drm_device *dev,
12683 struct drm_mode_set *set,
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012684 struct intel_set_config *config,
12685 struct drm_atomic_state *state)
Daniel Vetter50f56112012-07-02 09:35:43 +020012686{
Daniel Vetter9a935852012-07-05 22:34:27 +020012687 struct intel_connector *connector;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012688 struct drm_connector_state *connector_state;
Daniel Vetter9a935852012-07-05 22:34:27 +020012689 struct intel_encoder *encoder;
Ville Syrjälä76688512014-01-10 11:28:06 +020012690 struct intel_crtc *crtc;
Paulo Zanonif3f08572013-08-12 14:56:53 -030012691 int ro;
Daniel Vetter50f56112012-07-02 09:35:43 +020012692
Damien Lespiau9abdda72013-02-13 13:29:23 +000012693 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +020012694 * of connectors. For paranoia, double-check this. */
12695 WARN_ON(!set->fb && (set->num_connectors != 0));
12696 WARN_ON(set->fb && (set->num_connectors == 0));
12697
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012698 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012699 /* Otherwise traverse passed in connector list and get encoders
12700 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +020012701 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012702 if (set->connectors[ro] == &connector->base) {
Dave Airlie0e32b392014-05-02 14:02:48 +100012703 connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
Daniel Vetter50f56112012-07-02 09:35:43 +020012704 break;
12705 }
12706 }
12707
Daniel Vetter9a935852012-07-05 22:34:27 +020012708 /* If we disable the crtc, disable all its connectors. Also, if
12709 * the connector is on the changing crtc but not on the new
12710 * connector list, disable it. */
12711 if ((!set->fb || ro == set->num_connectors) &&
12712 connector->base.encoder &&
12713 connector->base.encoder->crtc == set->crtc) {
12714 connector->new_encoder = NULL;
12715
12716 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
12717 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012718 connector->base.name);
Daniel Vetter9a935852012-07-05 22:34:27 +020012719 }
12720
12721
12722 if (&connector->new_encoder->base != connector->base.encoder) {
Ander Conselvan de Oliveira10634182015-03-03 15:21:57 +020012723 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] encoder changed, full mode switch\n",
12724 connector->base.base.id,
12725 connector->base.name);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012726 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +020012727 }
Daniel Vetter9a935852012-07-05 22:34:27 +020012728 }
12729 /* connector->new_encoder is now updated for all connectors. */
12730
12731 /* Update crtc of enabled connectors. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012732 for_each_intel_connector(dev, connector) {
Ville Syrjälä76688512014-01-10 11:28:06 +020012733 struct drm_crtc *new_crtc;
12734
Daniel Vetter9a935852012-07-05 22:34:27 +020012735 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +020012736 continue;
12737
Daniel Vetter9a935852012-07-05 22:34:27 +020012738 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +020012739
12740 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012741 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +020012742 new_crtc = set->crtc;
12743 }
12744
12745 /* Make sure the new CRTC will work with the encoder */
Thierry Reding14509912014-01-13 12:00:22 +010012746 if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
12747 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012748 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +020012749 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012750 connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
Daniel Vetter9a935852012-07-05 22:34:27 +020012751
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012752 connector_state =
12753 drm_atomic_get_connector_state(state, &connector->base);
12754 if (IS_ERR(connector_state))
12755 return PTR_ERR(connector_state);
12756
12757 connector_state->crtc = new_crtc;
12758 connector_state->best_encoder = &connector->new_encoder->base;
12759
Daniel Vetter9a935852012-07-05 22:34:27 +020012760 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
12761 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012762 connector->base.name,
Daniel Vetter9a935852012-07-05 22:34:27 +020012763 new_crtc->base.id);
12764 }
12765
12766 /* Check for any encoders that needs to be disabled. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010012767 for_each_intel_encoder(dev, encoder) {
Paulo Zanoni5a65f352014-01-07 14:55:53 -020012768 int num_connectors = 0;
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012769 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012770 if (connector->new_encoder == encoder) {
12771 WARN_ON(!connector->new_encoder->new_crtc);
Paulo Zanoni5a65f352014-01-07 14:55:53 -020012772 num_connectors++;
Daniel Vetter9a935852012-07-05 22:34:27 +020012773 }
12774 }
Paulo Zanoni5a65f352014-01-07 14:55:53 -020012775
12776 if (num_connectors == 0)
12777 encoder->new_crtc = NULL;
12778 else if (num_connectors > 1)
12779 return -EINVAL;
12780
Daniel Vetter9a935852012-07-05 22:34:27 +020012781 /* Only now check for crtc changes so we don't miss encoders
12782 * that will be disabled. */
12783 if (&encoder->new_crtc->base != encoder->base.crtc) {
Ander Conselvan de Oliveira10634182015-03-03 15:21:57 +020012784 DRM_DEBUG_KMS("[ENCODER:%d:%s] crtc changed, full mode switch\n",
12785 encoder->base.base.id,
12786 encoder->base.name);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012787 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +020012788 }
12789 }
Daniel Vetter9a935852012-07-05 22:34:27 +020012790 /* Now we've also updated encoder->new_crtc for all encoders. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012791 for_each_intel_connector(dev, connector) {
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012792 connector_state =
12793 drm_atomic_get_connector_state(state, &connector->base);
Ander Conselvan de Oliveira9d918c12015-03-27 15:33:51 +020012794 if (IS_ERR(connector_state))
12795 return PTR_ERR(connector_state);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012796
12797 if (connector->new_encoder) {
Dave Airlie0e32b392014-05-02 14:02:48 +100012798 if (connector->new_encoder != connector->encoder)
12799 connector->encoder = connector->new_encoder;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012800 } else {
12801 connector_state->crtc = NULL;
Ander Conselvan de Oliveiraf61cccf2015-03-31 11:35:00 +030012802 connector_state->best_encoder = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012803 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012804 }
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012805 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020012806 crtc->new_enabled = false;
12807
Damien Lespiaub2784e12014-08-05 11:29:37 +010012808 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä76688512014-01-10 11:28:06 +020012809 if (encoder->new_crtc == crtc) {
12810 crtc->new_enabled = true;
12811 break;
12812 }
12813 }
12814
Matt Roper83d65732015-02-25 13:12:16 -080012815 if (crtc->new_enabled != crtc->base.state->enable) {
Ander Conselvan de Oliveira10634182015-03-03 15:21:57 +020012816 DRM_DEBUG_KMS("[CRTC:%d] %sabled, full mode switch\n",
12817 crtc->base.base.id,
Ville Syrjälä76688512014-01-10 11:28:06 +020012818 crtc->new_enabled ? "en" : "dis");
12819 config->mode_changed = true;
12820 }
12821 }
12822
Daniel Vetter2e431052012-07-04 22:42:15 +020012823 return 0;
12824}
12825
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020012826static void disable_crtc_nofb(struct intel_crtc *crtc)
12827{
12828 struct drm_device *dev = crtc->base.dev;
12829 struct intel_encoder *encoder;
12830 struct intel_connector *connector;
12831
12832 DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
12833 pipe_name(crtc->pipe));
12834
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012835 for_each_intel_connector(dev, connector) {
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020012836 if (connector->new_encoder &&
12837 connector->new_encoder->new_crtc == crtc)
12838 connector->new_encoder = NULL;
12839 }
12840
Damien Lespiaub2784e12014-08-05 11:29:37 +010012841 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020012842 if (encoder->new_crtc == crtc)
12843 encoder->new_crtc = NULL;
12844 }
12845
12846 crtc->new_enabled = false;
12847}
12848
Daniel Vetter2e431052012-07-04 22:42:15 +020012849static int intel_crtc_set_config(struct drm_mode_set *set)
12850{
12851 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +020012852 struct drm_mode_set save_set;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012853 struct drm_atomic_state *state = NULL;
Daniel Vetter2e431052012-07-04 22:42:15 +020012854 struct intel_set_config *config;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012855 struct intel_crtc_state *pipe_config;
Jesse Barnes50f52752014-11-07 13:11:00 -080012856 unsigned modeset_pipes, prepare_pipes, disable_pipes;
Daniel Vetter2e431052012-07-04 22:42:15 +020012857 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +020012858
Daniel Vetter8d3e3752012-07-05 16:09:09 +020012859 BUG_ON(!set);
12860 BUG_ON(!set->crtc);
12861 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +020012862
Daniel Vetter7e53f3a2013-01-21 10:52:17 +010012863 /* Enforce sane interface api - has been abused by the fb helper. */
12864 BUG_ON(!set->mode && set->fb);
12865 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +020012866
Daniel Vetter2e431052012-07-04 22:42:15 +020012867 if (set->fb) {
12868 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
12869 set->crtc->base.id, set->fb->base.id,
12870 (int)set->num_connectors, set->x, set->y);
12871 } else {
12872 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +020012873 }
12874
12875 dev = set->crtc->dev;
12876
12877 ret = -ENOMEM;
12878 config = kzalloc(sizeof(*config), GFP_KERNEL);
12879 if (!config)
12880 goto out_config;
12881
12882 ret = intel_set_config_save_state(dev, config);
12883 if (ret)
12884 goto out_config;
12885
12886 save_set.crtc = set->crtc;
12887 save_set.mode = &set->crtc->mode;
12888 save_set.x = set->crtc->x;
12889 save_set.y = set->crtc->y;
Matt Roperf4510a22014-04-01 15:22:40 -070012890 save_set.fb = set->crtc->primary->fb;
Daniel Vetter2e431052012-07-04 22:42:15 +020012891
12892 /* Compute whether we need a full modeset, only an fb base update or no
12893 * change at all. In the future we might also check whether only the
12894 * mode changed, e.g. for LVDS where we only change the panel fitter in
12895 * such cases. */
12896 intel_set_config_compute_mode_changes(set, config);
12897
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012898 state = drm_atomic_state_alloc(dev);
12899 if (!state) {
12900 ret = -ENOMEM;
12901 goto out_config;
12902 }
12903
12904 state->acquire_ctx = dev->mode_config.acquire_ctx;
12905
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012906 ret = intel_modeset_stage_output_state(dev, set, config, state);
Daniel Vetter2e431052012-07-04 22:42:15 +020012907 if (ret)
12908 goto fail;
12909
Jesse Barnes50f52752014-11-07 13:11:00 -080012910 pipe_config = intel_modeset_compute_config(set->crtc, set->mode,
Daniel Vetter8805aa72015-04-10 16:22:38 +020012911 state,
Jesse Barnes50f52752014-11-07 13:11:00 -080012912 &modeset_pipes,
12913 &prepare_pipes,
12914 &disable_pipes);
Jesse Barnes20664592014-11-05 14:26:09 -080012915 if (IS_ERR(pipe_config)) {
Matt Roper6ac04832014-11-17 09:59:28 -080012916 ret = PTR_ERR(pipe_config);
Jesse Barnes50f52752014-11-07 13:11:00 -080012917 goto fail;
Jesse Barnes20664592014-11-05 14:26:09 -080012918 } else if (pipe_config) {
Ville Syrjäläb9950a12014-11-21 21:00:36 +020012919 if (pipe_config->has_audio !=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012920 to_intel_crtc(set->crtc)->config->has_audio)
Jesse Barnes20664592014-11-05 14:26:09 -080012921 config->mode_changed = true;
12922
Jesse Barnesaf15d2c2014-12-01 09:54:28 -080012923 /*
12924 * Note we have an issue here with infoframes: current code
12925 * only updates them on the full mode set path per hw
12926 * requirements. So here we should be checking for any
12927 * required changes and forcing a mode set.
12928 */
Jesse Barnes20664592014-11-05 14:26:09 -080012929 }
Jesse Barnes50f52752014-11-07 13:11:00 -080012930
Jesse Barnes1f9954d2014-11-05 14:26:10 -080012931 intel_update_pipe_size(to_intel_crtc(set->crtc));
12932
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012933 if (config->mode_changed) {
Jesse Barnes50f52752014-11-07 13:11:00 -080012934 ret = intel_set_mode_pipes(set->crtc, set->mode,
12935 set->x, set->y, set->fb, pipe_config,
12936 modeset_pipes, prepare_pipes,
12937 disable_pipes);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012938 } else if (config->fb_changed) {
Matt Roper3b150f02014-05-29 08:06:53 -070012939 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080012940 struct drm_plane *primary = set->crtc->primary;
12941 int vdisplay, hdisplay;
Matt Roper3b150f02014-05-29 08:06:53 -070012942
Gustavo Padovan455a6802014-12-01 15:40:11 -080012943 drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
Matt Roper70a101f2015-04-08 18:56:53 -070012944 ret = drm_plane_helper_update(primary, set->crtc, set->fb,
12945 0, 0, hdisplay, vdisplay,
12946 set->x << 16, set->y << 16,
12947 hdisplay << 16, vdisplay << 16);
Matt Roper3b150f02014-05-29 08:06:53 -070012948
12949 /*
12950 * We need to make sure the primary plane is re-enabled if it
12951 * has previously been turned off.
12952 */
12953 if (!intel_crtc->primary_enabled && ret == 0) {
12954 WARN_ON(!intel_crtc->active);
Ville Syrjäläfdd508a2014-08-08 21:51:11 +030012955 intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070012956 }
12957
Jesse Barnes7ca51a32014-01-07 13:50:49 -080012958 /*
12959 * In the fastboot case this may be our only check of the
12960 * state after boot. It would be better to only do it on
12961 * the first update, but we don't have a nice way of doing that
12962 * (and really, set_config isn't used much for high freq page
12963 * flipping, so increasing its cost here shouldn't be a big
12964 * deal).
12965 */
Jani Nikulad330a952014-01-21 11:24:25 +020012966 if (i915.fastboot && ret == 0)
Jesse Barnes7ca51a32014-01-07 13:50:49 -080012967 intel_modeset_check_state(set->crtc->dev);
Daniel Vetter50f56112012-07-02 09:35:43 +020012968 }
12969
Chris Wilson2d05eae2013-05-03 17:36:25 +010012970 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +020012971 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
12972 set->crtc->base.id, ret);
Daniel Vetter50f56112012-07-02 09:35:43 +020012973fail:
Chris Wilson2d05eae2013-05-03 17:36:25 +010012974 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +020012975
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012976 drm_atomic_state_clear(state);
12977
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020012978 /*
12979 * HACK: if the pipe was on, but we didn't have a framebuffer,
12980 * force the pipe off to avoid oopsing in the modeset code
12981 * due to fb==NULL. This should only happen during boot since
12982 * we don't yet reconstruct the FB from the hardware state.
12983 */
12984 if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
12985 disable_crtc_nofb(to_intel_crtc(save_set.crtc));
12986
Chris Wilson2d05eae2013-05-03 17:36:25 +010012987 /* Try to restore the config */
12988 if (config->mode_changed &&
12989 intel_set_mode(save_set.crtc, save_set.mode,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012990 save_set.x, save_set.y, save_set.fb,
12991 state))
Chris Wilson2d05eae2013-05-03 17:36:25 +010012992 DRM_ERROR("failed to restore config after modeset failure\n");
12993 }
Daniel Vetter50f56112012-07-02 09:35:43 +020012994
Daniel Vetterd9e55602012-07-04 22:16:09 +020012995out_config:
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012996 if (state)
12997 drm_atomic_state_free(state);
12998
Daniel Vetterd9e55602012-07-04 22:16:09 +020012999 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +020013000 return ret;
13001}
13002
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013003static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013004 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +020013005 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013006 .destroy = intel_crtc_destroy,
13007 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013008 .atomic_duplicate_state = intel_crtc_duplicate_state,
13009 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013010};
13011
Daniel Vetter53589012013-06-05 13:34:16 +020013012static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
13013 struct intel_shared_dpll *pll,
13014 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013015{
Daniel Vetter53589012013-06-05 13:34:16 +020013016 uint32_t val;
13017
Daniel Vetterf458ebb2014-09-30 10:56:39 +020013018 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030013019 return false;
13020
Daniel Vetter53589012013-06-05 13:34:16 +020013021 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020013022 hw_state->dpll = val;
13023 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
13024 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020013025
13026 return val & DPLL_VCO_ENABLE;
13027}
13028
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013029static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
13030 struct intel_shared_dpll *pll)
13031{
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013032 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
13033 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013034}
13035
Daniel Vettere7b903d2013-06-05 13:34:14 +020013036static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
13037 struct intel_shared_dpll *pll)
13038{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013039 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020013040 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020013041
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013042 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013043
13044 /* Wait for the clocks to stabilize. */
13045 POSTING_READ(PCH_DPLL(pll->id));
13046 udelay(150);
13047
13048 /* The pixel multiplier can only be updated once the
13049 * DPLL is enabled and the clocks are stable.
13050 *
13051 * So write it again.
13052 */
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013053 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013054 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013055 udelay(200);
13056}
13057
13058static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
13059 struct intel_shared_dpll *pll)
13060{
13061 struct drm_device *dev = dev_priv->dev;
13062 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013063
13064 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013065 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020013066 if (intel_crtc_to_shared_dpll(crtc) == pll)
13067 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
13068 }
13069
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013070 I915_WRITE(PCH_DPLL(pll->id), 0);
13071 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013072 udelay(200);
13073}
13074
Daniel Vetter46edb022013-06-05 13:34:12 +020013075static char *ibx_pch_dpll_names[] = {
13076 "PCH DPLL A",
13077 "PCH DPLL B",
13078};
13079
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013080static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013081{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013082 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013083 int i;
13084
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013085 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013086
Daniel Vettere72f9fb2013-06-05 13:34:06 +020013087 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020013088 dev_priv->shared_dplls[i].id = i;
13089 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013090 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013091 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
13092 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020013093 dev_priv->shared_dplls[i].get_hw_state =
13094 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013095 }
13096}
13097
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013098static void intel_shared_dpll_init(struct drm_device *dev)
13099{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013100 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013101
Daniel Vetter9cd86932014-06-25 22:01:57 +030013102 if (HAS_DDI(dev))
13103 intel_ddi_pll_init(dev);
13104 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013105 ibx_pch_dpll_init(dev);
13106 else
13107 dev_priv->num_shared_dpll = 0;
13108
13109 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013110}
13111
Matt Roper6beb8c232014-12-01 15:40:14 -080013112/**
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +000013113 * intel_wm_need_update - Check whether watermarks need updating
13114 * @plane: drm plane
13115 * @state: new plane state
13116 *
13117 * Check current plane state versus the new one to determine whether
13118 * watermarks need to be recalculated.
13119 *
13120 * Returns true or false.
13121 */
13122bool intel_wm_need_update(struct drm_plane *plane,
13123 struct drm_plane_state *state)
13124{
13125 /* Update watermarks on tiling changes. */
13126 if (!plane->state->fb || !state->fb ||
13127 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
13128 plane->state->rotation != state->rotation)
13129 return true;
13130
13131 return false;
13132}
13133
13134/**
Matt Roper6beb8c232014-12-01 15:40:14 -080013135 * intel_prepare_plane_fb - Prepare fb for usage on plane
13136 * @plane: drm plane to prepare for
13137 * @fb: framebuffer to prepare for presentation
13138 *
13139 * Prepares a framebuffer for usage on a display plane. Generally this
13140 * involves pinning the underlying object and updating the frontbuffer tracking
13141 * bits. Some older platforms need special physical address handling for
13142 * cursor planes.
13143 *
13144 * Returns 0 on success, negative error code on failure.
13145 */
13146int
13147intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013148 struct drm_framebuffer *fb,
13149 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013150{
13151 struct drm_device *dev = plane->dev;
Matt Roper6beb8c232014-12-01 15:40:14 -080013152 struct intel_plane *intel_plane = to_intel_plane(plane);
13153 enum pipe pipe = intel_plane->pipe;
13154 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13155 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
13156 unsigned frontbuffer_bits = 0;
13157 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013158
Matt Roperea2c67b2014-12-23 10:41:52 -080013159 if (!obj)
Matt Roper465c1202014-05-29 08:06:54 -070013160 return 0;
13161
Matt Roper6beb8c232014-12-01 15:40:14 -080013162 switch (plane->type) {
13163 case DRM_PLANE_TYPE_PRIMARY:
13164 frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
13165 break;
13166 case DRM_PLANE_TYPE_CURSOR:
13167 frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
13168 break;
13169 case DRM_PLANE_TYPE_OVERLAY:
13170 frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
13171 break;
13172 }
Matt Roper465c1202014-05-29 08:06:54 -070013173
Matt Roper4c345742014-07-09 16:22:10 -070013174 mutex_lock(&dev->struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070013175
Matt Roper6beb8c232014-12-01 15:40:14 -080013176 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13177 INTEL_INFO(dev)->cursor_needs_physical) {
13178 int align = IS_I830(dev) ? 16 * 1024 : 256;
13179 ret = i915_gem_object_attach_phys(obj, align);
13180 if (ret)
13181 DRM_DEBUG_KMS("failed to attach phys object\n");
13182 } else {
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000013183 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
Matt Roper6beb8c232014-12-01 15:40:14 -080013184 }
13185
13186 if (ret == 0)
13187 i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
13188
13189 mutex_unlock(&dev->struct_mutex);
13190
13191 return ret;
13192}
13193
Matt Roper38f3ce32014-12-02 07:45:25 -080013194/**
13195 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13196 * @plane: drm plane to clean up for
13197 * @fb: old framebuffer that was on plane
13198 *
13199 * Cleans up a framebuffer that has just been removed from a plane.
13200 */
13201void
13202intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013203 struct drm_framebuffer *fb,
13204 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013205{
13206 struct drm_device *dev = plane->dev;
13207 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13208
13209 if (WARN_ON(!obj))
13210 return;
13211
13212 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
13213 !INTEL_INFO(dev)->cursor_needs_physical) {
13214 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000013215 intel_unpin_fb_obj(fb, old_state);
Matt Roper38f3ce32014-12-02 07:45:25 -080013216 mutex_unlock(&dev->struct_mutex);
13217 }
Matt Roper465c1202014-05-29 08:06:54 -070013218}
13219
Chandra Konduru6156a452015-04-27 13:48:39 -070013220int
13221skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13222{
13223 int max_scale;
13224 struct drm_device *dev;
13225 struct drm_i915_private *dev_priv;
13226 int crtc_clock, cdclk;
13227
13228 if (!intel_crtc || !crtc_state)
13229 return DRM_PLANE_HELPER_NO_SCALING;
13230
13231 dev = intel_crtc->base.dev;
13232 dev_priv = dev->dev_private;
13233 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
13234 cdclk = dev_priv->display.get_display_clock_speed(dev);
13235
13236 if (!crtc_clock || !cdclk)
13237 return DRM_PLANE_HELPER_NO_SCALING;
13238
13239 /*
13240 * skl max scale is lower of:
13241 * close to 3 but not 3, -1 is for that purpose
13242 * or
13243 * cdclk/crtc_clock
13244 */
13245 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13246
13247 return max_scale;
13248}
13249
Matt Roper465c1202014-05-29 08:06:54 -070013250static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013251intel_check_primary_plane(struct drm_plane *plane,
13252 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013253{
Matt Roper32b7eee2014-12-24 07:59:06 -080013254 struct drm_device *dev = plane->dev;
13255 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roper2b875c22014-12-01 15:40:13 -080013256 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013257 struct intel_crtc *intel_crtc;
Chandra Konduru6156a452015-04-27 13:48:39 -070013258 struct intel_crtc_state *crtc_state;
Matt Roper2b875c22014-12-01 15:40:13 -080013259 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013260 struct drm_rect *dest = &state->dst;
13261 struct drm_rect *src = &state->src;
13262 const struct drm_rect *clip = &state->clip;
Sonika Jindald8106362015-04-10 14:37:28 +053013263 bool can_position = false;
Chandra Konduru6156a452015-04-27 13:48:39 -070013264 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13265 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013266 int ret;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013267
Matt Roperea2c67b2014-12-23 10:41:52 -080013268 crtc = crtc ? crtc : plane->crtc;
13269 intel_crtc = to_intel_crtc(crtc);
Chandra Konduru6156a452015-04-27 13:48:39 -070013270 crtc_state = state->base.state ?
13271 intel_atomic_get_crtc_state(state->base.state, intel_crtc) : NULL;
Matt Roperea2c67b2014-12-23 10:41:52 -080013272
Chandra Konduru6156a452015-04-27 13:48:39 -070013273 if (INTEL_INFO(dev)->gen >= 9) {
13274 min_scale = 1;
13275 max_scale = skl_max_scale(intel_crtc, crtc_state);
Sonika Jindald8106362015-04-10 14:37:28 +053013276 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070013277 }
Sonika Jindald8106362015-04-10 14:37:28 +053013278
Matt Roperc59cb172014-12-01 15:40:16 -080013279 ret = drm_plane_helper_check_update(plane, crtc, fb,
13280 src, dest, clip,
Chandra Konduru6156a452015-04-27 13:48:39 -070013281 min_scale,
13282 max_scale,
Sonika Jindald8106362015-04-10 14:37:28 +053013283 can_position, true,
13284 &state->visible);
Matt Roperc59cb172014-12-01 15:40:16 -080013285 if (ret)
13286 return ret;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013287
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013288 if (intel_crtc->active) {
Matt Roper32b7eee2014-12-24 07:59:06 -080013289 intel_crtc->atomic.wait_for_flips = true;
13290
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013291 /*
13292 * FBC does not work on some platforms for rotated
13293 * planes, so disable it when rotation is not 0 and
13294 * update it when rotation is set back to 0.
13295 *
13296 * FIXME: This is redundant with the fbc update done in
13297 * the primary plane enable function except that that
13298 * one is done too late. We eventually need to unify
13299 * this.
13300 */
13301 if (intel_crtc->primary_enabled &&
13302 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
Paulo Zanonie35fef22015-02-09 14:46:29 -020013303 dev_priv->fbc.crtc == intel_crtc &&
Matt Roper8e7d6882015-01-21 16:35:41 -080013304 state->base.rotation != BIT(DRM_ROTATE_0)) {
Matt Roper32b7eee2014-12-24 07:59:06 -080013305 intel_crtc->atomic.disable_fbc = true;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013306 }
13307
13308 if (state->visible) {
Matt Roper32b7eee2014-12-24 07:59:06 -080013309 /*
13310 * BDW signals flip done immediately if the plane
13311 * is disabled, even if the plane enable is already
13312 * armed to occur at the next vblank :(
13313 */
13314 if (IS_BROADWELL(dev) && !intel_crtc->primary_enabled)
13315 intel_crtc->atomic.wait_vblank = true;
13316 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013317
Matt Roper32b7eee2014-12-24 07:59:06 -080013318 intel_crtc->atomic.fb_bits |=
13319 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
13320
13321 intel_crtc->atomic.update_fbc = true;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +000013322
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +000013323 if (intel_wm_need_update(plane, &state->base))
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +000013324 intel_crtc->atomic.update_wm = true;
Matt Roperc59cb172014-12-01 15:40:16 -080013325 }
13326
Chandra Konduru6156a452015-04-27 13:48:39 -070013327 if (INTEL_INFO(dev)->gen >= 9) {
13328 ret = skl_update_scaler_users(intel_crtc, crtc_state,
13329 to_intel_plane(plane), state, 0);
13330 if (ret)
13331 return ret;
13332 }
13333
Matt Roperc59cb172014-12-01 15:40:16 -080013334 return 0;
Matt Roper465c1202014-05-29 08:06:54 -070013335}
13336
Sonika Jindal48404c12014-08-22 14:06:04 +053013337static void
13338intel_commit_primary_plane(struct drm_plane *plane,
13339 struct intel_plane_state *state)
13340{
Matt Roper2b875c22014-12-01 15:40:13 -080013341 struct drm_crtc *crtc = state->base.crtc;
13342 struct drm_framebuffer *fb = state->base.fb;
13343 struct drm_device *dev = plane->dev;
Sonika Jindal48404c12014-08-22 14:06:04 +053013344 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperea2c67b2014-12-23 10:41:52 -080013345 struct intel_crtc *intel_crtc;
Sonika Jindalce54d852014-08-21 11:44:39 +053013346 struct drm_rect *src = &state->src;
Matt Ropercf4c7c12014-12-04 10:27:42 -080013347
Matt Roperea2c67b2014-12-23 10:41:52 -080013348 crtc = crtc ? crtc : plane->crtc;
13349 intel_crtc = to_intel_crtc(crtc);
13350
Matt Ropercf4c7c12014-12-04 10:27:42 -080013351 plane->fb = fb;
Sonika Jindalce54d852014-08-21 11:44:39 +053013352 crtc->x = src->x1 >> 16;
Matt Roper465c1202014-05-29 08:06:54 -070013353 crtc->y = src->y1 >> 16;
13354
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013355 if (intel_crtc->active) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013356 if (state->visible) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013357 /* FIXME: kill this fastboot hack */
13358 intel_update_pipe_size(intel_crtc);
13359
13360 intel_crtc->primary_enabled = true;
13361
13362 dev_priv->display.update_primary_plane(crtc, plane->fb,
13363 crtc->x, crtc->y);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013364 } else {
13365 /*
13366 * If clipping results in a non-visible primary plane,
13367 * we'll disable the primary plane. Note that this is
13368 * a bit different than what happens if userspace
13369 * explicitly disables the plane by passing fb=0
13370 * because plane->fb still gets set and pinned.
13371 */
13372 intel_disable_primary_hw_plane(plane, crtc);
13373 }
Matt Roper32b7eee2014-12-24 07:59:06 -080013374 }
13375}
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013376
Matt Roper32b7eee2014-12-24 07:59:06 -080013377static void intel_begin_crtc_commit(struct drm_crtc *crtc)
13378{
13379 struct drm_device *dev = crtc->dev;
13380 struct drm_i915_private *dev_priv = dev->dev_private;
13381 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080013382 struct intel_plane *intel_plane;
13383 struct drm_plane *p;
13384 unsigned fb_bits = 0;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013385
Matt Roperea2c67b2014-12-23 10:41:52 -080013386 /* Track fb's for any planes being disabled */
13387 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
13388 intel_plane = to_intel_plane(p);
13389
13390 if (intel_crtc->atomic.disabled_planes &
13391 (1 << drm_plane_index(p))) {
13392 switch (p->type) {
13393 case DRM_PLANE_TYPE_PRIMARY:
13394 fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
13395 break;
13396 case DRM_PLANE_TYPE_CURSOR:
13397 fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
13398 break;
13399 case DRM_PLANE_TYPE_OVERLAY:
13400 fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
13401 break;
13402 }
13403
13404 mutex_lock(&dev->struct_mutex);
13405 i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
13406 mutex_unlock(&dev->struct_mutex);
13407 }
13408 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013409
Matt Roper32b7eee2014-12-24 07:59:06 -080013410 if (intel_crtc->atomic.wait_for_flips)
13411 intel_crtc_wait_for_pending_flips(crtc);
13412
13413 if (intel_crtc->atomic.disable_fbc)
13414 intel_fbc_disable(dev);
13415
13416 if (intel_crtc->atomic.pre_disable_primary)
13417 intel_pre_disable_primary(crtc);
13418
13419 if (intel_crtc->atomic.update_wm)
13420 intel_update_watermarks(crtc);
13421
13422 intel_runtime_pm_get(dev_priv);
Matt Roperc34c9ee2014-12-23 10:41:50 -080013423
13424 /* Perform vblank evasion around commit operation */
13425 if (intel_crtc->active)
13426 intel_crtc->atomic.evade =
13427 intel_pipe_update_start(intel_crtc,
13428 &intel_crtc->atomic.start_vbl_count);
Matt Roper32b7eee2014-12-24 07:59:06 -080013429}
13430
13431static void intel_finish_crtc_commit(struct drm_crtc *crtc)
13432{
13433 struct drm_device *dev = crtc->dev;
13434 struct drm_i915_private *dev_priv = dev->dev_private;
13435 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13436 struct drm_plane *p;
13437
Matt Roperc34c9ee2014-12-23 10:41:50 -080013438 if (intel_crtc->atomic.evade)
13439 intel_pipe_update_end(intel_crtc,
13440 intel_crtc->atomic.start_vbl_count);
13441
Matt Roper32b7eee2014-12-24 07:59:06 -080013442 intel_runtime_pm_put(dev_priv);
13443
13444 if (intel_crtc->atomic.wait_vblank)
13445 intel_wait_for_vblank(dev, intel_crtc->pipe);
13446
13447 intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
13448
13449 if (intel_crtc->atomic.update_fbc) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013450 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020013451 intel_fbc_update(dev);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013452 mutex_unlock(&dev->struct_mutex);
13453 }
Matt Roper465c1202014-05-29 08:06:54 -070013454
Matt Roper32b7eee2014-12-24 07:59:06 -080013455 if (intel_crtc->atomic.post_enable_primary)
13456 intel_post_enable_primary(crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013457
Matt Roper32b7eee2014-12-24 07:59:06 -080013458 drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
13459 if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
13460 intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
13461 false, false);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013462
Matt Roper32b7eee2014-12-24 07:59:06 -080013463 memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013464}
13465
Matt Ropercf4c7c12014-12-04 10:27:42 -080013466/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013467 * intel_plane_destroy - destroy a plane
13468 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013469 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013470 * Common destruction function for all types of planes (primary, cursor,
13471 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013472 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013473void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013474{
13475 struct intel_plane *intel_plane = to_intel_plane(plane);
13476 drm_plane_cleanup(plane);
13477 kfree(intel_plane);
13478}
13479
Matt Roper65a3fea2015-01-21 16:35:42 -080013480const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013481 .update_plane = drm_atomic_helper_update_plane,
13482 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013483 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080013484 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080013485 .atomic_get_property = intel_plane_atomic_get_property,
13486 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013487 .atomic_duplicate_state = intel_plane_duplicate_state,
13488 .atomic_destroy_state = intel_plane_destroy_state,
13489
Matt Roper465c1202014-05-29 08:06:54 -070013490};
13491
13492static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13493 int pipe)
13494{
13495 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080013496 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070013497 const uint32_t *intel_primary_formats;
13498 int num_formats;
13499
13500 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13501 if (primary == NULL)
13502 return NULL;
13503
Matt Roper8e7d6882015-01-21 16:35:41 -080013504 state = intel_create_plane_state(&primary->base);
13505 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013506 kfree(primary);
13507 return NULL;
13508 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013509 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013510
Matt Roper465c1202014-05-29 08:06:54 -070013511 primary->can_scale = false;
13512 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013513 if (INTEL_INFO(dev)->gen >= 9) {
13514 primary->can_scale = true;
13515 }
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013516 state->scaler_id = -1;
Matt Roper465c1202014-05-29 08:06:54 -070013517 primary->pipe = pipe;
13518 primary->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080013519 primary->check_plane = intel_check_primary_plane;
13520 primary->commit_plane = intel_commit_primary_plane;
Chandra Konduru08e221f2015-04-07 15:28:37 -070013521 primary->ckey.flags = I915_SET_COLORKEY_NONE;
Matt Roper465c1202014-05-29 08:06:54 -070013522 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13523 primary->plane = !pipe;
13524
13525 if (INTEL_INFO(dev)->gen <= 3) {
13526 intel_primary_formats = intel_primary_formats_gen2;
13527 num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
13528 } else {
13529 intel_primary_formats = intel_primary_formats_gen4;
13530 num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
13531 }
13532
13533 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013534 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070013535 intel_primary_formats, num_formats,
13536 DRM_PLANE_TYPE_PRIMARY);
Sonika Jindal48404c12014-08-22 14:06:04 +053013537
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013538 if (INTEL_INFO(dev)->gen >= 4)
13539 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053013540
Matt Roperea2c67b2014-12-23 10:41:52 -080013541 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13542
Matt Roper465c1202014-05-29 08:06:54 -070013543 return &primary->base;
13544}
13545
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013546void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13547{
13548 if (!dev->mode_config.rotation_property) {
13549 unsigned long flags = BIT(DRM_ROTATE_0) |
13550 BIT(DRM_ROTATE_180);
13551
13552 if (INTEL_INFO(dev)->gen >= 9)
13553 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13554
13555 dev->mode_config.rotation_property =
13556 drm_mode_create_rotation_property(dev, flags);
13557 }
13558 if (dev->mode_config.rotation_property)
13559 drm_object_attach_property(&plane->base.base,
13560 dev->mode_config.rotation_property,
13561 plane->base.state->rotation);
13562}
13563
Matt Roper3d7d6512014-06-10 08:28:13 -070013564static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030013565intel_check_cursor_plane(struct drm_plane *plane,
13566 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070013567{
Matt Roper2b875c22014-12-01 15:40:13 -080013568 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013569 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080013570 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013571 struct drm_rect *dest = &state->dst;
13572 struct drm_rect *src = &state->src;
13573 const struct drm_rect *clip = &state->clip;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013574 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Matt Roperea2c67b2014-12-23 10:41:52 -080013575 struct intel_crtc *intel_crtc;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013576 unsigned stride;
13577 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013578
Matt Roperea2c67b2014-12-23 10:41:52 -080013579 crtc = crtc ? crtc : plane->crtc;
13580 intel_crtc = to_intel_crtc(crtc);
13581
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013582 ret = drm_plane_helper_check_update(plane, crtc, fb,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013583 src, dest, clip,
13584 DRM_PLANE_HELPER_NO_SCALING,
13585 DRM_PLANE_HELPER_NO_SCALING,
13586 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013587 if (ret)
13588 return ret;
13589
13590
13591 /* if we want to turn off the cursor ignore width and height */
13592 if (!obj)
Matt Roper32b7eee2014-12-24 07:59:06 -080013593 goto finish;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013594
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013595 /* Check for which cursor types we support */
Matt Roperea2c67b2014-12-23 10:41:52 -080013596 if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
13597 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13598 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013599 return -EINVAL;
13600 }
13601
Matt Roperea2c67b2014-12-23 10:41:52 -080013602 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13603 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013604 DRM_DEBUG_KMS("buffer is too small\n");
13605 return -ENOMEM;
13606 }
13607
Ville Syrjälä3a656b52015-03-09 21:08:37 +020013608 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013609 DRM_DEBUG_KMS("cursor cannot be tiled\n");
13610 ret = -EINVAL;
13611 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013612
Matt Roper32b7eee2014-12-24 07:59:06 -080013613finish:
13614 if (intel_crtc->active) {
Ville Syrjälä3749f462015-03-10 13:15:22 +020013615 if (plane->state->crtc_w != state->base.crtc_w)
Matt Roper32b7eee2014-12-24 07:59:06 -080013616 intel_crtc->atomic.update_wm = true;
13617
13618 intel_crtc->atomic.fb_bits |=
13619 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
13620 }
13621
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013622 return ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013623}
13624
Matt Roperf4a2cf22014-12-01 15:40:12 -080013625static void
Gustavo Padovan852e7872014-09-05 17:22:31 -030013626intel_commit_cursor_plane(struct drm_plane *plane,
13627 struct intel_plane_state *state)
13628{
Matt Roper2b875c22014-12-01 15:40:13 -080013629 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013630 struct drm_device *dev = plane->dev;
13631 struct intel_crtc *intel_crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080013632 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080013633 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070013634
Matt Roperea2c67b2014-12-23 10:41:52 -080013635 crtc = crtc ? crtc : plane->crtc;
13636 intel_crtc = to_intel_crtc(crtc);
Sonika Jindala919db92014-10-23 07:41:33 -070013637
Matt Roperea2c67b2014-12-23 10:41:52 -080013638 plane->fb = state->base.fb;
13639 crtc->cursor_x = state->base.crtc_x;
13640 crtc->cursor_y = state->base.crtc_y;
13641
Gustavo Padovana912f122014-12-01 15:40:10 -080013642 if (intel_crtc->cursor_bo == obj)
13643 goto update;
13644
Matt Roperf4a2cf22014-12-01 15:40:12 -080013645 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080013646 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080013647 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080013648 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080013649 else
Gustavo Padovana912f122014-12-01 15:40:10 -080013650 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080013651
Gustavo Padovana912f122014-12-01 15:40:10 -080013652 intel_crtc->cursor_addr = addr;
13653 intel_crtc->cursor_bo = obj;
13654update:
Gustavo Padovana912f122014-12-01 15:40:10 -080013655
Matt Roper32b7eee2014-12-24 07:59:06 -080013656 if (intel_crtc->active)
Gustavo Padovan852e7872014-09-05 17:22:31 -030013657 intel_crtc_update_cursor(crtc, state->visible);
Matt Roper3d7d6512014-06-10 08:28:13 -070013658}
Gustavo Padovan852e7872014-09-05 17:22:31 -030013659
Matt Roper3d7d6512014-06-10 08:28:13 -070013660static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
13661 int pipe)
13662{
13663 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080013664 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070013665
13666 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
13667 if (cursor == NULL)
13668 return NULL;
13669
Matt Roper8e7d6882015-01-21 16:35:41 -080013670 state = intel_create_plane_state(&cursor->base);
13671 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013672 kfree(cursor);
13673 return NULL;
13674 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013675 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013676
Matt Roper3d7d6512014-06-10 08:28:13 -070013677 cursor->can_scale = false;
13678 cursor->max_downscale = 1;
13679 cursor->pipe = pipe;
13680 cursor->plane = pipe;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013681 state->scaler_id = -1;
Matt Roperc59cb172014-12-01 15:40:16 -080013682 cursor->check_plane = intel_check_cursor_plane;
13683 cursor->commit_plane = intel_commit_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070013684
13685 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013686 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070013687 intel_cursor_formats,
13688 ARRAY_SIZE(intel_cursor_formats),
13689 DRM_PLANE_TYPE_CURSOR);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013690
13691 if (INTEL_INFO(dev)->gen >= 4) {
13692 if (!dev->mode_config.rotation_property)
13693 dev->mode_config.rotation_property =
13694 drm_mode_create_rotation_property(dev,
13695 BIT(DRM_ROTATE_0) |
13696 BIT(DRM_ROTATE_180));
13697 if (dev->mode_config.rotation_property)
13698 drm_object_attach_property(&cursor->base.base,
13699 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080013700 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013701 }
13702
Matt Roperea2c67b2014-12-23 10:41:52 -080013703 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
13704
Matt Roper3d7d6512014-06-10 08:28:13 -070013705 return &cursor->base;
13706}
13707
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013708static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
13709 struct intel_crtc_state *crtc_state)
13710{
13711 int i;
13712 struct intel_scaler *intel_scaler;
13713 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
13714
13715 for (i = 0; i < intel_crtc->num_scalers; i++) {
13716 intel_scaler = &scaler_state->scalers[i];
13717 intel_scaler->in_use = 0;
13718 intel_scaler->id = i;
13719
13720 intel_scaler->mode = PS_SCALER_MODE_DYN;
13721 }
13722
13723 scaler_state->scaler_id = -1;
13724}
13725
Hannes Ederb358d0a2008-12-18 21:18:47 +010013726static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080013727{
Jani Nikulafbee40d2014-03-31 14:27:18 +030013728 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080013729 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013730 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070013731 struct drm_plane *primary = NULL;
13732 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070013733 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080013734
Daniel Vetter955382f2013-09-19 14:05:45 +020013735 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080013736 if (intel_crtc == NULL)
13737 return;
13738
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013739 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
13740 if (!crtc_state)
13741 goto fail;
13742 intel_crtc_set_state(intel_crtc, crtc_state);
Matt Roper07878242015-02-25 11:43:26 -080013743 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013744
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013745 /* initialize shared scalers */
13746 if (INTEL_INFO(dev)->gen >= 9) {
13747 if (pipe == PIPE_C)
13748 intel_crtc->num_scalers = 1;
13749 else
13750 intel_crtc->num_scalers = SKL_NUM_SCALERS;
13751
13752 skl_init_scalers(dev, intel_crtc, crtc_state);
13753 }
13754
Matt Roper465c1202014-05-29 08:06:54 -070013755 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070013756 if (!primary)
13757 goto fail;
13758
13759 cursor = intel_cursor_plane_create(dev, pipe);
13760 if (!cursor)
13761 goto fail;
13762
Matt Roper465c1202014-05-29 08:06:54 -070013763 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Matt Roper3d7d6512014-06-10 08:28:13 -070013764 cursor, &intel_crtc_funcs);
13765 if (ret)
13766 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080013767
13768 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080013769 for (i = 0; i < 256; i++) {
13770 intel_crtc->lut_r[i] = i;
13771 intel_crtc->lut_g[i] = i;
13772 intel_crtc->lut_b[i] = i;
13773 }
13774
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020013775 /*
13776 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020013777 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020013778 */
Jesse Barnes80824002009-09-10 15:28:06 -070013779 intel_crtc->pipe = pipe;
13780 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010013781 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080013782 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010013783 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070013784 }
13785
Chris Wilson4b0e3332014-05-30 16:35:26 +030013786 intel_crtc->cursor_base = ~0;
13787 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030013788 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030013789
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080013790 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
13791 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
13792 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
13793 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
13794
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020013795 INIT_WORK(&intel_crtc->mmio_flip.work, intel_mmio_flip_work_func);
13796
Jesse Barnes79e53942008-11-07 14:24:08 -080013797 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020013798
13799 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070013800 return;
13801
13802fail:
13803 if (primary)
13804 drm_plane_cleanup(primary);
13805 if (cursor)
13806 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013807 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070013808 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080013809}
13810
Jesse Barnes752aa882013-10-31 18:55:49 +020013811enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
13812{
13813 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020013814 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020013815
Rob Clark51fd3712013-11-19 12:10:12 -050013816 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020013817
Ville Syrjäläd3babd32014-11-07 11:16:01 +020013818 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020013819 return INVALID_PIPE;
13820
13821 return to_intel_crtc(encoder->crtc)->pipe;
13822}
13823
Carl Worth08d7b3d2009-04-29 14:43:54 -070013824int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000013825 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070013826{
Carl Worth08d7b3d2009-04-29 14:43:54 -070013827 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040013828 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020013829 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013830
Rob Clark7707e652014-07-17 23:30:04 -040013831 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070013832
Rob Clark7707e652014-07-17 23:30:04 -040013833 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070013834 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030013835 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013836 }
13837
Rob Clark7707e652014-07-17 23:30:04 -040013838 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020013839 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013840
Daniel Vetterc05422d2009-08-11 16:05:30 +020013841 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013842}
13843
Daniel Vetter66a92782012-07-12 20:08:18 +020013844static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080013845{
Daniel Vetter66a92782012-07-12 20:08:18 +020013846 struct drm_device *dev = encoder->base.dev;
13847 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080013848 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080013849 int entry = 0;
13850
Damien Lespiaub2784e12014-08-05 11:29:37 +010013851 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020013852 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020013853 index_mask |= (1 << entry);
13854
Jesse Barnes79e53942008-11-07 14:24:08 -080013855 entry++;
13856 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010013857
Jesse Barnes79e53942008-11-07 14:24:08 -080013858 return index_mask;
13859}
13860
Chris Wilson4d302442010-12-14 19:21:29 +000013861static bool has_edp_a(struct drm_device *dev)
13862{
13863 struct drm_i915_private *dev_priv = dev->dev_private;
13864
13865 if (!IS_MOBILE(dev))
13866 return false;
13867
13868 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
13869 return false;
13870
Damien Lespiaue3589902014-02-07 19:12:50 +000013871 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000013872 return false;
13873
13874 return true;
13875}
13876
Jesse Barnes84b4e042014-06-25 08:24:29 -070013877static bool intel_crt_present(struct drm_device *dev)
13878{
13879 struct drm_i915_private *dev_priv = dev->dev_private;
13880
Damien Lespiau884497e2013-12-03 13:56:23 +000013881 if (INTEL_INFO(dev)->gen >= 9)
13882 return false;
13883
Damien Lespiaucf404ce2014-10-01 20:04:15 +010013884 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070013885 return false;
13886
13887 if (IS_CHERRYVIEW(dev))
13888 return false;
13889
13890 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
13891 return false;
13892
13893 return true;
13894}
13895
Jesse Barnes79e53942008-11-07 14:24:08 -080013896static void intel_setup_outputs(struct drm_device *dev)
13897{
Eric Anholt725e30a2009-01-22 13:01:02 -080013898 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010013899 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013900 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080013901
Daniel Vetterc9093352013-06-06 22:22:47 +020013902 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080013903
Jesse Barnes84b4e042014-06-25 08:24:29 -070013904 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020013905 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013906
Vandana Kannanc776eb22014-08-19 12:05:01 +053013907 if (IS_BROXTON(dev)) {
13908 /*
13909 * FIXME: Broxton doesn't support port detection via the
13910 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
13911 * detect the ports.
13912 */
13913 intel_ddi_init(dev, PORT_A);
13914 intel_ddi_init(dev, PORT_B);
13915 intel_ddi_init(dev, PORT_C);
13916 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013917 int found;
13918
Jesse Barnesde31fac2015-03-06 15:53:32 -080013919 /*
13920 * Haswell uses DDI functions to detect digital outputs.
13921 * On SKL pre-D0 the strap isn't connected, so we assume
13922 * it's there.
13923 */
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013924 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080013925 /* WaIgnoreDDIAStrap: skl */
13926 if (found ||
13927 (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013928 intel_ddi_init(dev, PORT_A);
13929
13930 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
13931 * register */
13932 found = I915_READ(SFUSE_STRAP);
13933
13934 if (found & SFUSE_STRAP_DDIB_DETECTED)
13935 intel_ddi_init(dev, PORT_B);
13936 if (found & SFUSE_STRAP_DDIC_DETECTED)
13937 intel_ddi_init(dev, PORT_C);
13938 if (found & SFUSE_STRAP_DDID_DETECTED)
13939 intel_ddi_init(dev, PORT_D);
13940 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013941 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020013942 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020013943
13944 if (has_edp_a(dev))
13945 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013946
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013947 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080013948 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010013949 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013950 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013951 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013952 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013953 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013954 }
13955
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013956 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013957 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013958
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013959 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013960 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013961
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013962 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013963 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013964
Daniel Vetter270b3042012-10-27 15:52:05 +020013965 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013966 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070013967 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013968 /*
13969 * The DP_DETECTED bit is the latched state of the DDC
13970 * SDA pin at boot. However since eDP doesn't require DDC
13971 * (no way to plug in a DP->HDMI dongle) the DDC pins for
13972 * eDP ports may have been muxed to an alternate function.
13973 * Thus we can't rely on the DP_DETECTED bit alone to detect
13974 * eDP ports. Consult the VBT as well as DP_DETECTED to
13975 * detect eDP ports.
13976 */
Ville Syrjäläd2182a62015-01-09 14:21:14 +020013977 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
13978 !intel_dp_is_edp(dev, PORT_B))
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030013979 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
13980 PORT_B);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013981 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
13982 intel_dp_is_edp(dev, PORT_B))
13983 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030013984
Ville Syrjäläd2182a62015-01-09 14:21:14 +020013985 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
13986 !intel_dp_is_edp(dev, PORT_C))
Jesse Barnes6f6005a2013-08-09 09:34:35 -070013987 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
13988 PORT_C);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013989 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
13990 intel_dp_is_edp(dev, PORT_C))
13991 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053013992
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030013993 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013994 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030013995 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
13996 PORT_D);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013997 /* eDP not supported on port D, so don't check VBT */
13998 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
13999 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014000 }
14001
Jani Nikula3cfca972013-08-27 15:12:26 +030014002 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +080014003 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014004 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014005
Paulo Zanonie2debe92013-02-18 19:00:27 -030014006 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014007 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014008 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014009 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
14010 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014011 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014012 }
Ma Ling27185ae2009-08-24 13:50:23 +080014013
Imre Deake7281ea2013-05-08 13:14:08 +030014014 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014015 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014016 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014017
14018 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014019
Paulo Zanonie2debe92013-02-18 19:00:27 -030014020 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014021 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014022 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014023 }
Ma Ling27185ae2009-08-24 13:50:23 +080014024
Paulo Zanonie2debe92013-02-18 19:00:27 -030014025 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014026
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014027 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
14028 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014029 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014030 }
Imre Deake7281ea2013-05-08 13:14:08 +030014031 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014032 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014033 }
Ma Ling27185ae2009-08-24 13:50:23 +080014034
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014035 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014036 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014037 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014038 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014039 intel_dvo_init(dev);
14040
Zhenyu Wang103a1962009-11-27 11:44:36 +080014041 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014042 intel_tv_init(dev);
14043
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014044 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014045
Damien Lespiaub2784e12014-08-05 11:29:37 +010014046 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014047 encoder->base.possible_crtcs = encoder->crtc_mask;
14048 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014049 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014050 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014051
Paulo Zanonidde86e22012-12-01 12:04:25 -020014052 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014053
14054 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014055}
14056
14057static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14058{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014059 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014060 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014061
Daniel Vetteref2d6332014-02-10 18:00:38 +010014062 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014063 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014064 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014065 drm_gem_object_unreference(&intel_fb->obj->base);
14066 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014067 kfree(intel_fb);
14068}
14069
14070static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014071 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014072 unsigned int *handle)
14073{
14074 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014075 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014076
Chris Wilson05394f32010-11-08 19:18:58 +000014077 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014078}
14079
14080static const struct drm_framebuffer_funcs intel_fb_funcs = {
14081 .destroy = intel_user_framebuffer_destroy,
14082 .create_handle = intel_user_framebuffer_create_handle,
14083};
14084
Damien Lespiaub3218032015-02-27 11:15:18 +000014085static
14086u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14087 uint32_t pixel_format)
14088{
14089 u32 gen = INTEL_INFO(dev)->gen;
14090
14091 if (gen >= 9) {
14092 /* "The stride in bytes must not exceed the of the size of 8K
14093 * pixels and 32K bytes."
14094 */
14095 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
14096 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
14097 return 32*1024;
14098 } else if (gen >= 4) {
14099 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14100 return 16*1024;
14101 else
14102 return 32*1024;
14103 } else if (gen >= 3) {
14104 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14105 return 8*1024;
14106 else
14107 return 16*1024;
14108 } else {
14109 /* XXX DSPC is limited to 4k tiled */
14110 return 8*1024;
14111 }
14112}
14113
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014114static int intel_framebuffer_init(struct drm_device *dev,
14115 struct intel_framebuffer *intel_fb,
14116 struct drm_mode_fb_cmd2 *mode_cmd,
14117 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014118{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014119 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014120 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014121 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014122
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014123 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14124
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014125 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14126 /* Enforce that fb modifier and tiling mode match, but only for
14127 * X-tiled. This is needed for FBC. */
14128 if (!!(obj->tiling_mode == I915_TILING_X) !=
14129 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14130 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14131 return -EINVAL;
14132 }
14133 } else {
14134 if (obj->tiling_mode == I915_TILING_X)
14135 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14136 else if (obj->tiling_mode == I915_TILING_Y) {
14137 DRM_DEBUG("No Y tiling for legacy addfb\n");
14138 return -EINVAL;
14139 }
14140 }
14141
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014142 /* Passed in modifier sanity checking. */
14143 switch (mode_cmd->modifier[0]) {
14144 case I915_FORMAT_MOD_Y_TILED:
14145 case I915_FORMAT_MOD_Yf_TILED:
14146 if (INTEL_INFO(dev)->gen < 9) {
14147 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14148 mode_cmd->modifier[0]);
14149 return -EINVAL;
14150 }
14151 case DRM_FORMAT_MOD_NONE:
14152 case I915_FORMAT_MOD_X_TILED:
14153 break;
14154 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014155 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14156 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014157 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014158 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014159
Damien Lespiaub3218032015-02-27 11:15:18 +000014160 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
14161 mode_cmd->pixel_format);
14162 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14163 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14164 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014165 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014166 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014167
Damien Lespiaub3218032015-02-27 11:15:18 +000014168 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14169 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014170 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014171 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14172 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014173 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014174 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014175 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014176 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014177
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014178 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014179 mode_cmd->pitches[0] != obj->stride) {
14180 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14181 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014182 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014183 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014184
Ville Syrjälä57779d02012-10-31 17:50:14 +020014185 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014186 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014187 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014188 case DRM_FORMAT_RGB565:
14189 case DRM_FORMAT_XRGB8888:
14190 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014191 break;
14192 case DRM_FORMAT_XRGB1555:
14193 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014194 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014195 DRM_DEBUG("unsupported pixel format: %s\n",
14196 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014197 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014198 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014199 break;
14200 case DRM_FORMAT_XBGR8888:
14201 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014202 case DRM_FORMAT_XRGB2101010:
14203 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014204 case DRM_FORMAT_XBGR2101010:
14205 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014206 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014207 DRM_DEBUG("unsupported pixel format: %s\n",
14208 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014209 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014210 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014211 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014212 case DRM_FORMAT_YUYV:
14213 case DRM_FORMAT_UYVY:
14214 case DRM_FORMAT_YVYU:
14215 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014216 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014217 DRM_DEBUG("unsupported pixel format: %s\n",
14218 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014219 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014220 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014221 break;
14222 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014223 DRM_DEBUG("unsupported pixel format: %s\n",
14224 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014225 return -EINVAL;
14226 }
14227
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014228 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14229 if (mode_cmd->offsets[0] != 0)
14230 return -EINVAL;
14231
Damien Lespiauec2c9812015-01-20 12:51:45 +000014232 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014233 mode_cmd->pixel_format,
14234 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014235 /* FIXME drm helper for size checks (especially planar formats)? */
14236 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14237 return -EINVAL;
14238
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014239 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14240 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020014241 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014242
Jesse Barnes79e53942008-11-07 14:24:08 -080014243 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14244 if (ret) {
14245 DRM_ERROR("framebuffer init failed %d\n", ret);
14246 return ret;
14247 }
14248
Jesse Barnes79e53942008-11-07 14:24:08 -080014249 return 0;
14250}
14251
Jesse Barnes79e53942008-11-07 14:24:08 -080014252static struct drm_framebuffer *
14253intel_user_framebuffer_create(struct drm_device *dev,
14254 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014255 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014256{
Chris Wilson05394f32010-11-08 19:18:58 +000014257 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014258
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014259 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
14260 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014261 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014262 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014263
Chris Wilsond2dff872011-04-19 08:36:26 +010014264 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080014265}
14266
Daniel Vetter4520f532013-10-09 09:18:51 +020014267#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020014268static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014269{
14270}
14271#endif
14272
Jesse Barnes79e53942008-11-07 14:24:08 -080014273static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014274 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014275 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014276 .atomic_check = intel_atomic_check,
14277 .atomic_commit = intel_atomic_commit,
Jesse Barnes79e53942008-11-07 14:24:08 -080014278};
14279
Jesse Barnese70236a2009-09-21 10:42:27 -070014280/* Set up chip specific display functions */
14281static void intel_init_display(struct drm_device *dev)
14282{
14283 struct drm_i915_private *dev_priv = dev->dev_private;
14284
Daniel Vetteree9300b2013-06-03 22:40:22 +020014285 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14286 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030014287 else if (IS_CHERRYVIEW(dev))
14288 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020014289 else if (IS_VALLEYVIEW(dev))
14290 dev_priv->display.find_dpll = vlv_find_best_dpll;
14291 else if (IS_PINEVIEW(dev))
14292 dev_priv->display.find_dpll = pnv_find_best_dpll;
14293 else
14294 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14295
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014296 if (INTEL_INFO(dev)->gen >= 9) {
14297 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014298 dev_priv->display.get_initial_plane_config =
14299 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014300 dev_priv->display.crtc_compute_clock =
14301 haswell_crtc_compute_clock;
14302 dev_priv->display.crtc_enable = haswell_crtc_enable;
14303 dev_priv->display.crtc_disable = haswell_crtc_disable;
14304 dev_priv->display.off = ironlake_crtc_off;
14305 dev_priv->display.update_primary_plane =
14306 skylake_update_primary_plane;
14307 } else if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014308 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014309 dev_priv->display.get_initial_plane_config =
14310 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014311 dev_priv->display.crtc_compute_clock =
14312 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014313 dev_priv->display.crtc_enable = haswell_crtc_enable;
14314 dev_priv->display.crtc_disable = haswell_crtc_disable;
Daniel Vetterdf8ad702014-06-25 22:02:03 +030014315 dev_priv->display.off = ironlake_crtc_off;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014316 dev_priv->display.update_primary_plane =
14317 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030014318 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014319 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014320 dev_priv->display.get_initial_plane_config =
14321 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014322 dev_priv->display.crtc_compute_clock =
14323 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014324 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14325 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014326 dev_priv->display.off = ironlake_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070014327 dev_priv->display.update_primary_plane =
14328 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014329 } else if (IS_VALLEYVIEW(dev)) {
14330 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014331 dev_priv->display.get_initial_plane_config =
14332 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014333 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014334 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14335 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14336 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070014337 dev_priv->display.update_primary_plane =
14338 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014339 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014340 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014341 dev_priv->display.get_initial_plane_config =
14342 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014343 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014344 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14345 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014346 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070014347 dev_priv->display.update_primary_plane =
14348 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014349 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014350
Jesse Barnese70236a2009-09-21 10:42:27 -070014351 /* Returns the core display clock speed */
Ville Syrjälä1652d192015-03-31 14:12:01 +030014352 if (IS_SKYLAKE(dev))
14353 dev_priv->display.get_display_clock_speed =
14354 skylake_get_display_clock_speed;
14355 else if (IS_BROADWELL(dev))
14356 dev_priv->display.get_display_clock_speed =
14357 broadwell_get_display_clock_speed;
14358 else if (IS_HASWELL(dev))
14359 dev_priv->display.get_display_clock_speed =
14360 haswell_get_display_clock_speed;
14361 else if (IS_VALLEYVIEW(dev))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070014362 dev_priv->display.get_display_clock_speed =
14363 valleyview_get_display_clock_speed;
Ville Syrjäläb37a6432015-03-31 14:11:54 +030014364 else if (IS_GEN5(dev))
14365 dev_priv->display.get_display_clock_speed =
14366 ilk_get_display_clock_speed;
Ville Syrjäläa7c66cd2015-03-31 14:11:56 +030014367 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
14368 IS_GEN6(dev) || IS_IVYBRIDGE(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -070014369 dev_priv->display.get_display_clock_speed =
14370 i945_get_display_clock_speed;
14371 else if (IS_I915G(dev))
14372 dev_priv->display.get_display_clock_speed =
14373 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014374 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014375 dev_priv->display.get_display_clock_speed =
14376 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014377 else if (IS_PINEVIEW(dev))
14378 dev_priv->display.get_display_clock_speed =
14379 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014380 else if (IS_I915GM(dev))
14381 dev_priv->display.get_display_clock_speed =
14382 i915gm_get_display_clock_speed;
14383 else if (IS_I865G(dev))
14384 dev_priv->display.get_display_clock_speed =
14385 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020014386 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014387 dev_priv->display.get_display_clock_speed =
14388 i855_get_display_clock_speed;
14389 else /* 852, 830 */
14390 dev_priv->display.get_display_clock_speed =
14391 i830_get_display_clock_speed;
14392
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014393 if (IS_GEN5(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014394 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014395 } else if (IS_GEN6(dev)) {
14396 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014397 } else if (IS_IVYBRIDGE(dev)) {
14398 /* FIXME: detect B0+ stepping and use auto training */
14399 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Paulo Zanoni059b2fe2014-09-02 16:53:57 -030014400 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014401 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Jesse Barnes30a970c2013-11-04 13:48:12 -080014402 } else if (IS_VALLEYVIEW(dev)) {
14403 dev_priv->display.modeset_global_resources =
14404 valleyview_modeset_global_resources;
Vandana Kannanf8437dd12014-11-24 13:37:39 +053014405 } else if (IS_BROXTON(dev)) {
14406 dev_priv->display.modeset_global_resources =
14407 broxton_modeset_global_resources;
Jesse Barnese70236a2009-09-21 10:42:27 -070014408 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014409
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014410 switch (INTEL_INFO(dev)->gen) {
14411 case 2:
14412 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14413 break;
14414
14415 case 3:
14416 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14417 break;
14418
14419 case 4:
14420 case 5:
14421 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14422 break;
14423
14424 case 6:
14425 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14426 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014427 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070014428 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014429 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14430 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000014431 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000014432 /* Drop through - unsupported since execlist only. */
14433 default:
14434 /* Default just returns -ENODEV to indicate unsupported */
14435 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014436 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020014437
14438 intel_panel_init_backlight_funcs(dev);
Ville Syrjäläe39b9992014-09-04 14:53:14 +030014439
14440 mutex_init(&dev_priv->pps_mutex);
Jesse Barnese70236a2009-09-21 10:42:27 -070014441}
14442
Jesse Barnesb690e962010-07-19 13:53:12 -070014443/*
14444 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14445 * resume, or other times. This quirk makes sure that's the case for
14446 * affected systems.
14447 */
Akshay Joshi0206e352011-08-16 15:34:10 -040014448static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070014449{
14450 struct drm_i915_private *dev_priv = dev->dev_private;
14451
14452 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014453 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014454}
14455
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014456static void quirk_pipeb_force(struct drm_device *dev)
14457{
14458 struct drm_i915_private *dev_priv = dev->dev_private;
14459
14460 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14461 DRM_INFO("applying pipe b force quirk\n");
14462}
14463
Keith Packard435793d2011-07-12 14:56:22 -070014464/*
14465 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14466 */
14467static void quirk_ssc_force_disable(struct drm_device *dev)
14468{
14469 struct drm_i915_private *dev_priv = dev->dev_private;
14470 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014471 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070014472}
14473
Carsten Emde4dca20e2012-03-15 15:56:26 +010014474/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010014475 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14476 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010014477 */
14478static void quirk_invert_brightness(struct drm_device *dev)
14479{
14480 struct drm_i915_private *dev_priv = dev->dev_private;
14481 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014482 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014483}
14484
Scot Doyle9c72cc62014-07-03 23:27:50 +000014485/* Some VBT's incorrectly indicate no backlight is present */
14486static void quirk_backlight_present(struct drm_device *dev)
14487{
14488 struct drm_i915_private *dev_priv = dev->dev_private;
14489 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14490 DRM_INFO("applying backlight present quirk\n");
14491}
14492
Jesse Barnesb690e962010-07-19 13:53:12 -070014493struct intel_quirk {
14494 int device;
14495 int subsystem_vendor;
14496 int subsystem_device;
14497 void (*hook)(struct drm_device *dev);
14498};
14499
Egbert Eich5f85f172012-10-14 15:46:38 +020014500/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14501struct intel_dmi_quirk {
14502 void (*hook)(struct drm_device *dev);
14503 const struct dmi_system_id (*dmi_id_list)[];
14504};
14505
14506static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14507{
14508 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14509 return 1;
14510}
14511
14512static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14513 {
14514 .dmi_id_list = &(const struct dmi_system_id[]) {
14515 {
14516 .callback = intel_dmi_reverse_brightness,
14517 .ident = "NCR Corporation",
14518 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14519 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14520 },
14521 },
14522 { } /* terminating entry */
14523 },
14524 .hook = quirk_invert_brightness,
14525 },
14526};
14527
Ben Widawskyc43b5632012-04-16 14:07:40 -070014528static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070014529 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -040014530 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -070014531
Jesse Barnesb690e962010-07-19 13:53:12 -070014532 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14533 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14534
Jesse Barnesb690e962010-07-19 13:53:12 -070014535 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14536 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14537
Ville Syrjälä5f080c02014-08-15 01:22:06 +030014538 /* 830 needs to leave pipe A & dpll A up */
14539 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14540
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014541 /* 830 needs to leave pipe B & dpll B up */
14542 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14543
Keith Packard435793d2011-07-12 14:56:22 -070014544 /* Lenovo U160 cannot use SSC on LVDS */
14545 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020014546
14547 /* Sony Vaio Y cannot use SSC on LVDS */
14548 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010014549
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010014550 /* Acer Aspire 5734Z must invert backlight brightness */
14551 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14552
14553 /* Acer/eMachines G725 */
14554 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14555
14556 /* Acer/eMachines e725 */
14557 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14558
14559 /* Acer/Packard Bell NCL20 */
14560 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14561
14562 /* Acer Aspire 4736Z */
14563 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020014564
14565 /* Acer Aspire 5336 */
14566 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000014567
14568 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14569 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000014570
Scot Doyledfb3d47b2014-08-21 16:08:02 +000014571 /* Acer C720 Chromebook (Core i3 4005U) */
14572 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14573
jens steinb2a96012014-10-28 20:25:53 +010014574 /* Apple Macbook 2,1 (Core 2 T7400) */
14575 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14576
Scot Doyled4967d82014-07-03 23:27:52 +000014577 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14578 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000014579
14580 /* HP Chromebook 14 (Celeron 2955U) */
14581 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020014582
14583 /* Dell Chromebook 11 */
14584 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070014585};
14586
14587static void intel_init_quirks(struct drm_device *dev)
14588{
14589 struct pci_dev *d = dev->pdev;
14590 int i;
14591
14592 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14593 struct intel_quirk *q = &intel_quirks[i];
14594
14595 if (d->device == q->device &&
14596 (d->subsystem_vendor == q->subsystem_vendor ||
14597 q->subsystem_vendor == PCI_ANY_ID) &&
14598 (d->subsystem_device == q->subsystem_device ||
14599 q->subsystem_device == PCI_ANY_ID))
14600 q->hook(dev);
14601 }
Egbert Eich5f85f172012-10-14 15:46:38 +020014602 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14603 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14604 intel_dmi_quirks[i].hook(dev);
14605 }
Jesse Barnesb690e962010-07-19 13:53:12 -070014606}
14607
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014608/* Disable the VGA plane that we never use */
14609static void i915_disable_vga(struct drm_device *dev)
14610{
14611 struct drm_i915_private *dev_priv = dev->dev_private;
14612 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020014613 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014614
Ville Syrjälä2b37c612014-01-22 21:32:38 +020014615 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014616 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070014617 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014618 sr1 = inb(VGA_SR_DATA);
14619 outb(sr1 | 1<<5, VGA_SR_DATA);
14620 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
14621 udelay(300);
14622
Ville Syrjälä01f5a622014-12-16 18:38:37 +020014623 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014624 POSTING_READ(vga_reg);
14625}
14626
Daniel Vetterf8175862012-04-10 15:50:11 +020014627void intel_modeset_init_hw(struct drm_device *dev)
14628{
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030014629 intel_prepare_ddi(dev);
14630
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +030014631 if (IS_VALLEYVIEW(dev))
14632 vlv_update_cdclk(dev);
14633
Daniel Vetterf8175862012-04-10 15:50:11 +020014634 intel_init_clock_gating(dev);
14635
Daniel Vetter8090c6b2012-06-24 16:42:32 +020014636 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020014637}
14638
Jesse Barnes79e53942008-11-07 14:24:08 -080014639void intel_modeset_init(struct drm_device *dev)
14640{
Jesse Barnes652c3932009-08-17 13:31:43 -070014641 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000014642 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014643 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080014644 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080014645
14646 drm_mode_config_init(dev);
14647
14648 dev->mode_config.min_width = 0;
14649 dev->mode_config.min_height = 0;
14650
Dave Airlie019d96c2011-09-29 16:20:42 +010014651 dev->mode_config.preferred_depth = 24;
14652 dev->mode_config.prefer_shadow = 1;
14653
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000014654 dev->mode_config.allow_fb_modifiers = true;
14655
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020014656 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080014657
Jesse Barnesb690e962010-07-19 13:53:12 -070014658 intel_init_quirks(dev);
14659
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030014660 intel_init_pm(dev);
14661
Ben Widawskye3c74752013-04-05 13:12:39 -070014662 if (INTEL_INFO(dev)->num_pipes == 0)
14663 return;
14664
Jesse Barnese70236a2009-09-21 10:42:27 -070014665 intel_init_display(dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014666 intel_init_audio(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070014667
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014668 if (IS_GEN2(dev)) {
14669 dev->mode_config.max_width = 2048;
14670 dev->mode_config.max_height = 2048;
14671 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070014672 dev->mode_config.max_width = 4096;
14673 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080014674 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014675 dev->mode_config.max_width = 8192;
14676 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080014677 }
Damien Lespiau068be562014-03-28 14:17:49 +000014678
Ville Syrjälädc41c152014-08-13 11:57:05 +030014679 if (IS_845G(dev) || IS_I865G(dev)) {
14680 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
14681 dev->mode_config.cursor_height = 1023;
14682 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000014683 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
14684 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
14685 } else {
14686 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
14687 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
14688 }
14689
Ben Widawsky5d4545a2013-01-17 12:45:15 -080014690 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080014691
Zhao Yakui28c97732009-10-09 11:39:41 +080014692 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014693 INTEL_INFO(dev)->num_pipes,
14694 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080014695
Damien Lespiau055e3932014-08-18 13:49:10 +010014696 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014697 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000014698 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000014699 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070014700 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030014701 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000014702 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070014703 }
Jesse Barnes79e53942008-11-07 14:24:08 -080014704 }
14705
Jesse Barnesf42bb702013-12-16 16:34:23 -080014706 intel_init_dpio(dev);
14707
Daniel Vettere72f9fb2013-06-05 13:34:06 +020014708 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014709
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014710 /* Just disable it once at startup */
14711 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014712 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000014713
14714 /* Just in case the BIOS is doing something questionable. */
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020014715 intel_fbc_disable(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080014716
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014717 drm_modeset_lock_all(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080014718 intel_modeset_setup_hw_state(dev, false);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014719 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014720
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014721 for_each_intel_crtc(dev, crtc) {
Jesse Barnes46f297f2014-03-07 08:57:48 -080014722 if (!crtc->active)
14723 continue;
14724
Jesse Barnes46f297f2014-03-07 08:57:48 -080014725 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080014726 * Note that reserving the BIOS fb up front prevents us
14727 * from stuffing other stolen allocations like the ring
14728 * on top. This prevents some ugliness at boot time, and
14729 * can even allow for smooth boot transitions if the BIOS
14730 * fb is large enough for the active pipe configuration.
14731 */
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014732 if (dev_priv->display.get_initial_plane_config) {
14733 dev_priv->display.get_initial_plane_config(crtc,
Jesse Barnes46f297f2014-03-07 08:57:48 -080014734 &crtc->plane_config);
14735 /*
14736 * If the fb is shared between multiple heads, we'll
14737 * just get the first one.
14738 */
Daniel Vetterf6936e22015-03-26 12:17:05 +010014739 intel_find_initial_plane_obj(crtc, &crtc->plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014740 }
Jesse Barnes46f297f2014-03-07 08:57:48 -080014741 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010014742}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080014743
Daniel Vetter7fad7982012-07-04 17:51:47 +020014744static void intel_enable_pipe_a(struct drm_device *dev)
14745{
14746 struct intel_connector *connector;
14747 struct drm_connector *crt = NULL;
14748 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030014749 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020014750
14751 /* We can't just switch on the pipe A, we need to set things up with a
14752 * proper mode and output configuration. As a gross hack, enable pipe A
14753 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014754 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020014755 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
14756 crt = &connector->base;
14757 break;
14758 }
14759 }
14760
14761 if (!crt)
14762 return;
14763
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030014764 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020014765 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020014766}
14767
Daniel Vetterfa555832012-10-10 23:14:00 +020014768static bool
14769intel_check_plane_mapping(struct intel_crtc *crtc)
14770{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014771 struct drm_device *dev = crtc->base.dev;
14772 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020014773 u32 reg, val;
14774
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014775 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020014776 return true;
14777
14778 reg = DSPCNTR(!crtc->plane);
14779 val = I915_READ(reg);
14780
14781 if ((val & DISPLAY_PLANE_ENABLE) &&
14782 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
14783 return false;
14784
14785 return true;
14786}
14787
Daniel Vetter24929352012-07-02 20:28:59 +020014788static void intel_sanitize_crtc(struct intel_crtc *crtc)
14789{
14790 struct drm_device *dev = crtc->base.dev;
14791 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020014792 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020014793
Daniel Vetter24929352012-07-02 20:28:59 +020014794 /* Clear any frame start delays used for debugging left by the BIOS */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014795 reg = PIPECONF(crtc->config->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020014796 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
14797
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030014798 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010014799 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030014800 if (crtc->active) {
14801 update_scanline_offset(crtc);
Daniel Vetter96256042015-02-13 21:03:42 +010014802 drm_crtc_vblank_on(&crtc->base);
14803 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030014804
Daniel Vetter24929352012-07-02 20:28:59 +020014805 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020014806 * disable the crtc (and hence change the state) if it is wrong. Note
14807 * that gen4+ has a fixed plane -> pipe mapping. */
14808 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020014809 struct intel_connector *connector;
14810 bool plane;
14811
Daniel Vetter24929352012-07-02 20:28:59 +020014812 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
14813 crtc->base.base.id);
14814
14815 /* Pipe has the wrong plane attached and the plane is active.
14816 * Temporarily change the plane mapping and disable everything
14817 * ... */
14818 plane = crtc->plane;
14819 crtc->plane = !plane;
Daniel Vetter9c8958b2014-07-14 19:35:31 +020014820 crtc->primary_enabled = true;
Daniel Vetter24929352012-07-02 20:28:59 +020014821 dev_priv->display.crtc_disable(&crtc->base);
14822 crtc->plane = plane;
14823
14824 /* ... and break all links. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014825 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020014826 if (connector->encoder->base.crtc != &crtc->base)
14827 continue;
14828
Egbert Eich7f1950f2014-04-25 10:56:22 +020014829 connector->base.dpms = DRM_MODE_DPMS_OFF;
14830 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020014831 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020014832 /* multiple connectors may have the same encoder:
14833 * handle them and break crtc link separately */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014834 for_each_intel_connector(dev, connector)
Egbert Eich7f1950f2014-04-25 10:56:22 +020014835 if (connector->encoder->base.crtc == &crtc->base) {
14836 connector->encoder->base.crtc = NULL;
14837 connector->encoder->connectors_active = false;
14838 }
Daniel Vetter24929352012-07-02 20:28:59 +020014839
14840 WARN_ON(crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080014841 crtc->base.state->enable = false;
Daniel Vetter24929352012-07-02 20:28:59 +020014842 crtc->base.enabled = false;
14843 }
Daniel Vetter24929352012-07-02 20:28:59 +020014844
Daniel Vetter7fad7982012-07-04 17:51:47 +020014845 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
14846 crtc->pipe == PIPE_A && !crtc->active) {
14847 /* BIOS forgot to enable pipe A, this mostly happens after
14848 * resume. Force-enable the pipe to fix this, the update_dpms
14849 * call below we restore the pipe to the right state, but leave
14850 * the required bits on. */
14851 intel_enable_pipe_a(dev);
14852 }
14853
Daniel Vetter24929352012-07-02 20:28:59 +020014854 /* Adjust the state of the output pipe according to whether we
14855 * have active connectors/encoders. */
14856 intel_crtc_update_dpms(&crtc->base);
14857
Matt Roper83d65732015-02-25 13:12:16 -080014858 if (crtc->active != crtc->base.state->enable) {
Daniel Vetter24929352012-07-02 20:28:59 +020014859 struct intel_encoder *encoder;
14860
14861 /* This can happen either due to bugs in the get_hw_state
14862 * functions or because the pipe is force-enabled due to the
14863 * pipe A quirk. */
14864 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
14865 crtc->base.base.id,
Matt Roper83d65732015-02-25 13:12:16 -080014866 crtc->base.state->enable ? "enabled" : "disabled",
Daniel Vetter24929352012-07-02 20:28:59 +020014867 crtc->active ? "enabled" : "disabled");
14868
Matt Roper83d65732015-02-25 13:12:16 -080014869 crtc->base.state->enable = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020014870 crtc->base.enabled = crtc->active;
14871
14872 /* Because we only establish the connector -> encoder ->
14873 * crtc links if something is active, this means the
14874 * crtc is now deactivated. Break the links. connector
14875 * -> encoder links are only establish when things are
14876 * actually up, hence no need to break them. */
14877 WARN_ON(crtc->active);
14878
14879 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
14880 WARN_ON(encoder->connectors_active);
14881 encoder->base.crtc = NULL;
14882 }
14883 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020014884
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030014885 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010014886 /*
14887 * We start out with underrun reporting disabled to avoid races.
14888 * For correct bookkeeping mark this on active crtcs.
14889 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020014890 * Also on gmch platforms we dont have any hardware bits to
14891 * disable the underrun reporting. Which means we need to start
14892 * out with underrun reporting disabled also on inactive pipes,
14893 * since otherwise we'll complain about the garbage we read when
14894 * e.g. coming up after runtime pm.
14895 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010014896 * No protection against concurrent access is required - at
14897 * worst a fifo underrun happens which also sets this to false.
14898 */
14899 crtc->cpu_fifo_underrun_disabled = true;
14900 crtc->pch_fifo_underrun_disabled = true;
14901 }
Daniel Vetter24929352012-07-02 20:28:59 +020014902}
14903
14904static void intel_sanitize_encoder(struct intel_encoder *encoder)
14905{
14906 struct intel_connector *connector;
14907 struct drm_device *dev = encoder->base.dev;
14908
14909 /* We need to check both for a crtc link (meaning that the
14910 * encoder is active and trying to read from a pipe) and the
14911 * pipe itself being active. */
14912 bool has_active_crtc = encoder->base.crtc &&
14913 to_intel_crtc(encoder->base.crtc)->active;
14914
14915 if (encoder->connectors_active && !has_active_crtc) {
14916 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
14917 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014918 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020014919
14920 /* Connector is active, but has no active pipe. This is
14921 * fallout from our resume register restoring. Disable
14922 * the encoder manually again. */
14923 if (encoder->base.crtc) {
14924 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
14925 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014926 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020014927 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030014928 if (encoder->post_disable)
14929 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020014930 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020014931 encoder->base.crtc = NULL;
14932 encoder->connectors_active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020014933
14934 /* Inconsistent output/port/pipe state happens presumably due to
14935 * a bug in one of the get_hw_state functions. Or someplace else
14936 * in our code, like the register restore mess on resume. Clamp
14937 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014938 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020014939 if (connector->encoder != encoder)
14940 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020014941 connector->base.dpms = DRM_MODE_DPMS_OFF;
14942 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020014943 }
14944 }
14945 /* Enabled encoders without active connectors will be fixed in
14946 * the crtc fixup. */
14947}
14948
Imre Deak04098752014-02-18 00:02:16 +020014949void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014950{
14951 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020014952 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014953
Imre Deak04098752014-02-18 00:02:16 +020014954 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
14955 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
14956 i915_disable_vga(dev);
14957 }
14958}
14959
14960void i915_redisable_vga(struct drm_device *dev)
14961{
14962 struct drm_i915_private *dev_priv = dev->dev_private;
14963
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030014964 /* This function can be called both from intel_modeset_setup_hw_state or
14965 * at a very early point in our resume sequence, where the power well
14966 * structures are not yet restored. Since this function is at a very
14967 * paranoid "someone might have enabled VGA while we were not looking"
14968 * level, just check if the power well is enabled instead of trying to
14969 * follow the "don't touch the power well if we don't need it" policy
14970 * the rest of the driver uses. */
Daniel Vetterf458ebb2014-09-30 10:56:39 +020014971 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030014972 return;
14973
Imre Deak04098752014-02-18 00:02:16 +020014974 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014975}
14976
Ville Syrjälä98ec7732014-04-30 17:43:01 +030014977static bool primary_get_hw_state(struct intel_crtc *crtc)
14978{
14979 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
14980
14981 if (!crtc->active)
14982 return false;
14983
14984 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
14985}
14986
Daniel Vetter30e984d2013-06-05 13:34:17 +020014987static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020014988{
14989 struct drm_i915_private *dev_priv = dev->dev_private;
14990 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020014991 struct intel_crtc *crtc;
14992 struct intel_encoder *encoder;
14993 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020014994 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020014995
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014996 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014997 memset(crtc->config, 0, sizeof(*crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +020014998
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014999 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
Daniel Vetter99535992014-04-13 12:00:33 +020015000
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015001 crtc->active = dev_priv->display.get_pipe_config(crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015002 crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015003
Matt Roper83d65732015-02-25 13:12:16 -080015004 crtc->base.state->enable = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020015005 crtc->base.enabled = crtc->active;
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015006 crtc->primary_enabled = primary_get_hw_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020015007
15008 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15009 crtc->base.base.id,
15010 crtc->active ? "enabled" : "disabled");
15011 }
15012
Daniel Vetter53589012013-06-05 13:34:16 +020015013 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15014 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15015
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015016 pll->on = pll->get_hw_state(dev_priv, pll,
15017 &pll->config.hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020015018 pll->active = 0;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015019 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015020 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015021 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
Daniel Vetter53589012013-06-05 13:34:16 +020015022 pll->active++;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015023 pll->config.crtc_mask |= 1 << crtc->pipe;
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015024 }
Daniel Vetter53589012013-06-05 13:34:16 +020015025 }
Daniel Vetter53589012013-06-05 13:34:16 +020015026
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015027 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015028 pll->name, pll->config.crtc_mask, pll->on);
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015029
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015030 if (pll->config.crtc_mask)
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015031 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
Daniel Vetter53589012013-06-05 13:34:16 +020015032 }
15033
Damien Lespiaub2784e12014-08-05 11:29:37 +010015034 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015035 pipe = 0;
15036
15037 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015038 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15039 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015040 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015041 } else {
15042 encoder->base.crtc = NULL;
15043 }
15044
15045 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015046 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020015047 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015048 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015049 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015050 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015051 }
15052
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015053 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015054 if (connector->get_hw_state(connector)) {
15055 connector->base.dpms = DRM_MODE_DPMS_ON;
15056 connector->encoder->connectors_active = true;
15057 connector->base.encoder = &connector->encoder->base;
15058 } else {
15059 connector->base.dpms = DRM_MODE_DPMS_OFF;
15060 connector->base.encoder = NULL;
15061 }
15062 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15063 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030015064 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015065 connector->base.encoder ? "enabled" : "disabled");
15066 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015067}
15068
15069/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
15070 * and i915 state tracking structures. */
15071void intel_modeset_setup_hw_state(struct drm_device *dev,
15072 bool force_restore)
15073{
15074 struct drm_i915_private *dev_priv = dev->dev_private;
15075 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015076 struct intel_crtc *crtc;
15077 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015078 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015079
15080 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015081
Jesse Barnesbabea612013-06-26 18:57:38 +030015082 /*
15083 * Now that we have the config, copy it to each CRTC struct
15084 * Note that this could go away if we move to using crtc_config
15085 * checking everywhere.
15086 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015087 for_each_intel_crtc(dev, crtc) {
Jani Nikulad330a952014-01-21 11:24:25 +020015088 if (crtc->active && i915.fastboot) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015089 intel_mode_from_pipe_config(&crtc->base.mode,
15090 crtc->config);
Jesse Barnesbabea612013-06-26 18:57:38 +030015091 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
15092 crtc->base.base.id);
15093 drm_mode_debug_printmodeline(&crtc->base.mode);
15094 }
15095 }
15096
Daniel Vetter24929352012-07-02 20:28:59 +020015097 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010015098 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015099 intel_sanitize_encoder(encoder);
15100 }
15101
Damien Lespiau055e3932014-08-18 13:49:10 +010015102 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020015103 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15104 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015105 intel_dump_pipe_config(crtc, crtc->config,
15106 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015107 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015108
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015109 intel_modeset_update_connector_atomic_state(dev);
15110
Daniel Vetter35c95372013-07-17 06:55:04 +020015111 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15112 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15113
15114 if (!pll->on || pll->active)
15115 continue;
15116
15117 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15118
15119 pll->disable(dev_priv, pll);
15120 pll->on = false;
15121 }
15122
Pradeep Bhat30789992014-11-04 17:06:45 +000015123 if (IS_GEN9(dev))
15124 skl_wm_get_hw_state(dev);
15125 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015126 ilk_wm_get_hw_state(dev);
15127
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015128 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015129 i915_redisable_vga(dev);
15130
Daniel Vetterf30da182013-04-11 20:22:50 +020015131 /*
15132 * We need to use raw interfaces for restoring state to avoid
15133 * checking (bogus) intermediate states.
15134 */
Damien Lespiau055e3932014-08-18 13:49:10 +010015135 for_each_pipe(dev_priv, pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070015136 struct drm_crtc *crtc =
15137 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020015138
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020015139 intel_crtc_restore_mode(crtc);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015140 }
15141 } else {
15142 intel_modeset_update_staged_output_state(dev);
15143 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020015144
15145 intel_modeset_check_state(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015146}
15147
15148void intel_modeset_gem_init(struct drm_device *dev)
15149{
Jesse Barnes92122782014-10-09 12:57:42 -070015150 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015151 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070015152 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015153 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015154
Imre Deakae484342014-03-31 15:10:44 +030015155 mutex_lock(&dev->struct_mutex);
15156 intel_init_gt_powersave(dev);
15157 mutex_unlock(&dev->struct_mutex);
15158
Jesse Barnes92122782014-10-09 12:57:42 -070015159 /*
15160 * There may be no VBT; and if the BIOS enabled SSC we can
15161 * just keep using it to avoid unnecessary flicker. Whereas if the
15162 * BIOS isn't using it, don't assume it will work even if the VBT
15163 * indicates as much.
15164 */
15165 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
15166 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15167 DREF_SSC1_ENABLE);
15168
Chris Wilson1833b132012-05-09 11:56:28 +010015169 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020015170
15171 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015172
15173 /*
15174 * Make sure any fbs we allocated at startup are properly
15175 * pinned & fenced. When we do the allocation it's too early
15176 * for this.
15177 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010015178 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070015179 obj = intel_fb_obj(c->primary->fb);
15180 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080015181 continue;
15182
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015183 mutex_lock(&dev->struct_mutex);
15184 ret = intel_pin_and_fence_fb_obj(c->primary,
15185 c->primary->fb,
15186 c->primary->state,
15187 NULL);
15188 mutex_unlock(&dev->struct_mutex);
15189 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080015190 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15191 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100015192 drm_framebuffer_unreference(c->primary->fb);
15193 c->primary->fb = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080015194 update_state_fb(c->primary);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015195 }
15196 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015197
15198 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015199}
15200
Imre Deak4932e2c2014-02-11 17:12:48 +020015201void intel_connector_unregister(struct intel_connector *intel_connector)
15202{
15203 struct drm_connector *connector = &intel_connector->base;
15204
15205 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010015206 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015207}
15208
Jesse Barnes79e53942008-11-07 14:24:08 -080015209void intel_modeset_cleanup(struct drm_device *dev)
15210{
Jesse Barnes652c3932009-08-17 13:31:43 -070015211 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid9255d52013-09-26 20:05:59 -030015212 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070015213
Imre Deak2eb52522014-11-19 15:30:05 +020015214 intel_disable_gt_powersave(dev);
15215
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015216 intel_backlight_unregister(dev);
15217
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015218 /*
15219 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020015220 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015221 * experience fancy races otherwise.
15222 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020015223 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070015224
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015225 /*
15226 * Due to the hpd irq storm handling the hotplug work can re-arm the
15227 * poll handlers. Hence disable polling after hpd handling is shut down.
15228 */
Keith Packardf87ea762010-10-03 19:36:26 -070015229 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015230
Jesse Barnes652c3932009-08-17 13:31:43 -070015231 mutex_lock(&dev->struct_mutex);
15232
Jesse Barnes723bfd72010-10-07 16:01:13 -070015233 intel_unregister_dsm_handler();
15234
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020015235 intel_fbc_disable(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070015236
Kristian Høgsberg69341a52009-11-11 12:19:17 -050015237 mutex_unlock(&dev->struct_mutex);
15238
Chris Wilson1630fe72011-07-08 12:22:42 +010015239 /* flush any delayed tasks or pending work */
15240 flush_scheduled_work();
15241
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015242 /* destroy the backlight and sysfs files before encoders/connectors */
15243 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020015244 struct intel_connector *intel_connector;
15245
15246 intel_connector = to_intel_connector(connector);
15247 intel_connector->unregister(intel_connector);
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015248 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030015249
Jesse Barnes79e53942008-11-07 14:24:08 -080015250 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010015251
15252 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030015253
15254 mutex_lock(&dev->struct_mutex);
15255 intel_cleanup_gt_powersave(dev);
15256 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080015257}
15258
Dave Airlie28d52042009-09-21 14:33:58 +100015259/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080015260 * Return which encoder is currently attached for connector.
15261 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010015262struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080015263{
Chris Wilsondf0e9242010-09-09 16:20:55 +010015264 return &intel_attached_encoder(connector)->base;
15265}
Jesse Barnes79e53942008-11-07 14:24:08 -080015266
Chris Wilsondf0e9242010-09-09 16:20:55 +010015267void intel_connector_attach_encoder(struct intel_connector *connector,
15268 struct intel_encoder *encoder)
15269{
15270 connector->encoder = encoder;
15271 drm_mode_connector_attach_encoder(&connector->base,
15272 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080015273}
Dave Airlie28d52042009-09-21 14:33:58 +100015274
15275/*
15276 * set vga decode state - true == enable VGA decode
15277 */
15278int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15279{
15280 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000015281 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100015282 u16 gmch_ctrl;
15283
Chris Wilson75fa0412014-02-07 18:37:02 -020015284 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15285 DRM_ERROR("failed to read control word\n");
15286 return -EIO;
15287 }
15288
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020015289 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15290 return 0;
15291
Dave Airlie28d52042009-09-21 14:33:58 +100015292 if (state)
15293 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15294 else
15295 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020015296
15297 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15298 DRM_ERROR("failed to write control word\n");
15299 return -EIO;
15300 }
15301
Dave Airlie28d52042009-09-21 14:33:58 +100015302 return 0;
15303}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015304
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015305struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015306
15307 u32 power_well_driver;
15308
Chris Wilson63b66e52013-08-08 15:12:06 +020015309 int num_transcoders;
15310
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015311 struct intel_cursor_error_state {
15312 u32 control;
15313 u32 position;
15314 u32 base;
15315 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010015316 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015317
15318 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015319 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015320 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030015321 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010015322 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015323
15324 struct intel_plane_error_state {
15325 u32 control;
15326 u32 stride;
15327 u32 size;
15328 u32 pos;
15329 u32 addr;
15330 u32 surface;
15331 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010015332 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020015333
15334 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015335 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020015336 enum transcoder cpu_transcoder;
15337
15338 u32 conf;
15339
15340 u32 htotal;
15341 u32 hblank;
15342 u32 hsync;
15343 u32 vtotal;
15344 u32 vblank;
15345 u32 vsync;
15346 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015347};
15348
15349struct intel_display_error_state *
15350intel_display_capture_error_state(struct drm_device *dev)
15351{
Jani Nikulafbee40d2014-03-31 14:27:18 +030015352 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015353 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020015354 int transcoders[] = {
15355 TRANSCODER_A,
15356 TRANSCODER_B,
15357 TRANSCODER_C,
15358 TRANSCODER_EDP,
15359 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015360 int i;
15361
Chris Wilson63b66e52013-08-08 15:12:06 +020015362 if (INTEL_INFO(dev)->num_pipes == 0)
15363 return NULL;
15364
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015365 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015366 if (error == NULL)
15367 return NULL;
15368
Imre Deak190be112013-11-25 17:15:31 +020015369 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015370 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15371
Damien Lespiau055e3932014-08-18 13:49:10 +010015372 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020015373 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015374 __intel_display_power_is_enabled(dev_priv,
15375 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020015376 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015377 continue;
15378
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030015379 error->cursor[i].control = I915_READ(CURCNTR(i));
15380 error->cursor[i].position = I915_READ(CURPOS(i));
15381 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015382
15383 error->plane[i].control = I915_READ(DSPCNTR(i));
15384 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015385 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030015386 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015387 error->plane[i].pos = I915_READ(DSPPOS(i));
15388 }
Paulo Zanonica291362013-03-06 20:03:14 -030015389 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15390 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015391 if (INTEL_INFO(dev)->gen >= 4) {
15392 error->plane[i].surface = I915_READ(DSPSURF(i));
15393 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15394 }
15395
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015396 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030015397
Sonika Jindal3abfce72014-07-21 15:23:43 +053015398 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e2014-04-18 15:55:04 +030015399 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020015400 }
15401
15402 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
15403 if (HAS_DDI(dev_priv->dev))
15404 error->num_transcoders++; /* Account for eDP. */
15405
15406 for (i = 0; i < error->num_transcoders; i++) {
15407 enum transcoder cpu_transcoder = transcoders[i];
15408
Imre Deakddf9c532013-11-27 22:02:02 +020015409 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015410 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020015411 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015412 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015413 continue;
15414
Chris Wilson63b66e52013-08-08 15:12:06 +020015415 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15416
15417 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15418 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15419 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15420 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15421 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15422 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15423 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015424 }
15425
15426 return error;
15427}
15428
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015429#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15430
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015431void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015432intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015433 struct drm_device *dev,
15434 struct intel_display_error_state *error)
15435{
Damien Lespiau055e3932014-08-18 13:49:10 +010015436 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015437 int i;
15438
Chris Wilson63b66e52013-08-08 15:12:06 +020015439 if (!error)
15440 return;
15441
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015442 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020015443 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015444 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015445 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010015446 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015447 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020015448 err_printf(m, " Power: %s\n",
15449 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015450 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030015451 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015452
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015453 err_printf(m, "Plane [%d]:\n", i);
15454 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15455 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015456 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015457 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15458 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015459 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030015460 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015461 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015462 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015463 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15464 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015465 }
15466
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015467 err_printf(m, "Cursor [%d]:\n", i);
15468 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15469 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15470 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015471 }
Chris Wilson63b66e52013-08-08 15:12:06 +020015472
15473 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010015474 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020015475 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015476 err_printf(m, " Power: %s\n",
15477 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020015478 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15479 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15480 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15481 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15482 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15483 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15484 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15485 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015486}
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015487
15488void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
15489{
15490 struct intel_crtc *crtc;
15491
15492 for_each_intel_crtc(dev, crtc) {
15493 struct intel_unpin_work *work;
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015494
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015495 spin_lock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015496
15497 work = crtc->unpin_work;
15498
15499 if (work && work->event &&
15500 work->event->base.file_priv == file) {
15501 kfree(work->event);
15502 work->event = NULL;
15503 }
15504
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015505 spin_unlock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015506 }
15507}